forked from buzzhub/lsyupdate
check_config 修改
This commit is contained in:
parent
0c868705e1
commit
4359ace81e
@ -6,12 +6,16 @@ FRPC_FILE="/data/lsyfrpc/frpc"
|
|||||||
FRPC_DIR="/data/lsyfrpc"
|
FRPC_DIR="/data/lsyfrpc"
|
||||||
CONFIG_FILE="$FRPC_DIR/frpc.ini"
|
CONFIG_FILE="$FRPC_DIR/frpc.ini"
|
||||||
|
|
||||||
# 云端接口获取当前端口和 MAC 地址
|
# 获取本机 MAC 地址
|
||||||
function get_cloud_config() {
|
function get_mac_address() {
|
||||||
mac_address=$(cat /sys/class/net/eth0/address)
|
cat /sys/class/net/eth0/address | tr -d ':'
|
||||||
|
}
|
||||||
|
|
||||||
|
# 从云端获取当前分配的端口
|
||||||
|
function get_cloud_port() {
|
||||||
|
local mac_address="$1"
|
||||||
echo "MAC Address: $mac_address"
|
echo "MAC Address: $mac_address"
|
||||||
|
|
||||||
# 尝试获取云端端口,最多重试 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,7 +25,6 @@ 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"
|
|
||||||
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
|
||||||
@ -36,64 +39,54 @@ function get_cloud_config() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
cloud_mac_address_v1="$mac_address-v1"
|
echo "Cloud Port for 22: $cloud_port"
|
||||||
cloud_mac_address_v2="$mac_address-v2"
|
echo "Cloud Port for 9100: $((cloud_port + 10000))"
|
||||||
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"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# 检查 FRPC 配置是否与云端一致
|
# 读取本地配置的端口
|
||||||
function check_config() {
|
function get_local_port() {
|
||||||
local cloud_port="$1"
|
local section="$1"
|
||||||
local cloud_mac_address_v1="$2"
|
awk -v section="[${section}]" '
|
||||||
local cloud_mac_address_v2="$3"
|
$0 == section { found=1; next }
|
||||||
local cloud_port_v2="$4"
|
found && /^remote_port[ \t]*=[ \t]*/ {
|
||||||
|
split($0, parts, "=");
|
||||||
|
gsub(/[ \t]/, "", parts[2]);
|
||||||
|
print parts[2];
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
' "$CONFIG_FILE"
|
||||||
|
}
|
||||||
|
|
||||||
current_port_v1=$(awk -v section="[$cloud_mac_address_v1]" '
|
# 更新 frpc.ini 配置
|
||||||
$0 ~ section { found=1 }
|
function update_config() {
|
||||||
found && /remote_port = [0-9]+/ { print $3; exit }
|
local mac_address="$1"
|
||||||
' "$CONFIG_FILE")
|
local cloud_port="$2"
|
||||||
|
local port_9100=$((cloud_port + 10000))
|
||||||
|
|
||||||
current_port_v2=$(awk -v section="[$cloud_mac_address_v2]" '
|
|
||||||
$0 ~ section { found=1 }
|
|
||||||
found && /remote_port = [0-9]+/ { print $3; exit }
|
|
||||||
' "$CONFIG_FILE")
|
|
||||||
|
|
||||||
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')"
|
|
||||||
|
|
||||||
if [ "$current_port_v1" != "$cloud_port" ] || [ "$current_port_v2" != "$cloud_port_v2" ]; then
|
|
||||||
echo "[INFO] Configuration mismatch detected. Updating frpc.ini..."
|
|
||||||
cat > "$CONFIG_FILE" <<EOF
|
cat > "$CONFIG_FILE" <<EOF
|
||||||
[common]
|
[common]
|
||||||
server_addr = box.jxm.cool
|
server_addr = box.jxm.cool
|
||||||
server_port = 8000
|
server_port = 8000
|
||||||
token = 92098d16-1961-4bda-902b-e43e3d41d5a9
|
token = 92098d16-1961-4bda-902b-e43e3d41d5a9
|
||||||
|
|
||||||
[$cloud_mac_address_v1]
|
[${mac_address}-v1]
|
||||||
type = tcp
|
type = tcp
|
||||||
local_ip = 127.0.0.1
|
local_ip = 127.0.0.1
|
||||||
local_port = 22
|
local_port = 22
|
||||||
remote_port = $cloud_port
|
remote_port = $cloud_port
|
||||||
|
|
||||||
[$cloud_mac_address_v2]
|
[${mac_address}-9100]
|
||||||
type = tcp
|
type = tcp
|
||||||
local_ip = 127.0.0.1
|
local_ip = 127.0.0.1
|
||||||
local_port = 9100
|
local_port = 9100
|
||||||
remote_port = $cloud_port_v2
|
remote_port = $port_9100
|
||||||
EOF
|
EOF
|
||||||
echo "[INFO] Configuration updated. Restarting frpc..."
|
echo "[INFO] Configuration updated."
|
||||||
sudo systemctl restart lsyfrpc.service
|
|
||||||
else
|
|
||||||
echo "[INFO] Configuration is up to date."
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main() {
|
# 主函数
|
||||||
|
function main() {
|
||||||
|
# 确保不会重复运行
|
||||||
if [ -f "$LOCK_FILE" ]; then
|
if [ -f "$LOCK_FILE" ]; then
|
||||||
lock_pid=$(cat "$LOCK_FILE")
|
lock_pid=$(cat "$LOCK_FILE")
|
||||||
if [ -n "$lock_pid" ] && kill -0 "$lock_pid" 2>/dev/null; then
|
if [ -n "$lock_pid" ] && kill -0 "$lock_pid" 2>/dev/null; then
|
||||||
@ -105,11 +98,35 @@ main() {
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# 创建锁文件
|
||||||
echo $$ > "$LOCK_FILE"
|
echo $$ > "$LOCK_FILE"
|
||||||
trap "rm -f $LOCK_FILE" EXIT SIGHUP SIGINT SIGTERM SIGQUIT
|
trap "rm -f $LOCK_FILE" EXIT SIGHUP SIGINT SIGTERM SIGQUIT
|
||||||
|
|
||||||
get_cloud_config
|
# 获取本机 MAC 地址并处理冒号
|
||||||
check_config "$cloud_port" "$cloud_mac_address_v1" "$cloud_mac_address_v2" "$cloud_port_v2"
|
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
|
main
|
||||||
Loading…
Reference in New Issue
Block a user