dsh-local-link 0.2.1 → 1.1.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/lib/index.mjs CHANGED
@@ -333,6 +333,7 @@ const DIAGNOSTIC_CODES = /* @__PURE__ */ new Set([
333
333
  "REQUEST_REJECTED",
334
334
  "AUTH_REQUIRED",
335
335
  "HTTP_UPSTREAM_ERROR",
336
+ "BROWSER_AUTH_HANDOFF_FAILED",
336
337
  "INDEX_REWRITE_ERROR",
337
338
  "WS_REJECTED",
338
339
  "WS_UPSTREAM_ERROR",
@@ -464,8 +465,20 @@ function ensureResponsiveViewport(html) {
464
465
  const replacement = content.test(match[0]) ? match[0].replace(content, (_whole, quote, value) => `content=${quote}${value},viewport-fit=cover${quote}`) : match[0].replace(/\s*\/?>$/u, " content=\"width=device-width,initial-scale=1,viewport-fit=cover\">");
465
466
  return `${html.slice(0, match.index)}${replacement}${html.slice(match.index + match[0].length)}`;
466
467
  }
467
- function removeHostDirectoryPickers(entries) {
468
- for (let index = entries.length - 1; index >= 0; index -= 1) if (HOST_DIRECTORY_PICKER_IDS.has(String(entries[index]?.id))) entries.splice(index, 1);
468
+ function removeHostDirectoryPickers(manifest, entries) {
469
+ const removed = /* @__PURE__ */ new Set();
470
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
471
+ const id = String(entries[index]?.id);
472
+ if (!HOST_DIRECTORY_PICKER_IDS.has(id)) continue;
473
+ removed.add(id);
474
+ entries.splice(index, 1);
475
+ }
476
+ if (!Array.isArray(manifest.batches) || removed.size === 0) return;
477
+ for (const value of manifest.batches) {
478
+ if (value === null || typeof value !== "object" || Array.isArray(value)) continue;
479
+ const batch = value;
480
+ if (Array.isArray(batch.entries)) batch.entries = batch.entries.filter((id) => !removed.has(String(id)));
481
+ }
469
482
  }
470
483
  /**
471
484
  * Keep the shipped Harness root and every dynamic child slot. The gateway only
@@ -483,7 +496,7 @@ function rewriteAuthenticatedIndex(html) {
483
496
  if (!Array.isArray(manifest.entries)) throw new Error("DSH boot manifest has no entries");
484
497
  const entries = manifest.entries;
485
498
  if (!entries.some((entry) => entry.id === CLIENT_ID)) throw new Error("dsh-local-link client is absent from DSH boot manifest");
486
- removeHostDirectoryPickers(entries);
499
+ removeHostDirectoryPickers(manifest, entries);
487
500
  return ensureResponsiveViewport(`${html.slice(0, assignment.index)}${RANDOM_UUID_POLYFILL}${assignment[0]}${JSON.stringify(manifest)};${html.slice(scriptEnd)}`);
488
501
  }
489
502
  //#endregion
@@ -507,6 +520,7 @@ var pair_timeout = "连接超时。请生成新二维码后重试。";
507
520
  //#endregion
508
521
  //#region src/gateway/pair-page.ts
509
522
  const PAIR_PATH = "/__dsh-local-link/pair";
523
+ const CONNECT_PATH = "/__dsh-local-link/connect";
510
524
  const pairCopies = {
511
525
  en: {
512
526
  title: pair_title$1,
@@ -567,7 +581,7 @@ const PAIR_PAGE = `<!doctype html>
567
581
  request.send(JSON.stringify({token,device:{type,browser}}));
568
582
  });
569
583
  if(sessionId)try{localStorage.setItem('dsh.sessions.current',JSON.stringify({sessionId}));}catch{}
570
- location.replace('/');
584
+ location.replace('${CONNECT_PATH}');
571
585
  }catch(error){stop(error instanceof Error?error.message:copy.failed);}})();
572
586
  <\/script>
573
587
  </html>`;
@@ -575,7 +589,30 @@ const PAIR_PAGE = `<!doctype html>
575
589
  //#region src/gateway/local-gateway.ts
576
590
  const COOKIE_NAME = "dsh_local_link_device";
577
591
  const MAX_PAIR_BODY = 4096;
578
- const EVENT_PATHS = /* @__PURE__ */ new Set(["/api/events.mux", "/api/events.host"]);
592
+ const STREAM_PATHS = /* @__PURE__ */ new Set([
593
+ "/api/events.mux",
594
+ "/api/events.host",
595
+ "/api/remote.mux"
596
+ ]);
597
+ const LOOPBACK_ONLY_RPC_METHODS = /* @__PURE__ */ new Set([
598
+ "host.pickDirectory",
599
+ "host.openPath",
600
+ "settings.describe",
601
+ "settings.canOpenAgentPresetDirectory",
602
+ "settings.update",
603
+ "settings.replace",
604
+ "settings.mutate",
605
+ "settings.openDocument",
606
+ "settings.openSettingsDocument",
607
+ "settings.openAgentPresetDirectory",
608
+ "credentials.describe",
609
+ "credentials.set",
610
+ "credentials.unset",
611
+ "agentPreset.read",
612
+ "agentPreset.copy",
613
+ "agentPreset.openDocument",
614
+ "agentPreset.remove"
615
+ ]);
579
616
  const HOP_BY_HOP_HEADERS = /* @__PURE__ */ new Set([
580
617
  "connection",
581
618
  "keep-alive",
@@ -624,6 +661,10 @@ function send(response, status, body, type = "text/plain; charset=utf-8") {
624
661
  });
625
662
  response.end(body);
626
663
  }
664
+ function upstreamCookieHeader(request) {
665
+ const cookies = (request.headers.cookie ?? "").split(";").map((part) => part.trim()).filter((part) => part !== "" && part.split("=", 1)[0] !== COOKIE_NAME);
666
+ return cookies.length > 0 ? cookies.join("; ") : void 0;
667
+ }
627
668
  function proxyRequestHeaders(request, websocket = false) {
628
669
  const headers = {};
629
670
  for (const [name, value] of Object.entries(request.headers)) {
@@ -631,13 +672,15 @@ function proxyRequestHeaders(request, websocket = false) {
631
672
  if (value === void 0 || lower === "cookie" || lower.startsWith("x-forwarded-") || !websocket && HOP_BY_HOP_HEADERS.has(lower)) continue;
632
673
  headers[lower] = value;
633
674
  }
675
+ const cookie = upstreamCookieHeader(request);
676
+ if (cookie !== void 0) headers.cookie = cookie;
634
677
  return headers;
635
678
  }
636
- function proxyResponseHeaders(headers) {
679
+ function proxyResponseHeaders(headers, forwardSetCookie = false) {
637
680
  const result = {};
638
681
  for (const [name, value] of Object.entries(headers)) {
639
682
  const lower = name.toLowerCase();
640
- if (value === void 0 || HOP_BY_HOP_HEADERS.has(lower) || lower === "set-cookie") continue;
683
+ if (value === void 0 || HOP_BY_HOP_HEADERS.has(lower) || lower === "set-cookie" && !forwardSetCookie) continue;
641
684
  result[lower] = value;
642
685
  }
643
686
  return result;
@@ -653,19 +696,32 @@ function serializeHeaders(headers) {
653
696
  }
654
697
  function requestKind(pathname) {
655
698
  if (pathname === "/") return "root";
656
- if (EVENT_PATHS.has(pathname)) return "events";
699
+ if (STREAM_PATHS.has(pathname)) return "events";
657
700
  if (pathname.startsWith("/api/")) return "api";
658
701
  if (/\.[a-z0-9]{1,8}$/iu.test(pathname)) return "asset";
659
702
  return "page";
660
703
  }
704
+ function supportedWebSocketTarget(target) {
705
+ return target.search === "" && STREAM_PATHS.has(target.pathname);
706
+ }
707
+ function isLoopbackOnlyRpcTarget(target) {
708
+ if (!target.pathname.startsWith("/api/")) return false;
709
+ try {
710
+ return LOOPBACK_ONLY_RPC_METHODS.has(decodeURIComponent(target.pathname.slice(5)));
711
+ } catch {
712
+ return true;
713
+ }
714
+ }
661
715
  var LocalGateway = class {
662
716
  config;
663
717
  devices;
664
718
  pairing;
665
719
  diagnostics;
666
720
  upgradedSockets = /* @__PURE__ */ new Set();
721
+ deviceSockets = /* @__PURE__ */ new Map();
667
722
  server;
668
723
  addresses = [];
724
+ browserAuthenticationUrl;
669
725
  constructor(config) {
670
726
  this.config = config;
671
727
  this.devices = new DeviceStore(config.stateFile, config.deviceTtlMs);
@@ -711,6 +767,12 @@ var LocalGateway = class {
711
767
  trustedAuthorities() {
712
768
  return Object.freeze(this.addresses.map((address) => formatAuthority(address, this.config.listenPort)));
713
769
  }
770
+ attachBrowserAuthentication(factory) {
771
+ this.browserAuthenticationUrl = factory;
772
+ return () => {
773
+ if (this.browserAuthenticationUrl === factory) this.browserAuthenticationUrl = void 0;
774
+ };
775
+ }
714
776
  diagnosticEvents() {
715
777
  return this.diagnostics.list();
716
778
  }
@@ -721,7 +783,11 @@ var LocalGateway = class {
721
783
  this.diagnostics.record("error", code);
722
784
  }
723
785
  async revoke(id) {
724
- return this.devices.revoke(id);
786
+ try {
787
+ return await this.devices.revoke(id);
788
+ } finally {
789
+ this.closeDeviceSockets(id);
790
+ }
725
791
  }
726
792
  async renameDevice(id, name) {
727
793
  return this.devices.renameDevice(id, name);
@@ -732,6 +798,7 @@ var LocalGateway = class {
732
798
  if (server === void 0) return;
733
799
  for (const socket of this.upgradedSockets) socket.destroy();
734
800
  this.upgradedSockets.clear();
801
+ this.deviceSockets.clear();
735
802
  await new Promise((resolve, reject) => server.close((error) => error === void 0 ? resolve() : reject(error)));
736
803
  }
737
804
  requestTrusted(request) {
@@ -740,6 +807,23 @@ var LocalGateway = class {
740
807
  authorized(request) {
741
808
  return this.config.accessMode === "trusted-lan" || this.devices.authorize(cookieValue(request, COOKIE_NAME)) !== void 0;
742
809
  }
810
+ trackDeviceSocket(deviceId, socket) {
811
+ const sockets = this.deviceSockets.get(deviceId) ?? /* @__PURE__ */ new Set();
812
+ sockets.add(socket);
813
+ this.deviceSockets.set(deviceId, sockets);
814
+ }
815
+ untrackDeviceSocket(deviceId, socket) {
816
+ const sockets = this.deviceSockets.get(deviceId);
817
+ if (sockets === void 0) return;
818
+ sockets.delete(socket);
819
+ if (sockets.size === 0) this.deviceSockets.delete(deviceId);
820
+ }
821
+ closeDeviceSockets(deviceId) {
822
+ const sockets = this.deviceSockets.get(deviceId);
823
+ if (sockets === void 0) return;
824
+ this.deviceSockets.delete(deviceId);
825
+ for (const socket of sockets) socket.destroy();
826
+ }
743
827
  async handle(request, response) {
744
828
  if (!this.requestTrusted(request)) {
745
829
  this.diagnostics.record("warn", "REQUEST_REJECTED", { reason: "untrusted_source" });
@@ -784,10 +868,41 @@ var LocalGateway = class {
784
868
  send(response, 401, "This browser is not paired. Open Local access in the computer sidebar and scan its QR code.");
785
869
  return;
786
870
  }
871
+ if (url.pathname === "/__dsh-local-link/connect" && request.method === "GET") {
872
+ const authenticationUrl = this.browserAuthenticationUrl;
873
+ if (authenticationUrl === void 0) {
874
+ response.writeHead(303, {
875
+ location: "/",
876
+ "cache-control": "no-store"
877
+ });
878
+ response.end();
879
+ return;
880
+ }
881
+ try {
882
+ const origin = new URL(`http://${request.headers.host ?? ""}`);
883
+ const target = new URL(authenticationUrl(origin.href));
884
+ if (target.origin !== origin.origin || target.pathname !== "/" || target.hash !== "") throw new Error("invalid authentication handoff");
885
+ response.writeHead(303, {
886
+ location: `${target.pathname}${target.search}`,
887
+ "cache-control": "no-store",
888
+ "referrer-policy": "no-referrer"
889
+ });
890
+ response.end();
891
+ } catch {
892
+ this.diagnostics.record("error", "BROWSER_AUTH_HANDOFF_FAILED", { reason: "invalid_target" });
893
+ send(response, 502, "DeepSeek Harness browser authentication is unavailable");
894
+ }
895
+ return;
896
+ }
897
+ if (isLoopbackOnlyRpcTarget(url)) {
898
+ this.diagnostics.record("warn", "REQUEST_REJECTED", { reason: "loopback_only_rpc" });
899
+ send(response, 403, "This DeepSeek Harness action is available only on the host computer.");
900
+ return;
901
+ }
787
902
  const interceptIndex = request.method === "GET" && url.pathname === "/";
788
- this.proxyHttp(request, response, interceptIndex);
903
+ this.proxyHttp(request, response, interceptIndex, interceptIndex && url.searchParams.has("token"));
789
904
  }
790
- proxyHttp(request$1, response, interceptIndex) {
905
+ proxyHttp(request$1, response, interceptIndex, browserTokenExchange) {
791
906
  const headers = proxyRequestHeaders(request$1);
792
907
  if (interceptIndex) headers["accept-encoding"] = "identity";
793
908
  const upstreamRequest = request({
@@ -799,7 +914,7 @@ var LocalGateway = class {
799
914
  agent: false
800
915
  }, (upstreamResponse) => {
801
916
  if (!interceptIndex || !String(upstreamResponse.headers["content-type"] ?? "").includes("text/html")) {
802
- response.writeHead(upstreamResponse.statusCode ?? 502, proxyResponseHeaders(upstreamResponse.headers));
917
+ response.writeHead(upstreamResponse.statusCode ?? 502, proxyResponseHeaders(upstreamResponse.headers, browserTokenExchange));
803
918
  upstreamResponse.pipe(response);
804
919
  return;
805
920
  }
@@ -831,14 +946,14 @@ var LocalGateway = class {
831
946
  }
832
947
  async proxyWebSocket(request, client, head) {
833
948
  const trusted = this.requestTrusted(request);
834
- const authorized = trusted && this.authorized(request);
949
+ const device = this.config.accessMode === "pairing" ? this.devices.authorize(cookieValue(request, COOKIE_NAME)) : void 0;
950
+ const authorized = trusted && (this.config.accessMode === "trusted-lan" || device !== void 0);
835
951
  if (!trusted || !authorized) {
836
952
  this.diagnostics.record("warn", "WS_REJECTED", { reason: trusted ? "unauthorized" : "untrusted_source" });
837
953
  client.end("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n");
838
954
  return;
839
955
  }
840
- const target = new URL(request.url ?? "/", this.config.upstreamOrigin);
841
- if (target.search !== "" || !EVENT_PATHS.has(target.pathname)) {
956
+ if (!supportedWebSocketTarget(new URL(request.url ?? "/", this.config.upstreamOrigin))) {
842
957
  this.diagnostics.record("warn", "WS_REJECTED", { reason: "unsupported_path" });
843
958
  client.end("HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\n");
844
959
  return;
@@ -846,9 +961,17 @@ var LocalGateway = class {
846
961
  const upstream = connect(Number(this.config.upstreamOrigin.port), this.config.upstreamOrigin.hostname);
847
962
  this.upgradedSockets.add(client);
848
963
  this.upgradedSockets.add(upstream);
964
+ if (device !== void 0) {
965
+ this.trackDeviceSocket(device.id, client);
966
+ this.trackDeviceSocket(device.id, upstream);
967
+ }
849
968
  const cleanup = () => {
850
969
  this.upgradedSockets.delete(client);
851
970
  this.upgradedSockets.delete(upstream);
971
+ if (device !== void 0) {
972
+ this.untrackDeviceSocket(device.id, client);
973
+ this.untrackDeviceSocket(device.id, upstream);
974
+ }
852
975
  };
853
976
  client.once("close", cleanup);
854
977
  upstream.once("close", cleanup);
@@ -912,6 +1035,11 @@ async function apply(ctx, config) {
912
1035
  const gateway = new LocalGateway(parseConfig(config));
913
1036
  await gateway.start();
914
1037
  ctx.provide("localLinkGateway", Object.freeze({ trustedHosts: gateway.trustedAuthorities() }));
1038
+ ctx.inject(["connection"], (scope) => {
1039
+ const connection = scope.connection;
1040
+ if (typeof connection?.authenticatedUrl !== "function") return;
1041
+ scope.effect(() => gateway.attachBrowserAuthentication((baseUrl) => connection.authenticatedUrl?.(baseUrl) ?? baseUrl), "dsh-local-link: Harness browser authentication handoff");
1042
+ });
915
1043
  const adminRoute = {
916
1044
  kind: "prefix",
917
1045
  path: ADMIN_PREFIX,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-local-link",
3
- "version": "0.2.1",
3
+ "version": "1.1.0",
4
4
  "description": "DeepSeek Harness plugin for lightweight LAN access to the current Web UI from phones, tablets, and other computers.",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -27,6 +27,7 @@
27
27
  "docs/images/*.jpg",
28
28
  "docs/images/*.png",
29
29
  "docs/ARCHITECTURE.md",
30
+ "docs/COMPATIBILITY.md",
30
31
  "docs/DIAGNOSTICS.md",
31
32
  "docs/MOBILE_VIEW.md",
32
33
  "docs/SECURITY.md",
@@ -42,8 +43,9 @@
42
43
  "client": {
43
44
  "platform": "web",
44
45
  "inject": [
45
- "@deepseek-ai/dsh-client-runtime",
46
- "@deepseek-ai/dsh-client-ui-slots",
46
+ "@deepseek-ai/dsh-api-session-controller",
47
+ "@deepseek-ai/dsh-client-ui-renderer",
48
+ "@deepseek-ai/dsh-client-ui-session",
47
49
  "@deepseek-ai/dsh-client-locale",
48
50
  "@deepseek-ai/dsh-client-ui-theme",
49
51
  "@deepseek-ai/dsh-client-ui-layout",
@@ -54,7 +56,6 @@
54
56
  },
55
57
  "scripts": {
56
58
  "build": "tsdown",
57
- "clean": "node ./scripts/clean.mjs",
58
59
  "prepack": "npm run build",
59
60
  "release:check": "npm run verify && pnpm audit --prod && npm publish --dry-run",
60
61
  "test": "vitest run",
@@ -85,8 +86,8 @@
85
86
  "license": "MIT",
86
87
  "author": "DoNotEatMe",
87
88
  "peerDependencies": {
88
- "@deepseek-ai/cordis": "^4.0.1",
89
- "@deepseek-ai/dsh-host-webserver": "^0.1.1-rc.2",
89
+ "@deepseek-ai/cordis": "^4.0.2",
90
+ "@deepseek-ai/dsh-host-webserver": "0.1.0-rc.8 || 0.1.1-rc.2 || 0.1.2-rc.1",
90
91
  "react": "^18.2.0"
91
92
  },
92
93
  "peerDependenciesMeta": {
@@ -101,23 +102,31 @@
101
102
  }
102
103
  },
103
104
  "dependencies": {
104
- "@deepseek-ai/schemastery": "^3.18.1",
105
+ "@deepseek-ai/schemastery": "^3.18.2",
105
106
  "qrcode": "^1.5.4"
106
107
  },
107
108
  "devDependencies": {
108
- "@deepseek-ai/cordis": "^4.0.1",
109
- "@deepseek-ai/dsh-client-connection": "^0.1.1-rc.2",
110
- "@deepseek-ai/dsh-client-locale": "^0.1.1-rc.2",
111
- "@deepseek-ai/dsh-client-runtime": "^0.1.1-rc.2",
112
- "@deepseek-ai/dsh-client-ui-conversation": "^0.1.1-rc.2",
113
- "@deepseek-ai/dsh-client-ui-layout": "^0.1.1-rc.2",
114
- "@deepseek-ai/dsh-client-ui-primitives": "0.1.1-rc.2",
115
- "@deepseek-ai/dsh-client-ui-settings": "^0.1.1-rc.2",
116
- "@deepseek-ai/dsh-client-ui-sidebar": "^0.1.1-rc.2",
117
- "@deepseek-ai/dsh-client-ui-slots": "^0.1.1-rc.2",
118
- "@deepseek-ai/dsh-client-ui-theme": "^0.1.1-rc.2",
119
- "@deepseek-ai/dsh-host-webserver": "^0.1.1-rc.2",
120
- "@deepseek-ai/dsh-session-log-export": "0.1.1-rc.2",
109
+ "@deepseek-ai/cordis": "4.0.2",
110
+ "@deepseek-ai/dsh-api-session-controller": "0.1.2-rc.1",
111
+ "@deepseek-ai/dsh-attachment": "0.1.2-rc.1",
112
+ "@deepseek-ai/dsh-client-connection": "0.1.2-rc.1",
113
+ "@deepseek-ai/dsh-client-locale": "0.1.2-rc.1",
114
+ "@deepseek-ai/dsh-client-ui-conversation": "0.1.2-rc.1",
115
+ "@deepseek-ai/dsh-client-ui-layout": "0.1.2-rc.1",
116
+ "@deepseek-ai/dsh-client-ui-primitives": "0.1.2-rc.1",
117
+ "@deepseek-ai/dsh-client-ui-renderer": "0.1.2-rc.1",
118
+ "@deepseek-ai/dsh-client-ui-session": "0.1.2-rc.1",
119
+ "@deepseek-ai/dsh-client-ui-settings": "0.1.2-rc.1",
120
+ "@deepseek-ai/dsh-client-ui-sidebar": "0.1.2-rc.1",
121
+ "@deepseek-ai/dsh-client-ui-slots": "0.1.2-rc.1",
122
+ "@deepseek-ai/dsh-client-ui-theme": "0.1.2-rc.1",
123
+ "@deepseek-ai/dsh-host-webserver": "0.1.2-rc.1",
124
+ "@deepseek-ai/dsh-code-runtime": "0.1.2-rc.1",
125
+ "@deepseek-ai/dsh-session-persistence": "0.1.2-rc.1",
126
+ "@deepseek-ai/dsh-session-query": "0.1.2-rc.1",
127
+ "@deepseek-ai/dsh-session-log-export": "0.1.2-rc.1",
128
+ "@deepseek-ai/dsh-settings": "0.1.2-rc.1",
129
+ "@deepseek-ai/dsh-subagent": "0.1.2-rc.1",
121
130
  "@types/node": "^24.3.0",
122
131
  "@types/qrcode": "^1.5.5",
123
132
  "@types/react": "~18.3.1",
@@ -128,11 +137,11 @@
128
137
  },
129
138
  "repository": {
130
139
  "type": "git",
131
- "url": "git+https://github.com/DoNotEatMe/dsh-local-link.git"
140
+ "url": "git+https://github.com/donoteatme/dsh-local-link.git"
132
141
  },
133
142
  "packageManager": "pnpm@11.7.0",
134
143
  "bugs": {
135
- "url": "https://github.com/DoNotEatMe/dsh-local-link/issues"
144
+ "url": "https://github.com/donoteatme/dsh-local-link/issues"
136
145
  },
137
- "homepage": "https://github.com/DoNotEatMe/dsh-local-link#readme"
146
+ "homepage": "https://github.com/donoteatme/dsh-local-link#readme"
138
147
  }