# ServerControlGo **Repository Path**: jeckchen666/server-control-go ## Basic Information - **Project Name**: ServerControlGo - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-18 - **Last Updated**: 2026-04-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 云服务器管理平台 一个 Go 编写的 Web 服务器 + Python 客户端,提供文件上传和远程命令执行功能。 ## 功能特性 - **文件上传**: 支持上传文件到服务器任意位置,可选覆盖或报错模式 - **命令执行**: 在服务器上执行任意命令,返回 stdout/stderr 输出 - **传输加密**: HTTPS 加密传输 - **认证安全**: RSA 公钥/私钥签名验证 ## 环境要求 ### 服务器端 - Go 1.18+ - Linux/Windows 系统 ### 客户端 - Python 3.7+ - requests - pycryptodome ```bash pip install requests pycryptodome ``` --- ## 快速开始 ### 步骤 1: 生成密钥和证书 ```bash # 创建目录 mkdir -p keys cert # 生成 RSA 密钥对(私钥和公钥) openssl genrsa -out keys/private.pem 2048 openssl rsa -in keys/private.pem -pubout -out keys/public.pem # 生成自签名 SSL 证书(用于 HTTPS) openssl req -x509 -newkey rsa:2048 -keyout cert/server.key -out cert/server.crt -days 3650 -nodes ``` 生成的密钥说明: - `keys/private.pem` - 客户端用于签名认证(不要泄露) - `keys/public.pem` - 服务器用于验证签名 - `cert/server.crt` - SSL 证书 - `cert/server.key` - SSL 私钥 ### 步骤 2: 配置服务器 复制并编辑配置文件: ```bash cp config_example.json config.json ``` 修改 `config.json` 中的路径: ```json { "server": { "host": "0.0.0.0", "port": 21200, "cert": "cert/server.crt", "key": "cert/server.key" }, "auth": { "public_key": "keys/public.pem", "private_key": "keys/private.pem" }, "exec": { "timeout": 300 } } ``` 配置说明: - `server.host` - 监听地址,0.0.0.0 允许外部访问 - `server.port` - 监听端口 - `server.cert` - SSL 证书路径 - `server.key` - SSL 私钥路径 - `auth.public_key` - RSA 公钥路径(服务器验证签名用) - `auth.private_key` - RSA 私钥路径(可选,服务器不需要) - `exec.timeout` - 命令执行超时时间(秒) ### 步骤 3: 编译服务器 ```bash # Linux GOOS=linux GOARCH=amd64 go build -o server cmd/server/main.go # Windows go build -o server.exe cmd/server/main.go ``` ### 步骤 4: 启动服务器 ```bash # 前台运行 ./server config.json # 后台运行 nohup ./server config.json & ``` ### 步骤 5: 测试连接 ```bash # 健康检查 python client.py --key keys/private.pem health # 执行命令 python client.py --key keys/private.pem exec echo "Hello World" ``` --- ## 客户端使用 ### 基本用法 ```bash # 健康检查 - 检查服务器是否在线 python client.py --key keys/private.pem health # 执行命令 python client.py --key keys/private.pem exec echo hello python client.py --key keys/private.pem exec ls -la /home python client.py --key keys/private.pem exec python -c "print('hello')" # 上传文件(文件存在则报错) python client.py --key keys/private.pem upload local.txt /tmp/remote.txt # 上传文件(覆盖模式) python client.py --key keys/private.pem upload local.txt /tmp/remote.txt overwrite ``` ### 参数说明 | 参数 | 说明 | |------|------| | `--server` | 服务器地址,格式:`https://host:port` | | `--key` | RSA 私钥文件路径 | ### Python 代码调用 ```python from client import Client # 创建客户端 client = Client( server_url="https://127.0.0.1:21200", key_path="keys/private.pem" ) # 健康检查 client.health() # 执行命令 result = client.exec("echo", ["hello"]) print(result["stdout"]) # 上传文件 client.upload("/local/file.txt", "/tmp/remote.txt") ``` --- ## API 文档 ### 1. 健康检查 ```http GET /api/health ``` 无需认证,返回 `OK` ### 2. 获取公钥 ```http GET /api/pubkey ``` 返回服务器公钥的模数 n 和指数 e: ```json { "n": "a1b2c3...", "e": 65537 } ``` ### 3. 命令执行 ```http POST /api/exec Content-Type: application/json ``` 请求体: ```json { "cmd": "命令", "args": ["参数1", "参数2"], "cwd": "工作目录" } ``` 响应: ```json { "success": true, "stdout": "输出内容", "stderr": "错误内容", "exit_code": 0, "duration": "10.5ms" } ``` ### 4. 文件上传 ```http POST /api/upload?path=/tmp/test.txt&mode=overwrite Content-Type: multipart/form-data ``` 参数: - `path` - 远程保存路径 - `mode` - 上传模式 - `overwrite` - 覆盖已存在的文件 - `error` - 文件存在则报错(默认) 响应: ```json { "success": true, "message": "File uploaded", "filename": "test.txt", "size": 1234 } ``` --- ## 认证方式 除健康检查外,所有 API 需要 RSA 签名认证。 ### 签名流程 1. 构造消息:`timestamp + nonce` 2. 计算 SHA256 哈希 3. 使用 RSA 私钥对哈希进行 PKCS1-v1.5 签名 4. 将签名 Base64 编码 ### 请求头 | 头名称 | 说明 | |--------|------| | `X-Timestamp` | Unix 时间戳(秒) | | `X-Nonce` | 随机字符串(Base64 编码) | | `X-Signature` | RSA 签名(Base64 编码) | ### 时间戳验证 服务器会验证时间戳,有效期为 5 分钟。如果时间戳过期,返回 401 错误。 --- ## 云服务器部署 ### 1. 编译 Linux 版本 ```bash GOOS=linux GOARCH=amd64 go build -o server cmd/server/main.go ``` ### 2. 上传到服务器 ```bash scp server root@your-server:/root/ scp -r keys root@your-server:/root/ scp -r cert root@your-server:/root/ scp config.json root@your-server:/root/ ``` ### 3. 启动服务 ```bash ssh root@your-server cd /root chmod +x server nohup ./server config.json > server.log 2>&1 & ``` ### 4. 配置安全组 在云控制台安全组添加入方向规则: - 协议:TCP - 端口:21200 - 来源:0.0.0.0/0(或限制特定 IP) --- ## 故障排除 ### 连接失败 1. 检查服务器是否运行:`ps aux | grep server` 2. 检查端口是否监听:`netstat -tlnp | grep 21200` 3. 检查防火墙:`iptables -L INPUT` 或安全组规则 ### 签名验证失败 1. 确保客户端和服务器使用配对的密钥 2. 检查私钥路径是否正确 3. 确保时间戳未过期(5分钟内) ### SSL 证书错误 客户端默认跳过证书验证。如需验证,使用正式的 SSL 证书或手动信任自签名证书。 --- ## 项目结构 ``` . ├── client.py # Python 客户端 ├── config_example.json # 配置示例 ├── keys/ # RSA 密钥(需自行生成) │ ├── private.pem # 私钥(客户端使用) │ └── public.pem # 公钥(服务器使用) ├── cert/ # SSL 证书(需自行生成) │ ├── server.crt # 证书 │ └── server.key # 私钥 ├── cmd/ # Go 服务器源码 │ └── server/main.go └── internal/ # Go 内部包 ├── config/ # 配置加载 ├── crypto/ # RSA 加密/签名 ├── handlers/ # API 处理器 └── middleware/ # 认证中间件 ``` --- ## 安全注意 1. **私钥安全**:`keys/private.pem` 是认证的唯一凭证,切勿泄露或提交到代码仓库 2. **网络限制**:建议通过云安全组限制 21200 端口的访问来源 3. **生产环境**:使用正式的 SSL 证书(Let's Encrypt 等) 4. **命令执行**:服务器端执行命令没有权限限制,请仅授权可信客户端 --- ## 许可证 MIT