lsyupdate/lsy-frpc/data/lsyfrpc/check_config.sh

132 lines
3.9 KiB
Bash
Raw Normal View History

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"
2025-02-25 17:04:13 +08:00
# 获取本机 MAC 地址
function get_mac_address() {
cat /sys/class/net/eth0/address | tr -d ':'
}
# 从云端获取当前分配的端口
function get_cloud_port() {
local mac_address="$1"
2025-01-08 18:03:14 +08:00
echo "MAC Address: $mac_address"
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-02-25 17:04:13 +08:00
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 17:04:13 +08:00
echo "Cloud Port for 22: $cloud_port"
echo "Cloud Port for 9100: $((cloud_port + 10000))"
2025-01-08 18:03:14 +08:00
}
2025-02-25 17:04:13 +08:00
# 读取本地配置的端口
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"
}
2025-01-09 15:10:53 +08:00
2025-02-25 17:04:13 +08:00
# 更新 frpc.ini 配置
function update_config() {
local mac_address="$1"
local cloud_port="$2"
local port_9100=$((cloud_port + 10000))
2025-01-08 18:03:14 +08:00
2025-02-25 17:04:13 +08:00
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 17:04:13 +08:00
[${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
2025-02-25 17:04:13 +08:00
[${mac_address}-9100]
2025-02-25 15:55:33 +08:00
type = tcp
local_ip = 127.0.0.1
local_port = 9100
2025-02-25 17:04:13 +08:00
remote_port = $port_9100
2025-01-08 19:53:34 +08:00
EOF
2025-02-25 17:04:13 +08:00
echo "[INFO] Configuration updated."
2025-01-08 18:03:14 +08:00
}
2025-02-25 17:04:13 +08:00
# 主函数
function 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-02-25 17:04:13 +08:00
# 创建锁文件
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-02-25 17:04:13 +08:00
# 获取本机 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
2025-01-08 18:03:14 +08:00
}
2025-02-25 17:04:13 +08:00
main