dsh-plugin-mesh 0.5.17 → 0.5.18

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/lib/index.js CHANGED
@@ -129,7 +129,7 @@ import {
129
129
  export const name = "dsh-plugin-mesh";
130
130
  export const inject = ["tools", "webServer"];
131
131
 
132
- const VERSION = "0.5.17";
132
+ const VERSION = "0.5.18";
133
133
  // 诊断用:进程启动时间 + 入口真实路径。磁盘上的版本**不在这里缓存** ——
134
134
  // 升级之后进程还跑旧代码,版本号必须现读磁盘才能立刻反映出来(真踩过:
135
135
  // 磁盘升到 0.5.1,进程还在 0.5.0,界面显示 0.5.0)。
package/lib/peer-web.js CHANGED
@@ -29,9 +29,14 @@
29
29
  * 共享密钥与票据都**不转发给上游 GUI**(见 rewrite())。
30
30
  */
31
31
  import { createHmac, timingSafeEqual } from "node:crypto";
32
- import { request as httpRequest } from "node:http";
32
+ import { Agent as HttpAgent, request as httpRequest } from "node:http";
33
33
  import { connect as netConnect } from "node:net";
34
34
  import { signSessionCookie } from "./desktop-auth.js";
35
+ import { createGzip } from "node:zlib";
36
+ // [mesh-keepalive] 复用上游连接:几十个 bundle 请求不该各建一次 TCP。
37
+ // tailnet 上每次握手就是一个 RTT;局域网/回环上也省掉握手与端口分配。
38
+ const upstreamAgent = new HttpAgent({ keepAlive: true, maxSockets: 16, maxFreeSockets: 8, timeout: 30000 });
39
+
35
40
 
36
41
  /** 隧道前缀(挂在 mesh 口上,和 Web 口上的 /mesh/web 不是一回事)。 */
37
42
  export const PEER_WEB_PREFIX = "/mesh/peer-web";
@@ -170,12 +175,26 @@ export function createPeerWebForwarder({ portOf, secretOf, log = () => {}, warn
170
175
  }
171
176
  const authority = `127.0.0.1:${port}`;
172
177
  const up = httpRequest(
173
- { host: "127.0.0.1", port, method: req.method, path, headers: rewrite(req.headers, authority) },
178
+ { host: "127.0.0.1", port, method: req.method, path, headers: rewrite(req.headers, authority), agent: upstreamAgent },
174
179
  (reply) => {
175
180
  const out = { ...reply.headers };
176
181
  // 会话由本转发维护,别把 Set-Cookie 泄到隧道外(观看端没有对应的 authority)。
177
182
  delete out["set-cookie"];
178
183
  delete out["transfer-encoding"];
184
+ // [mesh-gzip] 上游没压时在隧道这一跳压缩(tailnet 是细管,收益最大)
185
+ if (out["content-encoding"] === undefined
186
+ && String(req.headers["accept-encoding"] ?? "").includes("gzip")
187
+ // HTML 不压:观看端代理要往文档里注入垫片,必须拿到明文;
188
+ // 大文档也就几十 KB,压不压对总时长没有意义,而注入冲突是硬错误。
189
+ && /^text\/|javascript|json|svg|css/i.test(String(out["content-type"] ?? ""))
190
+ && !/^text\/html/i.test(String(out["content-type"] ?? ""))) {
191
+ out["content-encoding"] = "gzip";
192
+ out["vary"] = "accept-encoding";
193
+ delete out["content-length"];
194
+ res.writeHead(reply.statusCode ?? 502, out);
195
+ reply.pipe(createGzip({ level: 1 })).pipe(res);
196
+ return;
197
+ }
179
198
  res.writeHead(reply.statusCode ?? 502, out);
180
199
  reply.pipe(res);
181
200
  },
package/lib/web-proxy.js CHANGED
@@ -18,11 +18,14 @@
18
18
  * 这不是"安全方案",只是别让它成为同机任意 App 的免费后门;
19
19
  * 真正的边界在"谁能碰到这台手机"。
20
20
  */
21
- import { createServer, request as httpRequest } from "node:http";
21
+ import { Agent as HttpAgent, createServer, request as httpRequest } from "node:http";
22
22
  import { appendFileSync } from "node:fs";
23
23
  import { join } from "node:path";
24
24
  import { homedir } from "node:os";
25
25
  import { connect as netConnect } from "node:net";
26
+ // [mesh-keepalive] 复用上游连接:几十个 bundle 请求不该各建一次 TCP。
27
+ // tailnet 上每次握手就是一个 RTT;局域网/回环上也省掉握手与端口分配。
28
+ const upstreamAgent = new HttpAgent({ keepAlive: true, maxSockets: 16, maxFreeSockets: 8, timeout: 30000 });
26
29
 
27
30
  /** 我们自己发的门禁 cookie:名字固定,值就是 nonce。 */
28
31
  const GATE_COOKIE = "dshmesh-web";
@@ -211,7 +214,7 @@ export async function createWebProxy({
211
214
  if (wantsHtml) headers["accept-encoding"] = "identity";
212
215
 
213
216
  const up = httpRequest(
214
- { host: hostname, port: remotePort, method: req.method, path: `${prefix}${stripGate(req.url)}`, headers },
217
+ { host: hostname, port: remotePort, method: req.method, path: `${prefix}${stripGate(req.url)}`, headers, agent: upstreamAgent },
215
218
  (reply) => {
216
219
  // 401 = 我们的服务端 cookie 过期/轮换过:换一次再重试(只重试幂等方法)。
217
220
  const idempotent = req.method === "GET" || req.method === "HEAD";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-mesh",
3
- "version": "0.5.17",
3
+ "version": "0.5.18",
4
4
  "type": "module",
5
5
  "description": "把跑不完的活交给另一台电脑;跑完,结果回到你面前。两台装了 DSH 的电脑各装一次,自动互相发现,任务派过去由对端的 agent 执行,结果派回来。Hand a long task to another machine: your DSH instances find each other, the peer runs it, and the result comes back. No pairing codes, no IPs, no Tailscale client.",
6
6
  "license": "MIT",