Initial commit: crypto_key API key manager with PM2 and Ubuntu deploy docs
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
venv/
|
||||
logs/*.log
|
||||
data.json
|
||||
__pycache__/
|
||||
@@ -0,0 +1,287 @@
|
||||
# 部署文档 — crypto_key(Ubuntu /opt)
|
||||
|
||||
> 仓库地址:[https://git.bz121.com/dekun/crypto_key](https://git.bz121.com/dekun/crypto_key.git)
|
||||
> 部署路径:**`/opt/crypto_key`**
|
||||
> 进程守护:**PM2** · 服务端口:**5200**
|
||||
|
||||
币圈 API 密钥本地管理工具。数据保存在服务器本地 `data.json`,无登录,默认仅监听 `127.0.0.1`。
|
||||
|
||||
---
|
||||
|
||||
## 一、架构
|
||||
|
||||
```
|
||||
浏览器 / SSH 隧道 → http://127.0.0.1:5200
|
||||
↓
|
||||
PM2 (api-key-manager)
|
||||
↓
|
||||
/opt/crypto_key/venv/python app.py
|
||||
↓
|
||||
/opt/crypto_key/data.json
|
||||
```
|
||||
|
||||
| 路径 | 说明 |
|
||||
|------|------|
|
||||
| `/opt/crypto_key` | 项目根目录(从 Git 克隆) |
|
||||
| `/opt/crypto_key/data.json` | 账户数据(不纳入 Git) |
|
||||
| `/opt/crypto_key/logs/` | PM2 日志 |
|
||||
| `/opt/crypto_key/venv/` | Python 虚拟环境 |
|
||||
|
||||
---
|
||||
|
||||
## 二、服务器要求
|
||||
|
||||
| 项目 | 要求 |
|
||||
|------|------|
|
||||
| 系统 | Ubuntu 20.04 / 22.04 / 24.04 LTS |
|
||||
| 权限 | 具备 `sudo` 的普通用户或 root |
|
||||
| Python | 3.8+(`python3`) |
|
||||
| Node.js | 16+(用于 PM2) |
|
||||
| 磁盘 | ≥ 100MB |
|
||||
|
||||
---
|
||||
|
||||
## 三、首次部署(推荐:一键脚本)
|
||||
|
||||
### 3.1 克隆仓库并执行安装
|
||||
|
||||
```bash
|
||||
# 若仓库尚未克隆,可先克隆再安装
|
||||
sudo mkdir -p /opt
|
||||
sudo git clone https://git.bz121.com/dekun/crypto_key.git /opt/crypto_key
|
||||
cd /opt/crypto_key
|
||||
sudo bash scripts/install-ubuntu.sh
|
||||
```
|
||||
|
||||
脚本将自动完成:安装 `python3` / `git` / `Node.js` / `PM2` → 创建 `venv` → 安装 Flask → PM2 启动服务。
|
||||
|
||||
### 3.2 验证
|
||||
|
||||
```bash
|
||||
pm2 status
|
||||
curl -s http://127.0.0.1:5200/api/accounts
|
||||
```
|
||||
|
||||
浏览器或 SSH 端口转发访问:**http://127.0.0.1:5200**
|
||||
|
||||
```bash
|
||||
# 本机 SSH 转发示例(在你自己的电脑上执行)
|
||||
ssh -L 5200:127.0.0.1:5200 user@your-server-ip
|
||||
# 然后浏览器打开 http://127.0.0.1:5200
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 四、手动部署(逐步)
|
||||
|
||||
适合需要自定义权限或不用一键脚本的环境。
|
||||
|
||||
### 4.1 安装系统依赖
|
||||
|
||||
```bash
|
||||
sudo apt update
|
||||
sudo apt install -y python3 python3-venv python3-pip git curl
|
||||
|
||||
# Node.js + PM2
|
||||
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
|
||||
sudo apt install -y nodejs
|
||||
sudo npm install -g pm2
|
||||
```
|
||||
|
||||
### 4.2 克隆到 /opt
|
||||
|
||||
```bash
|
||||
sudo mkdir -p /opt
|
||||
sudo git clone https://git.bz121.com/dekun/crypto_key.git /opt/crypto_key
|
||||
cd /opt/crypto_key
|
||||
```
|
||||
|
||||
若目录已存在,更新代码:
|
||||
|
||||
```bash
|
||||
cd /opt/crypto_key
|
||||
sudo git pull
|
||||
```
|
||||
|
||||
### 4.3 Python 环境
|
||||
|
||||
```bash
|
||||
cd /opt/crypto_key
|
||||
python3 -m venv venv
|
||||
source venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
### 4.4 PM2 启动
|
||||
|
||||
```bash
|
||||
cd /opt/crypto_key
|
||||
mkdir -p logs
|
||||
chmod +x pm2-start.sh pm2-stop.sh
|
||||
./pm2-start.sh
|
||||
```
|
||||
|
||||
或:
|
||||
|
||||
```bash
|
||||
pm2 start ecosystem.config.cjs
|
||||
pm2 save
|
||||
```
|
||||
|
||||
### 4.5 目录权限(可选)
|
||||
|
||||
若用普通用户运行 PM2,建议将项目目录属主改为该用户:
|
||||
|
||||
```bash
|
||||
sudo chown -R $USER:$USER /opt/crypto_key
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 五、开机自启
|
||||
|
||||
```bash
|
||||
pm2 startup
|
||||
# 终端会输出一行 sudo 命令,复制并执行
|
||||
pm2 save
|
||||
```
|
||||
|
||||
重启后检查:
|
||||
|
||||
```bash
|
||||
pm2 status
|
||||
# api-key-manager 应为 online
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 六、日常运维
|
||||
|
||||
### 查看状态与日志
|
||||
|
||||
```bash
|
||||
pm2 status
|
||||
pm2 logs api-key-manager
|
||||
pm2 logs api-key-manager --lines 100
|
||||
```
|
||||
|
||||
### 重启 / 停止
|
||||
|
||||
```bash
|
||||
cd /opt/crypto_key
|
||||
./pm2-start.sh # 启动
|
||||
# 或
|
||||
pm2 restart api-key-manager
|
||||
|
||||
./pm2-stop.sh # 停止
|
||||
# 或
|
||||
pm2 stop api-key-manager
|
||||
```
|
||||
|
||||
### 更新代码
|
||||
|
||||
```bash
|
||||
cd /opt/crypto_key
|
||||
git pull
|
||||
source venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
pm2 restart api-key-manager
|
||||
```
|
||||
|
||||
### 备份数据
|
||||
|
||||
```bash
|
||||
sudo cp /opt/crypto_key/data.json /opt/crypto_key/data.json.bak.$(date +%F)
|
||||
```
|
||||
|
||||
`data.json` 含明文 API 密钥,请加密存储或限制文件权限:
|
||||
|
||||
```bash
|
||||
chmod 600 /opt/crypto_key/data.json
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 七、PM2 配置说明
|
||||
|
||||
文件:`/opt/crypto_key/ecosystem.config.cjs`
|
||||
|
||||
| 配置 | 值 |
|
||||
|------|-----|
|
||||
| 进程名 | `api-key-manager` |
|
||||
| 工作目录 | 项目根目录(自动) |
|
||||
| Python | `venv/bin/python` |
|
||||
| 自动重启 | 是 |
|
||||
| 日志 | `logs/pm2-out.log` / `logs/pm2-error.log` |
|
||||
|
||||
---
|
||||
|
||||
## 八、项目文件结构
|
||||
|
||||
```
|
||||
/opt/crypto_key/
|
||||
├── app.py
|
||||
├── index.html
|
||||
├── requirements.txt
|
||||
├── ecosystem.config.cjs
|
||||
├── pm2-start.sh
|
||||
├── pm2-stop.sh
|
||||
├── scripts/
|
||||
│ └── install-ubuntu.sh
|
||||
├── logs/
|
||||
├── venv/
|
||||
├── data.json # 运行时生成,已 .gitignore
|
||||
├── README.md
|
||||
└── DEPLOY.md
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 九、故障排查
|
||||
|
||||
| 现象 | 处理 |
|
||||
|------|------|
|
||||
| `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 |
|
||||
| 更新后异常 | `pip install -r requirements.txt` 后 `pm2 restart api-key-manager` |
|
||||
|
||||
---
|
||||
|
||||
## 十、安全说明
|
||||
|
||||
- 默认绑定 **127.0.0.1**,不对外网直接暴露。
|
||||
- **无登录鉴权**,切勿将 `0.0.0.0:5200` 开放到公网。
|
||||
- `data.json` 已加入 `.gitignore`,不会推送到仓库。
|
||||
- 建议在防火墙仅允许 SSH,通过隧道访问管理页。
|
||||
|
||||
---
|
||||
|
||||
## 十一、Windows 本地开发(附录)
|
||||
|
||||
```powershell
|
||||
cd C:\path\to\crypto_key
|
||||
python -m venv venv
|
||||
.\venv\Scripts\activate
|
||||
pip install -r requirements.txt
|
||||
npm install -g pm2
|
||||
.\pm2-start.ps1
|
||||
```
|
||||
|
||||
访问:http://127.0.0.1:5200
|
||||
|
||||
---
|
||||
|
||||
## 十二、命令速查
|
||||
|
||||
```bash
|
||||
# 首次部署
|
||||
sudo git clone https://git.bz121.com/dekun/crypto_key.git /opt/crypto_key
|
||||
cd /opt/crypto_key && sudo bash scripts/install-ubuntu.sh
|
||||
|
||||
# 日常
|
||||
pm2 status
|
||||
pm2 restart api-key-manager
|
||||
cd /opt/crypto_key && git pull && pm2 restart api-key-manager
|
||||
```
|
||||
@@ -0,0 +1,106 @@
|
||||
# crypto_key — 币圈 API 密钥管理工具
|
||||
|
||||
本地 Web 工具,管理多个交易所/平台 API 账户(账户名称、API Key、API Secret)。数据保存在本地 `data.json`,无登录。
|
||||
|
||||
| 项目 | 说明 |
|
||||
|------|------|
|
||||
| 仓库 | [git.bz121.com/dekun/crypto_key](https://git.bz121.com/dekun/crypto_key.git) |
|
||||
| Ubuntu 部署路径 | `/opt/crypto_key` |
|
||||
| 服务端口 | `5200` |
|
||||
| 进程守护 | PM2 |
|
||||
|
||||
---
|
||||
|
||||
## 功能
|
||||
|
||||
- 账户数量不限,每项含 `username` / `api_key` / `api_secret`
|
||||
- 黑色专业界面,列表展示 + 每项 3 个复制按钮(**复制始终为明文**)
|
||||
- 可选界面打码显示,不影响复制内容
|
||||
- 数据持久化至 `data.json`
|
||||
|
||||
---
|
||||
|
||||
## Ubuntu 服务器部署(/opt)
|
||||
|
||||
完整步骤见 **[DEPLOY.md](./DEPLOY.md)**。
|
||||
|
||||
```bash
|
||||
sudo git clone https://git.bz121.com/dekun/crypto_key.git /opt/crypto_key
|
||||
cd /opt/crypto_key
|
||||
sudo bash scripts/install-ubuntu.sh
|
||||
```
|
||||
|
||||
访问(需在本机或通过 SSH 隧道):**http://127.0.0.1:5200**
|
||||
|
||||
```bash
|
||||
pm2 status
|
||||
pm2 logs api-key-manager
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 本地开发
|
||||
|
||||
```bash
|
||||
python3 -m venv venv
|
||||
source venv/bin/activate # Windows: venv\Scripts\activate
|
||||
pip install -r requirements.txt
|
||||
python app.py
|
||||
```
|
||||
|
||||
浏览器打开 http://127.0.0.1:5200
|
||||
|
||||
PM2 守护:`./pm2-start.sh`(Linux)或 `.\pm2-start.ps1`(Windows)
|
||||
|
||||
---
|
||||
|
||||
## 文件说明
|
||||
|
||||
| 文件 | 说明 |
|
||||
|------|------|
|
||||
| `app.py` | Flask 后端 |
|
||||
| `index.html` | 前端页面 |
|
||||
| `ecosystem.config.cjs` | PM2 配置 |
|
||||
| `scripts/install-ubuntu.sh` | Ubuntu 一键安装 |
|
||||
| `DEPLOY.md` | 完整部署文档 |
|
||||
|
||||
---
|
||||
|
||||
## 数据格式
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "uuid",
|
||||
"username": "账户名称",
|
||||
"api_key": "API Key",
|
||||
"api_secret": "API Secret"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## API
|
||||
|
||||
| 方法 | 路径 | 说明 |
|
||||
|------|------|------|
|
||||
| GET | `/` | 前端页面 |
|
||||
| GET | `/api/accounts` | 获取全部账户 |
|
||||
| POST | `/api/accounts` | 新增账户 |
|
||||
| DELETE | `/api/accounts/<id>` | 删除账户 |
|
||||
|
||||
---
|
||||
|
||||
## 安全提示
|
||||
|
||||
- 无认证,仅绑定 `127.0.0.1`,请勿暴露公网
|
||||
- `data.json` 为明文密钥,已 `.gitignore`,请定期备份
|
||||
|
||||
---
|
||||
|
||||
## 依赖
|
||||
|
||||
```
|
||||
flask>=3.0.0,<4.0.0
|
||||
```
|
||||
|
||||
服务器另需:Python 3.8+、Node.js(PM2)
|
||||
@@ -0,0 +1,83 @@
|
||||
"""
|
||||
多账户 API 密钥管理工具 — Flask 后端
|
||||
端口: 5200 | 数据: data.json
|
||||
"""
|
||||
import json
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
from flask import Flask, jsonify, request, send_from_directory
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent
|
||||
DATA_FILE = BASE_DIR / "data.json"
|
||||
|
||||
|
||||
def load_accounts():
|
||||
if not DATA_FILE.exists():
|
||||
return []
|
||||
try:
|
||||
with open(DATA_FILE, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
if isinstance(data, list):
|
||||
return data
|
||||
return []
|
||||
except (json.JSONDecodeError, OSError):
|
||||
return []
|
||||
|
||||
|
||||
def save_accounts(accounts):
|
||||
with open(DATA_FILE, "w", encoding="utf-8") as f:
|
||||
json.dump(accounts, f, ensure_ascii=False, indent=2)
|
||||
|
||||
|
||||
def validate_account_payload(payload):
|
||||
username = (payload.get("username") or "").strip()
|
||||
api_key = (payload.get("api_key") or "").strip()
|
||||
api_secret = (payload.get("api_secret") or "").strip()
|
||||
if not username:
|
||||
return None, "账户名称不能为空"
|
||||
if not api_key:
|
||||
return None, "API Key 不能为空"
|
||||
if not api_secret:
|
||||
return None, "API Secret 不能为空"
|
||||
return {"username": username, "api_key": api_key, "api_secret": api_secret}, None
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return send_from_directory(BASE_DIR, "index.html")
|
||||
|
||||
|
||||
@app.route("/api/accounts", methods=["GET"])
|
||||
def list_accounts():
|
||||
return jsonify(load_accounts())
|
||||
|
||||
|
||||
@app.route("/api/accounts", methods=["POST"])
|
||||
def create_account():
|
||||
body = request.get_json(silent=True) or {}
|
||||
account, err = validate_account_payload(body)
|
||||
if err:
|
||||
return jsonify({"error": err}), 400
|
||||
|
||||
accounts = load_accounts()
|
||||
account["id"] = str(uuid.uuid4())
|
||||
accounts.append(account)
|
||||
save_accounts(accounts)
|
||||
return jsonify(account), 201
|
||||
|
||||
|
||||
@app.route("/api/accounts/<account_id>", methods=["DELETE"])
|
||||
def delete_account(account_id):
|
||||
accounts = load_accounts()
|
||||
new_accounts = [a for a in accounts if a.get("id") != account_id]
|
||||
if len(new_accounts) == len(accounts):
|
||||
return jsonify({"error": "账户不存在"}), 404
|
||||
save_accounts(new_accounts)
|
||||
return jsonify({"ok": True})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="127.0.0.1", port=5200, debug=False)
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* PM2 进程配置 — API 密钥管理工具
|
||||
* 用法: pm2 start ecosystem.config.cjs
|
||||
*/
|
||||
const path = require("path");
|
||||
|
||||
const ROOT = __dirname;
|
||||
const isWin = process.platform === "win32";
|
||||
|
||||
// 优先使用项目内 venv;若无 venv,改为 "python" 或系统 Python 绝对路径
|
||||
const venvPython = isWin
|
||||
? path.join(ROOT, "venv", "Scripts", "python.exe")
|
||||
: path.join(ROOT, "venv", "bin", "python");
|
||||
|
||||
module.exports = {
|
||||
apps: [
|
||||
{
|
||||
name: "api-key-manager",
|
||||
script: "app.py",
|
||||
cwd: ROOT,
|
||||
interpreter: venvPython,
|
||||
instances: 1,
|
||||
exec_mode: "fork",
|
||||
autorestart: true,
|
||||
watch: false,
|
||||
max_restarts: 10,
|
||||
min_uptime: "5s",
|
||||
max_memory_restart: "200M",
|
||||
error_file: path.join(ROOT, "logs", "pm2-error.log"),
|
||||
out_file: path.join(ROOT, "logs", "pm2-out.log"),
|
||||
log_date_format: "YYYY-MM-DD HH:mm:ss",
|
||||
merge_logs: true,
|
||||
env: {
|
||||
PYTHONUNBUFFERED: "1",
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
+473
@@ -0,0 +1,473 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>API 密钥管理</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg: #0d0d0f;
|
||||
--surface: #16161a;
|
||||
--surface-hover: #1c1c22;
|
||||
--border: #2a2a32;
|
||||
--text: #e4e4e7;
|
||||
--text-muted: #71717a;
|
||||
--accent: #3b82f6;
|
||||
--accent-hover: #2563eb;
|
||||
--success: #22c55e;
|
||||
--danger: #ef4444;
|
||||
--radius: 8px;
|
||||
--font: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
|
||||
--mono: "Cascadia Code", "Consolas", "SF Mono", monospace;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
|
||||
body {
|
||||
font-family: var(--font);
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
min-height: 100vh;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.wrap {
|
||||
max-width: 960px;
|
||||
margin: 0 auto;
|
||||
padding: 32px 20px 48px;
|
||||
}
|
||||
|
||||
header {
|
||||
margin-bottom: 32px;
|
||||
padding-bottom: 20px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
header h1 {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 600;
|
||||
letter-spacing: -0.02em;
|
||||
}
|
||||
|
||||
header p {
|
||||
margin-top: 6px;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.panel {
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
padding: 20px;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.panel h2 {
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.06em;
|
||||
color: var(--text-muted);
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.form-grid {
|
||||
display: grid;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
@media (min-width: 640px) {
|
||||
.form-grid { grid-template-columns: 1fr 1fr 1fr; }
|
||||
.form-actions { grid-column: 1 / -1; }
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-muted);
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
input {
|
||||
width: 100%;
|
||||
padding: 10px 12px;
|
||||
background: var(--bg);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
color: var(--text);
|
||||
font-family: var(--mono);
|
||||
font-size: 0.8125rem;
|
||||
}
|
||||
|
||||
input:focus {
|
||||
outline: none;
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 6px;
|
||||
padding: 8px 14px;
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 500;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
transition: background 0.15s, color 0.15s;
|
||||
font-family: var(--font);
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.btn-primary:hover { background: var(--accent-hover); }
|
||||
|
||||
.btn-ghost {
|
||||
background: transparent;
|
||||
color: var(--text-muted);
|
||||
border: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.btn-ghost:hover {
|
||||
background: var(--surface-hover);
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.btn-copy {
|
||||
background: var(--bg);
|
||||
color: var(--text-muted);
|
||||
border: 1px solid var(--border);
|
||||
padding: 4px 10px;
|
||||
font-size: 0.75rem;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.btn-copy:hover {
|
||||
color: var(--text);
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.btn-copy.copied {
|
||||
color: var(--success);
|
||||
border-color: var(--success);
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background: transparent;
|
||||
color: var(--danger);
|
||||
border: 1px solid transparent;
|
||||
padding: 4px 8px;
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
.btn-danger:hover {
|
||||
background: rgba(239, 68, 68, 0.1);
|
||||
border-color: var(--danger);
|
||||
}
|
||||
|
||||
.toolbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.toolbar .count {
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.toggle-mask {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-muted);
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.toggle-mask input { width: auto; accent-color: var(--accent); }
|
||||
|
||||
.account-list { display: flex; flex-direction: column; gap: 12px; }
|
||||
|
||||
.account-card {
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.account-card:hover { border-color: #3f3f48; }
|
||||
|
||||
.account-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 12px;
|
||||
padding-bottom: 10px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.account-name {
|
||||
font-weight: 600;
|
||||
font-size: 0.9375rem;
|
||||
}
|
||||
|
||||
.field {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.field:last-child { margin-bottom: 0; }
|
||||
|
||||
.field-label {
|
||||
width: 88px;
|
||||
flex-shrink: 0;
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.field-value {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
font-family: var(--mono);
|
||||
font-size: 0.8125rem;
|
||||
word-break: break-all;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.empty {
|
||||
text-align: center;
|
||||
padding: 48px 20px;
|
||||
color: var(--text-muted);
|
||||
font-size: 0.875rem;
|
||||
border: 1px dashed var(--border);
|
||||
border-radius: var(--radius);
|
||||
}
|
||||
|
||||
.toast {
|
||||
position: fixed;
|
||||
bottom: 24px;
|
||||
right: 24px;
|
||||
padding: 10px 16px;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--success);
|
||||
color: var(--success);
|
||||
border-radius: 6px;
|
||||
font-size: 0.8125rem;
|
||||
opacity: 0;
|
||||
transform: translateY(8px);
|
||||
transition: opacity 0.2s, transform 0.2s;
|
||||
pointer-events: none;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.toast.show {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
.error-msg {
|
||||
color: var(--danger);
|
||||
font-size: 0.8125rem;
|
||||
margin-top: 8px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrap">
|
||||
<header>
|
||||
<h1>API 密钥管理</h1>
|
||||
<p>本地多账户存储 · 数据保存在 data.json</p>
|
||||
</header>
|
||||
|
||||
<section class="panel">
|
||||
<h2>添加账户</h2>
|
||||
<form id="addForm" class="form-grid">
|
||||
<div>
|
||||
<label for="username">账户名称 (username)</label>
|
||||
<input type="text" id="username" name="username" placeholder="例如: main_account" autocomplete="off" required>
|
||||
</div>
|
||||
<div>
|
||||
<label for="api_key">API Key</label>
|
||||
<input type="text" id="api_key" name="api_key" placeholder="输入 API Key" autocomplete="off" required>
|
||||
</div>
|
||||
<div>
|
||||
<label for="api_secret">API Secret</label>
|
||||
<input type="text" id="api_secret" name="api_secret" placeholder="输入 API Secret" autocomplete="off" required>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="btn btn-primary">添加账户</button>
|
||||
<p id="formError" class="error-msg" hidden></p>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<div class="toolbar">
|
||||
<span class="count" id="accountCount">共 0 个账户</span>
|
||||
<label class="toggle-mask">
|
||||
<input type="checkbox" id="maskToggle">
|
||||
隐藏敏感字段(复制仍为明文)
|
||||
</label>
|
||||
</div>
|
||||
<div id="accountList" class="account-list"></div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div id="toast" class="toast">已复制到剪贴板</div>
|
||||
|
||||
<script>
|
||||
const API = "/api/accounts";
|
||||
let accounts = [];
|
||||
let masked = false;
|
||||
|
||||
const listEl = document.getElementById("accountList");
|
||||
const countEl = document.getElementById("accountCount");
|
||||
const maskToggle = document.getElementById("maskToggle");
|
||||
const toast = document.getElementById("toast");
|
||||
const form = document.getElementById("addForm");
|
||||
const formError = document.getElementById("formError");
|
||||
|
||||
function maskValue(value) {
|
||||
if (!masked || !value) return value;
|
||||
return "•".repeat(Math.min(value.length, 24));
|
||||
}
|
||||
|
||||
function showToast(msg) {
|
||||
toast.textContent = msg;
|
||||
toast.classList.add("show");
|
||||
clearTimeout(showToast._t);
|
||||
showToast._t = setTimeout(() => toast.classList.remove("show"), 1800);
|
||||
}
|
||||
|
||||
async function copyText(text, btn) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
btn.classList.add("copied");
|
||||
btn.textContent = "已复制";
|
||||
showToast("已复制到剪贴板");
|
||||
setTimeout(() => {
|
||||
btn.classList.remove("copied");
|
||||
btn.textContent = "复制";
|
||||
}, 1500);
|
||||
} catch {
|
||||
const ta = document.createElement("textarea");
|
||||
ta.value = text;
|
||||
document.body.appendChild(ta);
|
||||
ta.select();
|
||||
document.execCommand("copy");
|
||||
document.body.removeChild(ta);
|
||||
showToast("已复制到剪贴板");
|
||||
}
|
||||
}
|
||||
|
||||
function renderField(label, value, copyLabel) {
|
||||
const row = document.createElement("div");
|
||||
row.className = "field";
|
||||
row.innerHTML = `
|
||||
<span class="field-label">${label}</span>
|
||||
<span class="field-value"></span>
|
||||
`;
|
||||
row.querySelector(".field-value").textContent = maskValue(value);
|
||||
|
||||
const btn = document.createElement("button");
|
||||
btn.type = "button";
|
||||
btn.className = "btn btn-copy";
|
||||
btn.textContent = "复制";
|
||||
btn.title = `复制${copyLabel}`;
|
||||
btn.addEventListener("click", () => copyText(value, btn));
|
||||
row.appendChild(btn);
|
||||
return row;
|
||||
}
|
||||
|
||||
function renderAccount(acc) {
|
||||
const card = document.createElement("article");
|
||||
card.className = "account-card";
|
||||
card.dataset.id = acc.id;
|
||||
|
||||
const header = document.createElement("div");
|
||||
header.className = "account-header";
|
||||
header.innerHTML = `<span class="account-name"></span>`;
|
||||
header.querySelector(".account-name").textContent = maskValue(acc.username);
|
||||
|
||||
const delBtn = document.createElement("button");
|
||||
delBtn.type = "button";
|
||||
delBtn.className = "btn btn-danger";
|
||||
delBtn.textContent = "删除";
|
||||
delBtn.addEventListener("click", () => deleteAccount(acc.id));
|
||||
header.appendChild(delBtn);
|
||||
card.appendChild(header);
|
||||
|
||||
card.appendChild(renderField("账户名称", acc.username, "账户名称"));
|
||||
card.appendChild(renderField("API Key", acc.api_key, "API Key"));
|
||||
card.appendChild(renderField("API Secret", acc.api_secret, "API Secret"));
|
||||
|
||||
return card;
|
||||
}
|
||||
|
||||
function renderList() {
|
||||
countEl.textContent = `共 ${accounts.length} 个账户`;
|
||||
listEl.innerHTML = "";
|
||||
if (accounts.length === 0) {
|
||||
listEl.innerHTML = '<div class="empty">暂无账户,请在上方添加</div>';
|
||||
return;
|
||||
}
|
||||
accounts.forEach(acc => listEl.appendChild(renderAccount(acc)));
|
||||
}
|
||||
|
||||
async function loadAccounts() {
|
||||
const res = await fetch(API);
|
||||
accounts = await res.json();
|
||||
renderList();
|
||||
}
|
||||
|
||||
async function deleteAccount(id) {
|
||||
if (!confirm("确定删除该账户?")) return;
|
||||
await fetch(`${API}/${id}`, { method: "DELETE" });
|
||||
accounts = accounts.filter(a => a.id !== id);
|
||||
renderList();
|
||||
}
|
||||
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
formError.hidden = true;
|
||||
const payload = {
|
||||
username: document.getElementById("username").value,
|
||||
api_key: document.getElementById("api_key").value,
|
||||
api_secret: document.getElementById("api_secret").value,
|
||||
};
|
||||
const res = await fetch(API, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) {
|
||||
formError.textContent = data.error || "添加失败";
|
||||
formError.hidden = false;
|
||||
return;
|
||||
}
|
||||
accounts.push(data);
|
||||
form.reset();
|
||||
renderList();
|
||||
showToast("账户已添加");
|
||||
});
|
||||
|
||||
maskToggle.addEventListener("change", () => {
|
||||
masked = maskToggle.checked;
|
||||
renderList();
|
||||
});
|
||||
|
||||
loadAccounts();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,13 @@
|
||||
# PM2 重启脚本 (Windows PowerShell)
|
||||
# 用法: .\pm2-restart.ps1
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
if (-not (Get-Command pm2 -ErrorAction SilentlyContinue)) {
|
||||
Write-Host "[错误] 未找到 pm2" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
pm2 restart api-key-manager
|
||||
Write-Host "已重启 api-key-manager" -ForegroundColor Green
|
||||
Write-Host "访问: http://127.0.0.1:5200" -ForegroundColor Cyan
|
||||
@@ -0,0 +1,27 @@
|
||||
# PM2 启动脚本 (Windows PowerShell)
|
||||
# 用法: .\pm2-start.ps1
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
$Root = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
Set-Location $Root
|
||||
|
||||
if (-not (Get-Command pm2 -ErrorAction SilentlyContinue)) {
|
||||
Write-Host "[错误] 未找到 pm2,请先执行: npm install -g pm2" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
$venvPy = Join-Path $Root "venv\Scripts\python.exe"
|
||||
if (-not (Test-Path $venvPy)) {
|
||||
Write-Host "[警告] 未检测到 venv,请先运行: python -m venv venv ; .\venv\Scripts\activate ; pip install -r requirements.txt" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
if (-not (Test-Path "logs")) {
|
||||
New-Item -ItemType Directory -Path "logs" | Out-Null
|
||||
}
|
||||
|
||||
pm2 start ecosystem.config.cjs
|
||||
pm2 save 2>$null
|
||||
Write-Host ""
|
||||
Write-Host "已启动。访问: http://127.0.0.1:5200" -ForegroundColor Green
|
||||
Write-Host "查看状态: pm2 status" -ForegroundColor Cyan
|
||||
Write-Host "查看日志: pm2 logs api-key-manager" -ForegroundColor Cyan
|
||||
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
# PM2 启动脚本 (Linux / macOS)
|
||||
# 用法: chmod +x pm2-start.sh && ./pm2-start.sh
|
||||
|
||||
set -e
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
if ! command -v pm2 >/dev/null 2>&1; then
|
||||
echo "[错误] 未找到 pm2,请先执行: npm install -g pm2"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -f "venv/bin/python" ]; then
|
||||
echo "[警告] 未检测到 venv,请先: python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt"
|
||||
fi
|
||||
|
||||
mkdir -p logs
|
||||
pm2 start ecosystem.config.cjs
|
||||
pm2 save 2>/dev/null || true
|
||||
|
||||
echo ""
|
||||
echo "已启动。访问: http://127.0.0.1:5200"
|
||||
echo "查看状态: pm2 status"
|
||||
echo "查看日志: pm2 logs api-key-manager"
|
||||
@@ -0,0 +1,12 @@
|
||||
# PM2 停止脚本 (Windows PowerShell)
|
||||
# 用法: .\pm2-stop.ps1
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
if (-not (Get-Command pm2 -ErrorAction SilentlyContinue)) {
|
||||
Write-Host "[错误] 未找到 pm2" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
pm2 stop api-key-manager
|
||||
Write-Host "已停止 api-key-manager" -ForegroundColor Green
|
||||
@@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# PM2 停止脚本 (Linux / macOS)
|
||||
|
||||
set -e
|
||||
pm2 stop api-key-manager
|
||||
echo "已停止 api-key-manager"
|
||||
@@ -0,0 +1 @@
|
||||
flask>=3.0.0,<4.0.0
|
||||
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env bash
|
||||
# Ubuntu 一键安装 — 部署到 /opt/crypto_key
|
||||
# 用法: sudo bash scripts/install-ubuntu.sh
|
||||
|
||||
set -e
|
||||
|
||||
REPO_URL="https://git.bz121.com/dekun/crypto_key.git"
|
||||
INSTALL_DIR="/opt/crypto_key"
|
||||
APP_USER="${SUDO_USER:-$USER}"
|
||||
|
||||
echo "==> 安装系统依赖..."
|
||||
export DEBIAN_FRONTEND=noninteractive
|
||||
apt-get update -qq
|
||||
apt-get install -y -qq python3 python3-venv python3-pip git curl ca-certificates
|
||||
|
||||
if ! command -v node >/dev/null 2>&1; then
|
||||
echo "==> 安装 Node.js (用于 PM2)..."
|
||||
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
|
||||
apt-get install -y -qq nodejs
|
||||
fi
|
||||
|
||||
if ! command -v pm2 >/dev/null 2>&1; then
|
||||
echo "==> 安装 PM2..."
|
||||
npm install -g pm2
|
||||
fi
|
||||
|
||||
echo "==> 拉取代码到 ${INSTALL_DIR} ..."
|
||||
mkdir -p /opt
|
||||
if [ -d "${INSTALL_DIR}/.git" ]; then
|
||||
cd "${INSTALL_DIR}"
|
||||
git pull origin main 2>/dev/null || git pull origin master 2>/dev/null || git pull
|
||||
else
|
||||
git clone "${REPO_URL}" "${INSTALL_DIR}"
|
||||
cd "${INSTALL_DIR}"
|
||||
fi
|
||||
|
||||
echo "==> 创建 Python 虚拟环境..."
|
||||
python3 -m venv venv
|
||||
source venv/bin/activate
|
||||
pip install -q --upgrade pip
|
||||
pip install -q -r requirements.txt
|
||||
|
||||
mkdir -p logs
|
||||
chmod +x pm2-start.sh pm2-stop.sh 2>/dev/null || true
|
||||
|
||||
if [ -n "${APP_USER}" ] && [ "${APP_USER}" != "root" ]; then
|
||||
chown -R "${APP_USER}:${APP_USER}" "${INSTALL_DIR}"
|
||||
fi
|
||||
|
||||
echo "==> 启动 PM2 守护..."
|
||||
cd "${INSTALL_DIR}"
|
||||
pm2 delete api-key-manager 2>/dev/null || true
|
||||
pm2 start ecosystem.config.cjs
|
||||
pm2 save
|
||||
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo " 安装完成"
|
||||
echo " 目录: ${INSTALL_DIR}"
|
||||
echo " 访问: http://127.0.0.1:5200"
|
||||
echo " 状态: pm2 status"
|
||||
echo " 日志: pm2 logs api-key-manager"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
echo "开机自启请执行: pm2 startup"
|
||||
echo "按提示复制 sudo 命令执行后,再运行: pm2 save"
|
||||
Reference in New Issue
Block a user