dsh-plugin-reverse-proxy 0.5.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/README.en.md +51 -3
- package/README.md +61 -9
- package/assets/settings-lan-access.jpg +0 -0
- package/index.js +324 -7
- package/lan-access.js +97 -3
- package/package.json +2 -2
- package/screenshots.json +1 -2
- package/vendor/skip/client.js +192 -15
- package/assets/access-ip.jpg +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,47 @@
|
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
+
## 0.7.0
|
|
8
|
+
|
|
9
|
+
**更新内容**
|
|
10
|
+
- 新增 **IPv6 监听开关**(默认关闭,位置在「局域网访问」总开关下方):打开后除了 IPv4 还会多绑一条 `[::]:<端口>`,
|
|
11
|
+
走 IPv6 的同网络设备也能直接进后台。开启前先弹安全提示——很多宽带的 IPv6 没有 NAT,路由器放行、地址被扫到就可能从外面连进来。
|
|
12
|
+
- IPv6 开关同样受总开关约束:没开「局域网访问」时点不动、接口返回 400;关总开关会一并关掉 IPv6 监听。
|
|
13
|
+
公网访问开着时,IPv6 来源也要过访问密钥,跟 IPv4 规则一致。
|
|
14
|
+
- 访问密钥输入框前新增 **「随机」按钮**:点一下自动填入 12 位随机密钥(小写字母 + 数字,去掉了容易看错的 i/l/o/0/1),
|
|
15
|
+
再点「保存并开启」即可,不用自己想。
|
|
16
|
+
- 斜杠命令扩展:`/lan ipv6 on|off`(切换前回同一份安全提示)。
|
|
17
|
+
- 终端脚本扩展:`reproxy-lan ipv6 on|off`(先安全提示 → 输入 `yes` 确认;`status` 多一行 IPv6 状态)。
|
|
18
|
+
- 新增接口:`POST /reproxy-lan/ipv6`(`{enabled, confirm:true}`)。
|
|
19
|
+
|
|
20
|
+
**修复内容**
|
|
21
|
+
- IPv6 不再由 `cordis.patch.yml` 里的 `listenIpv6` 决定,改由运行时的 IPv6 开关决定,改配置不用再重启。
|
|
22
|
+
- 改 IPv6 / 公网 / 密钥只重建必要的监听,不会再出现「切一下开关整站断连一秒」。
|
|
23
|
+
|
|
24
|
+
## 0.6.0
|
|
25
|
+
|
|
26
|
+
**更新内容**
|
|
27
|
+
- **总开关是公网访问的前提**:没打开「局域网访问」时,公网开关点不动、接口直接 400(端口只绑 127.0.0.1,开了也进不来);
|
|
28
|
+
反过来关掉总开关时,公网访问会一起被关掉,不会留下一个「看着开着其实没用」的开关。
|
|
29
|
+
- `/lan public on <密钥>` 与 `reproxy-lan public on` 一条命令把**总开关 + 公网开关一起打开**。
|
|
30
|
+
- 终端新增一键开启公网:`reproxy-lan public on`(不带密钥时先看安全提示 → 输入访问密钥 → 输入 yes 确认),省掉记两条命令。
|
|
31
|
+
- 访问 IP 改为卡片内 **固定高度、可上下滚动**,设备再多也不会把设置页撑长;按「公网 / 内网」分组,各组标数量,黑名单胶囊同样收进滚动区。
|
|
32
|
+
- 总开关(局域网访问)语义收紧:开启后**只放行局域网地址**,公网 IP 直接收到 403「只允许局域网访问」页面。
|
|
33
|
+
以前的版本只要开了总开关,任何能连上 8080 的地址都能进后台。
|
|
34
|
+
- 新增**公网访问开关**(默认关闭)与**访问密钥**输入框:密钥至少 5 位,只能英文字母和数字(拼音可以,符号不行)。
|
|
35
|
+
开启后访问地址变成 `http://<公网IP>:<端口>/<访问密钥>`,不知道密钥的人打开只会看到 404;
|
|
36
|
+
地址自带密钥访问一次后会种一个 30 天有效的 cookie,之后正常刷新、资源加载都不需要再带密钥。
|
|
37
|
+
- 设置页新增「公网访问」卡片:密钥输入框 + 「保存并开启」+ 公网开关 + 当前访问地址提示。
|
|
38
|
+
- 新增接口:`POST /reproxy-lan/public`(`{enabled, key, confirm:true}`)、`POST /reproxy-lan/key`(`{key}`)。
|
|
39
|
+
- 斜杠命令扩展:`/lan public`(看状态)、`/lan public on <密钥>`、`/lan public off`、`/lan key <密钥>`。
|
|
40
|
+
- 终端脚本扩展:`reproxy-lan public [on <密钥>|off]`、`reproxy-lan key [<密钥>]`,`status` 多显示公网状态与密钥掩码。
|
|
41
|
+
- `reset` 现在会一并关掉公网访问并清空访问密钥。
|
|
42
|
+
|
|
43
|
+
**修复内容**
|
|
44
|
+
- 修掉一个隐患:0.5.x 只靠监听的地址判断来源,路由器把端口映射出去后,公网设备能直接进后台。
|
|
45
|
+
- 终端脚本 `key` 子命令只改密钥、不动开关状态;改密钥同样会重建访问地址(旧地址立刻失效)。
|
|
46
|
+
- WebSocket 升级请求与普通请求走同一套闸门,公网模式下没有有效 cookie 的实时通道会被拒。
|
|
47
|
+
|
|
7
48
|
## 0.5.1
|
|
8
49
|
|
|
9
50
|
仅元数据:`package.json` 的 `description` 改成与仓库简介一致(补上「端口可自定义、访问 IP 黑名单与一键重置」),
|
package/README.en.md
CHANGED
|
@@ -55,7 +55,7 @@ No GUI around? The same switch works from a terminal (`yes` is required) — see
|
|
|
55
55
|
</p>
|
|
56
56
|
---
|
|
57
57
|
|
|
58
|
-
##
|
|
58
|
+
## Six capabilities
|
|
59
59
|
|
|
60
60
|
> Since 0.3.5: the settings switch is purple, and the **listening port** is editable
|
|
61
61
|
> (default 8080, applied on save, kept across restarts).
|
|
@@ -128,7 +128,44 @@ edits to that file are picked up within a second (`fs.watch`).
|
|
|
128
128
|
|
|
129
129
|
---
|
|
130
130
|
|
|
131
|
-
### 3.
|
|
131
|
+
### 3. Public access and access key (off by default)
|
|
132
|
+
|
|
133
|
+
The main switch only covers the LAN: requests arriving from a public IP get a **403 "LAN only"** page.
|
|
134
|
+
To open it to the internet, add a second gate — **public switch + access key**:
|
|
135
|
+
|
|
136
|
+
- Settings: **Settings → LAN access → Public access**, type an access key (5–64 characters, letters and digits only;
|
|
137
|
+
pinyin is fine, symbols are not), then hit **Save and enable**. The switch will not turn on without a key.
|
|
138
|
+
- Slash command: `/lan public` (status), `/lan public on <key>`, `/lan public off`, `/lan key <key>`.
|
|
139
|
+
- Terminal: `reproxy-lan public on <key>`, `reproxy-lan public off`, `reproxy-lan key <key>`; `status` shows the masked key.
|
|
140
|
+
|
|
141
|
+
The URL becomes:
|
|
142
|
+
|
|
143
|
+
```
|
|
144
|
+
http://<public-ip>:<port>/<access-key>
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Opening that URL once plants a **30-day cookie**; refreshes, assets and the WebSocket channel then work without the key
|
|
148
|
+
in the URL. A request with no key, or the wrong key, gets a **404** — it does not advertise that a backend lives there.
|
|
149
|
+
|
|
150
|
+
- The key is the only credential: change it and every old URL stops working immediately.
|
|
151
|
+
- Public mode only constrains **non-private** sources: LAN devices (192.168 / 10 / 172.16-31 …) still connect without a key, and loopback is always allowed (so a forgotten key can always be fixed locally).
|
|
152
|
+
- `reset` turns public access off and clears the key.
|
|
153
|
+
|
|
154
|
+
> ⚠️ Two things to keep in mind: (1) this plugin only *allows* traffic — reaching it from the internet still depends on
|
|
155
|
+
> your router / firewall port forwarding; (2) treat the key like a key — don't paste it into group chats, screenshots or issues.
|
|
156
|
+
|
|
157
|
+
### 4. IPv6 listener (off by default)
|
|
158
|
+
|
|
159
|
+
Right below the main switch there is an **IPv6 listener** toggle. When enabled, the proxy binds an extra `[::]:<port>`:
|
|
160
|
+
|
|
161
|
+
- Devices on the same network that use **IPv6** (most phones and many ISP links prefer IPv6) can open `http://[ipv6-address]:<port>` directly.
|
|
162
|
+
- It follows the main switch: it is disabled while "LAN access" is off (the API answers 400), and turning the main switch off disables IPv6 too.
|
|
163
|
+
- It follows public access as well: when public access is on, IPv6 sources must present the access key, same as IPv4.
|
|
164
|
+
- A warning dialog appears first: **many ISP IPv6 links have no NAT**, so if your router lets traffic through and the address gets scanned, outside devices may reach you. Leave it off if unsure.
|
|
165
|
+
|
|
166
|
+
Terminal: `reproxy-lan ipv6 on` / `reproxy-lan ipv6 off`; slash command: `/lan ipv6 on|off`.
|
|
167
|
+
|
|
168
|
+
### 5. Access IPs and blacklist (off by default)
|
|
132
169
|
|
|
133
170
|
- Lists every device that recently hit the port (IP, last seen, request count).
|
|
134
171
|
- **Add** drops that IP into the blacklist; the device then gets a 403 page until you remove it.
|
|
@@ -145,7 +182,7 @@ reproxy-lan reset # shows what will be restored, then asks you to type yes
|
|
|
145
182
|
Or `/lan reset` in chat, confirmed with `/lan reset yes`. Reset turns LAN access off, port back to 8080,
|
|
146
183
|
blacklist off and clears the list — devices currently connected over 8080 drop immediately, by design.
|
|
147
184
|
|
|
148
|
-
###
|
|
185
|
+
### 6. Token-free login (version-gated)
|
|
149
186
|
|
|
150
187
|
Replaces the official `@deepseek-ai/dsh-client-connection` provider and skips BrowserAuth.
|
|
151
188
|
|
|
@@ -161,6 +198,17 @@ Replaces the official `@deepseek-ai/dsh-client-connection` provider and skips Br
|
|
|
161
198
|
|
|
162
199
|
---
|
|
163
200
|
|
|
201
|
+
## Notes (read before turning on public access)
|
|
202
|
+
|
|
203
|
+
- **Reaching you from the internet is not the plugin's job.** The plugin only decides "allowed here, key required" on this machine; the real door is your router / firewall port mapping (`8080` → this host). Without it, a public URL simply will not connect.
|
|
204
|
+
- **Traffic is plain HTTP.** The key guards *who can get in*, not *what travels*. For long-term public exposure, put it behind HTTPS (Cloudflare Tunnel, frp + TLS, Tailscale) or use it only temporarily.
|
|
205
|
+
- **Lost the key?** `127.0.0.1` is always allowed: open `http://127.0.0.1:8080`, change it in Settings, or run `reproxy-lan key <new-key>`; if things are messy, `reproxy-lan reset` restores defaults (LAN + public off, blacklist and key cleared).
|
|
206
|
+
- **The blacklist stops accidents, not attackers.** It refuses a specific IP; it does not stop port scans or traffic floods. On the public internet, restrict ports at the firewall too.
|
|
207
|
+
- **One tab per browser.** That is DSH's own identity model (one browser = one connection) and the plugin cannot bypass it; other devices or a private window are unaffected.
|
|
208
|
+
- **Pick a sane port.** `80`/`443` need root and are often taken; `3080` is dsh itself. The plugin accepts `1024–65535`.
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
164
212
|
## Scope
|
|
165
213
|
|
|
166
214
|
| Where | Supported |
|
package/README.md
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
|
|
22
22
|
> 一条命令装上,局域网里的手机、平板、另一台电脑就能直接打开你的 DSH 网页界面,
|
|
23
23
|
> **而且不用再从启动日志里抄那串随机 token**。
|
|
24
|
+
> 出门在外想用?打开「公网访问」设个密钥,地址变成 `http://公网IP:8080/你的密钥`——不知道密钥的人只会看到 404。
|
|
24
25
|
|
|
25
26
|
```bash
|
|
26
27
|
dsh plugin --profile web add dsh-plugin-reverse-proxy # 推荐:npm 包,免构建
|
|
@@ -33,17 +34,13 @@ dsh plugin --profile web add dsh-plugin-reverse-proxy # 推荐:npm 包,免
|
|
|
33
34
|
|
|
34
35
|
## 长什么样
|
|
35
36
|
|
|
36
|
-
**设置 →
|
|
37
|
+
**设置 → 局域网访问**(0.6.0 一页搞定):总开关、监听端口、公网访问与密钥、访问 IP(每行带「公网 / 内网」标签,卡片内滚动)。点开关会先弹一次安全确认:
|
|
37
38
|
|
|
38
39
|
<p align="center">
|
|
39
40
|
<img src="assets/settings-lan-access.jpg" width="620" alt="设置 · 局域网访问">
|
|
40
41
|
</p>
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
<p align="center">
|
|
45
|
-
<img src="assets/access-ip.jpg" width="620" alt="设置 · 访问 IP 与黑名单">
|
|
46
|
-
</p>
|
|
43
|
+
点「添加」把设备拉黑,它再打开后台只会看到 403:
|
|
47
44
|
|
|
48
45
|
<p align="center">
|
|
49
46
|
<img src="assets/blocked-403.jpg" width="320" alt="被拉黑设备看到的 403 页面">
|
|
@@ -53,7 +50,7 @@ dsh plugin --profile web add dsh-plugin-reverse-proxy # 推荐:npm 包,免
|
|
|
53
50
|
|
|
54
51
|
---
|
|
55
52
|
|
|
56
|
-
##
|
|
53
|
+
## 六个能力
|
|
57
54
|
|
|
58
55
|
> 0.3.5 起:设置页开关换成紫色,**监听端口**也能自己填了(默认 8080,保存即生效,重启保留)。
|
|
59
56
|
|
|
@@ -94,6 +91,9 @@ dsh plugin --profile web add dsh-plugin-reverse-proxy # 推荐:npm 包,免
|
|
|
94
91
|
|
|
95
92
|
> ⚠️ 开启后,同一网络里的任何设备直接访问 `http://<这台机器的IP>:<端口>` 就能进入完整后台
|
|
96
93
|
> (会话、终端、设置),不需要 Token、不需要登录。只在信任当前局域网时开启。
|
|
94
|
+
>
|
|
95
|
+
> 总开关只管**局域网**:来自公网的 IP 会被直接拦下(403「只允许局域网访问」)。
|
|
96
|
+
> 要从公网进来,得再开下面的「公网访问」并设置访问密钥。
|
|
97
97
|
|
|
98
98
|
#### 怎么用(三个入口,改的是同一份状态)
|
|
99
99
|
|
|
@@ -149,7 +149,43 @@ reproxy-lan --help
|
|
|
149
149
|
想彻底关闭又不点开关?`reproxy-lan disable` 或 `/lan no` 都行;想手动改文件也可以,
|
|
150
150
|
插件用 `fs.watch` 盯着它,改完一两秒内自动生效(`updatedBy` 可以写 `manual`)。
|
|
151
151
|
|
|
152
|
-
### 3.
|
|
152
|
+
### 3. 公网访问与访问密钥(默认关闭)
|
|
153
|
+
|
|
154
|
+
总开关只管局域网。要让公网也能进来,再开一道闸——**公网开关 + 访问密钥**:
|
|
155
|
+
|
|
156
|
+
- 设置页:**设置 → 局域网访问 → 公网访问**,填「访问密钥」(至少 5 位,只能英文字母和数字,拼音可以、符号不行),
|
|
157
|
+
密钥框前面有个 **「随机」** 按钮,点一下就自动填 12 位随机密钥(小写字母 + 数字);填好点「保存并开启」,没有密钥时开关点不动(后端直接 400)。**没打开「局域网访问」总开关时公网开关也点不动**——端口只绑 127.0.0.1,开了也没人能连进来。
|
|
158
|
+
- 斜杠命令:`/lan public` 看状态、`/lan public on <密钥>` 开启、`/lan public off` 关闭、`/lan key <密钥>` 只换密钥。
|
|
159
|
+
- 终端:`reproxy-lan public on`(**一键开启**:先看安全提示 → 输入访问密钥 → 输入 `yes` 确认,**总开关和公网开关会一起打开**)、`reproxy-lan public on <密钥>`(密钥直接写在命令里)、`reproxy-lan public off`、`reproxy-lan key <密钥>`;`reproxy-lan status` 里能看到公网状态与密钥掩码。
|
|
160
|
+
|
|
161
|
+
开启后访问地址变成:
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
http://<公网IP>:<端口>/<访问密钥>
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
用这条地址访问一次,会种一个 **30 天有效**的 cookie;之后刷新页面、加载资源、WebSocket 实时通道都不用再带密钥。
|
|
168
|
+
地址里没带密钥、或者密钥不对,返回 **404**——不告诉对面「这里有个后台」,只是查不到。
|
|
169
|
+
|
|
170
|
+
- 密钥是唯一凭证:换密钥后旧地址立刻失效,所有设备都得重新用新地址访问一次。
|
|
171
|
+
- 公网模式只约束**公网来源**:局域网设备(192.168 / 10 / 172.16-31 等)照样直连,不用带密钥;`127.0.0.1` 永远放行(安全阀:密钥忘了也能在本机改回来)。
|
|
172
|
+
- `reset` 会一并关掉公网访问并清空密钥。
|
|
173
|
+
|
|
174
|
+
> ⚠️ 两件事要记住:① 插件只负责「放行」,公网能不能进来还取决于路由器 / 防火墙有没有把端口映射出去;
|
|
175
|
+
> ② 密钥等于钥匙,别发到群里、别写进截图、别贴在公开 issue 里。
|
|
176
|
+
|
|
177
|
+
### 4. IPv6 监听(默认关闭)
|
|
178
|
+
|
|
179
|
+
总开关下面还有一个 **IPv6 监听** 开关,默认关着。打开后插件除了绑 IPv4,还会多绑一条 `[::]:<端口>`:
|
|
180
|
+
|
|
181
|
+
- 同网络里**走 IPv6** 的设备(很多手机、运营商宽带默认就是 IPv6 优先)不用你折腾,直接用 `http://[IPv6地址]:<端口>` 就能进后台。
|
|
182
|
+
- 跟总开关联动:没开「局域网访问」时这个开关点不动(后端 400);关总开关会把它一起关掉。
|
|
183
|
+
- 跟公网访问联动:公网开着时,IPv6 来源同样要过访问密钥,规则和 IPv4 一样。
|
|
184
|
+
- 打开前会弹一次安全提示:**不少宽带的 IPv6 没有 NAT**,路由器放行、地址被扫到,外面的设备也可能连进来。不确定就先别开。
|
|
185
|
+
|
|
186
|
+
终端:`reproxy-lan ipv6 on` / `reproxy-lan ipv6 off`(先看提示,再输入 `yes`);斜杠命令 `/lan ipv6 on|off`。
|
|
187
|
+
|
|
188
|
+
### 5. 访问 IP 与黑名单(默认关闭)
|
|
153
189
|
|
|
154
190
|
设置页还有一张「访问 IP」卡片:
|
|
155
191
|
|
|
@@ -176,7 +212,7 @@ reproxy-lan reset # 先给你看会重置什么,输入 yes 才执行
|
|
|
176
212
|
> ⚠️ 不要拉黑「当前正在使用的这台设备」——后端会直接拒绝(400),回环地址 `127.0.0.1` 也不允许拉黑,
|
|
177
213
|
> 免得管理员把自己锁在外面。真要封自己,先在另一台设备上操作。
|
|
178
214
|
|
|
179
|
-
###
|
|
215
|
+
### 6. 免 Token 登录(版本门控,命中才启用)
|
|
180
216
|
|
|
181
217
|
替换官方 `@deepseek-ai/dsh-client-connection` 提供方,跳过 BrowserAuth:
|
|
182
218
|
浏览器直接打开 Web 地址就能用。
|
|
@@ -191,6 +227,17 @@ reproxy-lan reset # 先给你看会重置什么,输入 yes 才执行
|
|
|
191
227
|
|
|
192
228
|
---
|
|
193
229
|
|
|
230
|
+
## 说明(开「公网访问」之前请先读)
|
|
231
|
+
|
|
232
|
+
- **能不能从外网进来,不归插件管。** 插件只负责「在这台机器上放行 + 校验密钥」;真正的入口是路由器 / 防火墙的端口映射(把 `8080` 转发到这台机器),映射没做,公网地址打不开是正常的。
|
|
233
|
+
- **传输是明文 HTTP。** 密钥只保护「谁能进后台」,不保护「路上的内容」。公网长期用建议套一层 HTTPS(Cloudflare Tunnel、frp + TLS、Tailscale 之类),或者只在临时场景开。
|
|
234
|
+
- **密钥丢了不用慌。** 本机 `127.0.0.1` 永远放行:直接打开 `http://127.0.0.1:8080` 改设置,或在终端 `reproxy-lan key <新密钥>`;实在乱了就 `reproxy-lan reset` 回到默认(会关掉局域网与公网、清空黑名单,密钥一并清掉)。
|
|
235
|
+
- **黑名单防的是误入,不是攻击。** 它拦的是「这个 IP 不给进」,不拦端口扫描和流量攻击;公网暴露时建议在防火墙上只放行需要的端口。
|
|
236
|
+
- **同一浏览器同时只能开一个标签页。** 这是 DSH 官方身份模型的限制(一个浏览器 = 一份连接),插件绕不过去;换设备或用无痕窗口不受影响。
|
|
237
|
+
- **端口别贪心。** `80` / `443` 要 root 且常被别的服务占着,`3080` 是 dsh 本体,插件只收 `1024–65535`。
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
194
241
|
## 适用范围
|
|
195
242
|
|
|
196
243
|
| 场景 | 能不能用 |
|
|
@@ -260,6 +307,11 @@ reproxy-lan enable # 读完提示,输入 yes
|
|
|
260
307
|
| `maxBodyBytes` | `209715200` | `client_max_body_size 200m` |
|
|
261
308
|
| `retryMs` | `5000` | —(端口冲突重试间隔) |
|
|
262
309
|
|
|
310
|
+
开关类设置(`lanAccess` / `port` / `publicAccess` / `accessKey` / `blacklistEnabled` / `blockedIps`)
|
|
311
|
+
不写进 `cordis.patch.yml`,而是存在状态文件 `$DSH_HOME/plugin-reverse-proxy/lan-access.json`
|
|
312
|
+
(默认 `~/.dsh/plugin-reverse-proxy/lan-access.json`,权限 600)。改这个文件也会被插件立刻读到,
|
|
313
|
+
但更推荐用设置页、`/lan` 命令或 `reproxy-lan` 脚本改 —— 它们会顺手校验并广播。
|
|
314
|
+
|
|
263
315
|
免 Token 登录(`dsh-plugin-reverse-proxy-trusted-connection*` 行)的 `trustedHosts`
|
|
264
316
|
由 DSH 的 Web 运行时配置自动注入,一般不用手改。
|
|
265
317
|
|
|
Binary file
|
package/index.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
* 端口被占用(例如 nginx 还没停)时每 5 秒重试,不抛异常拖垮宿主。
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
import crypto from 'node:crypto'
|
|
19
20
|
import http from 'node:http'
|
|
20
21
|
import net from 'node:net'
|
|
21
22
|
import zlib from 'node:zlib'
|
|
@@ -27,16 +28,23 @@ import {
|
|
|
27
28
|
effectiveListenHost,
|
|
28
29
|
effectivePort,
|
|
29
30
|
getLanState,
|
|
31
|
+
IPV6_WARNING_TEXT,
|
|
30
32
|
LAN_WARNING_TEXT,
|
|
33
|
+
MIN_KEY_LENGTH,
|
|
34
|
+
PUBLIC_WARNING_TEXT,
|
|
31
35
|
isBlocked,
|
|
32
36
|
normalizeIp,
|
|
33
37
|
resetConfig,
|
|
34
38
|
onLanChange,
|
|
39
|
+
setAccessKey,
|
|
35
40
|
setBlacklistEnabled,
|
|
41
|
+
setIpv6,
|
|
36
42
|
setLanAccess,
|
|
43
|
+
setPublicAccess,
|
|
37
44
|
setPort,
|
|
38
45
|
STATE_FILE,
|
|
39
46
|
unblockIp,
|
|
47
|
+
validateKey,
|
|
40
48
|
validatePort,
|
|
41
49
|
} from './lan-access.js'
|
|
42
50
|
|
|
@@ -125,6 +133,28 @@ function isImmutableAsset(url) {
|
|
|
125
133
|
* 局域网开关的两个入口(设置页走 HTTP,命令行走 /lan 斜杠命令)。
|
|
126
134
|
* 两边都改同一份 lan-access 状态文件,所以永远同步;改完立刻重建监听。
|
|
127
135
|
*/
|
|
136
|
+
/** 内网/本机地址判定:总开关开着但没开公网时,只放行这些来源。 */
|
|
137
|
+
export function isPrivateIp(value) {
|
|
138
|
+
const ip = normalizeIp(value)
|
|
139
|
+
if (ip === '') return false
|
|
140
|
+
if (ip === '::1') return true
|
|
141
|
+
if (net.isIPv6(ip)) {
|
|
142
|
+
const lower = ip.toLowerCase()
|
|
143
|
+
if (lower.startsWith('fc') || lower.startsWith('fd')) return true // fc00::/7
|
|
144
|
+
if (lower.startsWith('fe8') || lower.startsWith('fe9') || lower.startsWith('fea') || lower.startsWith('feb')) return true // fe80::/10
|
|
145
|
+
return false
|
|
146
|
+
}
|
|
147
|
+
const parts = ip.split('.').map((item) => Number(item))
|
|
148
|
+
if (parts.length !== 4 || parts.some((n) => !Number.isInteger(n))) return false
|
|
149
|
+
const [a, b] = parts
|
|
150
|
+
if (a === 127) return true // 回环
|
|
151
|
+
if (a === 10) return true
|
|
152
|
+
if (a === 172 && b >= 16 && b <= 31) return true
|
|
153
|
+
if (a === 192 && b === 168) return true
|
|
154
|
+
if (a === 169 && b === 254) return true // 链路本地
|
|
155
|
+
return false
|
|
156
|
+
}
|
|
157
|
+
|
|
128
158
|
function registerLanControl(ctx, cfg, log, extra = {}) {
|
|
129
159
|
const sendJson = (res, status, body) => {
|
|
130
160
|
const raw = JSON.stringify(body)
|
|
@@ -361,6 +391,100 @@ function registerLanControl(ctx, cfg, log, extra = {}) {
|
|
|
361
391
|
},
|
|
362
392
|
}
|
|
363
393
|
|
|
394
|
+
/** IPv6 监听开关:同样要带 confirm,且总开关必须先开。 */
|
|
395
|
+
const routeIpv6 = {
|
|
396
|
+
kind: 'exact',
|
|
397
|
+
path: '/reproxy-lan/ipv6',
|
|
398
|
+
handler: async (req, res) => {
|
|
399
|
+
if (req.method !== 'POST') {
|
|
400
|
+
sendJson(res, 405, { ok: false, error: '只支持 POST' })
|
|
401
|
+
return
|
|
402
|
+
}
|
|
403
|
+
let body
|
|
404
|
+
try {
|
|
405
|
+
body = await readBody(req)
|
|
406
|
+
} catch (e) {
|
|
407
|
+
sendJson(res, 400, { ok: false, error: e.message })
|
|
408
|
+
return
|
|
409
|
+
}
|
|
410
|
+
if (body.confirm !== true) {
|
|
411
|
+
sendJson(res, 400, { ok: false, error: '缺少用户确认(confirm:true)', warning: IPV6_WARNING_TEXT })
|
|
412
|
+
return
|
|
413
|
+
}
|
|
414
|
+
if (typeof body.enabled !== 'boolean') {
|
|
415
|
+
sendJson(res, 400, { ok: false, error: 'enabled 必须是布尔值' })
|
|
416
|
+
return
|
|
417
|
+
}
|
|
418
|
+
try {
|
|
419
|
+
const next = setIpv6(body.enabled, 'settings')
|
|
420
|
+
log(body.enabled ? 'warn' : 'info', '设置页把 IPv6 监听' + (body.enabled ? '打开' : '关掉'))
|
|
421
|
+
sendJson(res, 200, { ...describeLanState(), warning: IPV6_WARNING_TEXT })
|
|
422
|
+
} catch (e) {
|
|
423
|
+
sendJson(res, 400, { ok: false, error: e.message })
|
|
424
|
+
}
|
|
425
|
+
},
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** 公网开关:必须同时给合法密钥;缺密钥直接 400。 */
|
|
429
|
+
const routePublic = {
|
|
430
|
+
kind: 'exact',
|
|
431
|
+
path: '/reproxy-lan/public',
|
|
432
|
+
handler: async (req, res) => {
|
|
433
|
+
if (req.method !== 'POST') {
|
|
434
|
+
sendJson(res, 405, { ok: false, error: '只支持 POST' })
|
|
435
|
+
return
|
|
436
|
+
}
|
|
437
|
+
let body
|
|
438
|
+
try {
|
|
439
|
+
body = await readBody(req)
|
|
440
|
+
} catch (e) {
|
|
441
|
+
sendJson(res, 400, { ok: false, error: e.message })
|
|
442
|
+
return
|
|
443
|
+
}
|
|
444
|
+
if (body.confirm !== true) {
|
|
445
|
+
sendJson(res, 400, { ok: false, error: '缺少用户确认(confirm:true)', warning: PUBLIC_WARNING_TEXT })
|
|
446
|
+
return
|
|
447
|
+
}
|
|
448
|
+
if (typeof body.enabled !== 'boolean') {
|
|
449
|
+
sendJson(res, 400, { ok: false, error: 'enabled 必须是布尔值' })
|
|
450
|
+
return
|
|
451
|
+
}
|
|
452
|
+
try {
|
|
453
|
+
const next = setPublicAccess(body.enabled, body.key, 'settings')
|
|
454
|
+
log(body.enabled ? 'warn' : 'info', '设置页把公网访问' + (body.enabled ? '打开' : '关掉'))
|
|
455
|
+
sendJson(res, 200, { ...describeLanState(), warning: PUBLIC_WARNING_TEXT })
|
|
456
|
+
} catch (e) {
|
|
457
|
+
sendJson(res, 400, { ok: false, error: e.message })
|
|
458
|
+
}
|
|
459
|
+
},
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/** 只改密钥:不允许在公网已开时改成空。 */
|
|
463
|
+
const routeKey = {
|
|
464
|
+
kind: 'exact',
|
|
465
|
+
path: '/reproxy-lan/key',
|
|
466
|
+
handler: async (req, res) => {
|
|
467
|
+
if (req.method !== 'POST') {
|
|
468
|
+
sendJson(res, 405, { ok: false, error: '只支持 POST' })
|
|
469
|
+
return
|
|
470
|
+
}
|
|
471
|
+
let body
|
|
472
|
+
try {
|
|
473
|
+
body = await readBody(req)
|
|
474
|
+
} catch (e) {
|
|
475
|
+
sendJson(res, 400, { ok: false, error: e.message })
|
|
476
|
+
return
|
|
477
|
+
}
|
|
478
|
+
try {
|
|
479
|
+
setAccessKey(body.key, 'settings')
|
|
480
|
+
log('warn', '设置页更新了公网访问密钥')
|
|
481
|
+
sendJson(res, 200, describeLanState())
|
|
482
|
+
} catch (e) {
|
|
483
|
+
sendJson(res, 400, { ok: false, error: e.message })
|
|
484
|
+
}
|
|
485
|
+
},
|
|
486
|
+
}
|
|
487
|
+
|
|
364
488
|
const webServer = ctx && typeof ctx.get === 'function' ? ctx.get('webServer') : null
|
|
365
489
|
if (webServer && typeof webServer.register === 'function' && typeof ctx.effect === 'function') {
|
|
366
490
|
ctx.effect(() => webServer.register(routeStatus))
|
|
@@ -370,6 +494,9 @@ function registerLanControl(ctx, cfg, log, extra = {}) {
|
|
|
370
494
|
ctx.effect(() => webServer.register(routeBlacklist))
|
|
371
495
|
ctx.effect(() => webServer.register(routeBlock))
|
|
372
496
|
ctx.effect(() => webServer.register(routeUnblock))
|
|
497
|
+
ctx.effect(() => webServer.register(routePublic))
|
|
498
|
+
ctx.effect(() => webServer.register(routeIpv6))
|
|
499
|
+
ctx.effect(() => webServer.register(routeKey))
|
|
373
500
|
} else {
|
|
374
501
|
log('warn', 'webServer 不可用,设置页开关接口未注册')
|
|
375
502
|
}
|
|
@@ -382,20 +509,30 @@ function registerLanControl(ctx, cfg, log, extra = {}) {
|
|
|
382
509
|
return
|
|
383
510
|
}
|
|
384
511
|
|
|
512
|
+
const v6LineOf = (s) => (s.ipv6 === true
|
|
513
|
+
? 'IPv6 监听:已开启([::]:' + effectivePort() + ',走 IPv6 的设备也能进)'
|
|
514
|
+
: 'IPv6 监听:已关闭(只绑 IPv4)')
|
|
515
|
+
|
|
385
516
|
const statusText = () => {
|
|
386
517
|
const s = getLanState()
|
|
387
518
|
const port = effectivePort()
|
|
388
519
|
const head = s.lanAccess
|
|
389
520
|
? '局域网访问:已开启(监听 0.0.0.0:' + port + ',同网段设备可直接进入后台)'
|
|
390
521
|
: '局域网访问:已关闭(只监听 127.0.0.1:' + port + ',只有本机能访问)'
|
|
522
|
+
const v6Line = s.ipv6
|
|
523
|
+
? 'IPv6 监听:已开启([::]:' + port + ',走 IPv6 的设备也能进)'
|
|
524
|
+
: 'IPv6 监听:已关闭(只绑 IPv4)'
|
|
391
525
|
const when = s.updatedAt ? '上次修改:' + s.updatedAt + '(' + (s.updatedBy || 'unknown') + ')' : '还没有改过,当前是默认值'
|
|
392
|
-
|
|
526
|
+
const pub = s.publicAccess
|
|
527
|
+
? '公网访问:已开启(访问地址 http://<公网IP>:' + port + '/' + (s.accessKey || '') + ')'
|
|
528
|
+
: '公网访问:已关闭(只有局域网内设备能访问)'
|
|
529
|
+
return [head, v6Line, pub, when].join('\n')
|
|
393
530
|
}
|
|
394
531
|
|
|
395
532
|
ctx.effect(() => commands.register({
|
|
396
533
|
name: 'lan',
|
|
397
|
-
description: '
|
|
398
|
-
input: { hint: 'yes | no | status | port <n>' },
|
|
534
|
+
description: '查看或切换局域网/公网访问开关、端口与访问密钥',
|
|
535
|
+
input: { hint: 'yes | no | status | port <n> | ipv6 on|off | public on <key> | public off | key <key>' },
|
|
399
536
|
recordInput: true,
|
|
400
537
|
handler: ({ rawInput }) => {
|
|
401
538
|
const arg = String(rawInput || '').trim().toLowerCase()
|
|
@@ -425,6 +562,77 @@ function registerLanControl(ctx, cfg, log, extra = {}) {
|
|
|
425
562
|
log('info', '命令行切换局域网访问 → 关闭')
|
|
426
563
|
return { kind: 'success', text: '已关闭,其他设备会立刻断开。\n' + statusText() }
|
|
427
564
|
}
|
|
565
|
+
if (arg === 'public' || arg === 'public status') {
|
|
566
|
+
const s = getLanState()
|
|
567
|
+
return {
|
|
568
|
+
kind: 'success',
|
|
569
|
+
text: [
|
|
570
|
+
statusText(),
|
|
571
|
+
'',
|
|
572
|
+
'开启公网:/lan public on <访问密钥>(至少 ' + MIN_KEY_LENGTH + ' 位,只能字母数字)',
|
|
573
|
+
'关闭公网:/lan public off',
|
|
574
|
+
'只改密钥:/lan key <访问密钥>',
|
|
575
|
+
'',
|
|
576
|
+
PUBLIC_WARNING_TEXT,
|
|
577
|
+
].join('\n'),
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
if (arg.indexOf('public on') === 0) {
|
|
581
|
+
const key = arg.slice('public on'.length).trim()
|
|
582
|
+
try {
|
|
583
|
+
// 一条命令把两个开关都打开:总开关(端口朝外听)+ 公网开关(校验密钥)
|
|
584
|
+
const wasLan = getLanState().lanAccess
|
|
585
|
+
if (!wasLan) setLanAccess(true, 'command')
|
|
586
|
+
setPublicAccess(true, key, 'command')
|
|
587
|
+
log('warn', '命令行开启公网访问' + (wasLan ? '' : '(同时打开局域网访问)'))
|
|
588
|
+
} catch (e) {
|
|
589
|
+
return { kind: 'error', text: '开启失败:' + e.message }
|
|
590
|
+
}
|
|
591
|
+
return { kind: 'success', text: PUBLIC_WARNING_TEXT + '\n\n已开启:局域网访问 + 公网访问都打开了。\n' + statusText() }
|
|
592
|
+
}
|
|
593
|
+
if (arg === 'public off') {
|
|
594
|
+
try {
|
|
595
|
+
setPublicAccess(false, undefined, 'command')
|
|
596
|
+
} catch (e) {
|
|
597
|
+
return { kind: 'error', text: '关闭失败:' + e.message }
|
|
598
|
+
}
|
|
599
|
+
log('info', '命令行关闭公网访问')
|
|
600
|
+
return { kind: 'success', text: '公网访问已关闭。\n' + statusText() }
|
|
601
|
+
}
|
|
602
|
+
if (arg.indexOf('key') === 0) {
|
|
603
|
+
const key = arg.slice(3).trim()
|
|
604
|
+
if (key === '') return { kind: 'error', text: '用法:/lan key <访问密钥>(至少 ' + MIN_KEY_LENGTH + ' 位)' }
|
|
605
|
+
const parsed = validateKey(key)
|
|
606
|
+
if (!parsed.ok) return { kind: 'error', text: parsed.error }
|
|
607
|
+
try {
|
|
608
|
+
setAccessKey(parsed.key, 'command')
|
|
609
|
+
} catch (e) {
|
|
610
|
+
return { kind: 'error', text: '保存失败:' + e.message }
|
|
611
|
+
}
|
|
612
|
+
log('warn', '命令行更新了公网访问密钥')
|
|
613
|
+
return { kind: 'success', text: '密钥已更新。\n' + statusText() }
|
|
614
|
+
}
|
|
615
|
+
if (arg.indexOf('ipv6') === 0) {
|
|
616
|
+
const value = arg.slice(4).trim().toLowerCase()
|
|
617
|
+
const state = getLanState()
|
|
618
|
+
if (value === '') {
|
|
619
|
+
return { kind: 'success', text: v6LineOf(state) + '\n\n用法:/lan ipv6 on 开启、/lan ipv6 off 关闭' }
|
|
620
|
+
}
|
|
621
|
+
if (value !== 'on' && value !== 'off') {
|
|
622
|
+
return { kind: 'error', text: '用法:/lan ipv6 on | off' }
|
|
623
|
+
}
|
|
624
|
+
const want = value === 'on'
|
|
625
|
+
if (want === (state.ipv6 === true)) {
|
|
626
|
+
return { kind: 'success', text: '已经是' + (want ? '开启' : '关闭') + '状态了。\n' + statusText() }
|
|
627
|
+
}
|
|
628
|
+
try {
|
|
629
|
+
setIpv6(want, 'command')
|
|
630
|
+
} catch (e) {
|
|
631
|
+
return { kind: 'error', text: e.message }
|
|
632
|
+
}
|
|
633
|
+
log(want ? 'warn' : 'info', '命令行把 IPv6 监听' + (want ? '打开' : '关掉'))
|
|
634
|
+
return { kind: 'success', text: (want ? IPV6_WARNING_TEXT + '\n\n已开启 IPv6 监听,正在重建监听。' : 'IPv6 监听已关闭。') + '\n' + statusText() }
|
|
635
|
+
}
|
|
428
636
|
if (arg === 'reset') {
|
|
429
637
|
return {
|
|
430
638
|
kind: 'success',
|
|
@@ -467,7 +675,7 @@ function registerLanControl(ctx, cfg, log, extra = {}) {
|
|
|
467
675
|
log('info', '命令行把监听端口改为 ' + parsed.port)
|
|
468
676
|
return { kind: 'success', text: '端口已改为 ' + parsed.port + ',正在重建监听(端口被占用的话会每 5 秒重试)。\n' + statusText() }
|
|
469
677
|
}
|
|
470
|
-
return { kind: 'error', text: '看不懂「' + arg + '」。用法:/lan(看状态)、/lan yes
|
|
678
|
+
return { kind: 'error', text: '看不懂「' + arg + '」。用法:/lan(看状态)、/lan yes(开局域网)、/lan no(关局域网)、/lan port 9000(改端口)、/lan public on <密钥>(开公网)、/lan public off、/lan key <密钥>' }
|
|
471
679
|
},
|
|
472
680
|
}))
|
|
473
681
|
console.log('[reproxy-lan] /lan 命令已注册')
|
|
@@ -519,7 +727,7 @@ function applyProxy(ctx, config) {
|
|
|
519
727
|
/** 给设置页看的快照(带 blocked 标记,前端据此决定按钮是「添加」还是「已在黑名单」)。 */
|
|
520
728
|
const listClients = () => [...seenClients.values()]
|
|
521
729
|
.sort((a, b) => b.lastSeen - a.lastSeen)
|
|
522
|
-
.map((entry) => ({ ...entry, blocked: isBlocked(entry.ip) }))
|
|
730
|
+
.map((entry) => ({ ...entry, blocked: isBlocked(entry.ip), private: isPrivateIp(entry.ip) }))
|
|
523
731
|
|
|
524
732
|
const BLOCKED_HTML = '<!doctype html><html lang="zh-CN"><meta charset="utf-8">'
|
|
525
733
|
+ '<meta name="viewport" content="width=device-width,initial-scale=1">'
|
|
@@ -531,6 +739,84 @@ function applyProxy(ctx, config) {
|
|
|
531
739
|
+ '如果认为这是误会,请联系管理员在「设置 → 局域网访问 → 访问 IP」里移除。'
|
|
532
740
|
+ '</p></body></html>'
|
|
533
741
|
|
|
742
|
+
const LAN_ONLY_HTML = '<!doctype html><html lang="zh-CN"><meta charset="utf-8">'
|
|
743
|
+
+ '<meta name="viewport" content="width=device-width,initial-scale=1">'
|
|
744
|
+
+ '<title>403 · 只允许局域网访问</title>'
|
|
745
|
+
+ '<body style="margin:0;font:15px/1.8 -apple-system,system-ui,sans-serif;background:#12141a;color:#e6e8ee;padding:56px 24px">'
|
|
746
|
+
+ '<h2 style="margin:0 0 12px;font-size:20px">403 · 只允许局域网访问</h2>'
|
|
747
|
+
+ '<p style="margin:0;color:#9aa0ad;max-width:560px">'
|
|
748
|
+
+ '这台设备不在同一个局域网里。管理员如果想允许公网访问,需要在「设置 → 局域网访问 → 公网访问」里打开开关并设置访问密钥。'
|
|
749
|
+
+ '</p></body></html>'
|
|
750
|
+
|
|
751
|
+
const NOT_FOUND_HTML = '<!doctype html><html lang="zh-CN"><meta charset="utf-8">'
|
|
752
|
+
+ '<meta name="viewport" content="width=device-width,initial-scale=1">'
|
|
753
|
+
+ '<title>404</title>'
|
|
754
|
+
+ '<body style="margin:0;font:15px/1.8 -apple-system,system-ui,sans-serif;background:#12141a;color:#e6e8ee;padding:56px 24px">'
|
|
755
|
+
+ '<h2 style="margin:0 0 12px;font-size:20px">404</h2>'
|
|
756
|
+
+ '<p style="margin:0;color:#9aa0ad;max-width:560px">页面不存在。</p></body></html>'
|
|
757
|
+
|
|
758
|
+
const sendHtml = (res, status, html, extraHeaders = {}) => {
|
|
759
|
+
try {
|
|
760
|
+
const body = Buffer.from(html, 'utf8')
|
|
761
|
+
res.writeHead(status, {
|
|
762
|
+
'content-type': 'text/html; charset=utf-8',
|
|
763
|
+
'content-length': body.length,
|
|
764
|
+
'cache-control': 'no-store',
|
|
765
|
+
...extraHeaders,
|
|
766
|
+
})
|
|
767
|
+
res.end(body)
|
|
768
|
+
} catch { /* 连接已断 */ }
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
const COOKIE_NAME = 'reproxy_access'
|
|
772
|
+
const tokenOf = (key) => crypto.createHash('sha256').update('reproxy:' + key).digest('hex')
|
|
773
|
+
|
|
774
|
+
const readCookie = (req, name) => {
|
|
775
|
+
const raw = req && req.headers ? req.headers.cookie : ''
|
|
776
|
+
if (typeof raw !== 'string' || raw === '') return ''
|
|
777
|
+
for (const part of raw.split(';')) {
|
|
778
|
+
const index = part.indexOf('=')
|
|
779
|
+
if (index === -1) continue
|
|
780
|
+
if (part.slice(0, index).trim() === name) return part.slice(index + 1).trim()
|
|
781
|
+
}
|
|
782
|
+
return ''
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* 访问闸门:
|
|
787
|
+
* - 总开关关着 → 只听回环,非回环一律当「不在局域网」(理论上不该出现)
|
|
788
|
+
* - 总开关开、公网关 → 只放行内网地址,公网 IP 返回 403「只允许局域网访问」
|
|
789
|
+
* - 公网开 → 必须带密钥:地址里带 `/<密钥>`(访问一次换 cookie)或已持有有效 cookie,否则 404
|
|
790
|
+
* 返回 'ok' | 'grant-key' | 'lan-only' | 'need-key' | 'key-broken'
|
|
791
|
+
*/
|
|
792
|
+
const accessGate = (req) => {
|
|
793
|
+
const state = getLanState()
|
|
794
|
+
const ip = normalizeIp(req && req.socket ? req.socket.remoteAddress : '')
|
|
795
|
+
const local = ip === '' || ip === '::1' || /^127\./.test(ip)
|
|
796
|
+
// 本机永远放行:要不就没有「安全阀」了——公网模式开着、密钥忘了,本机也进不来
|
|
797
|
+
if (local) return 'ok'
|
|
798
|
+
if (state.lanAccess !== true) return 'lan-only'
|
|
799
|
+
// 内网地址永远直连(是否允许内网由总开关管),公网模式只约束公网来源
|
|
800
|
+
if (isPrivateIp(ip)) return 'ok'
|
|
801
|
+
if (state.publicAccess !== true) return 'lan-only'
|
|
802
|
+
const key = typeof state.accessKey === 'string' ? state.accessKey : ''
|
|
803
|
+
if (key === '') return 'key-broken'
|
|
804
|
+
const pathOnly = String(req.url || '/').split('?')[0]
|
|
805
|
+
if (pathOnly === '/' + key || pathOnly === '/' + key + '/') return 'grant-key'
|
|
806
|
+
const token = readCookie(req, COOKIE_NAME)
|
|
807
|
+
if (token !== '' && token === tokenOf(key)) return 'ok'
|
|
808
|
+
return 'need-key'
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
const grantKeyCookie = (res) => {
|
|
812
|
+
const key = (getLanState().accessKey || '')
|
|
813
|
+
const maxAge = 30 * 24 * 3600
|
|
814
|
+
return {
|
|
815
|
+
'set-cookie': COOKIE_NAME + '=' + tokenOf(key) + '; Max-Age=' + maxAge + '; Path=/; HttpOnly; SameSite=Lax',
|
|
816
|
+
location: '/',
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
|
|
534
820
|
const sendBlocked = (res) => {
|
|
535
821
|
try {
|
|
536
822
|
const body = Buffer.from(BLOCKED_HTML, 'utf8')
|
|
@@ -573,6 +859,14 @@ function applyProxy(ctx, config) {
|
|
|
573
859
|
sendBlocked(res)
|
|
574
860
|
return
|
|
575
861
|
}
|
|
862
|
+
const verdict = accessGate(req)
|
|
863
|
+
if (verdict === 'lan-only') { sendHtml(res, 403, LAN_ONLY_HTML); return }
|
|
864
|
+
if (verdict === 'need-key' || verdict === 'key-broken') { sendHtml(res, 404, NOT_FOUND_HTML); return }
|
|
865
|
+
if (verdict === 'grant-key') {
|
|
866
|
+
const headers = grantKeyCookie(res)
|
|
867
|
+
sendHtml(res, 302, '<!doctype html><meta charset="utf-8"><title>ok</title>', headers)
|
|
868
|
+
return
|
|
869
|
+
}
|
|
576
870
|
let received = 0
|
|
577
871
|
let aborted = false
|
|
578
872
|
const upstream = http.request({
|
|
@@ -638,6 +932,12 @@ function applyProxy(ctx, config) {
|
|
|
638
932
|
try { clientSocket.end('HTTP/1.1 403 Forbidden\r\ncontent-length: 0\r\nconnection: close\r\n\r\n') } catch { /* 已关 */ }
|
|
639
933
|
return
|
|
640
934
|
}
|
|
935
|
+
const upgradeVerdict = accessGate(req)
|
|
936
|
+
if (upgradeVerdict !== 'ok' && upgradeVerdict !== 'grant-key') {
|
|
937
|
+
const status = upgradeVerdict === 'lan-only' ? '403 Forbidden' : '404 Not Found'
|
|
938
|
+
try { clientSocket.end('HTTP/1.1 ' + status + '\r\ncontent-length: 0\r\nconnection: close\r\n\r\n') } catch { /* 已关 */ }
|
|
939
|
+
return
|
|
940
|
+
}
|
|
641
941
|
const upstream = net.connect(cfg.upstreamPort, cfg.upstreamHost, () => {
|
|
642
942
|
const headers = upstreamHeaders(req, cfg, {
|
|
643
943
|
connection: 'Upgrade',
|
|
@@ -699,13 +999,22 @@ function applyProxy(ctx, config) {
|
|
|
699
999
|
return server
|
|
700
1000
|
}
|
|
701
1001
|
|
|
1002
|
+
// 记住「当前真正在监听」的地址:公网开关/密钥变化不该触发重建监听
|
|
1003
|
+
let appliedHost = ''
|
|
1004
|
+
let appliedPort = 0
|
|
1005
|
+
let appliedV6 = null
|
|
1006
|
+
|
|
702
1007
|
const startHosts = () => {
|
|
703
1008
|
// 端口可能在运行期被改过:每次重建监听都从状态文件取一次
|
|
704
1009
|
cfg.listenPort = effectivePort()
|
|
705
1010
|
const host = cfg.listenHost = effectiveListenHost()
|
|
1011
|
+
const wantV6 = getLanState().ipv6 === true
|
|
1012
|
+
appliedHost = host
|
|
1013
|
+
appliedPort = cfg.listenPort
|
|
1014
|
+
appliedV6 = wantV6
|
|
706
1015
|
start(host)
|
|
707
1016
|
// IPv6 用 ipv6Only,否则 Linux 双栈会把 0.0.0.0 占的端口算作冲突
|
|
708
|
-
if (
|
|
1017
|
+
if (wantV6 && host === '0.0.0.0') {
|
|
709
1018
|
try { start('::') } catch (e) { log('info', 'IPv6 监听跳过: ' + (e && e.message)) }
|
|
710
1019
|
}
|
|
711
1020
|
if (getLanState().lanAccess) {
|
|
@@ -722,7 +1031,15 @@ function applyProxy(ctx, config) {
|
|
|
722
1031
|
|
|
723
1032
|
let unsubscribeLan = () => {}
|
|
724
1033
|
try {
|
|
725
|
-
unsubscribeLan = onLanChange(() =>
|
|
1034
|
+
unsubscribeLan = onLanChange(() => {
|
|
1035
|
+
// 只有「监听地址 / 端口」变了才需要重建监听;
|
|
1036
|
+
// 公网开关、访问密钥、黑名单这些只影响请求判定,重建反而会让正在用的连接断掉
|
|
1037
|
+
const host = effectiveListenHost()
|
|
1038
|
+
const port = effectivePort()
|
|
1039
|
+
const v6 = getLanState().ipv6 === true
|
|
1040
|
+
if (host === appliedHost && port === appliedPort && v6 === appliedV6) return
|
|
1041
|
+
rebind()
|
|
1042
|
+
})
|
|
726
1043
|
} catch { /* 订阅失败不连坐 */ }
|
|
727
1044
|
|
|
728
1045
|
try {
|
package/lan-access.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 局域网访问开关(0.3.2)+ 自定义端口(0.3.5)+ 访问 IP / 黑名单(0.5.0
|
|
2
|
+
* 局域网访问开关(0.3.2)+ 自定义端口(0.3.5)+ 访问 IP / 黑名单(0.5.0)
|
|
3
|
+
* + 公网访问与访问密钥(0.6.0)。
|
|
3
4
|
*
|
|
4
5
|
* 默认关闭:反代只监听 127.0.0.1,同一台机器上的浏览器才能打开。
|
|
5
6
|
* 用户必须在设置页点开关(弹安全提示、点「确定」)或者在命令行里确认,
|
|
@@ -27,7 +28,28 @@ export const DEFAULT_PORT = 8080
|
|
|
27
28
|
/** 黑名单最多记这么多条,防止状态文件无限长。 */
|
|
28
29
|
export const MAX_BLOCKED = 200
|
|
29
30
|
|
|
31
|
+
/** 访问密钥:5–64 位,只允许英文字母和数字(拼音就是字母,不能有符号)。 */
|
|
32
|
+
export const MIN_KEY_LENGTH = 5
|
|
33
|
+
export const MAX_KEY_LENGTH = 64
|
|
34
|
+
|
|
35
|
+
export const PUBLIC_WARNING_TITLE = '对公网开放访问?'
|
|
36
|
+
export const PUBLIC_WARNING_LINES = [
|
|
37
|
+
'开启后,任何设备只要拿到 公网IP:端口/访问密钥 这条地址,就能直接进入 DSH 后台。',
|
|
38
|
+
'密钥至少 5 位、只用字母和数字;它等于后台的钥匙,请勿外传,也不要贴在公开的地方。',
|
|
39
|
+
'公网访问还取决于路由器/防火墙有没有把该端口映射出去——插件只管放行,不管打洞。',
|
|
40
|
+
]
|
|
41
|
+
export const PUBLIC_WARNING_TEXT = [PUBLIC_WARNING_TITLE, ...PUBLIC_WARNING_LINES].join('\n')
|
|
42
|
+
|
|
30
43
|
/** 安全提示文案:设置页弹窗与命令行共用同一份,避免两处说法不一致。 */
|
|
44
|
+
export const IPV6_WARNING_TITLE = '开启 IPv6 监听?'
|
|
45
|
+
export const IPV6_WARNING_LINES = [
|
|
46
|
+
'开启后除了 IPv4,还会多绑一条 [::]:端口 —— 同一网络里走 IPv6 的设备也能直接进入 DSH 后台。',
|
|
47
|
+
'很多宽带的 IPv6 本来就没有 NAT:只要路由器放行、地址被扫到,外面的设备也可能连进来,不像 IPv4 那样「没做端口映射就进不来」。',
|
|
48
|
+
'如果同时开了公网访问,IPv6 同样要过访问密钥;只开 IPv6 不开公网时,非内网来源仍按公网规则拦下(403)。',
|
|
49
|
+
'不确定就先别开:默认关闭,只监听本机最安全。',
|
|
50
|
+
]
|
|
51
|
+
export const IPV6_WARNING_TEXT = [IPV6_WARNING_TITLE, ...IPV6_WARNING_LINES].join('\n')
|
|
52
|
+
|
|
31
53
|
export const LAN_WARNING_TITLE = '对局域网开放访问?'
|
|
32
54
|
export const LAN_WARNING_LINES = [
|
|
33
55
|
'开启后,同一网络里的任何设备打开这台机器的 8080 端口,都能直接进入 DSH 后台,',
|
|
@@ -53,6 +75,18 @@ export const validatePort = (value) => {
|
|
|
53
75
|
return { ok: true, port: n }
|
|
54
76
|
}
|
|
55
77
|
|
|
78
|
+
/**
|
|
79
|
+
* 访问密钥校验:5–64 位,只允许 A-Z a-z 0-9(拼音是字母,符号一律不收)。
|
|
80
|
+
* 返回 { ok, key } 或 { ok:false, error }。
|
|
81
|
+
*/
|
|
82
|
+
export const validateKey = (value) => {
|
|
83
|
+
const raw = typeof value === 'string' ? value.trim() : ''
|
|
84
|
+
if (raw.length < MIN_KEY_LENGTH) return { ok: false, error: `访问密钥至少 ${MIN_KEY_LENGTH} 位` }
|
|
85
|
+
if (raw.length > MAX_KEY_LENGTH) return { ok: false, error: `访问密钥不能超过 ${MAX_KEY_LENGTH} 位` }
|
|
86
|
+
if (!/^[A-Za-z0-9]+$/.test(raw)) return { ok: false, error: '访问密钥只能包含英文字母和数字(拼音可以,符号不行)' }
|
|
87
|
+
return { ok: true, key: raw }
|
|
88
|
+
}
|
|
89
|
+
|
|
56
90
|
/**
|
|
57
91
|
* 归一化 IP:双栈 socket 会给 `::ffff:192.168.1.7`,去掉前缀才好比较、好显示。
|
|
58
92
|
* 非法字符串返回空串(调用方据此跳过)。
|
|
@@ -77,6 +111,9 @@ const DEFAULT_STATE = {
|
|
|
77
111
|
port: DEFAULT_PORT,
|
|
78
112
|
blacklistEnabled: false,
|
|
79
113
|
blockedIps: [],
|
|
114
|
+
publicAccess: false,
|
|
115
|
+
accessKey: '',
|
|
116
|
+
ipv6: false,
|
|
80
117
|
updatedAt: null,
|
|
81
118
|
updatedBy: null,
|
|
82
119
|
}
|
|
@@ -98,11 +135,16 @@ const readState = () => {
|
|
|
98
135
|
try {
|
|
99
136
|
const raw = JSON.parse(fs.readFileSync(STATE_FILE, 'utf8'))
|
|
100
137
|
const parsed = validatePort(raw?.port)
|
|
138
|
+
const key = validateKey(raw?.accessKey)
|
|
101
139
|
return {
|
|
102
140
|
lanAccess: raw?.lanAccess === true,
|
|
103
141
|
port: parsed.ok ? parsed.port : DEFAULT_PORT,
|
|
104
142
|
blacklistEnabled: raw?.blacklistEnabled === true,
|
|
105
143
|
blockedIps: sanitizeBlocked(raw?.blockedIps),
|
|
144
|
+
publicAccess: raw?.publicAccess === true && key.ok,
|
|
145
|
+
accessKey: key.ok ? key.key : '',
|
|
146
|
+
// IPv6 只有总开关开着时才有意义:总开关关着,端口只绑 127.0.0.1
|
|
147
|
+
ipv6: raw?.ipv6 === true && raw?.lanAccess === true,
|
|
106
148
|
updatedAt: typeof raw?.updatedAt === 'string' ? raw.updatedAt : null,
|
|
107
149
|
updatedBy: typeof raw?.updatedBy === 'string' ? raw.updatedBy : null,
|
|
108
150
|
}
|
|
@@ -162,8 +204,13 @@ export const onLanChange = (listener) => {
|
|
|
162
204
|
return () => { listeners.delete(listener) }
|
|
163
205
|
}
|
|
164
206
|
|
|
165
|
-
/**
|
|
166
|
-
|
|
207
|
+
/**
|
|
208
|
+
* 改总开关:写盘 → 通知监听者。`by` 只用于记录是谁动的(settings / command / cli)。
|
|
209
|
+
* 关掉总开关时顺带把公网访问也关掉——端口都不朝外听了,留着公网开关只会让人误会。
|
|
210
|
+
*/
|
|
211
|
+
export const setLanAccess = (enabled, by = 'unknown') => (
|
|
212
|
+
enabled === true ? commit({ lanAccess: true }, by) : commit({ lanAccess: false, publicAccess: false, ipv6: false }, by)
|
|
213
|
+
)
|
|
167
214
|
|
|
168
215
|
/** 改端口:校验 → 写盘 → 通知监听者(调用方负责确认端口没被占用)。 */
|
|
169
216
|
export const setPort = (value, by = 'unknown') => {
|
|
@@ -172,6 +219,30 @@ export const setPort = (value, by = 'unknown') => {
|
|
|
172
219
|
return commit({ port: parsed.port }, by)
|
|
173
220
|
}
|
|
174
221
|
|
|
222
|
+
/** 设访问密钥:校验 → 写盘。返回状态。 */
|
|
223
|
+
export const setAccessKey = (value, by = 'unknown') => {
|
|
224
|
+
const parsed = validateKey(value)
|
|
225
|
+
if (!parsed.ok) throw new Error(parsed.error)
|
|
226
|
+
return commit({ accessKey: parsed.key }, by)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 公网开关:打开必须先有合法密钥(可以同一次一起给)。
|
|
231
|
+
* 关掉只表示「不再放行公网」,总开关与局域网访问不受影响。
|
|
232
|
+
*/
|
|
233
|
+
export const setPublicAccess = (enabled, value, by = 'unknown') => {
|
|
234
|
+
if (enabled === true) {
|
|
235
|
+
// 总开关没开时端口只绑 127.0.0.1,公网根本进不来:与其让人以为「开了公网」,不如直接拒绝
|
|
236
|
+
if (state.lanAccess !== true) {
|
|
237
|
+
throw new Error('请先开启「局域网访问」总开关:端口朝外监听由它管,公网开关只决定要不要校验密钥。')
|
|
238
|
+
}
|
|
239
|
+
const parsed = value === undefined ? (state.accessKey !== '' ? { ok: true, key: state.accessKey } : validateKey('')) : validateKey(value)
|
|
240
|
+
if (!parsed.ok) throw new Error(parsed.error)
|
|
241
|
+
return commit({ publicAccess: true, accessKey: parsed.key }, by)
|
|
242
|
+
}
|
|
243
|
+
return commit({ publicAccess: false }, by)
|
|
244
|
+
}
|
|
245
|
+
|
|
175
246
|
/** 黑名单总开关。关掉时不拦任何人,但名单保留,下次打开继续生效。 */
|
|
176
247
|
export const setBlacklistEnabled = (enabled, by = 'unknown') => commit({ blacklistEnabled: enabled === true }, by)
|
|
177
248
|
|
|
@@ -217,8 +288,19 @@ export const resetConfig = (by = 'unknown') => commit({
|
|
|
217
288
|
port: DEFAULT_PORT,
|
|
218
289
|
blacklistEnabled: false,
|
|
219
290
|
blockedIps: [],
|
|
291
|
+
publicAccess: false,
|
|
292
|
+
accessKey: '',
|
|
293
|
+
ipv6: false,
|
|
220
294
|
}, by)
|
|
221
295
|
|
|
296
|
+
export const setIpv6 = (enabled, by = 'unknown') => {
|
|
297
|
+
if (enabled === true && state.lanAccess !== true) {
|
|
298
|
+
// 总开关没开时只监听 127.0.0.1,IPv6 监听没有任何意义
|
|
299
|
+
throw new Error('请先开启「局域网访问」总开关:端口朝外监听由它管,IPv6 开关只决定要不要再绑一条 [::]。')
|
|
300
|
+
}
|
|
301
|
+
return commit({ ipv6: enabled === true }, by)
|
|
302
|
+
}
|
|
303
|
+
|
|
222
304
|
/** 盯住状态文件:命令行脚本 / 手动改 json 也能触发重建监听。 */
|
|
223
305
|
const startWatch = () => {
|
|
224
306
|
try {
|
|
@@ -230,6 +312,9 @@ const startWatch = () => {
|
|
|
230
312
|
&& next.port === state.port
|
|
231
313
|
&& next.blacklistEnabled === state.blacklistEnabled
|
|
232
314
|
&& next.blockedIps.join(',') === state.blockedIps.join(',')
|
|
315
|
+
&& next.publicAccess === state.publicAccess
|
|
316
|
+
&& next.accessKey === state.accessKey
|
|
317
|
+
&& next.ipv6 === state.ipv6
|
|
233
318
|
if (same) return
|
|
234
319
|
state = next
|
|
235
320
|
notify()
|
|
@@ -252,6 +337,15 @@ export const describeLanState = (port) => {
|
|
|
252
337
|
blacklistEnabled: current.blacklistEnabled,
|
|
253
338
|
blockedIps: current.blockedIps,
|
|
254
339
|
blockedCount: current.blockedIps.length,
|
|
340
|
+
publicAccess: current.publicAccess,
|
|
341
|
+
hasKey: current.accessKey !== '',
|
|
342
|
+
// 打开公网时把「该用哪条地址」直接给出去,省得用户自己拼
|
|
343
|
+
accessPath: current.publicAccess && current.accessKey !== '' ? '/' + current.accessKey : '',
|
|
344
|
+
keyHint: current.accessKey === '' ? '' : current.accessKey.slice(0, 2) + '***' + current.accessKey.slice(-1),
|
|
345
|
+
minKeyLength: MIN_KEY_LENGTH,
|
|
346
|
+
ipv6: current.ipv6,
|
|
347
|
+
warningPublic: PUBLIC_WARNING_TEXT,
|
|
348
|
+
warningIpv6: IPV6_WARNING_TEXT,
|
|
255
349
|
stateFile: STATE_FILE,
|
|
256
350
|
}
|
|
257
351
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-reverse-proxy",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "DSH Web
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "DSH Web 局域网/公网访问插件:内置反向代理 + 免 Token 登录,手机同 Wi-Fi 直接打开后台;公网访问需开关 + 访问密钥(地址变成 /密钥,密钥可一键随机生成);可选 IPv6 监听;端口可改,带访问 IP 黑名单与一键重置。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"exports": {
|
package/screenshots.json
CHANGED
package/vendor/skip/client.js
CHANGED
|
@@ -95,6 +95,29 @@ window.__ModuleLoader__.load({
|
|
|
95
95
|
'不需要 Token、不需要登录,等于把整台机器的控制权交给同一网段的所有人。',
|
|
96
96
|
'只在你信任当前 Wi-Fi / 局域网(比如家里、自己手机的热点)时才开启。'
|
|
97
97
|
];
|
|
98
|
+
const IPV6_WARNING_TITLE = '开启 IPv6 监听?';
|
|
99
|
+
const IPV6_WARNING_LINES = [
|
|
100
|
+
'开启后除了 IPv4,还会多绑一条 [::]:端口,同网络里走 IPv6 的设备也能直接进入 DSH 后台。',
|
|
101
|
+
'很多宽带的 IPv6 本来就没有 NAT:只要路由器放行、地址被扫到,外面的设备也可能连进来。',
|
|
102
|
+
'公网访问开着时,IPv6 同样要过访问密钥;不确定就先别开。'
|
|
103
|
+
];
|
|
104
|
+
// 按钮点一下就能得到一串随机密钥:小写字母 + 数字,12 位,远高于最少 5 位
|
|
105
|
+
const randomKey = () => {
|
|
106
|
+
const alphabet = 'abcdefghijkmnpqrstuvwxyz23456789';
|
|
107
|
+
let out = '';
|
|
108
|
+
try {
|
|
109
|
+
const buf = new Uint8Array(12);
|
|
110
|
+
(globalThis.crypto || {}).getRandomValues ? globalThis.crypto.getRandomValues(buf) : null;
|
|
111
|
+
for (let i = 0; i < buf.length; i += 1) {
|
|
112
|
+
out += buf[i] ? alphabet[buf[i] % alphabet.length] : alphabet[(Date.now() + i) % alphabet.length];
|
|
113
|
+
}
|
|
114
|
+
} catch (e) { out = ''; }
|
|
115
|
+
if (out.length !== 12) {
|
|
116
|
+
out = '';
|
|
117
|
+
for (let i = 0; i < 12; i += 1) out += alphabet[Math.floor(Math.random() * alphabet.length)];
|
|
118
|
+
}
|
|
119
|
+
return out;
|
|
120
|
+
};
|
|
98
121
|
const STYLE = [
|
|
99
122
|
'.reproxy-lan{display:flex;flex-direction:column;gap:14px;padding:4px 0}',
|
|
100
123
|
'.reproxy-lan-row{display:flex;align-items:center;justify-content:space-between;gap:16px;border:1px solid var(--dsw-alias-border-l1,#333947);border-radius:10px;padding:14px 16px}',
|
|
@@ -110,6 +133,9 @@ window.__ModuleLoader__.load({
|
|
|
110
133
|
'.reproxy-lan-port{flex:none;display:flex;align-items:center;gap:10px}',
|
|
111
134
|
'.reproxy-lan-input{width:96px;padding:7px 10px;border-radius:8px;border:1px solid var(--dsw-alias-border-l1,#3a3f4b);background:rgba(0,0,0,.22);color:var(--dsw-alias-label-primary,#e6e8ee);font-size:13px;font-variant-numeric:tabular-nums;text-align:center}',
|
|
112
135
|
'.reproxy-lan-input:focus{outline:none;border-color:#8b5cf6}',
|
|
136
|
+
'.reproxy-lan-random{border-radius:8px;padding:7px 12px;font-size:13px;cursor:pointer;border:1px solid rgba(139,92,246,.55);background:rgba(139,92,246,.16);color:#c4b5fd;margin-right:8px;flex:none}',
|
|
137
|
+
'.reproxy-lan-random:hover{background:rgba(139,92,246,.26)}',
|
|
138
|
+
'.reproxy-lan-random[disabled]{opacity:.55;cursor:default}',
|
|
113
139
|
'.reproxy-lan-save{border-radius:8px;padding:7px 14px;font-size:13px;cursor:pointer;border:1px solid transparent;background:#8b5cf6;color:#fff}',
|
|
114
140
|
'.reproxy-lan-save:hover{background:#9b71f8}',
|
|
115
141
|
'.reproxy-lan-save[disabled]{opacity:.5;cursor:default}',
|
|
@@ -119,6 +145,15 @@ window.__ModuleLoader__.load({
|
|
|
119
145
|
'.reproxy-lan-note{margin-top:10px;padding:8px 10px;border-radius:8px;font-size:12px;line-height:1.75;color:var(--dsw-alias-label-secondary,#9aa0ad);background:rgba(255,255,255,.04);border:1px solid var(--dsw-alias-border-l1,#333947)}',
|
|
120
146
|
'.reproxy-lan-note b{color:var(--dsw-alias-label-primary,#e8ebf2);font-weight:600}',
|
|
121
147
|
'.reproxy-lan-err{font-size:12px;color:#ff6b6b}',
|
|
148
|
+
'.reproxy-lan-scroll{margin-top:10px;max-height:212px;overflow-y:auto;overflow-x:hidden;display:flex;flex-direction:column;gap:6px;padding:8px;border-radius:10px;border:1px solid var(--dsw-alias-border-l1,#333947);background:rgba(0,0,0,.16);overscroll-behavior:contain}',
|
|
149
|
+
'.reproxy-lan-scroll::-webkit-scrollbar{width:8px}',
|
|
150
|
+
'.reproxy-lan-scroll::-webkit-scrollbar-thumb{background:rgba(255,255,255,.16);border-radius:999px}',
|
|
151
|
+
'.reproxy-lan-group{display:flex;flex-direction:column;gap:6px}',
|
|
152
|
+
'.reproxy-lan-grouphead{display:flex;align-items:center;gap:8px;margin:2px 0 2px}',
|
|
153
|
+
'.reproxy-lan-badge{flex:none;font-size:11px;line-height:1;padding:4px 8px;border-radius:999px;border:1px solid rgba(139,92,246,.5);color:#c4b5fd;background:rgba(139,92,246,.12)}',
|
|
154
|
+
'.reproxy-lan-badge.lan{border-color:rgba(94,224,138,.45);color:#8fe6b0;background:rgba(94,224,138,.1)}',
|
|
155
|
+
'.reproxy-lan-empty{font-size:12px;color:var(--dsw-alias-label-secondary,#8b90a0);padding:2px 0 6px}',
|
|
156
|
+
'.reproxy-lan-ip{display:flex;align-items:center;gap:10px;font-size:13px;font-variant-numeric:tabular-nums;color:var(--dsw-alias-label-primary,#e6e8ee);padding:2px 0}',
|
|
122
157
|
'.reproxy-lan-list{margin-top:10px;display:flex;flex-direction:column;gap:6px}',
|
|
123
158
|
'.reproxy-lan-ip{display:flex;align-items:center;gap:10px;font-size:13px;font-variant-numeric:tabular-nums;color:var(--dsw-alias-label-primary,#e6e8ee)}',
|
|
124
159
|
'.reproxy-lan-add{border-radius:6px;padding:3px 10px;font-size:12px;cursor:pointer;border:1px solid rgba(139,92,246,.7);background:transparent;color:#c4b5fd}',
|
|
@@ -163,6 +198,21 @@ window.__ModuleLoader__.load({
|
|
|
163
198
|
headers: { 'content-type': 'application/json' },
|
|
164
199
|
body: JSON.stringify({ enabled })
|
|
165
200
|
}).then((r) => r.json());
|
|
201
|
+
const writePublic = (enabled, key) => fetch('/reproxy-lan/public', {
|
|
202
|
+
method: 'POST',
|
|
203
|
+
headers: { 'content-type': 'application/json' },
|
|
204
|
+
body: JSON.stringify({ enabled, key, confirm: true })
|
|
205
|
+
}).then((r) => r.json());
|
|
206
|
+
const writeIpv6 = (enabled) => fetch('/reproxy-lan/ipv6', {
|
|
207
|
+
method: 'POST',
|
|
208
|
+
headers: { 'content-type': 'application/json' },
|
|
209
|
+
body: JSON.stringify({ enabled, confirm: true })
|
|
210
|
+
}).then((r) => r.json());
|
|
211
|
+
const writeKey = (key) => fetch('/reproxy-lan/key', {
|
|
212
|
+
method: 'POST',
|
|
213
|
+
headers: { 'content-type': 'application/json' },
|
|
214
|
+
body: JSON.stringify({ key })
|
|
215
|
+
}).then((r) => r.json());
|
|
166
216
|
function Panel() {
|
|
167
217
|
const [state, setState] = React.useState(null);
|
|
168
218
|
const [asking, setAsking] = React.useState(false);
|
|
@@ -177,6 +227,14 @@ window.__ModuleLoader__.load({
|
|
|
177
227
|
const [ipBusy, setIpBusy] = React.useState('');
|
|
178
228
|
const [ipMsg, setIpMsg] = React.useState('');
|
|
179
229
|
const [ipBad, setIpBad] = React.useState(false);
|
|
230
|
+
const [keyDraft, setKeyDraft] = React.useState('');
|
|
231
|
+
const [pubBusy, setPubBusy] = React.useState(false);
|
|
232
|
+
const [pubMsg, setPubMsg] = React.useState('');
|
|
233
|
+
const [pubBad, setPubBad] = React.useState(false);
|
|
234
|
+
const [askV6, setAskV6] = React.useState(false);
|
|
235
|
+
const [v6Busy, setV6Busy] = React.useState(false);
|
|
236
|
+
const [v6Msg, setV6Msg] = React.useState('');
|
|
237
|
+
const [v6Bad, setV6Bad] = React.useState(false);
|
|
180
238
|
React.useEffect(() => {
|
|
181
239
|
let alive = true;
|
|
182
240
|
readState().then((body) => {
|
|
@@ -225,6 +283,37 @@ window.__ModuleLoader__.load({
|
|
|
225
283
|
setPortMsg('已保存,正在切到 ' + body.port + ' 端口');
|
|
226
284
|
}).catch((e) => fail(String((e && e.message) || e))).then(() => setPortBusy(false));
|
|
227
285
|
};
|
|
286
|
+
const applyPublic = (enabled) => {
|
|
287
|
+
const fail = (msg) => { setPubBad(true); setPubMsg(msg); };
|
|
288
|
+
const key = String(keyDraft).trim();
|
|
289
|
+
if (enabled && key === '' && !(state && state.publicAccess === true)) {
|
|
290
|
+
return fail('开启公网访问前必须输入访问密钥(至少 5 位字母或数字)');
|
|
291
|
+
}
|
|
292
|
+
if (key !== '' && !/^[A-Za-z0-9]{5,64}$/.test(key)) {
|
|
293
|
+
return fail('访问密钥至少 5 位,只能包含英文字母和数字(拼音可以,符号不行)');
|
|
294
|
+
}
|
|
295
|
+
setPubBusy(true);
|
|
296
|
+
setPubMsg('');
|
|
297
|
+
setPubBad(false);
|
|
298
|
+
writePublic(enabled, key === '' ? undefined : key).then((body) => {
|
|
299
|
+
if (!body || body.ok !== true) throw new Error((body && body.error) || '保存失败');
|
|
300
|
+
setState(body);
|
|
301
|
+
if (key !== '') setKeyDraft('');
|
|
302
|
+
setPubMsg(enabled ? '公网访问已开启,用下面那条地址访问' : '公网访问已关闭');
|
|
303
|
+
}).catch((e) => fail(String((e && e.message) || e))).then(() => setPubBusy(false));
|
|
304
|
+
};
|
|
305
|
+
const applyV6 = (enabled) => {
|
|
306
|
+
setV6Busy(true);
|
|
307
|
+
setV6Msg('');
|
|
308
|
+
setV6Bad(false);
|
|
309
|
+
writeIpv6(enabled).then((body) => {
|
|
310
|
+
const stateShaped = body && typeof body.ipv6 === 'boolean';
|
|
311
|
+
if (!body || (body.ok !== true && !stateShaped)) throw new Error((body && body.error) || '保存失败');
|
|
312
|
+
setState(body);
|
|
313
|
+
setAskV6(false);
|
|
314
|
+
setV6Msg(enabled ? 'IPv6 监听已开启,正在重建监听' : 'IPv6 监听已关闭');
|
|
315
|
+
}).catch((e) => { setV6Bad(true); setV6Msg(String((e && e.message) || e)); }).then(() => setV6Busy(false));
|
|
316
|
+
};
|
|
228
317
|
const applyBlock = (ip) => {
|
|
229
318
|
setIpBusy(ip);
|
|
230
319
|
setIpMsg('');
|
|
@@ -261,6 +350,28 @@ window.__ModuleLoader__.load({
|
|
|
261
350
|
setIpMsg(body.blacklistEnabled ? '黑名单已打开' : '黑名单已关闭(名单保留)');
|
|
262
351
|
}).catch((e) => { setIpBad(true); setIpMsg(String((e && e.message) || e)); }).then(() => setIpBusy(''));
|
|
263
352
|
};
|
|
353
|
+
// 每行自己带「公网 / 内网」标签(用户要的排法:标签在 IP 前,末尾「添加」)
|
|
354
|
+
const renderIpRow = (item) => h('div', { key: item.ip, className: 'reproxy-lan-ip' },
|
|
355
|
+
h('span', { className: 'reproxy-lan-badge' + (item.private === true ? ' lan' : '') }, item.private === true ? '内网' : '公网'),
|
|
356
|
+
h('span', { style: { minWidth: '116px' } }, item.ip),
|
|
357
|
+
h('span', { className: 'reproxy-lan-meta' }, '最近 ' + new Date(item.lastSeen).toLocaleTimeString() + ' · 共 ' + item.hits + ' 次'),
|
|
358
|
+
h('button', {
|
|
359
|
+
className: 'reproxy-lan-add',
|
|
360
|
+
type: 'button',
|
|
361
|
+
disabled: item.blocked || ipBusy === item.ip,
|
|
362
|
+
onClick: () => applyBlock(item.ip)
|
|
363
|
+
}, item.blocked ? '已在黑名单' : (ipBusy === item.ip ? '添加中…' : '添加'))
|
|
364
|
+
);
|
|
365
|
+
// 公网排前面、内网排后面,行内各自带标签,不再分两段标题
|
|
366
|
+
const groupOf = (list) => {
|
|
367
|
+
const pub = list.filter((item) => item.private !== true);
|
|
368
|
+
const lan = list.filter((item) => item.private === true);
|
|
369
|
+
if (list.length === 0) {
|
|
370
|
+
return [h('div', { key: 'empty', className: 'reproxy-lan-empty' }, '暂时没有记录')];
|
|
371
|
+
}
|
|
372
|
+
return pub.concat(lan).map(renderIpRow);
|
|
373
|
+
};
|
|
374
|
+
const setPubMsg2 = (text) => { setPubMsg(text || ''); setPubBad(true); };
|
|
264
375
|
const on = !!(state && state.lanAccess);
|
|
265
376
|
const port = state && state.port ? state.port : 8080;
|
|
266
377
|
const blockedIps = state && Array.isArray(state.blockedIps) ? state.blockedIps : [];
|
|
@@ -284,6 +395,24 @@ window.__ModuleLoader__.load({
|
|
|
284
395
|
}, h('i'))
|
|
285
396
|
)
|
|
286
397
|
),
|
|
398
|
+
h('div', { className: 'reproxy-lan-row' },
|
|
399
|
+
h('div', null,
|
|
400
|
+
h('div', { className: 'reproxy-lan-title' }, 'IPv6 监听(默认关闭)'),
|
|
401
|
+
h('div', { className: 'reproxy-lan-desc' }, '开启后除了 IPv4,还会多绑一条 [::]:' + port + ',走 IPv6 的设备也能进。很多宽带的 IPv6 没有 NAT,外面的设备可能直接连进来 —— 不确定就先别开。')
|
|
402
|
+
),
|
|
403
|
+
h('div', { className: 'reproxy-lan-ctl' },
|
|
404
|
+
h('span', { className: 'reproxy-lan-state' }, state !== null && state.ipv6 === true ? '已开启' : '已关闭'),
|
|
405
|
+
h('button', {
|
|
406
|
+
className: 'reproxy-lan-switch' + (state && state.ipv6 === true ? ' on' : ''),
|
|
407
|
+
type: 'button',
|
|
408
|
+
'aria-pressed': state && state.ipv6 === true ? 'true' : 'false',
|
|
409
|
+
disabled: v6Busy || state === null || !on,
|
|
410
|
+
title: !on ? '先打开上面的「局域网访问」总开关' : (state && state.ipv6 === true ? '点击关闭 IPv6 监听' : '点击开启 IPv6 监听(会先弹出安全提示)'),
|
|
411
|
+
onClick: () => setAskV6(true)
|
|
412
|
+
}, h('i'))
|
|
413
|
+
)
|
|
414
|
+
),
|
|
415
|
+
v6Msg === '' ? null : h('div', { className: v6Bad ? 'reproxy-lan-err' : 'reproxy-lan-ok' }, v6Msg),
|
|
287
416
|
h('div', { className: 'reproxy-lan-row' },
|
|
288
417
|
h('div', null,
|
|
289
418
|
h('div', { className: 'reproxy-lan-title' }, '监听端口'),
|
|
@@ -308,24 +437,59 @@ window.__ModuleLoader__.load({
|
|
|
308
437
|
}, portBusy ? '保存中…' : '保存')
|
|
309
438
|
)
|
|
310
439
|
),
|
|
440
|
+
h('div', { className: 'reproxy-lan-row' },
|
|
441
|
+
h('div', { style: { width: '100%', minWidth: 0 } },
|
|
442
|
+
h('div', { className: 'reproxy-lan-title' }, '公网访问'),
|
|
443
|
+
h('div', { className: 'reproxy-lan-desc' }, '默认只放行局域网地址,公网 IP 会被拦下(403)。要允许公网访问,请开启这个开关并填写访问密钥——开启后访问地址会变成 http://<公网IP>:' + port + '/<访问密钥>,不知道密钥的人打开只会看到 404。'),
|
|
444
|
+
state && state.publicAccess ? h('div', { className: 'reproxy-lan-meta', style: { marginTop: '10px' } }, '当前访问地址:http://<公网IP>:' + port + (state.accessPath || '/<访问密钥>')) : null,
|
|
445
|
+
h('div', { className: 'reproxy-lan-port', style: { marginTop: '12px' } },
|
|
446
|
+
h('button', {
|
|
447
|
+
className: 'reproxy-lan-random',
|
|
448
|
+
type: 'button',
|
|
449
|
+
title: '生成一个 12 位随机访问密钥',
|
|
450
|
+
disabled: pubBusy || state === null,
|
|
451
|
+
onClick: () => { setKeyDraft(randomKey()); setPubMsg(''); setPubBad(false); }
|
|
452
|
+
}, '随机'),
|
|
453
|
+
h('input', {
|
|
454
|
+
className: 'reproxy-lan-input',
|
|
455
|
+
style: { width: '180px', textAlign: 'left' },
|
|
456
|
+
type: 'text',
|
|
457
|
+
value: keyDraft,
|
|
458
|
+
disabled: pubBusy || state === null,
|
|
459
|
+
placeholder: state && state.hasKey ? ('已设置,' + String(state.keyHint || '') + ',输入新的可更换') : '访问密钥(至少 5 位)',
|
|
460
|
+
onChange: (e) => { setKeyDraft(e.target.value); setPubMsg(''); setPubBad(false); }
|
|
461
|
+
}),
|
|
462
|
+
h('button', {
|
|
463
|
+
className: 'reproxy-lan-save',
|
|
464
|
+
type: 'button',
|
|
465
|
+
disabled: pubBusy || state === null,
|
|
466
|
+
onClick: () => applyPublic(true)
|
|
467
|
+
}, pubBusy ? '保存中…' : '保存并开启')
|
|
468
|
+
),
|
|
469
|
+
h('div', { className: 'reproxy-lan-ctl', style: { marginTop: '14px', justifyContent: 'flex-start' } },
|
|
470
|
+
h('span', { className: 'reproxy-lan-state' }, '公网开关(无需求请勿开启)'),
|
|
471
|
+
h('button', {
|
|
472
|
+
className: 'reproxy-lan-switch' + (state && state.publicAccess ? ' on' : ''),
|
|
473
|
+
type: 'button',
|
|
474
|
+
'aria-pressed': state && state.publicAccess ? 'true' : 'false',
|
|
475
|
+
// 总开关没开时端口只绑 127.0.0.1,公网开了也进不来 —— 干脆不让点
|
|
476
|
+
disabled: pubBusy || state === null || !on,
|
|
477
|
+
title: !on ? '先打开上面的「局域网访问」总开关' : (state && state.publicAccess ? '点击关闭公网访问' : '点击开启公网访问(需先填密钥)'),
|
|
478
|
+
onClick: () => (on ? applyPublic(!(state && state.publicAccess === true)) : setPubMsg2('请先打开上面的「局域网访问」总开关,公网访问才有意义。'))
|
|
479
|
+
}, h('i'))
|
|
480
|
+
),
|
|
481
|
+
on ? null : h('div', { className: 'reproxy-lan-meta', style: { marginTop: '6px' } }, '总开关关着时端口只监听 127.0.0.1,公网开关点不动 —— 先打开上面的「局域网访问」。'),
|
|
482
|
+
pubMsg === '' ? null : h('div', { className: pubBad ? 'reproxy-lan-err' : 'reproxy-lan-ok', style: { marginTop: '8px' } }, pubMsg)
|
|
483
|
+
)
|
|
484
|
+
),
|
|
311
485
|
h('div', { className: 'reproxy-lan-row' },
|
|
312
486
|
h('div', { style: { width: '100%', minWidth: 0 } },
|
|
313
487
|
h('div', { className: 'reproxy-lan-title' }, '访问 IP'),
|
|
314
488
|
h('div', { className: 'reproxy-lan-desc' }, '最近连过这个端口的设备。点「添加」把它的 IP 拉进黑名单:被拉黑的设备再打开后台只会看到 403,直到你在这里移除。'),
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
h('span', null, item.ip),
|
|
320
|
-
h('span', { className: 'reproxy-lan-meta' }, '最近 ' + new Date(item.lastSeen).toLocaleTimeString() + ' · 共 ' + item.hits + ' 次'),
|
|
321
|
-
h('button', {
|
|
322
|
-
className: 'reproxy-lan-add',
|
|
323
|
-
type: 'button',
|
|
324
|
-
disabled: item.blocked || ipBusy === item.ip,
|
|
325
|
-
onClick: () => applyBlock(item.ip)
|
|
326
|
-
}, item.blocked ? '已在黑名单' : (ipBusy === item.ip ? '添加中…' : '添加'))
|
|
327
|
-
))
|
|
328
|
-
),
|
|
489
|
+
clients.length === 0
|
|
490
|
+
? h('div', { className: 'reproxy-lan-list' },
|
|
491
|
+
h('div', { className: 'reproxy-lan-meta' }, '还没有记录(有人访问后台后才会出现)'))
|
|
492
|
+
: h('div', { className: 'reproxy-lan-scroll' }, groupOf(clients)),
|
|
329
493
|
h('div', { className: 'reproxy-lan-ctl', style: { marginTop: '14px', justifyContent: 'flex-start' } },
|
|
330
494
|
h('span', { className: 'reproxy-lan-state' }, '黑名单开关(无需求请勿开启)'),
|
|
331
495
|
h('button', {
|
|
@@ -337,7 +501,7 @@ window.__ModuleLoader__.load({
|
|
|
337
501
|
onClick: toggleBlacklist
|
|
338
502
|
}, h('i'))
|
|
339
503
|
),
|
|
340
|
-
blockedIps.length === 0 ? null : h('div', { className: 'reproxy-lan-chips' },
|
|
504
|
+
blockedIps.length === 0 ? null : h('div', { className: 'reproxy-lan-chips reproxy-lan-scroll', style: { flexDirection: 'row', flexWrap: 'wrap' } },
|
|
341
505
|
blockedIps.map((ip) => h('span', { key: ip, className: 'reproxy-lan-chip' },
|
|
342
506
|
ip,
|
|
343
507
|
h('button', { type: 'button', disabled: ipBusy === ip, onClick: () => removeBlock(ip) }, ipBusy === ip ? '…' : '移除')
|
|
@@ -357,6 +521,19 @@ window.__ModuleLoader__.load({
|
|
|
357
521
|
'手机和电脑各开一个、或其中一个用无痕窗口,都不受影响。'
|
|
358
522
|
),
|
|
359
523
|
error ? h('div', { className: 'reproxy-lan-err' }, error) : null,
|
|
524
|
+
askV6 ? h('div', { className: 'reproxy-lan-mask' },
|
|
525
|
+
h('div', { className: 'reproxy-lan-card' },
|
|
526
|
+
h('h3', null, IPV6_WARNING_TITLE),
|
|
527
|
+
h('p', null, IPV6_WARNING_LINES[0]),
|
|
528
|
+
h('p', null, IPV6_WARNING_LINES[1]),
|
|
529
|
+
h('p', null, IPV6_WARNING_LINES[2]),
|
|
530
|
+
h('p', null, h('strong', null, state && state.ipv6 === true ? '这次点击会关闭 IPv6 监听。' : '这次点击会额外绑定 [::],确定要继续吗?')),
|
|
531
|
+
h('div', { className: 'reproxy-lan-actions' },
|
|
532
|
+
h('button', { className: 'reproxy-lan-btn', type: 'button', disabled: v6Busy, onClick: () => setAskV6(false) }, '取消'),
|
|
533
|
+
h('button', { className: 'reproxy-lan-btn primary', type: 'button', disabled: v6Busy, onClick: () => applyV6(!(state && state.ipv6 === true)) }, v6Busy ? '处理中…' : '确定')
|
|
534
|
+
)
|
|
535
|
+
)
|
|
536
|
+
) : null,
|
|
360
537
|
asking ? h('div', { className: 'reproxy-lan-mask' },
|
|
361
538
|
h('div', { className: 'reproxy-lan-card' },
|
|
362
539
|
h('h3', null, LAN_WARNING_TITLE),
|
package/assets/access-ip.jpg
DELETED
|
Binary file
|