feat: V5.0 边缘节点重构 - 引入 Python Webhook 被动监听与 Master 自动注册机制

This commit is contained in:
hotyue
2026-04-01 10:26:44 +00:00
parent f6512fb1c3
commit 5745f87dd2
2 changed files with 106 additions and 26 deletions

75
core/agent_daemon.sh Normal file
View File

@@ -0,0 +1,75 @@
#!/bin/bash
# ==========================================================
# 脚本名称: agent_daemon.sh (受控节点 Webhook 守护进程)
# 核心功能: 向 Master 汇报公网 IP 注册、监听本地 HTTP 唤醒指令
# ==========================================================
INSTALL_DIR="/opt/ip_sentinel"
CONFIG_FILE="${INSTALL_DIR}/config.conf"
[ ! -f "$CONFIG_FILE" ] && exit 1
source "$CONFIG_FILE"
# 如果没有配置 TG说明未开启联控模式直接退出
[ -z "$TG_TOKEN" ] || [ -z "$CHAT_ID" ] && exit 0
# 默认 Webhook 监听端口,可在安装时动态写入配置
AGENT_PORT=${AGENT_PORT:-9527}
# 截取主机名作为节点唯一标识 (可限制长度防超长)
NODE_NAME=$(hostname | cut -c 1-15)
# 1. 获取本机原生公网 IPv4
AGENT_IP=$(curl -4 -s -m 5 api.ip.sb/ip)
if [ -n "$AGENT_IP" ]; then
# 2. 向 Master 发送注册暗号 (借助 TG API)
# 格式严格匹配 Master 端的正则: #REGISTER#|<NodeName>|<IP>|<Port>
REG_MSG="#REGISTER#|${NODE_NAME}|${AGENT_IP}|${AGENT_PORT}"
curl -s -m 5 -X POST "https://api.telegram.org/bot${TG_TOKEN}/sendMessage" \
-d "chat_id=${CHAT_ID}" \
-d "text=${REG_MSG}" > /dev/null
echo "✅ [Agent] 已向司令部发送注册/续期请求: $NODE_NAME ($AGENT_IP:$AGENT_PORT)"
fi
# 3. 启动轻量级 Python3 Webhook 监听服务
# (相比纯 Bash 的 nc 命令Python3 的 HTTP 库在各发行版间兼容性最完美,且支持并发)
cat > "${INSTALL_DIR}/core/webhook.py" << 'EOF'
import http.server
import socketserver
import subprocess
import sys
PORT = int(sys.argv[1])
class AgentHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
# 统一返回成功,防止 Master 请求超时阻塞
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(b"Agent Received Action\n")
# 路由分发
if self.path == '/trigger_run':
# 另起后台进程执行深度伪装,不阻塞 Webhook 响应
subprocess.Popen(['bash', '/opt/ip_sentinel/core/mod_google.sh'])
elif self.path == '/trigger_report':
# 另起后台进程执行战报生成
subprocess.Popen(['bash', '/opt/ip_sentinel/core/tg_report.sh'])
def log_message(self, format, *args):
# 关闭默认的控制台日志输出,保持后台清爽
pass
try:
with socketserver.TCPServer(("", PORT), AgentHandler) as httpd:
httpd.serve_forever()
except Exception as e:
sys.exit(1)
EOF
# 保持前台运行 (被 Cron 的 nohup 放入后台守护)
python3 "${INSTALL_DIR}/core/webhook.py" "$AGENT_PORT"