forked from buzzhub/lsyupdate
101 lines
3.2 KiB
Bash
Executable File
101 lines
3.2 KiB
Bash
Executable File
#!/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_cloud_config() {
|
|
mac_address=$(cat /sys/class/net/eth0/address)
|
|
echo "MAC Address: $mac_address"
|
|
|
|
# 尝试获取云端端口,最多重试 5 次
|
|
retries=1
|
|
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\"}")
|
|
|
|
echo "Response: $response"
|
|
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 # 等待 10 秒后再重试
|
|
fi
|
|
done
|
|
|
|
cloud_mac_address="$mac_address-v1"
|
|
echo "Cloud MAC Address: $cloud_mac_address"
|
|
echo "Cloud Port: $cloud_port"
|
|
}
|
|
|
|
# 检查 FRPC 配置是否与云端一致
|
|
function check_config() {
|
|
local cloud_port="$1"
|
|
local cloud_mac_address="$2"
|
|
|
|
# 从配置文件中读取现有的 MAC 地址和端口
|
|
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) # 用 xargs 去掉两端空格
|
|
|
|
# 输出调试信息
|
|
echo "Current MAC Address in frpc.ini: '$current_mac_address' '$cloud_mac_address'"
|
|
echo "Current Port in frpc.ini: '$current_port' '$cloud_port'"
|
|
|
|
# 比较配置文件中的 MAC 地址和端口是否与云端匹配
|
|
if [ "$current_mac_address" != "$cloud_mac_address" ] || [ "$current_port" != "$cloud_port" ]; then
|
|
echo "[INFO] Configuration mismatch detected. Re-running install.sh..."
|
|
cat > "$FRPC_DIR/frpc.ini" <<EOF
|
|
[common]
|
|
server_addr = box.jxm.cool
|
|
server_port = 8000
|
|
token = 92098d16-1961-4bda-902b-e43e3d41d5a9
|
|
|
|
[$cloud_mac_address]
|
|
type = tcp
|
|
local_ip = 127.0.0.1
|
|
local_port = 22
|
|
remote_port = $cloud_port
|
|
EOF
|
|
echo "[INFO] Configuration updated. Restarting frpc..."
|
|
sudo systemctl restart lsyfrpc.service
|
|
|
|
else
|
|
echo "[INFO] Configuration is up to date."
|
|
fi
|
|
}
|
|
|
|
# 主函数,获取云端配置并检查配置
|
|
main() {
|
|
# 检查锁文件
|
|
if [ -f "$LOCK_FILE" ]; then
|
|
echo "[INFO] Lock file exists. Script is already running."
|
|
exit 0
|
|
fi
|
|
|
|
# 创建锁文件
|
|
touch "$LOCK_FILE"
|
|
|
|
# 设置脚本在退出时删除锁文件
|
|
trap "rm -f $LOCK_FILE" EXIT
|
|
|
|
# 主逻辑
|
|
get_cloud_config
|
|
check_config "$cloud_port" "$cloud_mac_address"
|
|
}
|
|
|
|
main
|