dsh-plugin-reverse-proxy 0.4.4 → 0.5.1
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 +25 -0
- package/README.en.md +30 -2
- package/README.md +39 -2
- package/assets/access-ip.jpg +0 -0
- package/assets/blocked-403.jpg +0 -0
- package/index.js +204 -2
- package/lan-access.js +130 -37
- package/package.json +4 -3
- package/screenshots.json +6 -0
- package/vendor/skip/client.js +112 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,31 @@
|
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
+
## 0.5.1
|
|
8
|
+
|
|
9
|
+
仅元数据:`package.json` 的 `description` 改成与仓库简介一致(补上「端口可自定义、访问 IP 黑名单与一键重置」),
|
|
10
|
+
让 npm 包页面与应用市场的简介不再是 0.4.3 那版旧话。代码与 0.5.0 完全相同。
|
|
11
|
+
|
|
12
|
+
## 0.5.0
|
|
13
|
+
|
|
14
|
+
**更新内容**
|
|
15
|
+
- 设置页新增「访问 IP」卡片:列出最近连过这个端口的设备(IP、最近访问时间、请求次数),
|
|
16
|
+
IP 右边就是「添加」按钮,点一下把该设备丢进黑名单——被拉黑的设备再打开后台只会看到 403 页面。
|
|
17
|
+
- 新增「黑名单」列表与**黑名单开关**:开关关掉只是不拦人(名单保留),开关旁边标注「无需求请勿开启」。
|
|
18
|
+
- 同一个 IP 重复点「添加」不会重复写入(接口返回 `added:false`,界面提示「已经在黑名单里」)。
|
|
19
|
+
- 黑名单状态与「局域网访问」「端口」共用同一个状态文件,**重启 dsh 照样保留**。
|
|
20
|
+
- 新增接口:`GET /reproxy-lan/clients`、`POST /reproxy-lan/block`、`POST /reproxy-lan/unblock`、`POST /reproxy-lan/blacklist`。
|
|
21
|
+
|
|
22
|
+
- 新增重置命令 `reproxy-lan reset` 与 `/lan reset`(需输入 `yes` / `/lan reset yes` 二次确认):
|
|
23
|
+
一键恢复默认(局域网访问关、端口 8080、黑名单关并清空),给误操作留一条退路。
|
|
24
|
+
- README 补两张真机截图:设置页的「访问 IP」卡片、被拉黑设备看到的 403 页面;`screenshots.json` 一并补上(插件市场卡片会取)。
|
|
25
|
+
|
|
26
|
+
**修复内容**
|
|
27
|
+
- 终端脚本 `reproxy-lan` 改状态时会丢掉 `blacklistEnabled` / `blockedIps` 两个字段(等于悄悄清了黑名单),
|
|
28
|
+
改成读旧值合并、只改关心的字段。
|
|
29
|
+
- 黑名单会同时拦普通请求与 WebSocket 升级请求(`onRequest` / `onUpgrade` 两处都校验),不会只挡页面不挡实时通道。
|
|
30
|
+
- 禁止把「当前正在使用的这台设备」或回环地址拉黑,避免管理员把自己锁在门外(接口返回 400 并说明原因)。
|
|
31
|
+
|
|
7
32
|
## 0.4.4
|
|
8
33
|
|
|
9
34
|
仅文档与截图,代码同 0.4.2。
|
package/README.en.md
CHANGED
|
@@ -42,9 +42,20 @@ confirm — your phone can connect.
|
|
|
42
42
|
|
|
43
43
|
No GUI around? The same switch works from a terminal (`yes` is required) — see [Usage](#usage).
|
|
44
44
|
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
**Access IPs and blacklist** (0.5.0+) — hit "Add" to blacklist a device; it then only sees a 403 page:
|
|
48
|
+
|
|
49
|
+
<p align="center">
|
|
50
|
+
<img src="assets/access-ip.jpg" width="620" alt="Settings · access IPs and blacklist">
|
|
51
|
+
</p>
|
|
52
|
+
|
|
53
|
+
<p align="center">
|
|
54
|
+
<img src="assets/blocked-403.jpg" width="320" alt="403 page shown to a blacklisted device">
|
|
55
|
+
</p>
|
|
45
56
|
---
|
|
46
57
|
|
|
47
|
-
##
|
|
58
|
+
## Four capabilities
|
|
48
59
|
|
|
49
60
|
> Since 0.3.5: the settings switch is purple, and the **listening port** is editable
|
|
50
61
|
> (default 8080, applied on save, kept across restarts).
|
|
@@ -117,7 +128,24 @@ edits to that file are picked up within a second (`fs.watch`).
|
|
|
117
128
|
|
|
118
129
|
---
|
|
119
130
|
|
|
120
|
-
### 3.
|
|
131
|
+
### 3. Access IPs and blacklist (off by default)
|
|
132
|
+
|
|
133
|
+
- Lists every device that recently hit the port (IP, last seen, request count).
|
|
134
|
+
- **Add** drops that IP into the blacklist; the device then gets a 403 page until you remove it.
|
|
135
|
+
- A **blacklist switch** sits right below the list ("no need, do not enable") — turning it off stops blocking but keeps the list.
|
|
136
|
+
- Adding the same IP twice is a no-op (`added:false`); loopback and the device you are currently using cannot be blocked (HTTP 400), so you cannot lock yourself out.
|
|
137
|
+
- The list is stored in the same state file (`blacklistEnabled`, `blockedIps`) and survives restarts.
|
|
138
|
+
|
|
139
|
+
**Reset (safety valve for misconfiguration):**
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
reproxy-lan reset # shows what will be restored, then asks you to type yes
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Or `/lan reset` in chat, confirmed with `/lan reset yes`. Reset turns LAN access off, port back to 8080,
|
|
146
|
+
blacklist off and clears the list — devices currently connected over 8080 drop immediately, by design.
|
|
147
|
+
|
|
148
|
+
### 4. Token-free login (version-gated)
|
|
121
149
|
|
|
122
150
|
Replaces the official `@deepseek-ai/dsh-client-connection` provider and skips BrowserAuth.
|
|
123
151
|
|
package/README.md
CHANGED
|
@@ -39,11 +39,21 @@ dsh plugin --profile web add dsh-plugin-reverse-proxy # 推荐:npm 包,免
|
|
|
39
39
|
<img src="assets/settings-lan-access.jpg" width="620" alt="设置 · 局域网访问">
|
|
40
40
|
</p>
|
|
41
41
|
|
|
42
|
+
**访问 IP 与黑名单**(0.5.0 起)——点「添加」把设备拉黑,它再打开后台只会看到 403:
|
|
43
|
+
|
|
44
|
+
<p align="center">
|
|
45
|
+
<img src="assets/access-ip.jpg" width="620" alt="设置 · 访问 IP 与黑名单">
|
|
46
|
+
</p>
|
|
47
|
+
|
|
48
|
+
<p align="center">
|
|
49
|
+
<img src="assets/blocked-403.jpg" width="320" alt="被拉黑设备看到的 403 页面">
|
|
50
|
+
</p>
|
|
51
|
+
|
|
42
52
|
没开图形界面时,终端里也能开关(`yes` 才真的开)——见下面 [怎么用](#怎么用)。
|
|
43
53
|
|
|
44
54
|
---
|
|
45
55
|
|
|
46
|
-
##
|
|
56
|
+
## 四个能力
|
|
47
57
|
|
|
48
58
|
> 0.3.5 起:设置页开关换成紫色,**监听端口**也能自己填了(默认 8080,保存即生效,重启保留)。
|
|
49
59
|
|
|
@@ -139,7 +149,34 @@ reproxy-lan --help
|
|
|
139
149
|
想彻底关闭又不点开关?`reproxy-lan disable` 或 `/lan no` 都行;想手动改文件也可以,
|
|
140
150
|
插件用 `fs.watch` 盯着它,改完一两秒内自动生效(`updatedBy` 可以写 `manual`)。
|
|
141
151
|
|
|
142
|
-
### 3.
|
|
152
|
+
### 3. 访问 IP 与黑名单(默认关闭)
|
|
153
|
+
|
|
154
|
+
设置页还有一张「访问 IP」卡片:
|
|
155
|
+
|
|
156
|
+
- 上半截是**最近连过这个端口的设备**:IP、最近访问时间、请求次数,每行右边一个「添加」按钮;
|
|
157
|
+
- 点「添加」就把这个 IP 丢进黑名单,**该设备再打开后台只会看到 403 页面**,直到你在这里点「移除」;
|
|
158
|
+
- 下半截是**黑名单开关**,旁边标着「无需求请勿开启」——关掉只是不拦人,名单会保留,下次打开继续生效;
|
|
159
|
+
- 同一个 IP 重复添加不会写两遍(接口返回 `added:false`),列表里会显示「已在黑名单」;
|
|
160
|
+
- 名单和开关都存在同一个状态文件里(`blacklistEnabled` + `blockedIps` 两个字段),**重启 dsh 不丢**。
|
|
161
|
+
|
|
162
|
+
#### 重置配置(防误操作的安全阀)
|
|
163
|
+
|
|
164
|
+
万一配置被改坏了——整段网段进了黑名单、端口填成了别人占着的、局域网开着但谁都连不上——
|
|
165
|
+
用重置命令一键回到默认值(局域网访问关、端口 8080、黑名单关并清空):
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
reproxy-lan reset # 先给你看会重置什么,输入 yes 才执行
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
对话里也可以:`/lan reset` 会先列清楚要恢复什么,确认请输入 `/lan reset yes`。
|
|
172
|
+
|
|
173
|
+
> 重置会把局域网访问**关掉**,所以正从 `8080` 访问的设备会立刻断开——这本来就是它的用途:
|
|
174
|
+
> 把自己从「进不去」里救出来,再从本机重新配置。
|
|
175
|
+
|
|
176
|
+
> ⚠️ 不要拉黑「当前正在使用的这台设备」——后端会直接拒绝(400),回环地址 `127.0.0.1` 也不允许拉黑,
|
|
177
|
+
> 免得管理员把自己锁在外面。真要封自己,先在另一台设备上操作。
|
|
178
|
+
|
|
179
|
+
### 4. 免 Token 登录(版本门控,命中才启用)
|
|
143
180
|
|
|
144
181
|
替换官方 `@deepseek-ai/dsh-client-connection` 提供方,跳过 BrowserAuth:
|
|
145
182
|
浏览器直接打开 Web 地址就能用。
|
|
Binary file
|
|
Binary file
|
package/index.js
CHANGED
|
@@ -21,16 +21,22 @@ import net from 'node:net'
|
|
|
21
21
|
import zlib from 'node:zlib'
|
|
22
22
|
import { apply as applyTrustedConnection } from './vendor/skip/host.js'
|
|
23
23
|
import {
|
|
24
|
+
blockIp,
|
|
24
25
|
DEFAULT_PORT,
|
|
25
26
|
describeLanState,
|
|
26
27
|
effectiveListenHost,
|
|
27
28
|
effectivePort,
|
|
28
29
|
getLanState,
|
|
29
30
|
LAN_WARNING_TEXT,
|
|
31
|
+
isBlocked,
|
|
32
|
+
normalizeIp,
|
|
33
|
+
resetConfig,
|
|
30
34
|
onLanChange,
|
|
35
|
+
setBlacklistEnabled,
|
|
31
36
|
setLanAccess,
|
|
32
37
|
setPort,
|
|
33
38
|
STATE_FILE,
|
|
39
|
+
unblockIp,
|
|
34
40
|
validatePort,
|
|
35
41
|
} from './lan-access.js'
|
|
36
42
|
|
|
@@ -119,7 +125,7 @@ function isImmutableAsset(url) {
|
|
|
119
125
|
* 局域网开关的两个入口(设置页走 HTTP,命令行走 /lan 斜杠命令)。
|
|
120
126
|
* 两边都改同一份 lan-access 状态文件,所以永远同步;改完立刻重建监听。
|
|
121
127
|
*/
|
|
122
|
-
function registerLanControl(ctx, cfg, log) {
|
|
128
|
+
function registerLanControl(ctx, cfg, log, extra = {}) {
|
|
123
129
|
const sendJson = (res, status, body) => {
|
|
124
130
|
const raw = JSON.stringify(body)
|
|
125
131
|
try {
|
|
@@ -252,11 +258,118 @@ function registerLanControl(ctx, cfg, log) {
|
|
|
252
258
|
},
|
|
253
259
|
}
|
|
254
260
|
|
|
261
|
+
const listClients = typeof extra.listClients === 'function' ? extra.listClients : () => []
|
|
262
|
+
const withClients = (body) => ({ ...body, clients: listClients() })
|
|
263
|
+
|
|
264
|
+
const routeClients = {
|
|
265
|
+
kind: 'exact',
|
|
266
|
+
path: '/reproxy-lan/clients',
|
|
267
|
+
handler: (req, res) => {
|
|
268
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
269
|
+
sendJson(res, 405, { ok: false, error: '只支持 GET' })
|
|
270
|
+
return
|
|
271
|
+
}
|
|
272
|
+
sendJson(res, 200, withClients(describeLanState()))
|
|
273
|
+
},
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** 黑名单总开关:关掉只是不拦,名单留着。 */
|
|
277
|
+
const routeBlacklist = {
|
|
278
|
+
kind: 'exact',
|
|
279
|
+
path: '/reproxy-lan/blacklist',
|
|
280
|
+
handler: async (req, res) => {
|
|
281
|
+
if (req.method !== 'POST') {
|
|
282
|
+
sendJson(res, 405, { ok: false, error: '只支持 POST' })
|
|
283
|
+
return
|
|
284
|
+
}
|
|
285
|
+
let body
|
|
286
|
+
try {
|
|
287
|
+
body = await readBody(req)
|
|
288
|
+
} catch (e) {
|
|
289
|
+
sendJson(res, 400, { ok: false, error: e.message })
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
if (typeof body.enabled !== 'boolean') {
|
|
293
|
+
sendJson(res, 400, { ok: false, error: 'enabled 必须是布尔值' })
|
|
294
|
+
return
|
|
295
|
+
}
|
|
296
|
+
try {
|
|
297
|
+
setBlacklistEnabled(body.enabled, 'settings')
|
|
298
|
+
log(body.enabled ? 'warn' : 'info', '设置页把黑名单' + (body.enabled ? '打开' : '关掉'))
|
|
299
|
+
sendJson(res, 200, withClients(describeLanState()))
|
|
300
|
+
} catch (e) {
|
|
301
|
+
sendJson(res, 500, { ok: false, error: e.message })
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** 拉黑:同一个 IP 重复点不会重复写(added:false)。 */
|
|
307
|
+
const routeBlock = {
|
|
308
|
+
kind: 'exact',
|
|
309
|
+
path: '/reproxy-lan/block',
|
|
310
|
+
handler: async (req, res) => {
|
|
311
|
+
if (req.method !== 'POST') {
|
|
312
|
+
sendJson(res, 405, { ok: false, error: '只支持 POST' })
|
|
313
|
+
return
|
|
314
|
+
}
|
|
315
|
+
let body
|
|
316
|
+
try {
|
|
317
|
+
body = await readBody(req)
|
|
318
|
+
} catch (e) {
|
|
319
|
+
sendJson(res, 400, { ok: false, error: e.message })
|
|
320
|
+
return
|
|
321
|
+
}
|
|
322
|
+
// 别把自己锁在门外:当前请求来自哪个 IP,就不许封哪个
|
|
323
|
+
const selfIp = normalizeIp(req.socket ? req.socket.remoteAddress : '')
|
|
324
|
+
const targetIp = normalizeIp(body.ip)
|
|
325
|
+
if (selfIp !== '' && targetIp === selfIp) {
|
|
326
|
+
sendJson(res, 400, { ok: false, error: '不能封禁当前正在使用的这台设备,换一台设备来操作' })
|
|
327
|
+
return
|
|
328
|
+
}
|
|
329
|
+
try {
|
|
330
|
+
const result = blockIp(body.ip, 'settings')
|
|
331
|
+
if (result.added) log('warn', '设置页拉黑 IP:' + result.ip)
|
|
332
|
+
sendJson(res, 200, withClients({ ...describeLanState(), added: result.added, ip: result.ip }))
|
|
333
|
+
} catch (e) {
|
|
334
|
+
sendJson(res, 400, { ok: false, error: e.message })
|
|
335
|
+
}
|
|
336
|
+
},
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const routeUnblock = {
|
|
340
|
+
kind: 'exact',
|
|
341
|
+
path: '/reproxy-lan/unblock',
|
|
342
|
+
handler: async (req, res) => {
|
|
343
|
+
if (req.method !== 'POST') {
|
|
344
|
+
sendJson(res, 405, { ok: false, error: '只支持 POST' })
|
|
345
|
+
return
|
|
346
|
+
}
|
|
347
|
+
let body
|
|
348
|
+
try {
|
|
349
|
+
body = await readBody(req)
|
|
350
|
+
} catch (e) {
|
|
351
|
+
sendJson(res, 400, { ok: false, error: e.message })
|
|
352
|
+
return
|
|
353
|
+
}
|
|
354
|
+
try {
|
|
355
|
+
const result = unblockIp(body.ip, 'settings')
|
|
356
|
+
if (result.removed) log('info', '设置页解除拉黑:' + result.ip)
|
|
357
|
+
sendJson(res, 200, withClients({ ...describeLanState(), removed: result.removed, ip: result.ip }))
|
|
358
|
+
} catch (e) {
|
|
359
|
+
sendJson(res, 400, { ok: false, error: e.message })
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
}
|
|
363
|
+
|
|
255
364
|
const webServer = ctx && typeof ctx.get === 'function' ? ctx.get('webServer') : null
|
|
256
365
|
if (webServer && typeof webServer.register === 'function' && typeof ctx.effect === 'function') {
|
|
257
366
|
ctx.effect(() => webServer.register(routeStatus))
|
|
258
367
|
ctx.effect(() => webServer.register(routeSet))
|
|
259
368
|
ctx.effect(() => webServer.register(routePort))
|
|
369
|
+
ctx.effect(() => webServer.register(routeClients))
|
|
370
|
+
ctx.effect(() => webServer.register(routeBlacklist))
|
|
371
|
+
ctx.effect(() => webServer.register(routeBlock))
|
|
372
|
+
ctx.effect(() => webServer.register(routeUnblock))
|
|
260
373
|
} else {
|
|
261
374
|
log('warn', 'webServer 不可用,设置页开关接口未注册')
|
|
262
375
|
}
|
|
@@ -312,6 +425,29 @@ function registerLanControl(ctx, cfg, log) {
|
|
|
312
425
|
log('info', '命令行切换局域网访问 → 关闭')
|
|
313
426
|
return { kind: 'success', text: '已关闭,其他设备会立刻断开。\n' + statusText() }
|
|
314
427
|
}
|
|
428
|
+
if (arg === 'reset') {
|
|
429
|
+
return {
|
|
430
|
+
kind: 'success',
|
|
431
|
+
text: [
|
|
432
|
+
'重置会把配置恢复到默认值:局域网访问关闭、端口 8080、黑名单关闭并清空名单。',
|
|
433
|
+
'注意:关掉局域网访问后,正从 8080 访问的设备会立刻断开,只有本机能打开后台。',
|
|
434
|
+
'确认要重置,请输入:/lan reset yes',
|
|
435
|
+
].join('\n'),
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
if (arg === 'reset yes' || arg === 'reset y') {
|
|
439
|
+
const before = statusText()
|
|
440
|
+
try {
|
|
441
|
+
resetConfig('command')
|
|
442
|
+
} catch (e) {
|
|
443
|
+
return { kind: 'error', text: '重置失败:' + e.message }
|
|
444
|
+
}
|
|
445
|
+
log('warn', '命令行重置配置 → 默认值')
|
|
446
|
+
return {
|
|
447
|
+
kind: 'success',
|
|
448
|
+
text: '已重置为默认配置。\n重置前:\n' + before + '\n重置后:\n' + statusText(),
|
|
449
|
+
}
|
|
450
|
+
}
|
|
315
451
|
if (arg.indexOf('port') === 0) {
|
|
316
452
|
const rest = arg.slice(4).trim()
|
|
317
453
|
if (rest === '') {
|
|
@@ -351,6 +487,62 @@ function applyProxy(ctx, config) {
|
|
|
351
487
|
let retryTimer = null
|
|
352
488
|
let disposed = false
|
|
353
489
|
|
|
490
|
+
/**
|
|
491
|
+
* 访问过本反代的设备(只留内存,重启清空):ip → { ip, firstSeen, lastSeen, hits }。
|
|
492
|
+
* 设置页把这份列表显示出来,点「添加」就把那条 IP 丢进黑名单。
|
|
493
|
+
*/
|
|
494
|
+
const seenClients = new Map()
|
|
495
|
+
const MAX_SEEN = 50
|
|
496
|
+
|
|
497
|
+
const noteClient = (req) => {
|
|
498
|
+
const ip = normalizeIp(req && req.socket ? req.socket.remoteAddress : '')
|
|
499
|
+
if (ip === '') return ''
|
|
500
|
+
const now = Date.now()
|
|
501
|
+
const prev = seenClients.get(ip)
|
|
502
|
+
if (prev) {
|
|
503
|
+
prev.lastSeen = now
|
|
504
|
+
prev.hits += 1
|
|
505
|
+
} else {
|
|
506
|
+
seenClients.set(ip, { ip, firstSeen: now, lastSeen: now, hits: 1 })
|
|
507
|
+
if (seenClients.size > MAX_SEEN) {
|
|
508
|
+
let oldestKey = ''
|
|
509
|
+
let oldest = Infinity
|
|
510
|
+
for (const [key, value] of seenClients) {
|
|
511
|
+
if (value.lastSeen < oldest) { oldest = value.lastSeen; oldestKey = key }
|
|
512
|
+
}
|
|
513
|
+
if (oldestKey !== '') seenClients.delete(oldestKey)
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return ip
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/** 给设置页看的快照(带 blocked 标记,前端据此决定按钮是「添加」还是「已在黑名单」)。 */
|
|
520
|
+
const listClients = () => [...seenClients.values()]
|
|
521
|
+
.sort((a, b) => b.lastSeen - a.lastSeen)
|
|
522
|
+
.map((entry) => ({ ...entry, blocked: isBlocked(entry.ip) }))
|
|
523
|
+
|
|
524
|
+
const BLOCKED_HTML = '<!doctype html><html lang="zh-CN"><meta charset="utf-8">'
|
|
525
|
+
+ '<meta name="viewport" content="width=device-width,initial-scale=1">'
|
|
526
|
+
+ '<title>403 · 已被拉黑</title>'
|
|
527
|
+
+ '<body style="margin:0;font:15px/1.8 -apple-system,system-ui,sans-serif;background:#12141a;color:#e6e8ee;padding:56px 24px">'
|
|
528
|
+
+ '<h2 style="margin:0 0 12px;font-size:20px">403 · 这台设备已被拉黑</h2>'
|
|
529
|
+
+ '<p style="margin:0;color:#9aa0ad;max-width:560px">'
|
|
530
|
+
+ '管理员在 DSH 设置里把当前 IP 加进了黑名单,所以这台设备无法访问后台。'
|
|
531
|
+
+ '如果认为这是误会,请联系管理员在「设置 → 局域网访问 → 访问 IP」里移除。'
|
|
532
|
+
+ '</p></body></html>'
|
|
533
|
+
|
|
534
|
+
const sendBlocked = (res) => {
|
|
535
|
+
try {
|
|
536
|
+
const body = Buffer.from(BLOCKED_HTML, 'utf8')
|
|
537
|
+
res.writeHead(403, {
|
|
538
|
+
'content-type': 'text/html; charset=utf-8',
|
|
539
|
+
'content-length': body.length,
|
|
540
|
+
'cache-control': 'no-store',
|
|
541
|
+
})
|
|
542
|
+
res.end(body)
|
|
543
|
+
} catch { /* 连接已断 */ }
|
|
544
|
+
}
|
|
545
|
+
|
|
354
546
|
/** 关掉现有监听(换地址前必须先让出端口)。 */
|
|
355
547
|
const stopListening = () => {
|
|
356
548
|
clearTimeout(retryTimer)
|
|
@@ -376,6 +568,11 @@ function applyProxy(ctx, config) {
|
|
|
376
568
|
}
|
|
377
569
|
|
|
378
570
|
const onRequest = (req, res) => {
|
|
571
|
+
const clientIp = noteClient(req)
|
|
572
|
+
if (clientIp !== '' && isBlocked(clientIp)) {
|
|
573
|
+
sendBlocked(res)
|
|
574
|
+
return
|
|
575
|
+
}
|
|
379
576
|
let received = 0
|
|
380
577
|
let aborted = false
|
|
381
578
|
const upstream = http.request({
|
|
@@ -436,6 +633,11 @@ function applyProxy(ctx, config) {
|
|
|
436
633
|
}
|
|
437
634
|
|
|
438
635
|
const onUpgrade = (req, clientSocket, head) => {
|
|
636
|
+
const upgradeIp = noteClient(req)
|
|
637
|
+
if (upgradeIp !== '' && isBlocked(upgradeIp)) {
|
|
638
|
+
try { clientSocket.end('HTTP/1.1 403 Forbidden\r\ncontent-length: 0\r\nconnection: close\r\n\r\n') } catch { /* 已关 */ }
|
|
639
|
+
return
|
|
640
|
+
}
|
|
439
641
|
const upstream = net.connect(cfg.upstreamPort, cfg.upstreamHost, () => {
|
|
440
642
|
const headers = upstreamHeaders(req, cfg, {
|
|
441
643
|
connection: 'Upgrade',
|
|
@@ -524,7 +726,7 @@ function applyProxy(ctx, config) {
|
|
|
524
726
|
} catch { /* 订阅失败不连坐 */ }
|
|
525
727
|
|
|
526
728
|
try {
|
|
527
|
-
registerLanControl(ctx, cfg, log)
|
|
729
|
+
registerLanControl(ctx, cfg, log, { listClients })
|
|
528
730
|
} catch (e) {
|
|
529
731
|
log('error', '局域网开关接口注册失败: ' + (e && e.message))
|
|
530
732
|
}
|
package/lan-access.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 局域网访问开关(0.3.2)。
|
|
2
|
+
* 局域网访问开关(0.3.2)+ 自定义端口(0.3.5)+ 访问 IP / 黑名单(0.5.0)。
|
|
3
3
|
*
|
|
4
4
|
* 默认关闭:反代只监听 127.0.0.1,同一台机器上的浏览器才能打开。
|
|
5
5
|
* 用户必须在设置页点开关(弹安全提示、点「确定」)或者在命令行里确认,
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
* 开关切换后马上写盘,重启 dsh 不需要重新打开。
|
|
10
10
|
*/
|
|
11
11
|
import fs from 'node:fs'
|
|
12
|
+
import net from 'node:net'
|
|
12
13
|
import os from 'node:os'
|
|
13
14
|
import path from 'node:path'
|
|
14
15
|
|
|
@@ -23,6 +24,9 @@ export const STATE_FILE = path.join(DSH_HOME, 'plugin-reverse-proxy', 'lan-acces
|
|
|
23
24
|
/** 默认监听端口:0.3.5 起端口也进状态文件,可在设置页改,重启保留。 */
|
|
24
25
|
export const DEFAULT_PORT = 8080
|
|
25
26
|
|
|
27
|
+
/** 黑名单最多记这么多条,防止状态文件无限长。 */
|
|
28
|
+
export const MAX_BLOCKED = 200
|
|
29
|
+
|
|
26
30
|
/** 安全提示文案:设置页弹窗与命令行共用同一份,避免两处说法不一致。 */
|
|
27
31
|
export const LAN_WARNING_TITLE = '对局域网开放访问?'
|
|
28
32
|
export const LAN_WARNING_LINES = [
|
|
@@ -49,6 +53,47 @@ export const validatePort = (value) => {
|
|
|
49
53
|
return { ok: true, port: n }
|
|
50
54
|
}
|
|
51
55
|
|
|
56
|
+
/**
|
|
57
|
+
* 归一化 IP:双栈 socket 会给 `::ffff:192.168.1.7`,去掉前缀才好比较、好显示。
|
|
58
|
+
* 非法字符串返回空串(调用方据此跳过)。
|
|
59
|
+
*/
|
|
60
|
+
export const normalizeIp = (value) => {
|
|
61
|
+
if (typeof value !== 'string') return ''
|
|
62
|
+
let ip = value.trim()
|
|
63
|
+
if (ip === '') return ''
|
|
64
|
+
const mapped = ip.match(/^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/i)
|
|
65
|
+
if (mapped) ip = mapped[1]
|
|
66
|
+
const zone = ip.indexOf('%')
|
|
67
|
+
if (zone !== -1) ip = ip.slice(0, zone)
|
|
68
|
+
if (net.isIP(ip) === 0) return ''
|
|
69
|
+
return ip
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 回环地址不允许进黑名单:封了它等于把自己锁在门外。 */
|
|
73
|
+
const isLoopback = (ip) => ip === '::1' || /^127\./.test(ip)
|
|
74
|
+
|
|
75
|
+
const DEFAULT_STATE = {
|
|
76
|
+
lanAccess: false,
|
|
77
|
+
port: DEFAULT_PORT,
|
|
78
|
+
blacklistEnabled: false,
|
|
79
|
+
blockedIps: [],
|
|
80
|
+
updatedAt: null,
|
|
81
|
+
updatedBy: null,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const sanitizeBlocked = (value) => {
|
|
85
|
+
if (!Array.isArray(value)) return []
|
|
86
|
+
const out = []
|
|
87
|
+
for (const item of value) {
|
|
88
|
+
const ip = normalizeIp(item)
|
|
89
|
+
if (ip === '' || isLoopback(ip)) continue
|
|
90
|
+
if (out.includes(ip)) continue
|
|
91
|
+
out.push(ip)
|
|
92
|
+
if (out.length >= MAX_BLOCKED) break
|
|
93
|
+
}
|
|
94
|
+
return out
|
|
95
|
+
}
|
|
96
|
+
|
|
52
97
|
const readState = () => {
|
|
53
98
|
try {
|
|
54
99
|
const raw = JSON.parse(fs.readFileSync(STATE_FILE, 'utf8'))
|
|
@@ -56,12 +101,14 @@ const readState = () => {
|
|
|
56
101
|
return {
|
|
57
102
|
lanAccess: raw?.lanAccess === true,
|
|
58
103
|
port: parsed.ok ? parsed.port : DEFAULT_PORT,
|
|
104
|
+
blacklistEnabled: raw?.blacklistEnabled === true,
|
|
105
|
+
blockedIps: sanitizeBlocked(raw?.blockedIps),
|
|
59
106
|
updatedAt: typeof raw?.updatedAt === 'string' ? raw.updatedAt : null,
|
|
60
107
|
updatedBy: typeof raw?.updatedBy === 'string' ? raw.updatedBy : null,
|
|
61
108
|
}
|
|
62
109
|
} catch {
|
|
63
110
|
// 文件不存在 / 读坏了都当「关闭」——安全默认值
|
|
64
|
-
return {
|
|
111
|
+
return { ...DEFAULT_STATE, blockedIps: [] }
|
|
65
112
|
}
|
|
66
113
|
}
|
|
67
114
|
|
|
@@ -80,7 +127,28 @@ const writeState = (next) => {
|
|
|
80
127
|
}
|
|
81
128
|
}
|
|
82
129
|
|
|
83
|
-
|
|
130
|
+
const notify = () => {
|
|
131
|
+
for (const listener of listeners) {
|
|
132
|
+
try { listener(getLanState()) } catch { /* 单个监听者失败不连坐 */ }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 所有写操作走这里:保留未提到的字段、补时间戳、落盘、广播。 */
|
|
137
|
+
const commit = (patch, by) => {
|
|
138
|
+
const next = {
|
|
139
|
+
...state,
|
|
140
|
+
...patch,
|
|
141
|
+
updatedAt: new Date().toISOString(),
|
|
142
|
+
updatedBy: by,
|
|
143
|
+
}
|
|
144
|
+
next.blockedIps = sanitizeBlocked(next.blockedIps)
|
|
145
|
+
writeState(next)
|
|
146
|
+
state = next
|
|
147
|
+
notify()
|
|
148
|
+
return getLanState()
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const getLanState = () => ({ ...state, blockedIps: [...state.blockedIps] })
|
|
84
152
|
|
|
85
153
|
/** 默认监听地址:没开就是本机回环。 */
|
|
86
154
|
export const effectiveListenHost = () => (state.lanAccess ? '0.0.0.0' : '127.0.0.1')
|
|
@@ -94,61 +162,83 @@ export const onLanChange = (listener) => {
|
|
|
94
162
|
return () => { listeners.delete(listener) }
|
|
95
163
|
}
|
|
96
164
|
|
|
97
|
-
/** 改开关:写盘 → 通知监听者。`by` 只用于记录是谁动的(settings / command)。 */
|
|
98
|
-
export const setLanAccess = (enabled, by = 'unknown') => {
|
|
99
|
-
const next = {
|
|
100
|
-
lanAccess: enabled === true,
|
|
101
|
-
port: effectivePort(),
|
|
102
|
-
updatedAt: new Date().toISOString(),
|
|
103
|
-
updatedBy: by,
|
|
104
|
-
}
|
|
105
|
-
writeState(next)
|
|
106
|
-
state = next
|
|
107
|
-
for (const listener of listeners) {
|
|
108
|
-
try { listener(getLanState()) } catch { /* 单个监听者失败不连坐 */ }
|
|
109
|
-
}
|
|
110
|
-
return getLanState()
|
|
111
|
-
}
|
|
165
|
+
/** 改开关:写盘 → 通知监听者。`by` 只用于记录是谁动的(settings / command / cli)。 */
|
|
166
|
+
export const setLanAccess = (enabled, by = 'unknown') => commit({ lanAccess: enabled === true }, by)
|
|
112
167
|
|
|
113
168
|
/** 改端口:校验 → 写盘 → 通知监听者(调用方负责确认端口没被占用)。 */
|
|
114
169
|
export const setPort = (value, by = 'unknown') => {
|
|
115
170
|
const parsed = validatePort(value)
|
|
116
171
|
if (!parsed.ok) throw new Error(parsed.error)
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
172
|
+
return commit({ port: parsed.port }, by)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** 黑名单总开关。关掉时不拦任何人,但名单保留,下次打开继续生效。 */
|
|
176
|
+
export const setBlacklistEnabled = (enabled, by = 'unknown') => commit({ blacklistEnabled: enabled === true }, by)
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 拉黑一个 IP。
|
|
180
|
+
* 返回 `{ ok, added, state }`:`added:false` 表示本来就在名单里(同一个 IP 不需要重复添加)。
|
|
181
|
+
*/
|
|
182
|
+
export const blockIp = (value, by = 'unknown') => {
|
|
183
|
+
const ip = normalizeIp(value)
|
|
184
|
+
if (ip === '') throw new Error('IP 地址不合法:' + String(value))
|
|
185
|
+
if (isLoopback(ip)) throw new Error('不能封禁本机回环地址(会把设置页自己也锁在外面)')
|
|
186
|
+
if (state.blockedIps.includes(ip)) return { ok: true, added: false, ip, state: getLanState() }
|
|
187
|
+
const next = commit({ blockedIps: [...state.blockedIps, ip] }, by)
|
|
188
|
+
return { ok: true, added: true, ip, state: next }
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** 解除拉黑。返回 `{ ok, removed, ip, state }`。 */
|
|
192
|
+
export const unblockIp = (value, by = 'unknown') => {
|
|
193
|
+
const ip = normalizeIp(value)
|
|
194
|
+
if (ip === '') throw new Error('IP 地址不合法:' + String(value))
|
|
195
|
+
const index = state.blockedIps.indexOf(ip)
|
|
196
|
+
if (index === -1) return { ok: true, removed: false, ip, state: getLanState() }
|
|
197
|
+
const rest = state.blockedIps.filter((item) => item !== ip)
|
|
198
|
+
const next = commit({ blockedIps: rest }, by)
|
|
199
|
+
return { ok: true, removed: true, ip, state: next }
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** 当前是否拦这个 IP:黑名单没开就一律放行。 */
|
|
203
|
+
export const isBlocked = (value) => {
|
|
204
|
+
if (state.blacklistEnabled !== true) return false
|
|
205
|
+
const ip = normalizeIp(value)
|
|
206
|
+
if (ip === '') return false
|
|
207
|
+
return state.blockedIps.includes(ip)
|
|
129
208
|
}
|
|
130
209
|
|
|
131
|
-
/** 给 UI / 命令用的统一状态描述。 */
|
|
132
210
|
/**
|
|
133
|
-
*
|
|
134
|
-
*
|
|
211
|
+
* 一键恢复默认:局域网访问关闭、端口 8080、黑名单关闭且清空。
|
|
212
|
+
* 存在的意义是「误操作后的安全阀」——比如把自己或整个网段搞进黑名单、端口填成了别人占着的。
|
|
213
|
+
* 注意:关掉局域网访问后,正从 8080 访问的设备会立刻断开,所以命令入口都要求二次确认。
|
|
135
214
|
*/
|
|
215
|
+
export const resetConfig = (by = 'unknown') => commit({
|
|
216
|
+
lanAccess: false,
|
|
217
|
+
port: DEFAULT_PORT,
|
|
218
|
+
blacklistEnabled: false,
|
|
219
|
+
blockedIps: [],
|
|
220
|
+
}, by)
|
|
221
|
+
|
|
222
|
+
/** 盯住状态文件:命令行脚本 / 手动改 json 也能触发重建监听。 */
|
|
136
223
|
const startWatch = () => {
|
|
137
224
|
try {
|
|
138
225
|
fs.mkdirSync(path.dirname(STATE_FILE), { recursive: true })
|
|
139
226
|
fs.watch(path.dirname(STATE_FILE), { persistent: false }, (_event, filename) => {
|
|
140
227
|
if (filename && filename !== path.basename(STATE_FILE)) return
|
|
141
228
|
const next = readState()
|
|
142
|
-
|
|
229
|
+
const same = next.lanAccess === state.lanAccess
|
|
230
|
+
&& next.port === state.port
|
|
231
|
+
&& next.blacklistEnabled === state.blacklistEnabled
|
|
232
|
+
&& next.blockedIps.join(',') === state.blockedIps.join(',')
|
|
233
|
+
if (same) return
|
|
143
234
|
state = next
|
|
144
|
-
|
|
145
|
-
try { listener(getLanState()) } catch { /* 单个监听者失败不连坐 */ }
|
|
146
|
-
}
|
|
235
|
+
notify()
|
|
147
236
|
})
|
|
148
237
|
} catch { /* 盯不住就算了,开关本身还能用 */ }
|
|
149
238
|
}
|
|
150
239
|
startWatch()
|
|
151
240
|
|
|
241
|
+
/** 给 UI / 命令用的统一状态描述。 */
|
|
152
242
|
export const describeLanState = (port) => {
|
|
153
243
|
const current = getLanState()
|
|
154
244
|
return {
|
|
@@ -159,6 +249,9 @@ export const describeLanState = (port) => {
|
|
|
159
249
|
listenHost: effectiveListenHost(),
|
|
160
250
|
port: typeof port === 'number' ? port : effectivePort(),
|
|
161
251
|
defaultPort: DEFAULT_PORT,
|
|
252
|
+
blacklistEnabled: current.blacklistEnabled,
|
|
253
|
+
blockedIps: current.blockedIps,
|
|
254
|
+
blockedCount: current.blockedIps.length,
|
|
162
255
|
stateFile: STATE_FILE,
|
|
163
256
|
}
|
|
164
257
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-reverse-proxy",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "DSH
|
|
3
|
+
"version": "0.5.1",
|
|
4
|
+
"description": "DSH Web 局域网访问插件:内置反向代理 + 免 Token 登录,手机/平板同 Wi-Fi 直接打开后台;端口可自定义,支持访问 IP 黑名单与一键重置。默认只监听本机,需显式开启。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"exports": {
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"assets",
|
|
20
20
|
"vendor",
|
|
21
21
|
"LICENSE",
|
|
22
|
-
"CHANGELOG.md"
|
|
22
|
+
"CHANGELOG.md",
|
|
23
|
+
"screenshots.json"
|
|
23
24
|
],
|
|
24
25
|
"engines": {
|
|
25
26
|
"node": ">=22"
|
package/screenshots.json
ADDED
package/vendor/skip/client.js
CHANGED
|
@@ -119,6 +119,15 @@ window.__ModuleLoader__.load({
|
|
|
119
119
|
'.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
120
|
'.reproxy-lan-note b{color:var(--dsw-alias-label-primary,#e8ebf2);font-weight:600}',
|
|
121
121
|
'.reproxy-lan-err{font-size:12px;color:#ff6b6b}',
|
|
122
|
+
'.reproxy-lan-list{margin-top:10px;display:flex;flex-direction:column;gap:6px}',
|
|
123
|
+
'.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
|
+
'.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}',
|
|
125
|
+
'.reproxy-lan-add:hover{background:rgba(139,92,246,.16)}',
|
|
126
|
+
'.reproxy-lan-add[disabled]{opacity:.5;cursor:default}',
|
|
127
|
+
'.reproxy-lan-chips{display:flex;flex-wrap:wrap;gap:8px;margin-top:8px}',
|
|
128
|
+
'.reproxy-lan-chip{display:inline-flex;align-items:center;gap:8px;font-size:12px;font-variant-numeric:tabular-nums;padding:3px 10px;border-radius:999px;background:rgba(255,107,107,.12);border:1px solid rgba(255,107,107,.35);color:#ffb3b3}',
|
|
129
|
+
'.reproxy-lan-chip button{border:none;background:transparent;color:#ff8f8f;cursor:pointer;font-size:12px;padding:0}',
|
|
130
|
+
'.reproxy-lan-chip button[disabled]{opacity:.5;cursor:default}',
|
|
122
131
|
'.reproxy-lan-mask{position:fixed;inset:0;background:rgba(0,0,0,.55);display:flex;align-items:center;justify-content:center;z-index:9999}',
|
|
123
132
|
'.reproxy-lan-card{width:min(460px,92vw);background:var(--dsw-alias-bg-layer-1,#1c1f26);border:1px solid rgba(255,255,255,.12);border-radius:12px;padding:20px;box-shadow:0 18px 48px rgba(0,0,0,.45)}',
|
|
124
133
|
'.reproxy-lan-card h3{margin:0 0 10px;font-size:15px;color:var(--dsw-alias-label-primary,#e6e8ee)}',
|
|
@@ -143,6 +152,17 @@ window.__ModuleLoader__.load({
|
|
|
143
152
|
headers: { 'content-type': 'application/json' },
|
|
144
153
|
body: JSON.stringify({ port })
|
|
145
154
|
}).then((r) => r.json());
|
|
155
|
+
const readClients = () => fetch('/reproxy-lan/clients', { headers: { accept: 'application/json' }, cache: 'no-store' }).then((r) => r.json());
|
|
156
|
+
const postIp = (path, ip) => fetch(path, {
|
|
157
|
+
method: 'POST',
|
|
158
|
+
headers: { 'content-type': 'application/json' },
|
|
159
|
+
body: JSON.stringify({ ip })
|
|
160
|
+
}).then((r) => r.json());
|
|
161
|
+
const writeBlacklist = (enabled) => fetch('/reproxy-lan/blacklist', {
|
|
162
|
+
method: 'POST',
|
|
163
|
+
headers: { 'content-type': 'application/json' },
|
|
164
|
+
body: JSON.stringify({ enabled })
|
|
165
|
+
}).then((r) => r.json());
|
|
146
166
|
function Panel() {
|
|
147
167
|
const [state, setState] = React.useState(null);
|
|
148
168
|
const [asking, setAsking] = React.useState(false);
|
|
@@ -152,6 +172,11 @@ window.__ModuleLoader__.load({
|
|
|
152
172
|
const [portBusy, setPortBusy] = React.useState(false);
|
|
153
173
|
const [portMsg, setPortMsg] = React.useState('');
|
|
154
174
|
const [portBad, setPortBad] = React.useState(false);
|
|
175
|
+
const [clients, setClients] = React.useState([]);
|
|
176
|
+
const [blacklistOn, setBlacklistOn] = React.useState(false);
|
|
177
|
+
const [ipBusy, setIpBusy] = React.useState('');
|
|
178
|
+
const [ipMsg, setIpMsg] = React.useState('');
|
|
179
|
+
const [ipBad, setIpBad] = React.useState(false);
|
|
155
180
|
React.useEffect(() => {
|
|
156
181
|
let alive = true;
|
|
157
182
|
readState().then((body) => {
|
|
@@ -161,6 +186,18 @@ window.__ModuleLoader__.load({
|
|
|
161
186
|
}).catch((e) => { if (alive) setError(String((e && e.message) || e)); });
|
|
162
187
|
return () => { alive = false; };
|
|
163
188
|
}, []);
|
|
189
|
+
// 访问 IP 列表:进面板先拉一次,之后每 5 秒轻量刷新(只读,不写盘)
|
|
190
|
+
React.useEffect(() => {
|
|
191
|
+
let alive = true;
|
|
192
|
+
const load = () => readClients().then((body) => {
|
|
193
|
+
if (!alive || !body || body.ok !== true) return;
|
|
194
|
+
setClients(Array.isArray(body.clients) ? body.clients : []);
|
|
195
|
+
setBlacklistOn(body.blacklistEnabled === true);
|
|
196
|
+
}).catch(() => { /* 拉不到就保持上一份 */ });
|
|
197
|
+
load();
|
|
198
|
+
const timer = setInterval(load, 5000);
|
|
199
|
+
return () => { alive = false; clearInterval(timer); };
|
|
200
|
+
}, []);
|
|
164
201
|
const apply = (enabled) => {
|
|
165
202
|
setBusy(true);
|
|
166
203
|
setError('');
|
|
@@ -188,8 +225,45 @@ window.__ModuleLoader__.load({
|
|
|
188
225
|
setPortMsg('已保存,正在切到 ' + body.port + ' 端口');
|
|
189
226
|
}).catch((e) => fail(String((e && e.message) || e))).then(() => setPortBusy(false));
|
|
190
227
|
};
|
|
228
|
+
const applyBlock = (ip) => {
|
|
229
|
+
setIpBusy(ip);
|
|
230
|
+
setIpMsg('');
|
|
231
|
+
setIpBad(false);
|
|
232
|
+
postIp('/reproxy-lan/block', ip).then((body) => {
|
|
233
|
+
if (!body || body.ok !== true) throw new Error((body && body.error) || '添加失败');
|
|
234
|
+
setState(body);
|
|
235
|
+
setClients(Array.isArray(body.clients) ? body.clients : []);
|
|
236
|
+
setBlacklistOn(body.blacklistEnabled === true);
|
|
237
|
+
setIpMsg(body.added === false ? (ip + ' 已经在黑名单里,没有重复添加') : (ip + ' 已加入黑名单'));
|
|
238
|
+
}).catch((e) => { setIpBad(true); setIpMsg(String((e && e.message) || e)); }).then(() => setIpBusy(''));
|
|
239
|
+
};
|
|
240
|
+
const removeBlock = (ip) => {
|
|
241
|
+
setIpBusy(ip);
|
|
242
|
+
setIpMsg('');
|
|
243
|
+
setIpBad(false);
|
|
244
|
+
postIp('/reproxy-lan/unblock', ip).then((body) => {
|
|
245
|
+
if (!body || body.ok !== true) throw new Error((body && body.error) || '移除失败');
|
|
246
|
+
setState(body);
|
|
247
|
+
setClients(Array.isArray(body.clients) ? body.clients : []);
|
|
248
|
+
setBlacklistOn(body.blacklistEnabled === true);
|
|
249
|
+
setIpMsg(ip + ' 已移出黑名单');
|
|
250
|
+
}).catch((e) => { setIpBad(true); setIpMsg(String((e && e.message) || e)); }).then(() => setIpBusy(''));
|
|
251
|
+
};
|
|
252
|
+
const toggleBlacklist = () => {
|
|
253
|
+
setIpBusy('switch');
|
|
254
|
+
setIpMsg('');
|
|
255
|
+
setIpBad(false);
|
|
256
|
+
writeBlacklist(!blacklistOn).then((body) => {
|
|
257
|
+
if (!body || body.ok !== true) throw new Error((body && body.error) || '保存失败');
|
|
258
|
+
setState(body);
|
|
259
|
+
setClients(Array.isArray(body.clients) ? body.clients : []);
|
|
260
|
+
setBlacklistOn(body.blacklistEnabled === true);
|
|
261
|
+
setIpMsg(body.blacklistEnabled ? '黑名单已打开' : '黑名单已关闭(名单保留)');
|
|
262
|
+
}).catch((e) => { setIpBad(true); setIpMsg(String((e && e.message) || e)); }).then(() => setIpBusy(''));
|
|
263
|
+
};
|
|
191
264
|
const on = !!(state && state.lanAccess);
|
|
192
265
|
const port = state && state.port ? state.port : 8080;
|
|
266
|
+
const blockedIps = state && Array.isArray(state.blockedIps) ? state.blockedIps : [];
|
|
193
267
|
const status = state === null ? '读取中…' : (on ? '已开启:同一网络里的设备都能直接进入 0.0.0.0:' + port : '已关闭:只有本机能打开 127.0.0.1:' + port);
|
|
194
268
|
return h('div', { className: 'reproxy-lan' },
|
|
195
269
|
h('style', { 'data-plugin': 'dsh-plugin-reverse-proxy-lan' }, STYLE),
|
|
@@ -234,6 +308,44 @@ window.__ModuleLoader__.load({
|
|
|
234
308
|
}, portBusy ? '保存中…' : '保存')
|
|
235
309
|
)
|
|
236
310
|
),
|
|
311
|
+
h('div', { className: 'reproxy-lan-row' },
|
|
312
|
+
h('div', { style: { width: '100%', minWidth: 0 } },
|
|
313
|
+
h('div', { className: 'reproxy-lan-title' }, '访问 IP'),
|
|
314
|
+
h('div', { className: 'reproxy-lan-desc' }, '最近连过这个端口的设备。点「添加」把它的 IP 拉进黑名单:被拉黑的设备再打开后台只会看到 403,直到你在这里移除。'),
|
|
315
|
+
h('div', { className: 'reproxy-lan-list' },
|
|
316
|
+
clients.length === 0
|
|
317
|
+
? h('div', { className: 'reproxy-lan-meta' }, '还没有记录(有人访问后台后才会出现)')
|
|
318
|
+
: clients.map((item) => h('div', { key: item.ip, className: 'reproxy-lan-ip' },
|
|
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
|
+
),
|
|
329
|
+
h('div', { className: 'reproxy-lan-ctl', style: { marginTop: '14px', justifyContent: 'flex-start' } },
|
|
330
|
+
h('span', { className: 'reproxy-lan-state' }, '黑名单开关(无需求请勿开启)'),
|
|
331
|
+
h('button', {
|
|
332
|
+
className: 'reproxy-lan-switch' + (blacklistOn ? ' on' : ''),
|
|
333
|
+
type: 'button',
|
|
334
|
+
'aria-pressed': blacklistOn ? 'true' : 'false',
|
|
335
|
+
disabled: ipBusy === 'switch',
|
|
336
|
+
title: blacklistOn ? '点击关闭黑名单(名单保留)' : '点击打开黑名单',
|
|
337
|
+
onClick: toggleBlacklist
|
|
338
|
+
}, h('i'))
|
|
339
|
+
),
|
|
340
|
+
blockedIps.length === 0 ? null : h('div', { className: 'reproxy-lan-chips' },
|
|
341
|
+
blockedIps.map((ip) => h('span', { key: ip, className: 'reproxy-lan-chip' },
|
|
342
|
+
ip,
|
|
343
|
+
h('button', { type: 'button', disabled: ipBusy === ip, onClick: () => removeBlock(ip) }, ipBusy === ip ? '…' : '移除')
|
|
344
|
+
))
|
|
345
|
+
),
|
|
346
|
+
ipMsg ? h('div', { className: ipBad ? 'reproxy-lan-err' : 'reproxy-lan-ok', style: { marginTop: '8px' } }, ipMsg) : null
|
|
347
|
+
)
|
|
348
|
+
),
|
|
237
349
|
portMsg ? h('div', { className: portBad ? 'reproxy-lan-err' : 'reproxy-lan-ok' }, portMsg) : null,
|
|
238
350
|
h('div', { className: 'reproxy-lan-meta' }, status),
|
|
239
351
|
state && state.updatedAt ? h('div', { className: 'reproxy-lan-meta' }, '上次修改:' + state.updatedAt + '(' + (state.updatedBy || 'unknown') + ')') : null,
|