forked from buzzhub/lsyupdate
1
This commit is contained in:
parent
e496b20b8f
commit
72c22ffc6e
@ -11,7 +11,7 @@ function get_cloud_config() {
|
|||||||
mac_address=$(cat /sys/class/net/eth0/address)
|
mac_address=$(cat /sys/class/net/eth0/address)
|
||||||
echo "MAC Address: $mac_address"
|
echo "MAC Address: $mac_address"
|
||||||
|
|
||||||
# 尝试获取云端端口,最多重试 5 次
|
# 尝试获取云端端口,最多重试 3 次
|
||||||
retries=3
|
retries=3
|
||||||
for ((i=1; i<=retries; i++)); do
|
for ((i=1; i<=retries; i++)); do
|
||||||
response=$(curl -s -X 'POST' \
|
response=$(curl -s -X 'POST' \
|
||||||
@ -21,11 +21,10 @@ function get_cloud_config() {
|
|||||||
-H 'Content-Type: application/json' \
|
-H 'Content-Type: application/json' \
|
||||||
-d "{\"mac_address\": \"$mac_address\"}")
|
-d "{\"mac_address\": \"$mac_address\"}")
|
||||||
|
|
||||||
echo "Response: $response"
|
echo "Response: $response"
|
||||||
cloud_port=$(echo "$response" | grep -o '"port":[0-9]*' | awk -F: '{print $2}')
|
cloud_port=$(echo "$response" | grep -o '"port":[0-9]*' | awk -F: '{print $2}')
|
||||||
|
|
||||||
if [ -n "$cloud_port" ]; then
|
if [ -n "$cloud_port" ]; then
|
||||||
# 成功获取到端口
|
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
echo "[ERROR] Failed to get port from the cloud. Retry $i of $retries."
|
echo "[ERROR] Failed to get port from the cloud. Retry $i of $retries."
|
||||||
@ -33,7 +32,7 @@ function get_cloud_config() {
|
|||||||
echo "[ERROR] Max retries reached. Exiting."
|
echo "[ERROR] Max retries reached. Exiting."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
sleep 10 # 等待 10 秒后再重试
|
sleep 10
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
@ -47,18 +46,15 @@ function check_config() {
|
|||||||
local cloud_port="$1"
|
local cloud_port="$1"
|
||||||
local cloud_mac_address="$2"
|
local cloud_mac_address="$2"
|
||||||
|
|
||||||
# 从配置文件中读取现有的 MAC 地址和端口
|
current_mac_address=$(grep -Eo '^\[[a-f0-9:-]*-v1\]' "$CONFIG_FILE" | tr -d '[]')
|
||||||
current_mac_address=$(grep -o '\[.*\]' "$CONFIG_FILE" | tr -d '[]' | xargs) # 用 xargs 去掉两端空格
|
current_port=$(grep -o 'remote_port = [0-9]*' "$CONFIG_FILE" | awk '{print $3}' | xargs)
|
||||||
current_port=$(grep -o 'remote_port = [0-9]*' "$CONFIG_FILE" | awk '{print $3}' | xargs) # 用 xargs 去掉两端空格
|
|
||||||
|
|
||||||
# 输出调试信息
|
|
||||||
echo "Current MAC Address in frpc.ini: '$current_mac_address' '$cloud_mac_address'"
|
echo "Current MAC Address in frpc.ini: '$current_mac_address' '$cloud_mac_address'"
|
||||||
echo "Current Port in frpc.ini: '$current_port' '$cloud_port'"
|
echo "Current Port in frpc.ini: '$current_port' '$cloud_port'"
|
||||||
|
|
||||||
# 比较配置文件中的 MAC 地址和端口是否与云端匹配
|
|
||||||
if [ "$current_mac_address" != "$cloud_mac_address" ] || [ "$current_port" != "$cloud_port" ]; then
|
if [ "$current_mac_address" != "$cloud_mac_address" ] || [ "$current_port" != "$cloud_port" ]; then
|
||||||
echo "[INFO] Configuration mismatch detected. Re-running install.sh..."
|
echo "[INFO] Configuration mismatch detected. Updating frpc.ini..."
|
||||||
cat > "$FRPC_DIR/frpc.ini" <<EOF
|
cat > "$CONFIG_FILE" <<EOF
|
||||||
[common]
|
[common]
|
||||||
server_addr = box.jxm.cool
|
server_addr = box.jxm.cool
|
||||||
server_port = 8000
|
server_port = 8000
|
||||||
@ -72,27 +68,26 @@ remote_port = $cloud_port
|
|||||||
EOF
|
EOF
|
||||||
echo "[INFO] Configuration updated. Restarting frpc..."
|
echo "[INFO] Configuration updated. Restarting frpc..."
|
||||||
sudo systemctl restart lsyfrpc.service
|
sudo systemctl restart lsyfrpc.service
|
||||||
|
|
||||||
else
|
else
|
||||||
echo "[INFO] Configuration is up to date."
|
echo "[INFO] Configuration is up to date."
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# 主函数,获取云端配置并检查配置
|
|
||||||
main() {
|
main() {
|
||||||
# 检查锁文件
|
|
||||||
if [ -f "$LOCK_FILE" ]; then
|
if [ -f "$LOCK_FILE" ]; then
|
||||||
echo "[INFO] Lock file exists. Script is already running."
|
lock_pid=$(cat "$LOCK_FILE")
|
||||||
exit 0
|
if [ -n "$lock_pid" ] && kill -0 "$lock_pid" 2>/dev/null; then
|
||||||
|
echo "[INFO] Lock file exists and script is running (PID: $lock_pid). Exiting."
|
||||||
|
exit 0
|
||||||
|
else
|
||||||
|
echo "[INFO] Stale lock file detected. Removing."
|
||||||
|
rm -f "$LOCK_FILE"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 创建锁文件
|
echo $$ > "$LOCK_FILE"
|
||||||
touch "$LOCK_FILE"
|
trap "rm -f $LOCK_FILE" EXIT SIGHUP SIGINT SIGTERM SIGQUIT
|
||||||
|
|
||||||
# 设置脚本在退出时删除锁文件
|
|
||||||
trap "rm -f $LOCK_FILE" EXIT
|
|
||||||
|
|
||||||
# 主逻辑
|
|
||||||
get_cloud_config
|
get_cloud_config
|
||||||
check_config "$cloud_port" "$cloud_mac_address"
|
check_config "$cloud_port" "$cloud_mac_address"
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user