#!/bin/bash # FRPC 配置目录和文件路径 LOCK_FILE="/tmp/lsyfrpc_check_config_running.lock" FRPC_FILE="/data/lsyfrpc/frpc" FRPC_DIR="/data/lsyfrpc" CONFIG_FILE="$FRPC_DIR/frpc.ini" # 获取本机 MAC 地址 function get_mac_address() { cat /sys/class/net/eth0/address | tr -d ':' } # 从云端获取当前分配的端口 function get_cloud_port() { local mac_address="$1" echo "MAC Address: $mac_address" retries=3 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\"}") cloud_port=$(echo "$response" | grep -o '"port":[0-9]*' | awk -F: '{print $2}') if [ -n "$cloud_port" ]; then 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 sleep 10 fi done echo "Cloud Port for 22: $cloud_port" echo "Cloud Port for 9100: $((cloud_port + 10000))" } # 读取本地配置的端口 function get_local_port() { local section="$1" awk -v section="[${section}]" ' $0 == section { found=1; next } found && /^remote_port[ \t]*=[ \t]*/ { split($0, parts, "="); gsub(/[ \t]/, "", parts[2]); print parts[2]; exit; } ' "$CONFIG_FILE" } # 更新 frpc.ini 配置 function update_config() { local mac_address="$1" local cloud_port="$2" local port_9100=$((cloud_port + 10000)) cat > "$CONFIG_FILE" </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 # 创建锁文件 echo $$ > "$LOCK_FILE" trap "rm -f $LOCK_FILE" EXIT SIGHUP SIGINT SIGTERM SIGQUIT # 获取本机 MAC 地址并处理冒号 mac_address_raw=$(get_mac_address) mac_address="${mac_address_raw//:}" # 移除冒号,用于section名称 mac_address_orig=$(cat /sys/class/net/eth0/address) # 原始MAC用于API请求 # 获取云端分配的端口 get_cloud_port "$mac_address_orig" port_9100=$((cloud_port + 10000)) # 获取本地端口配置 local_port_v1=$(get_local_port "${mac_address}-v1") local_port_9100=$(get_local_port "${mac_address}-9100") echo "Local Port for [${mac_address}-v1]: $local_port_v1 (Expected: $cloud_port)" echo "Local Port for [${mac_address}-9100]: $local_port_9100 (Expected: $port_9100)" # 检查端口是否匹配 if [ "$local_port_v1" != "$cloud_port" ] || [ "$local_port_9100" != "$port_9100" ]; then echo "[INFO] Configuration mismatch detected. Updating frpc.ini..." update_config "$mac_address" "$cloud_port" systemctl restart lsyfrpc echo "[INFO] FRPC restarted with updated configuration." else echo "[INFO] Configuration is up to date. No changes needed." fi } main