From 4ad0ebe58155c17175d79b51dbfe0d279cb369c6 Mon Sep 17 00:00:00 2001 From: dekun Date: Tue, 19 May 2026 01:00:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B1=80=E5=9F=9F=E7=BD=91?= =?UTF-8?q?=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DEPLOY.md | 40 +++++++++++++++++++++++++++++---------- README.md | 11 ++++++++--- app.py | 5 +++-- pm2-restart.ps1 | 4 +++- pm2-start.ps1 | 4 +++- pm2-start.sh | 4 +++- scripts/install-ubuntu.sh | 4 +++- 7 files changed, 53 insertions(+), 19 deletions(-) diff --git a/DEPLOY.md b/DEPLOY.md index f4857e0..e16d073 100644 --- a/DEPLOY.md +++ b/DEPLOY.md @@ -4,14 +4,14 @@ > 部署路径:**`/opt/crypto_key`** > 进程守护:**PM2** · 服务端口:**5200** -币圈 API 密钥本地管理工具。数据保存在服务器本地 `data.json`,无登录,默认仅监听 `127.0.0.1`。 +币圈 API 密钥本地管理工具。数据保存在服务器本地 `data.json`,无登录,监听 `0.0.0.0:5200`(本机 + 局域网可访问)。 --- ## 一、架构 ``` -浏览器 / SSH 隧道 → http://127.0.0.1:5200 +浏览器(本机或局域网) → http://<服务器IP>:5200 ↓ PM2 (api-key-manager) ↓ @@ -62,14 +62,23 @@ pm2 status curl -s http://127.0.0.1:5200/api/accounts ``` -浏览器或 SSH 端口转发访问:**http://127.0.0.1:5200** +访问地址: + +| 场景 | 地址 | +|------|------| +| 服务器本机 | http://127.0.0.1:5200 | +| 局域网其他设备 | `http://<局域网IP>:5200` | + +查看服务器局域网 IP: ```bash -# 本机 SSH 转发示例(在你自己的电脑上执行) -ssh -L 5200:127.0.0.1:5200 user@your-server-ip -# 然后浏览器打开 http://127.0.0.1:5200 +hostname -I | awk '{print $1}' +# 或 +ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v 127.0.0.1 ``` +示例:若 IP 为 `192.168.1.100`,手机/电脑在同一 WiFi 下访问 **http://192.168.1.100:5200** + --- ## 四、手动部署(逐步) @@ -244,17 +253,28 @@ chmod 600 /opt/crypto_key/data.json | `pm2 status` 为 errored | `pm2 logs api-key-manager --err`;确认 `venv` 已创建 | | 端口 5200 占用 | `sudo ss -tlnp \| grep 5200`;修改 `app.py` 中 `port` 后 `pm2 restart` | | `git clone` 需认证 | 配置 SSH Key 或 `git config credential.helper store` | -| 无法访问页面 | 服务仅监听 127.0.0.1,需 SSH 隧道或在本机 curl | +| 局域网无法访问 | 检查防火墙是否放行 5200;`sudo ufw allow 5200/tcp` | +| 无法访问页面 | `pm2 logs api-key-manager`;确认 `ss -tlnp \| grep 5200` 显示 `0.0.0.0:5200` | | 更新后异常 | `pip install -r requirements.txt` 后 `pm2 restart api-key-manager` | --- ## 十、安全说明 -- 默认绑定 **127.0.0.1**,不对外网直接暴露。 -- **无登录鉴权**,切勿将 `0.0.0.0:5200` 开放到公网。 +- 服务绑定 **0.0.0.0:5200**,同一局域网内设备均可访问。 +- **无登录鉴权**,切勿将端口映射到公网或暴露于不可信网络。 - `data.json` 已加入 `.gitignore`,不会推送到仓库。 -- 建议在防火墙仅允许 SSH,通过隧道访问管理页。 +- 建议:仅在家/办公室可信局域网使用;云服务器请配合防火墙限制来源 IP。 + +### 防火墙(Ubuntu) + +```bash +# 仅允许局域网网段访问(示例 192.168.0.0/16) +sudo ufw allow from 192.168.0.0/16 to any port 5200 proto tcp + +# 或临时开放(测试用,范围更大) +sudo ufw allow 5200/tcp +``` --- diff --git a/README.md b/README.md index f9a9960..2e2cfba 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,12 @@ cd /opt/crypto_key sudo bash scripts/install-ubuntu.sh ``` -访问(需在本机或通过 SSH 隧道):**http://127.0.0.1:5200** +访问: + +- 本机:**http://127.0.0.1:5200** +- 局域网:**http://<服务器局域网IP>:5200**(如 `http://192.168.1.100:5200`) + +查看本机 IP:`hostname -I` 或 `ip addr` ```bash pm2 status @@ -48,7 +53,7 @@ pip install -r requirements.txt python app.py ``` -浏览器打开 http://127.0.0.1:5200 +浏览器打开 http://127.0.0.1:5200(局域网内其他设备用 `http://<本机IP>:5200`) PM2 守护:`./pm2-start.sh`(Linux)或 `.\pm2-start.ps1`(Windows) @@ -92,7 +97,7 @@ PM2 守护:`./pm2-start.sh`(Linux)或 `.\pm2-start.ps1`(Windows) ## 安全提示 -- 无认证,仅绑定 `127.0.0.1`,请勿暴露公网 +- 无认证,监听 `0.0.0.0`(局域网可访问),请勿暴露到公网;建议仅在可信局域网使用 - `data.json` 为明文密钥,已 `.gitignore`,请定期备份 --- diff --git a/app.py b/app.py index 0e39189..63df032 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ """ 多账户 API 密钥管理工具 — Flask 后端 -端口: 5200 | 数据: data.json +端口: 5200 (0.0.0.0 局域网可访问) | 数据: data.json """ import json import uuid @@ -80,4 +80,5 @@ def delete_account(account_id): if __name__ == "__main__": - app.run(host="127.0.0.1", port=5200, debug=False) + # 0.0.0.0:本机 + 局域网均可访问(如 http://192.168.1.100:5200) + app.run(host="0.0.0.0", port=5200, debug=False) diff --git a/pm2-restart.ps1 b/pm2-restart.ps1 index 283b6b3..03f234b 100644 --- a/pm2-restart.ps1 +++ b/pm2-restart.ps1 @@ -10,4 +10,6 @@ if (-not (Get-Command pm2 -ErrorAction SilentlyContinue)) { pm2 restart api-key-manager Write-Host "已重启 api-key-manager" -ForegroundColor Green -Write-Host "访问: http://127.0.0.1:5200" -ForegroundColor Cyan +$lan = (Get-NetIPAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue | Where-Object { $_.IPAddress -notmatch '^127\.' } | Select-Object -First 1).IPAddress +Write-Host "本机: http://127.0.0.1:5200" -ForegroundColor Cyan +if ($lan) { Write-Host "局域网: http://${lan}:5200" -ForegroundColor Cyan } diff --git a/pm2-start.ps1 b/pm2-start.ps1 index 7f89471..9c5948f 100644 --- a/pm2-start.ps1 +++ b/pm2-start.ps1 @@ -22,6 +22,8 @@ if (-not (Test-Path "logs")) { pm2 start ecosystem.config.cjs pm2 save 2>$null Write-Host "" -Write-Host "已启动。访问: http://127.0.0.1:5200" -ForegroundColor Green +$lan = (Get-NetIPAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue | Where-Object { $_.IPAddress -notmatch '^127\.' -and $_.PrefixOrigin -ne 'WellKnown' } | Select-Object -First 1).IPAddress +Write-Host "已启动。本机: http://127.0.0.1:5200" -ForegroundColor Green +if ($lan) { Write-Host "局域网: http://${lan}:5200" -ForegroundColor Green } Write-Host "查看状态: pm2 status" -ForegroundColor Cyan Write-Host "查看日志: pm2 logs api-key-manager" -ForegroundColor Cyan diff --git a/pm2-start.sh b/pm2-start.sh index 577d7aa..230d06f 100644 --- a/pm2-start.sh +++ b/pm2-start.sh @@ -19,6 +19,8 @@ pm2 start ecosystem.config.cjs pm2 save 2>/dev/null || true echo "" -echo "已启动。访问: http://127.0.0.1:5200" +LAN_IP=$(hostname -I 2>/dev/null | awk '{print $1}') +echo "已启动。本机: http://127.0.0.1:5200" +echo "局域网: http://${LAN_IP:-<你的IP>}:5200" echo "查看状态: pm2 status" echo "查看日志: pm2 logs api-key-manager" diff --git a/scripts/install-ubuntu.sh b/scripts/install-ubuntu.sh index bbfbc55..677f2a7 100644 --- a/scripts/install-ubuntu.sh +++ b/scripts/install-ubuntu.sh @@ -57,7 +57,9 @@ echo "" echo "==========================================" echo " 安装完成" echo " 目录: ${INSTALL_DIR}" -echo " 访问: http://127.0.0.1:5200" +LAN_IP=$(hostname -I 2>/dev/null | awk '{print $1}') +echo " 本机: http://127.0.0.1:5200" +echo " 局域网: http://${LAN_IP:-<你的IP>}:5200" echo " 状态: pm2 status" echo " 日志: pm2 logs api-key-manager" echo "=========================================="