@andy.ian/dsh-homing 2.3.0 → 2.3.2

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
@@ -1314,6 +1314,9 @@ function verifyTotp(secret, code, lastStep) {
1314
1314
  return false;
1315
1315
  }
1316
1316
 
1317
+ /** HSTS 有效期:一年。让浏览器在首次 HTTPS 访问后不再尝试明文 http。 */
1318
+ const HSTS_MAX_AGE = 'max-age=31536000';
1319
+
1317
1320
  function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCookies, caCertPath }) {
1318
1321
  const appRegistry = createLocalAppRegistry({
1319
1322
  settingsPath: path.join(dir, 'settings.json'),
@@ -1512,6 +1515,13 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
1512
1515
  }
1513
1516
  let csp = '';
1514
1517
  if (headers && typeof headers === 'object') csp = String(headers['content-security-policy'] ?? '');
1518
+ // 统一给所有响应加 HSTS(2026-11-19):手机浏览器升级后曾默认走 http://,
1519
+ // 远端 80 端口空关闭导致 ERR_EMPTY_RESPONSE;现在 80 已 301 到 https,
1520
+ // 再加上 HSTS 让浏览器第一次访问后直接走 https、不再经过 80。
1521
+ // 只有 HTTPS 响应会被浏览器采纳,明文响应里带这个头是无害的。
1522
+ if (headers && typeof headers === 'object' && headers['strict-transport-security'] === undefined) {
1523
+ headers = { ...headers, 'strict-transport-security': HSTS_MAX_AGE };
1524
+ }
1515
1525
  // 只有插件自己的页面以 default-src 'none' 开头;DSH 前端页面不能改写,
1516
1526
  // 否则会破坏 0.2.2 动态生成的 <script> 模板。
1517
1527
  if (csp.includes("default-src 'none'")) {
@@ -3150,20 +3160,12 @@ a{color:#8ab4f8}
3150
3160
  return;
3151
3161
  }
3152
3162
 
3153
- // Markdown 图片:只允许常见图片格式,内联返回(不下载)
3163
+ // 图片内联返回(Markdown 相对图片 + 文件页图片预览都用它)。
3164
+ // 白名单与 MIME 由 lib/files.mjs 统一维护;SVG 故意不收(同源内联会执行脚本)。
3154
3165
  if (url.pathname === '/files/raw' && req.method === 'GET') {
3155
3166
  const target = filebrowse.resolvePath(url.searchParams.get('path') ?? '');
3156
- const typeByExt = {
3157
- '.png': 'image/png',
3158
- '.jpg': 'image/jpeg',
3159
- '.jpeg': 'image/jpeg',
3160
- '.gif': 'image/gif',
3161
- '.webp': 'image/webp',
3162
- '.bmp': 'image/bmp',
3163
- };
3164
- const ext = path.extname(target).toLowerCase();
3165
- const contentType = typeByExt[ext];
3166
- if (contentType === undefined) {
3167
+ const contentType = filebrowse.imageMimeOf(target);
3168
+ if (contentType === null) {
3167
3169
  res.writeHead(415, { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' });
3168
3170
  res.end('不支持的图片格式\n');
3169
3171
  return;
@@ -3249,6 +3251,18 @@ a{color:#8ab4f8}
3249
3251
  res.end();
3250
3252
  return;
3251
3253
  }
3254
+ // 图片:直接渲染预览页(文件列表里点图片名就走这里);字节由 /files/raw 内联返回
3255
+ if (filebrowse.isImageFile(asked)) {
3256
+ const target = filebrowse.resolvePath(asked);
3257
+ let stat = null;
3258
+ try { stat = fs.statSync(target); } catch { stat = null; }
3259
+ const info = stat !== null && stat.isFile()
3260
+ ? { path: target, sizeText: filebrowse.fmtSize(stat.size), mtime: filebrowse.fmtTime(stat.mtime), image: true }
3261
+ : { path: target, error: '文件不存在' };
3262
+ appendAudit(dir, 'file-view', { path: target, kind: 'image', ok: info.image === true });
3263
+ sendHtml(renderFileView({ info }));
3264
+ return;
3265
+ }
3252
3266
  const info = filebrowse.readTextFile(asked);
3253
3267
  const raw = url.searchParams.get('raw') === '1';
3254
3268
  if (!raw && info.text !== undefined && markdown.isMarkdownFile(info.path)) {
@@ -4099,7 +4113,7 @@ ${message ? `<div class="msg">${escapeHtml(message)}</div>` : ''}
4099
4113
  delete staticHeaders['etag'];
4100
4114
  delete staticHeaders['last-modified'];
4101
4115
  }
4102
- staticHeaders['strict-transport-security'] = 'max-age=31536000';
4116
+ staticHeaders['strict-transport-security'] = HSTS_MAX_AGE;
4103
4117
  res.writeHead(upstreamStatus, staticHeaders);
4104
4118
  // SSE 诊断:应用靠 /plugins/events 拿到插件图。记录时长与字节数开销极小,
4105
4119
  // 出问题时能立刻判断"事件流是否送达"。
@@ -425,13 +425,18 @@ export function renderCompressPage({ target, archive, message, isError, done = f
425
425
  /** 文件内容页(text=源码;rendered=Markdown 渲染结果) */
426
426
  export function renderFile({ info, message, isError, rendered = null }) {
427
427
  const isMarkdown = /\.(?:md|markdown|mdown|mkd)$/iu.test(info.path)
428
+ const isImage = info.image === true
428
429
  const sourceHref = '/files/view?path=' + encodeURIComponent(info.path) + '&raw=1'
429
430
  const renderedHref = '/files/view?path=' + encodeURIComponent(info.path)
430
- const toggle = rendered
431
- ? `<div class="chips"><a class="chip" href="${sourceHref}">查看源码</a></div>`
432
- : (isMarkdown && info.error === undefined
433
- ? `<div class="chips"><a class="chip" href="${renderedHref}">渲染 Markdown</a></div>`
434
- : '')
431
+ const rawHref = '/files/raw?path=' + encodeURIComponent(info.path)
432
+ const downloadHref = '/files/download?path=' + encodeURIComponent(info.path)
433
+ const toggle = isImage
434
+ ? `<div class="chips"><a class="chip" href="${rawHref}" target="_blank" rel="noopener noreferrer">查看原图</a><a class="chip" href="${downloadHref}">下载</a></div>`
435
+ : rendered
436
+ ? `<div class="chips"><a class="chip" href="${sourceHref}">查看源码</a></div>`
437
+ : (isMarkdown && info.error === undefined
438
+ ? `<div class="chips"><a class="chip" href="${renderedHref}">渲染 Markdown</a></div>`
439
+ : '')
435
440
  const body = `
436
441
  <div class="bar">
437
442
  <h1>${escapeHtml(info.path.split(/[\\/]/u).pop())}</h1>
@@ -440,7 +445,12 @@ export function renderFile({ info, message, isError, rendered = null }) {
440
445
  </div>
441
446
  ${messageHtml(message, isError)}
442
447
  ${info.error ? `<div class="msg err">${escapeHtml(info.error)}</div>` : ''}
443
- ${rendered ? `<article class="md-body">${rendered.html}</article>` : (info.text !== undefined ? `<pre>${escapeHtml(info.text)}</pre>` : '')}
448
+ ${isImage && info.error === undefined
449
+ ? `<div style="margin:10px 0;padding:10px;border-radius:10px;background:#0f1319;text-align:center">
450
+ <a href="${rawHref}" target="_blank" rel="noopener noreferrer"><img src="${rawHref}" alt="${escapeHtml(info.path.split(/[\\/]/u).pop())}" style="max-width:100%;height:auto;display:block;margin:0 auto;border-radius:6px"></a>
451
+ <p class="crumbs" style="margin-top:8px">点图片可在新标签打开原图${info.sizeText ? ` · ${escapeHtml(info.sizeText)}` : ''}</p>
452
+ </div>`
453
+ : (rendered ? `<article class="md-body">${rendered.html}</article>` : (info.text !== undefined ? `<pre>${escapeHtml(info.text)}</pre>` : ''))}
444
454
  <div class="row">
445
455
  <a class="btn ghost" href="/files?path=${encodeURIComponent(info.path.replace(/[\\/][^\\/]*$/u, '') || '/')}"
446
456
  style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">返回目录</a>
package/lib/files.mjs CHANGED
@@ -275,6 +275,26 @@ export function runnableKind(file) {
275
275
  return null
276
276
  }
277
277
 
278
+ /**
279
+ * 可在线预览的图片类型(白名单)。
280
+ * 只收浏览器原生能渲染、且不执行脚本的格式:SVG 故意不收
281
+ * (直接同源打开 SVG 里的脚本会执行,而 /files/raw 是内联返回)。
282
+ */
283
+ const IMAGE_MIME = new Map([
284
+ ['.png', 'image/png'],
285
+ ['.jpg', 'image/jpeg'],
286
+ ['.jpeg', 'image/jpeg'],
287
+ ['.gif', 'image/gif'],
288
+ ['.webp', 'image/webp'],
289
+ ['.bmp', 'image/bmp'],
290
+ ])
291
+
292
+ /** 是不是可在线预览的图片文件 */
293
+ export const isImageFile = (name) => IMAGE_MIME.has(path.extname(String(name ?? '')).toLowerCase())
294
+
295
+ /** 图片 MIME;不是白名单图片格式返回 null */
296
+ export const imageMimeOf = (name) => IMAGE_MIME.get(path.extname(String(name ?? '')).toLowerCase()) ?? null
297
+
278
298
  /** 读文本文件;二进制或过大时不返回正文。 */
279
299
  export function readTextFile(absPath, limit = MAX_READ) {
280
300
  const target = resolvePath(absPath)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andy.ian/dsh-homing",
3
- "version": "2.3.0",
3
+ "version": "2.3.2",
4
4
  "type": "module",
5
5
  "description": "把本机 DSH 通过 SSH 反向隧道安全开放到手机浏览器:TLS 端到端(服务器只透传)、账号 + TOTP、局域网与公网双入口,随 DSH 启动。",
6
6
  "keywords": [