2025-01-08 18:03:14 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
# FRPC 配置目录和文件路径
|
2025-01-09 13:58:41 +08:00
|
|
|
LOCK_FILE="/tmp/lsyfrpc_check_config_running.lock"
|
2025-01-08 18:03:14 +08:00
|
|
|
FRPC_FILE="/data/lsyfrpc/frpc"
|
|
|
|
|
FRPC_DIR="/data/lsyfrpc"
|
|
|
|
|
CONFIG_FILE="$FRPC_DIR/frpc.ini"
|
|
|
|
|
|
|
|
|
|
# 云端接口获取当前端口和 MAC 地址
|
|
|
|
|
function get_cloud_config() {
|
|
|
|
|
mac_address=$(cat /sys/class/net/eth0/address)
|
|
|
|
|
echo "MAC Address: $mac_address"
|
|
|
|
|
|
2025-01-09 15:10:53 +08:00
|
|
|
# 尝试获取云端端口,最多重试 3 次
|
2025-01-09 14:38:36 +08:00
|
|
|
retries=3
|
2025-01-08 18:03:14 +08:00
|
|
|
for ((i=1; i<=retries; i++)); do
|
|
|
|
|
response=$(curl -s -X 'POST' \
|
|
|
|
|
'https://frp-box.jxm.cool/request_port/' \
|
|
|
|
|
-H 'accept: application/json' \
|
|
|
|
|
-H 'Authorization: Basic NTI0NzgxNTctMjY1Zi00ZGNjLWE0NDMtODE0YzJhMDMxYjhjOmM2ZDI3Nzc1LTJhMDgtNDkyZS1iMTExLTg5YWQzZDY5ZTliMA==' \
|
|
|
|
|
-H 'Content-Type: application/json' \
|
|
|
|
|
-d "{\"mac_address\": \"$mac_address\"}")
|
2025-01-08 20:58:05 +08:00
|
|
|
|
2025-01-09 15:10:53 +08:00
|
|
|
echo "Response: $response"
|
2025-01-08 18:03:14 +08:00
|
|
|
cloud_port=$(echo "$response" | grep -o '"port":[0-9]*' | awk -F: '{print $2}')
|
|
|
|
|
|
2025-01-08 21:10:22 +08:00
|
|
|
if [ -n "$cloud_port" ]; then
|
2025-01-08 18:03:14 +08:00
|
|
|
break
|
|
|
|
|
else
|
|
|
|
|
echo "[ERROR] Failed to get port from the cloud. Retry $i of $retries."
|
|
|
|
|
if [ "$i" -eq "$retries" ]; then
|
|
|
|
|
echo "[ERROR] Max retries reached. Exiting."
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2025-01-09 15:10:53 +08:00
|
|
|
sleep 10
|
2025-01-08 18:03:14 +08:00
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
2025-02-25 15:55:33 +08:00
|
|
|
cloud_mac_address_v1="$mac_address-v1"
|
|
|
|
|
cloud_mac_address_v2="$mac_address-v2"
|
|
|
|
|
cloud_port_v2=$((cloud_port + 10000))
|
|
|
|
|
|
|
|
|
|
echo "Cloud MAC Address V1: $cloud_mac_address_v1"
|
|
|
|
|
echo "Cloud MAC Address V2: $cloud_mac_address_v2"
|
|
|
|
|
echo "Cloud Port V1 (22): $cloud_port"
|
|
|
|
|
echo "Cloud Port V2 (9100): $cloud_port_v2"
|
2025-01-08 18:03:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 检查 FRPC 配置是否与云端一致
|
|
|
|
|
function check_config() {
|
|
|
|
|
local cloud_port="$1"
|
2025-02-25 15:55:33 +08:00
|
|
|
local cloud_mac_address_v1="$2"
|
|
|
|
|
local cloud_mac_address_v2="$3"
|
|
|
|
|
local cloud_port_v2="$4"
|
|
|
|
|
|
|
|
|
|
current_port_v1=$(awk -v section="[$cloud_mac_address_v1]" '
|
|
|
|
|
$0 ~ section { found=1 }
|
|
|
|
|
found && /remote_port = [0-9]+/ { print $3; exit }
|
|
|
|
|
' "$CONFIG_FILE")
|
2025-01-08 18:03:14 +08:00
|
|
|
|
2025-02-25 15:55:33 +08:00
|
|
|
current_port_v2=$(awk -v section="[$cloud_mac_address_v2]" '
|
|
|
|
|
$0 ~ section { found=1 }
|
|
|
|
|
found && /remote_port = [0-9]+/ { print $3; exit }
|
|
|
|
|
' "$CONFIG_FILE")
|
2025-01-09 15:10:53 +08:00
|
|
|
|
2025-02-25 15:55:33 +08:00
|
|
|
echo "Current Port for $cloud_mac_address_v1 (22): '$current_port_v1' (Expected: '$cloud_port')"
|
|
|
|
|
echo "Current Port for $cloud_mac_address_v2 (9100): '$current_port_v2' (Expected: '$cloud_port_v2')"
|
2025-01-08 18:03:14 +08:00
|
|
|
|
2025-02-25 15:55:33 +08:00
|
|
|
if [ "$current_port_v1" != "$cloud_port" ] || [ "$current_port_v2" != "$cloud_port_v2" ]; then
|
2025-01-09 15:10:53 +08:00
|
|
|
echo "[INFO] Configuration mismatch detected. Updating frpc.ini..."
|
|
|
|
|
cat > "$CONFIG_FILE" <<EOF
|
2025-01-08 19:53:34 +08:00
|
|
|
[common]
|
|
|
|
|
server_addr = box.jxm.cool
|
|
|
|
|
server_port = 8000
|
|
|
|
|
token = 92098d16-1961-4bda-902b-e43e3d41d5a9
|
|
|
|
|
|
2025-02-25 15:55:33 +08:00
|
|
|
[$cloud_mac_address_v1]
|
2025-01-08 19:53:34 +08:00
|
|
|
type = tcp
|
|
|
|
|
local_ip = 127.0.0.1
|
|
|
|
|
local_port = 22
|
|
|
|
|
remote_port = $cloud_port
|
2025-02-25 15:55:33 +08:00
|
|
|
|
|
|
|
|
[$cloud_mac_address_v2]
|
|
|
|
|
type = tcp
|
|
|
|
|
local_ip = 127.0.0.1
|
|
|
|
|
local_port = 9100
|
|
|
|
|
remote_port = $cloud_port_v2
|
2025-01-08 19:53:34 +08:00
|
|
|
EOF
|
2025-01-08 20:23:23 +08:00
|
|
|
echo "[INFO] Configuration updated. Restarting frpc..."
|
2025-01-08 21:54:07 +08:00
|
|
|
sudo systemctl restart lsyfrpc.service
|
2025-01-08 18:03:14 +08:00
|
|
|
else
|
|
|
|
|
echo "[INFO] Configuration is up to date."
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main() {
|
2025-01-09 13:58:41 +08:00
|
|
|
if [ -f "$LOCK_FILE" ]; then
|
2025-01-09 15:10:53 +08:00
|
|
|
lock_pid=$(cat "$LOCK_FILE")
|
|
|
|
|
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
|
2025-01-09 13:58:41 +08:00
|
|
|
fi
|
|
|
|
|
|
2025-01-09 15:10:53 +08:00
|
|
|
echo $$ > "$LOCK_FILE"
|
|
|
|
|
trap "rm -f $LOCK_FILE" EXIT SIGHUP SIGINT SIGTERM SIGQUIT
|
2025-01-09 13:58:41 +08:00
|
|
|
|
2025-01-08 18:03:14 +08:00
|
|
|
get_cloud_config
|
2025-02-25 15:55:33 +08:00
|
|
|
check_config "$cloud_port" "$cloud_mac_address_v1" "$cloud_mac_address_v2" "$cloud_port_v2"
|
2025-01-08 18:03:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main
|