@andy.ian/dsh-homing 2.3.8 → 2.3.9

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/index.js CHANGED
@@ -1661,6 +1661,8 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
1661
1661
  result = current ? appRegistry.update(id, { enabled: !current.enabled }) : { ok: false, error: '应用不存在' };
1662
1662
  } else if (action === 'delete') {
1663
1663
  result = appRegistry.remove(form.get('id') ?? '');
1664
+ } else if (action === 'lan') {
1665
+ result = appRegistry.setAllowLan(form.get('allowLan') === '1');
1664
1666
  }
1665
1667
  appendAudit(dir, 'app-registry-' + action, { id: String(form.get('id') ?? form.get('originalId') ?? ''), ok: result.ok, error: result.error ?? null });
1666
1668
  // appRegistry 的每条失败路径都带 error 字符串(门禁证明 `?? '保存失败'` 不可达)
@@ -1690,6 +1692,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
1690
1692
  });
1691
1693
  res.end(renderLocalAppsPage({
1692
1694
  apps, health, editing, confirmDelete,
1695
+ allowLan: appRegistry.allowLan(),
1693
1696
  message: url.searchParams.get('msg') ?? '',
1694
1697
  isError: url.searchParams.get('error') === '1',
1695
1698
  }));
@@ -20,8 +20,12 @@ form{margin:0}label{display:block;margin:10px 0 4px;color:#9aa4b2;font-size:12px
20
20
  @media(max-width:640px){.grid{grid-template-columns:1fr}.card{padding:12px}}
21
21
  `
22
22
 
23
- export function renderLocalAppsPage({ apps, health = {}, editing = null, confirmDelete = null, message = '', isError = false }) {
23
+ export function renderLocalAppsPage({ apps, health = {}, editing = null, confirmDelete = null, allowLan = false, message = '', isError = false }) {
24
24
  const formApp = editing ?? { id: '', name: '', target: 'http://127.0.0.1:', enabled: true, trusted: false, allowInsecureTls: false }
25
+ const targetPlaceholder = allowLan ? 'http://192.168.1.26:8899' : 'http://127.0.0.1:8765'
26
+ const targetLabel = allowLan
27
+ ? '目标地址(回环 127.0.0.1 / ::1,或局域网私网 10.x / 172.16–31.x / 192.168.x)'
28
+ : '目标地址(仅允许 127.0.0.1 / ::1;需要局域网服务请先在上方开启)'
25
29
  const rows = apps.length === 0
26
30
  ? '<div class="empty">还没有本地应用。先在下方添加一个本机服务。</div>'
27
31
  : apps.map((app) => {
@@ -41,17 +45,27 @@ export function renderLocalAppsPage({ apps, health = {}, editing = null, confirm
41
45
  ? `<div class="card"><h2>确认删除「${escapeHtml(confirmDelete.name)}」?</h2><p class="sub">只删除网关入口,不会停止或删除电脑上的服务。</p><div class="ops"><form method="post"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="${escapeHtml(confirmDelete.id)}"><button class="danger" type="submit">确认删除</button></form><a class="btn ghost" href="/dsh_homing/apps">取消</a></div></div>`
42
46
  : ''
43
47
 
48
+ const lanCard = `<div class="card"><h2>局域网服务(可选)</h2>
49
+ <p class="note">默认只允许本机回环地址(127.0.0.1 / ::1)。要接入同一局域网内的服务(如 <code>192.168.1.26:8899</code>),先在这里开启。</p>
50
+ <p class="note">${allowLan
51
+ ? '<b style="color:#f0b46b">已开启</b>:允许 10.x / 172.16–31.x / 192.168.x / 169.254.x 等私网地址;公网地址仍会被拒绝。'
52
+ : '当前未开启:目标地址只能是 127.0.0.1 / ::1。'}</p>
53
+ <form method="post"><input type="hidden" name="action" value="lan"><input type="hidden" name="allowLan" value="${allowLan ? '0' : '1'}"><button class="${allowLan ? 'danger' : 'primary'}" type="submit">${allowLan ? '关闭局域网访问' : '允许局域网地址(LAN)'}</button></form>
54
+ <p class="note">安全提示:只允许私网地址,不会变成公网开放代理;局域网服务本身也不要暴露到公网。</p>
55
+ </div>`
56
+
44
57
  return `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>本地应用 · DSH</title><style>${STYLE}${NAV_CSS}</style></head><body><div class="wrap">
45
58
  ${topNav({ current: '/dsh_homing/apps' })}
46
- <h1>本地应用</h1><p class="sub">把本机回环服务接入同一个安全入口。每个应用独立启用,互不影响。</p>
59
+ <h1>本地应用</h1><p class="sub">把本机回环(可选局域网)服务接入同一个安全入口。每个应用独立启用,互不影响。</p>
47
60
  ${message ? `<div class="msg${isError ? ' err' : ''}">${escapeHtml(message)}</div>` : ''}
48
61
  ${deleteCard}
62
+ ${lanCard}
49
63
  <div class="card"><h2>已登记应用(${apps.length})</h2>${rows}</div>
50
64
  <div class="card"><h2>${editing ? '编辑应用' : '添加应用'}</h2>
51
65
  <form method="post"><input type="hidden" name="action" value="save"><input type="hidden" name="originalId" value="${escapeHtml(editing?.id ?? '')}">
52
66
  <div class="grid"><div><label for="app-id">应用 ID(小写字母、数字、中划线)</label><input id="app-id" name="id" type="text" required maxlength="32" pattern="[a-z0-9]([a-z0-9-]*[a-z0-9])?" value="${escapeHtml(formApp.id)}" placeholder="m0"></div>
53
67
  <div><label for="app-name">显示名称</label><input id="app-name" name="name" type="text" required maxlength="60" value="${escapeHtml(formApp.name)}" placeholder="M0 决策弹窗"></div></div>
54
- <label for="app-target">目标地址(仅允许 127.0.0.1 / ::1)</label><input id="app-target" name="target" type="url" required value="${escapeHtml(formApp.target)}" placeholder="http://127.0.0.1:8765">
68
+ <label for="app-target">${targetLabel}</label><input id="app-target" name="target" type="url" required value="${escapeHtml(formApp.target)}" placeholder="${targetPlaceholder}">
55
69
  <label class="check"><input name="enabled" type="checkbox" value="1"${formApp.enabled ? ' checked' : ''}>启用这个应用</label>
56
70
  <label class="check"><input name="trusted" type="checkbox" value="1"${formApp.trusted ? ' checked' : ''}>我信任此应用的网页脚本(允许 HTML / JavaScript)</label>
57
71
  <p class="note">默认只允许 JSON、文本、图片等安全内容。勾选前请确认应用代码可信:网页脚本与 DSH 同源,能发起 DSH 操作;不可信应用或存在 XSS 的应用不要勾选。</p>
@@ -59,6 +73,6 @@ export function renderLocalAppsPage({ apps, health = {}, editing = null, confirm
59
73
  <div class="ops"><button class="primary" type="submit">${editing ? '保存修改' : '添加并启用'}</button>${editing ? '<a class="btn ghost" href="/dsh_homing/apps">取消编辑</a>' : ''}</div>
60
74
  </form>
61
75
  </div>
62
- <div class="card"><h2>访问方式</h2><p class="note">保存后立即生效,无需重启 DSH。公开地址为 <code>/apps/应用ID/</code>。所有访问仍要求 DSH 登录或有效设备令牌;管理操作要求重新处于登录会话中。<br>只代理本机回环地址,不会把插件会话 Cookie 转发给你的服务。网页如果写死根路径,请使用支持 base path 的服务或后续独立域名模式。</p></div>
76
+ <div class="card"><h2>访问方式</h2><p class="note">保存后立即生效,无需重启 DSH。公开地址为 <code>/apps/应用ID/</code>。所有访问仍要求 DSH 登录或有效设备令牌;管理操作要求重新处于登录会话中。<br>默认只代理本机回环地址;开启局域网后也只允许私网地址(10.x / 172.16–31.x / 192.168.x / link-local),公网地址会被拒绝,不会把插件变成开放代理。不会把插件会话 Cookie 转发给你的服务。网页如果写死根路径,请使用支持 base path 的服务或后续独立域名模式。</p></div>
63
77
  </div></body></html>`
64
78
  }
@@ -14,7 +14,35 @@ export function normalizeAppId(value) {
14
14
  return String(value ?? '').trim().toLowerCase()
15
15
  }
16
16
 
17
- export function appUrlError(value) {
17
+ /** 是否本机回环(与历史行为一致:只认 127.0.0.1 / ::1)。 */
18
+ export function isLoopbackHost(hostname) {
19
+ return hostname === '127.0.0.1' || hostname === '::1'
20
+ }
21
+
22
+ /**
23
+ * 是否局域网私网地址:10/8、172.16/12、192.168/16、169.254/16(IPv4 链路本地)、
24
+ * fc00::/7(IPv6 ULA)、fe80::/10(IPv6 链路本地)。
25
+ * 公网地址一律返回 false —— 开启局域网支持也不能把插件变成公网跳板。
26
+ */
27
+ export function isPrivateLanHost(hostname) {
28
+ const v4 = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/u.exec(hostname)
29
+ if (v4 !== null) {
30
+ const a = Number(v4[1]); const b = Number(v4[2]); const c = Number(v4[3]); const d = Number(v4[4])
31
+ if (a > 255 || b > 255 || c > 255 || d > 255) return false
32
+ if (a === 10) return true
33
+ if (a === 172 && b >= 16 && b <= 31) return true
34
+ if (a === 192 && b === 168) return true
35
+ if (a === 169 && b === 254) return true
36
+ return false
37
+ }
38
+ const v6 = hostname.toLowerCase()
39
+ if (v6 === '::1') return false
40
+ if (/^f[cd][0-9a-f]{0,2}:/u.test(v6)) return true
41
+ if (/^fe[89ab][0-9a-f]?:/u.test(v6)) return true
42
+ return false
43
+ }
44
+
45
+ export function appUrlError(value, { allowLan = false } = {}) {
18
46
  let target
19
47
  try {
20
48
  target = new URL(String(value ?? '').trim())
@@ -24,20 +52,23 @@ export function appUrlError(value) {
24
52
  if (target.protocol !== 'http:' && target.protocol !== 'https:') return '目标只支持 http / https'
25
53
  if (target.username || target.password) return '目标地址不能带用户名或密码'
26
54
  const hostname = target.hostname.replace(/^\[|\]$/gu, '')
27
- if (hostname !== '127.0.0.1' && hostname !== '::1') return '只允许代理本机回环地址(127.0.0.1 / ::1)'
55
+ if (!isLoopbackHost(hostname)) {
56
+ if (!allowLan) return '只允许代理本机回环地址(127.0.0.1 / ::1);如需访问局域网服务,请先开启「允许局域网地址」'
57
+ if (!isPrivateLanHost(hostname)) return '只允许本机回环或局域网私网地址(10.x / 172.16–31.x / 192.168.x / link-local)'
58
+ }
28
59
  if (target.search || target.hash) return '目标地址不能带查询参数或锚点'
29
60
  const port = target.port === '' ? (target.protocol === 'https:' ? 443 : 80) : Number(target.port)
30
61
  if (!Number.isInteger(port) || port < 1 || port > 65535) return '目标端口无效'
31
62
  return ''
32
63
  }
33
64
 
34
- export function validateApp(input, deniedPorts = []) {
65
+ export function validateApp(input, deniedPorts = [], { allowLan = false } = {}) {
35
66
  const id = normalizeAppId(input?.id)
36
67
  const name = String(input?.name ?? '').trim().slice(0, 60)
37
68
  const targetText = String(input?.target ?? '').trim()
38
69
  if (!APP_ID_RE.test(id)) return { ok: false, error: '应用 ID 只能用小写字母、数字和中划线,长度 1–32 位' }
39
70
  if (name === '') return { ok: false, error: '请填写应用名称' }
40
- const targetError = appUrlError(targetText)
71
+ const targetError = appUrlError(targetText, { allowLan })
41
72
  if (targetError) return { ok: false, error: targetError }
42
73
  const target = new URL(targetText)
43
74
  const port = Number(target.port || (target.protocol === 'https:' ? 443 : 80))
@@ -77,13 +108,14 @@ function readSettingsFile(file, { strict = false } = {}) {
77
108
 
78
109
  function normalizedList(raw, deniedPorts = []) {
79
110
  const list = Array.isArray(raw?.apps) ? raw.apps : []
111
+ const allowLan = raw?.allowLanApps === true
80
112
  const seen = new Set()
81
113
  const result = []
82
114
  for (const item of list) {
83
115
  if (!item || typeof item !== 'object') continue
84
116
  const id = normalizeAppId(item.id)
85
117
  if (!APP_ID_RE.test(id) || seen.has(id)) continue
86
- const app = validateApp({ ...item, id }, deniedPorts)
118
+ const app = validateApp({ ...item, id }, deniedPorts, { allowLan })
87
119
  if (!app.ok) continue
88
120
  seen.add(id)
89
121
  result.push(app.app)
@@ -198,9 +230,7 @@ export function createLocalAppRegistry({ settingsPath, deniedPorts = () => [] })
198
230
  return cache.apps.map(cloneApp)
199
231
  }
200
232
 
201
- function writeApps(apps) {
202
- const settings = readSettingsFile(settingsPath, { strict: true })
203
- settings.apps = apps
233
+ function writeSettings(settings) {
204
234
  fs.mkdirSync(path.dirname(settingsPath), { recursive: true })
205
235
  const temp = `${settingsPath}.${process.pid}.tmp`
206
236
  fs.writeFileSync(temp, JSON.stringify(settings, null, 2) + '\n', { encoding: 'utf8', mode: 0o600 })
@@ -208,17 +238,45 @@ export function createLocalAppRegistry({ settingsPath, deniedPorts = () => [] })
208
238
  cache = { mtime: fs.statSync(settingsPath).mtimeMs, apps: normalizedList(settings, deniedPorts()) }
209
239
  }
210
240
 
241
+ function writeApps(apps) {
242
+ const settings = readSettingsFile(settingsPath, { strict: true })
243
+ settings.apps = apps
244
+ writeSettings(settings)
245
+ }
246
+
247
+ /** 当前是否允许局域网目标(读 settings.json,由管理页切换)。 */
248
+ function allowLan() {
249
+ return readSettingsFile(settingsPath).allowLanApps === true
250
+ }
251
+
252
+ /** 打开/关闭局域网目标支持。关闭后已登记的局域网应用会在下次读取时被过滤掉。 */
253
+ function setAllowLan(value) {
254
+ let settings
255
+ try {
256
+ settings = readSettingsFile(settingsPath, { strict: true })
257
+ } catch {
258
+ return { ok: false, error: 'settings.json 已损坏,未做任何修改;请先修复配置文件' }
259
+ }
260
+ settings.allowLanApps = value === true
261
+ try {
262
+ writeSettings(settings)
263
+ } catch (error) {
264
+ return { ok: false, error: '配置写入失败:' + String(error?.message ?? error).slice(0, 160) }
265
+ }
266
+ return { ok: true, allowLan: value === true }
267
+ }
268
+
211
269
  function save(input) {
212
- const validated = validateApp(input, deniedPorts())
213
- if (!validated.ok) return validated
214
- const id = validated.app.id
215
- const originalId = normalizeAppId(input.originalId ?? '')
216
270
  let raw
217
271
  try {
218
272
  raw = readSettingsFile(settingsPath, { strict: true })
219
273
  } catch {
220
274
  return { ok: false, error: 'settings.json 已损坏,未做任何修改;请先修复配置文件' }
221
275
  }
276
+ const validated = validateApp(input, deniedPorts(), { allowLan: raw.allowLanApps === true })
277
+ if (!validated.ok) return validated
278
+ const id = validated.app.id
279
+ const originalId = normalizeAppId(input.originalId ?? '')
222
280
  const current = Array.isArray(raw.apps) ? raw.apps.filter((item) => item && typeof item === 'object') : []
223
281
  if (current.some((item) => normalizeAppId(item.id) === id && normalizeAppId(item.id) !== originalId)) {
224
282
  return { ok: false, error: `应用 ID ${id} 已存在` }
@@ -253,7 +311,7 @@ export function createLocalAppRegistry({ settingsPath, deniedPorts = () => [] })
253
311
  return { ok: true }
254
312
  }
255
313
 
256
- return { list, save, update, remove }
314
+ return { list, save, update, remove, allowLan, setAllowLan }
257
315
  }
258
316
 
259
317
  /** 管理页的端口探活;不是完整的应用健康检查,文案必须写“端口可达”。 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andy.ian/dsh-homing",
3
- "version": "2.3.8",
3
+ "version": "2.3.9",
4
4
  "type": "module",
5
5
  "description": "把本机 DSH 通过 SSH 反向隧道安全开放到手机浏览器:TLS 端到端(服务器只透传)、账号 + TOTP、局域网与公网双入口,随 DSH 启动。",
6
6
  "keywords": [