This commit is contained in:
yisier
2026-06-08 00:11:12 -06:00
parent 438d5084c9
commit 2b5aefd1e8
4 changed files with 414 additions and 179 deletions
+2 -2
View File
@@ -31,8 +31,8 @@ footer: GPL-3.0 License · 基于 ehang-io/nps 0.26.10 二次开发
本站文档对应 [`yisier/nps`](https://github.com/yisier/nps) fork 版本,当前版本 **v0.26.34**。
- 想升级 / 重新部署?看 [启动](/client/run.html) 和 [服务端使用](/server/nps_use.html)
- 第一次接触?走一遍 [安装](/install/) → [启动](/client/run.html) → [使用示例](/extend/example.html)
- 想升级 / 重新部署?看 [启动](/client/nps_use.html) 和 [服务端使用](/server/nps_use.html)
- 第一次接触?走一遍 [安装](/install/) → [启动](/client/nps_use.html) → [使用示例](/extend/example.html)
- 想知道每个版本改了什么?查 [更新日志](/changelog/)
- 想用 API 做自动化?查 [Web API](/extend/api.html) 和 [Web API 详细](/extend/webapi.html)
+102 -32
View File
@@ -1,45 +1,115 @@
# web api
# Web API 鉴权
需要开启请先去掉`nps.conf`中`auth_key`的注释并配置一个合适的密钥
## webAPI验证说明
- 采用auth_key的验证方式
- 在提交的每个请求后面附带两个参数,`auth_key` 和`timestamp`
在 `nps.conf` 中配置 `auth_key` 即可启用 API 鉴权(首次启动自动生成)。
## 鉴权方式
每个请求需附带两个参数:
| 参数 | 说明 |
| --- | --- |
| `auth_key` | `md5(配置文件中的 auth_key + 当前时间戳)` |
| `timestamp` | 当前 unix 时间戳(秒) |
时间戳有效范围为 **20 秒**,每次请求须重新生成。
## 获取服务端时间戳
由于客户端与服务端时间可能不一致,可先获取服务端时间:
```
auth_key的生成方式为:md5(配置文件中的auth_key+当前时间戳)
GET /auth/gettime/
```
```
timestamp为当前时间戳
```
```
curl --request POST \
--url http://127.0.0.1:8080/client/list \
--data 'auth_key=2a0000d9229e7dbcf79dd0f5e04bb084&timestamp=1553045344&start=0&limit=10'
```
**注意:** 为保证安全,时间戳的有效范围为20秒内,所以每次提交请求必须重新生成。
## 获取服务端时间
由于服务端与api请求的客户端时间差异不能太大,所以提供了一个可以获取服务端时间的接口
```
POST /auth/gettime
返回:
```json
{"time": 1717654321}
```
## 获取服务端authKey
> 此接口无需鉴权。
如果想获取authKey,服务端提供获取authKey的接口
## 获取服务端 auth_key(加密)
```
POST /auth/getauthkey
GET /auth/getauthkey/
```
将返回加密后的authKey,采用aes cbc加密,请使用与服务端配置文件中cryptKey相同的密钥进行解密
**注意:** nps配置文件中`auth_crypt_key`需为16位
- 解密密钥长度128
- 偏移量与密钥相同
- 补码方式pkcs5padding
- 解密串编码方式 十六进制
返回经 AES-CBC 加密后的 `auth_key`(hex 编码)。
## 详细文档
- **[详见](/extend/webapi.md)** (感谢@avengexyz)
> 此接口无需鉴权。需确保 `nps.conf` 中 `auth_crypt_key` 为 **16 位**。
解密参数:
- 算法:AES-128-CBC
- 密钥:`auth_crypt_key`(16 字节)
- IV:与密钥相同
- 填充:PKCS5Padding
- 密文编码:hex
---
## 接入示例
::: tabs
@tab curl
```bash
# 1. 获取服务端时间戳
ts=$(curl -s http://127.0.0.1:8080/auth/gettime/ | sed 's/.*"time":\([0-9]*\).*/\1/')
# 2. 计算签名(Linux)
sign=$(echo -n "your_auth_key${ts}" | md5sum | awk '{print $1}')
# 或 macOS:
# sign=$(echo -n "your_auth_key${ts}" | md5)
# 3. 调用接口
curl -s -X POST "http://127.0.0.1:8080/client/list/" \
-d "auth_key=${sign}&timestamp=${ts}&search=&order=asc&offset=0&limit=10"
```
@tab Python
```python
import hashlib, requests
host = "http://127.0.0.1:8080"
auth_key = "your_auth_key"
ts = requests.get(f"{host}/auth/gettime/").json()["time"]
sign = hashlib.md5(f"{auth_key}{ts}".encode()).hexdigest()
r = requests.post(f"{host}/client/list/", data={
"auth_key": sign, "timestamp": ts,
"search": "", "order": "asc", "offset": 0, "limit": 10
})
print(r.json())
```
@tab JavaScript
```javascript
const crypto = require("crypto");
const host = "http://127.0.0.1:8080";
const authKey = "your_auth_key";
(async () => {
const ts = (await (await fetch(`${host}/auth/gettime/`)).json()).time;
const sign = crypto.createHash("md5").update(`${authKey}${ts}`).digest("hex");
const r = await fetch(`${host}/client/list/`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({ auth_key: sign, timestamp: ts, search: "", order: "asc", offset: 0, limit: 10 }).toString(),
});
console.log(await r.json());
})();
```
:::
---
## 详细接口清单
- [Web API 接口文档](webapi.html)
+1 -1
View File
@@ -1,5 +1,5 @@
# 使用示例
## 统一准备工作(必做)
## 统一准备工作
- 开启服务端,假设公网服务器ip为1.1.1.1,配置文件中`bridge_port`为8024,配置文件中`web_port`为8080
- 访问1.1.1.1:8080
- 在客户端管理中创建一个客户端,记录下验证密钥
+309 -144
View File
@@ -1,31 +1,53 @@
获取客户端列表
# Web API
> 除 `AuthController` 和 `LoginController` 外,所有接口均需通过鉴权,详见 [API 鉴权说明](api.html)。
## 通用响应格式
| 响应类型 | 格式 |
| --- | --- |
| 成功 | `{"status": 1, "msg": "success"}` |
| 成功(含 id) | `{"status": 1, "msg": "success", "id": 123}` |
| 失败 | `{"status": 0, "msg": "error message"}` |
| 列表 | `{"rows": [...], "total": 100}` |
> 单个查询接口(`GetClient`、`GetOneTunnel`、`GetHost`)返回 `{"code": 1, "data": {...}}` 或 `{"code": 0}`。
---
## Client 客户端管理
### 客户端列表
```
POST /client/list/
```
| 参数 | 含义 |
| --- | --- |
| search | 搜索 |
| order | 排序asc 正序 desc倒序 |
| offset | 分页(第几页) |
| limit | 条数(分页显示的条数) |
| search | 搜索关键词 |
| sort | 排序字段 |
| order | asc 正序 / desc 倒序 |
| offset | 分页起始 |
| limit | 每页条数 |
***
获取单个客户端
返回 `AjaxTable` 格式,额外含 `ip`、`bridgeType`、`bridgePort` 字段。
---
### 获取单个客户端
```
POST /client/getclient/
```
| 参数 | 含义 |
| --- | --- |
| id | 客户端id |
| id | 客户端 id |
***
添加客户端
---
### 添加客户端
```
POST /client/add/
@@ -34,19 +56,27 @@ POST /client/add/
| 参数 | 含义 |
| --- | --- |
| remark | 备注 |
| u | basic权限认证用户名 |
| p | basic权限认证密码 |
| limit | 条数(分页显示的条数) |
| vkey | 客户端验证密钥 |
| config\_conn\_allow | 是否允许客户端以配置文件模式连接 1允许 0不允许 |
| rate\_limit | 带宽限制 单位KB/S 空则为不限制 |
| flow\_limit | 流量限制 单位M 空则为不限制 |
| max\_conn | 客户端最大连接数量 空则为不限制 |
| max\_tunnel | 客户端最大隧道数量 空则为不限制 |
| expire\_time | 到期时间,留空表示永不过期,到期后自动暂停。支持格式:`2006-01-02 15:04:05`、`2006-01-02 15:04`、`2006-01-02T15:04:05`、`2006-01-02T15:04`、`2006-01-02` |
| u | basic 权限认证用户名 |
| p | basic 权限认证密码 |
| compress | 是否压缩传输,`true` / `false` |
| crypt | 是否加密传输,`true` / `false` |
| config_conn_allow | 是否允许客户端以配置文件模式连接,`true` / `false` |
| rate_limit | 带宽限制,单位 KB/s,留空不限制 |
| flow_limit | 流量限制,单位 M,留空不限制 |
| max_conn | 最大连接数,留空不限制 |
| max_tunnel | 最大隧道数,留空不限制 |
| web_username | web 用户登录用户名 |
| web_password | web 用户登录密码 |
| blackiplist | 黑名单 IP 列表,`\r\n` 分隔 |
| ipwhite | 是否开启 IP 白名单,`true` / `false` |
| ipwhitepass | IP 白名单授权密码 |
| ipwhitelist | 白名单 IP 列表,`\r\n` 分隔 |
| expire_time | 到期时间,留空表示永不过期。支持格式:`2006-01-02 15:04:05`、`2006-01-02 15:04`、`2006-01-02T15:04:05`、`2006-01-02T15:04`、`2006-01-02` |
***
修改客户端
---
### 修改客户端
```
POST /client/edit/
@@ -54,21 +84,42 @@ POST /client/edit/
| 参数 | 含义 |
| --- | --- |
| id | 要修改的客户端 id |
| remark | 备注 |
| u | basic权限认证用户名 |
| p | basic权限认证密码 |
| limit | 条数(分页显示的条数) |
| vkey | 客户端验证密钥 |
| config\_conn\_allow | 是否允许客户端以配置文件模式连接 1允许 0不允许 |
| rate\_limit | 带宽限制 单位KB/S 空则为不限制 |
| flow\_limit | 流量限制 单位M 空则为不限制 |
| max\_conn | 客户端最大连接数量 空则为不限制 |
| max\_tunnel | 客户端最大隧道数量 空则为不限制 |
| expire\_time | 到期时间,留空表示永不过期,到期后自动暂停。格式同新增接口 |
| id | 要修改的客户端id |
| vkey | 客户端验证密钥(仅管理员可修改) |
| u | basic 权限认证用户名 |
| p | basic 权限认证密码 |
| compress | 是否压缩传输,`true` / `false` |
| crypt | 是否加密传输,`true` / `false` |
| config_conn_allow | 是否允许客户端以配置文件模式连接,`true` / `false` |
| rate_limit | 带宽限制,单位 KB/s(仅管理员可修改) |
| flow_limit | 流量限制,单位 M(仅管理员可修改) |
| max_conn | 最大连接数(仅管理员可修改) |
| max_tunnel | 最大隧道数(仅管理员可修改) |
| web_username | web 用户登录用户名 |
| web_password | web 用户登录密码 |
| blackiplist | 黑名单 IP 列表,`\r\n` 分隔 |
| ipwhite | 是否开启 IP 白名单,`true` / `false` |
| ipwhitepass | IP 白名单授权密码 |
| ipwhitelist | 白名单 IP 列表,`\r\n` 分隔 |
| expire_time | 到期时间,格式同新增接口 |
***
删除客户端
---
### 更改客户端状态
```
POST /client/changestatus/
```
| 参数 | 含义 |
| --- | --- |
| id | 客户端 id |
| status | `true` 启用 / `false` 禁用 |
---
### 删除客户端
```
POST /client/del/
@@ -76,83 +127,13 @@ POST /client/del/
| 参数 | 含义 |
| --- | --- |
| id | 要删除的客户端id |
| id | 要删除的客户端 id |
***
获取域名解析列表
---
```
POST /index/hostlist/
```
## Index 隧道管理
| 参数 | 含义 |
| --- | --- |
| search | 搜索(可以搜域名/备注什么的) |
| offset | 分页(第几页) |
| limit | 条数(分页显示的条数) |
***
添加域名解析
```
POST /index/addhost/
```
| 参数 | 含义 |
| --- | --- |
| remark | 备注 |
| host | 域名 |
| scheme | 协议类型(三种 all http https) |
| location | url路由 空则为不限制 |
| client\_id | 客户端id |
| target | 内网目标(ip:端口) |
| header | request header 请求头 |
| hostchange | request host 请求主机 |
***
修改域名解析
```
POST /index/edithost/
```
| 参数 | 含义 |
| --- | --- |
| remark | 备注 |
| host | 域名 |
| scheme | 协议类型(三种 all http https) |
| location | url路由 空则为不限制 |
| client\_id | 客户端id |
| target | 内网目标(ip:端口) |
| header | request header 请求头 |
| hostchange | request host 请求主机 |
| id | 需要修改的域名解析id |
***
删除域名解析
```
POST /index/delhost/
```
| 参数 | 含义 |
| --- | --- |
| id | 需要删除的域名解析id |
***
获取单条隧道信息
```
POST /index/getonetunnel/
```
| 参数 | 含义 |
| --- | --- |
| id | 隧道的id |
***
获取隧道列表
### 隧道列表
```
POST /index/gettunnel/
@@ -160,14 +141,29 @@ POST /index/gettunnel/
| 参数 | 含义 |
| --- | --- |
| client\_id | 穿透隧道的客户端id |
| type | 类型tcp udp httpProx socks5 secret p2p |
| search | 搜索 |
| offset | 分页(第几页) |
| limit | 条数(分页显示的条数) |
| client_id | 客户端 id |
| type | 隧道类型:`tcp`、`udp`、`httpProxy`、`socks5`、`secret`、`p2p`、`file` |
| search | 搜索关键词 |
| sort | 排序字段 |
| order | asc 正序 / desc 倒序 |
| offset | 分页起始 |
| limit | 每页条数 |
***
添加隧道
---
### 获取单条隧道
```
POST /index/getonetunnel/
```
| 参数 | 含义 |
| --- | --- |
| id | 隧道 id |
---
### 添加隧道
```
POST /index/add/
@@ -175,14 +171,35 @@ POST /index/add/
| 参数 | 含义 |
| --- | --- |
| type | 类型tcp udp httpProx socks5 secret p2p |
| client_id | 客户端 id |
| type | 隧道类型:`tcp`、`udp`、`httpProxy`、`socks5`、`secret`、`p2p`、`file` |
| remark | 备注 |
| port | 服务端端口 |
| target | 目标(ip:端口) |
| client\_id | 客户端id |
| port | 服务端端口(端口为 0 或留空时自动分配) |
| server_ip | 绑定的服务端 IP(多 IP 场景) |
| target | 内网目标,格式 `ip:端口` |
| local_proxy | 是否转发到 nps 服务器本地,`true` / `false` |
| password | 隧道密码(secret 模式) |
| local_path | 本地文件路径(file 模式) |
| strip_pre | URL 前缀去除(httpProxy 模式) |
| proto_version | 协议版本 |
***
修改隧道
---
### 复制隧道
```
POST /index/copy/
```
| 参数 | 含义 |
| --- | --- |
| id | 要复制的源隧道 id |
复制后自动分配新端口和新 id,其他配置沿用源隧道。
---
### 修改隧道
```
POST /index/edit/
@@ -190,26 +207,22 @@ POST /index/edit/
| 参数 | 含义 |
| --- | --- |
| type | 类型tcp udp httpProx socks5 secret p2p |
| remark | 备注 |
| id | 隧道 id |
| client_id | 客户端 id |
| type | 隧道类型 |
| port | 服务端端口 |
| target | 目标(ip:端口) |
| client\_id | 客户端id |
| id | 隧道id |
| server_ip | 绑定的服务端 IP |
| target | 内网目标 |
| local_proxy | 是否转发到 nps 服务器本地 |
| remark | 备注 |
| password | 隧道密码 |
| local_path | 本地文件路径 |
| strip_pre | URL 前缀去除 |
| proto_version | 协议版本 |
***
删除隧道
---
```
POST /index/del/
```
| 参数 | 含义 |
| --- | --- |
| id | 隧道id |
***
隧道停止工作
### 停止隧道
```
POST /index/stop/
@@ -217,10 +230,11 @@ POST /index/stop/
| 参数 | 含义 |
| --- | --- |
| id | 隧道id |
| id | 隧道 id |
***
隧道开始工作
---
### 启动隧道
```
POST /index/start/
@@ -228,4 +242,155 @@ POST /index/start/
| 参数 | 含义 |
| --- | --- |
| id | 隧道id |
| id | 隧道 id |
---
### 删除隧道
```
POST /index/del/
```
| 参数 | 含义 |
| --- | --- |
| id | 隧道 id |
---
## Host 域名解析管理
### 域名列表
```
POST /index/hostlist/
```
| 参数 | 含义 |
| --- | --- |
| client_id | 客户端 id |
| search | 搜索关键词(域名/备注) |
| offset | 分页起始 |
| limit | 每页条数 |
---
### 获取单条域名解析
```
POST /index/gethost/
```
| 参数 | 含义 |
| --- | --- |
| id | 域名解析 id |
---
### 添加域名解析
```
POST /index/addhost/
```
| 参数 | 含义 |
| --- | --- |
| client_id | 客户端 id |
| remark | 备注 |
| host | 域名 |
| scheme | 协议类型:`all`、`http`、`https` |
| location | URL 路由,留空不限制 |
| target | 内网目标,格式 `ip:端口` |
| local_proxy | 是否转发到 nps 服务器本地,`true` / `false` |
| header | 自定义 request header |
| hostchange | 修改 request host |
| key_file_path | HTTPS 证书私钥文本或路径 |
| cert_file_path | HTTPS 证书文件文本或路径 |
| AutoHttps | 是否自动 HTTPS(仅 scheme 非 `http` 时生效) |
---
### 修改域名解析
```
POST /index/edithost/
```
| 参数 | 含义 |
| --- | --- |
| id | 域名解析 id |
| client_id | 客户端 id |
| remark | 备注 |
| host | 域名 |
| scheme | 协议类型 |
| location | URL 路由 |
| target | 内网目标 |
| local_proxy | 是否转发到 nps 服务器本地 |
| header | 自定义 request header |
| hostchange | 修改 request host |
| key_file_path | HTTPS 证书私钥文本或路径 |
| cert_file_path | HTTPS 证书文件文本或路径 |
| AutoHttps | 是否自动 HTTPS |
---
### 停止域名解析
```
POST /index/hoststop/
```
| 参数 | 含义 |
| --- | --- |
| id | 域名解析 id |
---
### 启动域名解析
```
POST /index/hoststart/
```
| 参数 | 含义 |
| --- | --- |
| id | 域名解析 id |
---
### 删除域名解析
```
POST /index/delhost/
```
| 参数 | 含义 |
| --- | --- |
| id | 域名解析 id |
---
## Global 全局设置
### 查看全局设置
```
GET /global/index/
```
返回全局黑名单 IP 列表和服务端 URL。
---
### 保存全局设置
```
POST /global/save/
```
| 参数 | 含义 |
| --- | --- |
| globalBlackIpList | 全局黑名单 IP 列表,`\r\n` 分隔 |
| serverUrl | 服务端访问地址(用于更正显示 IP) |
---