@agent-dock/cli 0.2.1 → 0.2.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/dist/cli.js +233 -75
- package/dist/cli.js.map +4 -4
- package/package.json +1 -1
- package/public/assets/{ImageLightbox-g9W9xddX.js → ImageLightbox-B7wqVGl2.js} +1 -1
- package/public/assets/{PromptEditor-CDoemeM0.js → PromptEditor-Dq4OTHIi.js} +1 -1
- package/public/assets/index-Bvc0m8pa.css +1 -0
- package/public/assets/{main-BHjClXhJ.js → main-CzCxk59S.js} +241 -231
- package/public/assets/{sourcesRouter-BlYcFM8d.js → sourcesRouter-D7WM5uz_.js} +1 -1
- package/public/index.html +3 -3
- package/public/sources-router.html +3 -3
- package/public/assets/index-DY-CqbRH.css +0 -1
- /package/public/assets/{index-RVKphYoh.js → index-DFjomhpK.js} +0 -0
package/dist/cli.js
CHANGED
|
@@ -13672,6 +13672,7 @@ var SelfUpdateService = class {
|
|
|
13672
13672
|
cached;
|
|
13673
13673
|
checking;
|
|
13674
13674
|
updating = false;
|
|
13675
|
+
updateStartedAt;
|
|
13675
13676
|
lastError;
|
|
13676
13677
|
env;
|
|
13677
13678
|
lockFile;
|
|
@@ -13679,7 +13680,27 @@ var SelfUpdateService = class {
|
|
|
13679
13680
|
get distTag() {
|
|
13680
13681
|
return distTagForVersion(this.options.currentVersion);
|
|
13681
13682
|
}
|
|
13683
|
+
/**
|
|
13684
|
+
* 桌面壳把打包好的 CLI 当普通前台子进程 spawn,但它自己就是 supervisor。
|
|
13685
|
+
* 先判 desktop:Electron 从终端继承环境时可能带上 AGENTDOCK_DAEMONIZED,
|
|
13686
|
+
* 把桌面实例误认成 daemon 会放开一条完全错误(而且危险)的 npm 升级路径。
|
|
13687
|
+
*/
|
|
13688
|
+
get runtime() {
|
|
13689
|
+
if (this.env.AGENTDOCK_DESKTOP_INSTANCE?.trim()) return "desktop";
|
|
13690
|
+
return isDaemonChild(this.env) ? "daemon" : "foreground";
|
|
13691
|
+
}
|
|
13682
13692
|
async status(refresh = false) {
|
|
13693
|
+
const runtime = this.runtime;
|
|
13694
|
+
if (runtime === "desktop") {
|
|
13695
|
+
return {
|
|
13696
|
+
currentVersion: this.options.currentVersion,
|
|
13697
|
+
distTag: this.distTag,
|
|
13698
|
+
updateAvailable: false,
|
|
13699
|
+
canUpdate: false,
|
|
13700
|
+
updating: false,
|
|
13701
|
+
runtime
|
|
13702
|
+
};
|
|
13703
|
+
}
|
|
13683
13704
|
const age = this.cached ? this.now() - this.cached.checkedAt : Infinity;
|
|
13684
13705
|
const ttl = this.cached?.checkError ? CHECK_ERROR_TTL_MS : CHECK_TTL_MS;
|
|
13685
13706
|
if (refresh || age > ttl) await (this.checking ??= this.check().finally(() => {
|
|
@@ -13691,13 +13712,19 @@ var SelfUpdateService = class {
|
|
|
13691
13712
|
distTag: this.distTag,
|
|
13692
13713
|
...latestVersion ? { latestVersion } : {},
|
|
13693
13714
|
updateAvailable: Boolean(latestVersion && compareVersions(latestVersion, this.options.currentVersion) > 0),
|
|
13694
|
-
canUpdate:
|
|
13695
|
-
updating: this.
|
|
13715
|
+
canUpdate: runtime === "daemon",
|
|
13716
|
+
updating: this.updateActive() || this.lockHeld(),
|
|
13717
|
+
runtime,
|
|
13696
13718
|
...this.lastError ? { lastError: this.lastError } : {},
|
|
13697
13719
|
...this.cached ? { checkedAt: this.cached.checkedAt } : {},
|
|
13698
13720
|
...this.cached?.checkError ? { checkError: this.cached.checkError } : {}
|
|
13699
13721
|
};
|
|
13700
13722
|
}
|
|
13723
|
+
/** 超过升级最长时限的「进行中」标记视为过期,避免子进程卡死时页面永远转圈。 */
|
|
13724
|
+
updateActive() {
|
|
13725
|
+
if (!this.updating) return false;
|
|
13726
|
+
return this.updateStartedAt !== void 0 && this.now() - this.updateStartedAt < UPDATE_LOCK_MAX_AGE_MS;
|
|
13727
|
+
}
|
|
13701
13728
|
async check() {
|
|
13702
13729
|
const runNpm = this.options.runNpm ?? runNpmForAgentDockUpdate;
|
|
13703
13730
|
try {
|
|
@@ -13740,11 +13767,16 @@ var SelfUpdateService = class {
|
|
|
13740
13767
|
}
|
|
13741
13768
|
}
|
|
13742
13769
|
async start() {
|
|
13743
|
-
|
|
13744
|
-
if (
|
|
13745
|
-
|
|
13746
|
-
if (!
|
|
13770
|
+
const runtime = this.runtime;
|
|
13771
|
+
if (runtime === "desktop") throw new RuntimeError("UPDATE_DESKTOP_MANAGED", "\u684C\u9762\u7248\u7531\u5E94\u7528\u5185\u81EA\u52A8\u66F4\u65B0\u8D1F\u8D23\uFF0C\u8BF7\u5728\u4FA7\u8FB9\u680F\u7684\u66F4\u65B0\u5165\u53E3\u5347\u7EA7", 409);
|
|
13772
|
+
if (runtime !== "daemon") throw new RuntimeError("UPDATE_REQUIRES_DAEMON", "\u5F53\u524D\u662F\u524D\u53F0\u8FD0\u884C\uFF08agent-dock serve\uFF09\uFF0C\u754C\u9762\u5347\u7EA7\u540E\u65E0\u6CD5\u81EA\u52A8\u62C9\u8D77\u65B0\u7248\u672C\uFF1B\u8BF7\u5148\u505C\u6B62\u524D\u53F0\u8FDB\u7A0B\uFF0C\u518D\u5728\u7EC8\u7AEF\u6267\u884C npm install -g \u6216\u6539\u7528 agent-dock start", 409);
|
|
13773
|
+
if (!this.updateActive() && this.updating) {
|
|
13774
|
+
this.updating = false;
|
|
13775
|
+
this.updateStartedAt = void 0;
|
|
13776
|
+
}
|
|
13777
|
+
if (this.updateActive() || this.lockHeld()) throw new RuntimeError("UPDATE_IN_PROGRESS", "\u5DF2\u6709\u5347\u7EA7\u5728\u8FDB\u884C\uFF0C\u7A0D\u7B49\u7247\u523B", 409);
|
|
13747
13778
|
this.updating = true;
|
|
13779
|
+
this.updateStartedAt = this.now();
|
|
13748
13780
|
this.lastError = void 0;
|
|
13749
13781
|
const cwd = this.env[DAEMON_CWD_ENV] ?? process.cwd();
|
|
13750
13782
|
const logFile = join8(cwd, ".agentdock", "daemon", "update.log");
|
|
@@ -13753,11 +13785,21 @@ var SelfUpdateService = class {
|
|
|
13753
13785
|
delete env[DAEMON_CWD_ENV];
|
|
13754
13786
|
delete env[DAEMON_STARTED_AT_ENV];
|
|
13755
13787
|
try {
|
|
13788
|
+
const status = await this.status(true);
|
|
13789
|
+
if (!status.updateAvailable) throw new RuntimeError("UPDATE_NOT_AVAILABLE", status.checkError ?? `\u5DF2\u662F\u6700\u65B0\u7248\u672C\uFF08${status.currentVersion}\uFF09`, 409);
|
|
13756
13790
|
const child = (this.options.spawnUpdate ?? defaultSpawnUpdate)([this.options.cliEntrypoint, "update", "--dist-tag", this.distTag], { cwd, env, logFile });
|
|
13757
|
-
if (!child.pid
|
|
13791
|
+
if (!child.pid) throw new Error("\u65E0\u6CD5\u542F\u52A8\u5347\u7EA7\u8FDB\u7A0B");
|
|
13792
|
+
if (!this.acquireLock(child.pid)) {
|
|
13793
|
+
try {
|
|
13794
|
+
process.kill(child.pid, "SIGTERM");
|
|
13795
|
+
} catch {
|
|
13796
|
+
}
|
|
13797
|
+
throw new Error("\u53E6\u4E00\u4E2A\u5B9E\u4F8B\u5DF2\u7ECF\u5728\u5347\u7EA7");
|
|
13798
|
+
}
|
|
13758
13799
|
const lockedPid = child.pid;
|
|
13759
13800
|
child.onExit((code) => {
|
|
13760
13801
|
this.updating = false;
|
|
13802
|
+
this.updateStartedAt = void 0;
|
|
13761
13803
|
try {
|
|
13762
13804
|
if (readFileSync5(this.lockFile, "utf8").trim() === String(lockedPid)) rmSync2(this.lockFile, { force: true });
|
|
13763
13805
|
} catch {
|
|
@@ -13766,6 +13808,8 @@ var SelfUpdateService = class {
|
|
|
13766
13808
|
});
|
|
13767
13809
|
} catch (error) {
|
|
13768
13810
|
this.updating = false;
|
|
13811
|
+
this.updateStartedAt = void 0;
|
|
13812
|
+
if (error instanceof RuntimeError) throw error;
|
|
13769
13813
|
throw new RuntimeError("UPDATE_FAILED", error instanceof Error ? error.message : String(error), 500);
|
|
13770
13814
|
}
|
|
13771
13815
|
return this.status();
|
|
@@ -13920,6 +13964,7 @@ function isLoopbackHostHeader(host) {
|
|
|
13920
13964
|
}
|
|
13921
13965
|
function isLocalOwnerRequest(input) {
|
|
13922
13966
|
if (!isLoopbackAddress(input.address) || !isLoopbackHostHeader(input.host)) return false;
|
|
13967
|
+
if (input.forwardedFor?.trim()) return false;
|
|
13923
13968
|
const site = input.fetchSite?.trim().toLowerCase();
|
|
13924
13969
|
return site === void 0 || site === "" || site === "same-origin" || site === "none";
|
|
13925
13970
|
}
|
|
@@ -19569,6 +19614,172 @@ async function registerSystemRoutes(app, options = {}) {
|
|
|
19569
19614
|
});
|
|
19570
19615
|
}
|
|
19571
19616
|
|
|
19617
|
+
// src/share-link.ts
|
|
19618
|
+
init_src2();
|
|
19619
|
+
|
|
19620
|
+
// src/access-urls.ts
|
|
19621
|
+
import { hostname as osHostname, networkInterfaces } from "node:os";
|
|
19622
|
+
function isLoopbackV4(address) {
|
|
19623
|
+
const parts = address.split(".");
|
|
19624
|
+
return parts.length === 4 && parts[0] === "127";
|
|
19625
|
+
}
|
|
19626
|
+
function isLoopbackV6(address) {
|
|
19627
|
+
return address.toLowerCase() === "::1";
|
|
19628
|
+
}
|
|
19629
|
+
function isLinkLocalV6(address) {
|
|
19630
|
+
return /^fe[89ab]/.test(address.toLowerCase().split(":")[0] ?? "");
|
|
19631
|
+
}
|
|
19632
|
+
function bindsDualStack(host) {
|
|
19633
|
+
return host === "0.0.0.0" || host === "::";
|
|
19634
|
+
}
|
|
19635
|
+
function bindsLan(host) {
|
|
19636
|
+
return bindsDualStack(host) || !isLoopbackV4(host) && !isLoopbackV6(host) && host !== "localhost";
|
|
19637
|
+
}
|
|
19638
|
+
function origin(host, port) {
|
|
19639
|
+
return `http://${host.includes(":") ? `[${host}]` : host}:${port}`;
|
|
19640
|
+
}
|
|
19641
|
+
function withCredential(url, token) {
|
|
19642
|
+
return token ? `${url}/?t=${encodeURIComponent(token)}` : `${url}/`;
|
|
19643
|
+
}
|
|
19644
|
+
function hostnameEntry(hostname) {
|
|
19645
|
+
const name = (hostname ?? osHostname()).trim().replace(/\.$/, "");
|
|
19646
|
+
if (!name || isLoopbackV4(name) || isLoopbackV6(name) || name.toLowerCase() === "localhost") return void 0;
|
|
19647
|
+
return name;
|
|
19648
|
+
}
|
|
19649
|
+
function accessUrls(input) {
|
|
19650
|
+
const { host, port, token } = input;
|
|
19651
|
+
const locals = [{ kind: "local", url: origin("127.0.0.1", port) }];
|
|
19652
|
+
if (bindsDualStack(host)) locals.push({ kind: "local", url: origin("::1", port) });
|
|
19653
|
+
const lanV4 = [];
|
|
19654
|
+
const lanV6 = [];
|
|
19655
|
+
let stableHostname;
|
|
19656
|
+
if (bindsLan(host)) {
|
|
19657
|
+
stableHostname = hostnameEntry(input.hostname);
|
|
19658
|
+
const seen = /* @__PURE__ */ new Set();
|
|
19659
|
+
const candidates = [bindsDualStack(host) ? "" : host, ...Object.values(input.interfaces ?? networkInterfaces()).flat().map((info) => info?.address ?? "")];
|
|
19660
|
+
for (const address of candidates) {
|
|
19661
|
+
if (!address || seen.has(address)) continue;
|
|
19662
|
+
seen.add(address);
|
|
19663
|
+
if (isLoopbackV4(address) || isLoopbackV6(address)) continue;
|
|
19664
|
+
if (address.includes(":")) {
|
|
19665
|
+
if (!isLinkLocalV6(address)) lanV6.push(address);
|
|
19666
|
+
} else {
|
|
19667
|
+
lanV4.push(address);
|
|
19668
|
+
}
|
|
19669
|
+
}
|
|
19670
|
+
}
|
|
19671
|
+
return [
|
|
19672
|
+
...locals.map((entry) => ({ ...entry, url: withCredential(entry.url, token) })),
|
|
19673
|
+
...stableHostname ? [{ kind: "hostname", url: withCredential(origin(stableHostname, port), token) }] : [],
|
|
19674
|
+
...lanV4.map((address) => ({ kind: "lan-v4", url: withCredential(origin(address, port), token) })),
|
|
19675
|
+
...lanV6.map((address) => ({ kind: "lan-v6", url: withCredential(origin(address, port), token) }))
|
|
19676
|
+
];
|
|
19677
|
+
}
|
|
19678
|
+
|
|
19679
|
+
// src/share-link.ts
|
|
19680
|
+
var publicUrlConfigKey = "web.public_url";
|
|
19681
|
+
var labels = {
|
|
19682
|
+
hostname: "\u4E3B\u673A\u540D",
|
|
19683
|
+
"lan-v4": "\u5C40\u57DF\u7F51 IPv4",
|
|
19684
|
+
"lan-v6": "\u5C40\u57DF\u7F51 IPv6",
|
|
19685
|
+
public: "\u516C\u7F51"
|
|
19686
|
+
};
|
|
19687
|
+
function normalizePublicUrl(raw) {
|
|
19688
|
+
const value = raw?.trim();
|
|
19689
|
+
if (!value) return void 0;
|
|
19690
|
+
let url;
|
|
19691
|
+
try {
|
|
19692
|
+
url = new URL(value);
|
|
19693
|
+
} catch {
|
|
19694
|
+
return void 0;
|
|
19695
|
+
}
|
|
19696
|
+
if (url.protocol !== "https:") return void 0;
|
|
19697
|
+
if (url.username || url.password) return void 0;
|
|
19698
|
+
if (!url.hostname) return void 0;
|
|
19699
|
+
if (url.pathname !== "/" && url.pathname !== "") return void 0;
|
|
19700
|
+
url.search = "";
|
|
19701
|
+
url.hash = "";
|
|
19702
|
+
return url.origin;
|
|
19703
|
+
}
|
|
19704
|
+
function bindsLan2(host) {
|
|
19705
|
+
return host !== "127.0.0.1" && host !== "::1" && host.toLowerCase() !== "localhost";
|
|
19706
|
+
}
|
|
19707
|
+
var ShareLinkService = class {
|
|
19708
|
+
constructor(options) {
|
|
19709
|
+
this.options = options;
|
|
19710
|
+
this.now = options.now ?? Date.now;
|
|
19711
|
+
}
|
|
19712
|
+
now;
|
|
19713
|
+
get lanEnabled() {
|
|
19714
|
+
return bindsLan2(this.options.host);
|
|
19715
|
+
}
|
|
19716
|
+
async storedPublicUrl() {
|
|
19717
|
+
const fromEnv = normalizePublicUrl(this.options.env?.AGENTDOCK_PUBLIC_URL);
|
|
19718
|
+
if (fromEnv) return { url: fromEnv, source: "env" };
|
|
19719
|
+
const stored = normalizePublicUrl(await this.options.config?.get(publicUrlConfigKey));
|
|
19720
|
+
return stored ? { url: stored, source: "config" } : void 0;
|
|
19721
|
+
}
|
|
19722
|
+
async settings() {
|
|
19723
|
+
const publicUrl = await this.storedPublicUrl();
|
|
19724
|
+
return {
|
|
19725
|
+
...publicUrl ? { publicUrl: publicUrl.url, publicUrlSource: publicUrl.source } : {},
|
|
19726
|
+
envLocked: Boolean(normalizePublicUrl(this.options.env?.AGENTDOCK_PUBLIC_URL))
|
|
19727
|
+
};
|
|
19728
|
+
}
|
|
19729
|
+
/** 空串表示清除。非字符串或非 https 地址一律 400,避免把明文 http 公网地址写进二维码。 */
|
|
19730
|
+
async savePublicUrl(raw) {
|
|
19731
|
+
if (typeof raw !== "string") throw new RuntimeError("INVALID_PUBLIC_URL", "publicUrl \u5FC5\u987B\u662F\u5B57\u7B26\u4E32", 400);
|
|
19732
|
+
const trimmed = raw.trim();
|
|
19733
|
+
if (!trimmed) {
|
|
19734
|
+
await this.options.config?.set(publicUrlConfigKey, "");
|
|
19735
|
+
return this.settings();
|
|
19736
|
+
}
|
|
19737
|
+
const normalized = normalizePublicUrl(trimmed);
|
|
19738
|
+
if (!normalized) throw new RuntimeError("INVALID_PUBLIC_URL", "\u516C\u7F51\u5730\u5740\u5FC5\u987B\u662F\u65E0\u7528\u6237\u540D\u5BC6\u7801\u7684 https:// \u5730\u5740\uFF0C\u4F8B\u5982 https://dock.example.com", 400);
|
|
19739
|
+
await this.options.config?.set(publicUrlConfigKey, normalized);
|
|
19740
|
+
return this.settings();
|
|
19741
|
+
}
|
|
19742
|
+
async build(sessionId) {
|
|
19743
|
+
if (!this.options.secret) {
|
|
19744
|
+
throw new RuntimeError("SHARE_LINK_DISABLED", "\u672C\u5730 Web \u8BBF\u95EE\u9632\u62A4\u672A\u5F00\u542F\uFF0C\u65E0\u6CD5\u7B7E\u53D1\u626B\u7801\u94FE\u63A5\u3002\u684C\u9762\u5BA2\u6237\u7AEF\u8BF7\u5728\u300C\u5728\u624B\u673A\u4E0A\u6253\u5F00\u300D\u91CC\u5F00\u542F\u5C40\u57DF\u7F51\u8BBF\u95EE\uFF1B\u547D\u4EE4\u884C\u8BF7\u53BB\u6389 AGENTDOCK_WEB_AUTH=off\u3002", 409);
|
|
19745
|
+
}
|
|
19746
|
+
const token = issueMasterToken(this.options.secret, this.options.masterTokenTtlMs ?? defaultMasterTokenTtlMs);
|
|
19747
|
+
const query = `?t=${encodeURIComponent(token)}`;
|
|
19748
|
+
const suffix = `${sessionId ? `/sessions/${encodeURIComponent(sessionId)}` : "/"}${query}`;
|
|
19749
|
+
const urls = accessUrls({
|
|
19750
|
+
host: this.options.host,
|
|
19751
|
+
port: this.options.port,
|
|
19752
|
+
...this.options.interfaces ? { interfaces: this.options.interfaces } : {},
|
|
19753
|
+
...this.options.hostname ? { hostname: this.options.hostname } : {}
|
|
19754
|
+
}).filter((entry) => entry.kind !== "local").map((entry) => ({ kind: entry.kind, label: labels[entry.kind], url: `${entry.url.replace(/\/$/, "")}${suffix}` }));
|
|
19755
|
+
const publicUrl = await this.storedPublicUrl();
|
|
19756
|
+
if (publicUrl) urls.push({ kind: "public", label: labels.public, url: `${publicUrl.url}${suffix}` });
|
|
19757
|
+
return {
|
|
19758
|
+
...sessionId ? { sessionId } : {},
|
|
19759
|
+
expiresAt: this.now() + (this.options.masterTokenTtlMs ?? defaultMasterTokenTtlMs),
|
|
19760
|
+
urls,
|
|
19761
|
+
...publicUrl ? { publicUrl: publicUrl.url, publicUrlSource: publicUrl.source } : {},
|
|
19762
|
+
lanEnabled: this.lanEnabled
|
|
19763
|
+
};
|
|
19764
|
+
}
|
|
19765
|
+
};
|
|
19766
|
+
function registerShareLinkRoutes(app, service, runtime) {
|
|
19767
|
+
app.get("/api/system/share-link", async (request, reply) => {
|
|
19768
|
+
const sessionId = request.query.sessionId?.trim() || void 0;
|
|
19769
|
+
if (sessionId && !await runtime.getSession(sessionId)) throw new RuntimeError("SESSION_NOT_FOUND", `Session not found: ${sessionId}`, 404);
|
|
19770
|
+
reply.header("cache-control", "no-store");
|
|
19771
|
+
return service.build(sessionId);
|
|
19772
|
+
});
|
|
19773
|
+
app.get("/api/system/share-settings", async (_request, reply) => {
|
|
19774
|
+
reply.header("cache-control", "no-store");
|
|
19775
|
+
return service.settings();
|
|
19776
|
+
});
|
|
19777
|
+
app.put("/api/system/share-settings", async (request, reply) => {
|
|
19778
|
+
reply.header("cache-control", "no-store");
|
|
19779
|
+
return service.savePublicUrl(request.body?.publicUrl);
|
|
19780
|
+
});
|
|
19781
|
+
}
|
|
19782
|
+
|
|
19572
19783
|
// src/schedules/routes.ts
|
|
19573
19784
|
function registerScheduleRoutes(app, schedules2) {
|
|
19574
19785
|
app.get("/api/schedules", async (request) => {
|
|
@@ -20027,7 +20238,8 @@ function registerTerminalWebSockets(server, runtime, options = {}) {
|
|
|
20027
20238
|
const secret = options.webAuth.secret;
|
|
20028
20239
|
const queryClaim = verifyWebToken(url.searchParams.get("t") ?? void 0, secret);
|
|
20029
20240
|
const presented = queryClaim ?? verifyWebToken(parseCookieHeader(request.headers.cookie, webAccessCookieName), secret) ?? verifyWebToken(parseCookieHeader(request.headers.cookie, webAccessSessionCookieName), secret);
|
|
20030
|
-
const
|
|
20241
|
+
const forwardedFor = request.headers["x-forwarded-for"];
|
|
20242
|
+
const localOwner = isLocalOwnerRequest({ address: request.socket.remoteAddress, host: request.headers.host, fetchSite: request.headers["sec-fetch-site"], forwardedFor: Array.isArray(forwardedFor) ? forwardedFor.join(",") : forwardedFor });
|
|
20031
20243
|
const claim = !queryClaim && localOwner && presented?.kind !== "master" ? verifyWebToken(issueMasterToken(secret), secret) : presented;
|
|
20032
20244
|
if (!claim) return reject(socket, 401, '{"error":{"code":"UNAUTHORIZED","message":"\u8BBF\u95EE\u51ED\u8BC1\u7F3A\u5931\u6216\u5DF2\u8FC7\u671F\u3002"}}');
|
|
20033
20245
|
if (claim.kind === "session" && !sessionTokenAllows("GET", url.pathname, claim.sessionId)) {
|
|
@@ -21221,9 +21433,10 @@ async function buildApp(runtime, options = {}) {
|
|
|
21221
21433
|
request.webClaim = null;
|
|
21222
21434
|
const url = new URL(request.url, "http://agent-dock.local");
|
|
21223
21435
|
const queryToken = url.searchParams.get("t") ?? void 0;
|
|
21436
|
+
const forwardedHttps = request.headers["x-forwarded-proto"] === "https";
|
|
21224
21437
|
const setCredentialCookie = (token, value) => {
|
|
21225
21438
|
const maxAge = Math.max(0, Math.floor((value.expiresAt - Date.now()) / 1e3));
|
|
21226
|
-
reply.header("set-cookie", `${webAccessCookieNameFor(value.kind)}=${encodeURIComponent(token)}; HttpOnly; SameSite=Lax; Path=/; Max-Age=${maxAge}`);
|
|
21439
|
+
reply.header("set-cookie", `${webAccessCookieNameFor(value.kind)}=${encodeURIComponent(token)}; HttpOnly; SameSite=Lax; Path=/; Max-Age=${maxAge}${forwardedHttps ? "; Secure" : ""}`);
|
|
21227
21440
|
};
|
|
21228
21441
|
let claim = null;
|
|
21229
21442
|
let explicitCredential = false;
|
|
@@ -21243,7 +21456,8 @@ async function buildApp(runtime, options = {}) {
|
|
|
21243
21456
|
if (isSourceIngressRequest(request.method, url.pathname)) return;
|
|
21244
21457
|
if (url.pathname.startsWith("/api/lark/agent-tools/")) return;
|
|
21245
21458
|
claim ??= verifyWebToken(parseCookieHeader(request.headers.cookie, webAccessCookieName), secret) ?? verifyWebToken(parseCookieHeader(request.headers.cookie, webAccessSessionCookieName), secret);
|
|
21246
|
-
|
|
21459
|
+
const forwardedFor = request.headers["x-forwarded-for"];
|
|
21460
|
+
if (!explicitCredential && claim?.kind !== "master" && isLocalOwnerRequest({ address: request.ip, host: request.headers.host, fetchSite: request.headers["sec-fetch-site"], forwardedFor: Array.isArray(forwardedFor) ? forwardedFor.join(",") : forwardedFor })) {
|
|
21247
21461
|
const token = issueMasterToken(secret);
|
|
21248
21462
|
const localClaim = verifyWebToken(token, secret);
|
|
21249
21463
|
if (localClaim) {
|
|
@@ -21279,7 +21493,8 @@ async function buildApp(runtime, options = {}) {
|
|
|
21279
21493
|
const token = issueMasterToken(secret);
|
|
21280
21494
|
const claim = verifyWebToken(token, secret);
|
|
21281
21495
|
const maxAge = Math.max(0, Math.floor((claim.expiresAt - Date.now()) / 1e3));
|
|
21282
|
-
|
|
21496
|
+
const secure = request.headers["x-forwarded-proto"] === "https" ? "; Secure" : "";
|
|
21497
|
+
reply.header("set-cookie", `${webAccessCookieName}=${encodeURIComponent(token)}; HttpOnly; SameSite=Lax; Path=/; Max-Age=${maxAge}${secure}`);
|
|
21283
21498
|
return { ok: true };
|
|
21284
21499
|
});
|
|
21285
21500
|
}
|
|
@@ -21308,6 +21523,7 @@ async function buildApp(runtime, options = {}) {
|
|
|
21308
21523
|
return value;
|
|
21309
21524
|
});
|
|
21310
21525
|
await registerSystemRoutes(app, options.system);
|
|
21526
|
+
if (options.shareLinks) registerShareLinkRoutes(app, options.shareLinks, runtime);
|
|
21311
21527
|
if (options.selfUpdate) registerSelfUpdateRoutes(app, options.selfUpdate);
|
|
21312
21528
|
if (options.schedules) registerScheduleRoutes(app, options.schedules);
|
|
21313
21529
|
if (options.sourcesRouter) registerSourceRouterRoutes(app, options.sourcesRouter);
|
|
@@ -23650,7 +23866,7 @@ async function startLocalServer(options = {}) {
|
|
|
23650
23866
|
const ttlDays = Number(env.AGENTDOCK_WEB_TOKEN_TTL_DAYS);
|
|
23651
23867
|
const sessionTokenTtlMs = Number.isFinite(ttlDays) && ttlDays > 0 ? ttlDays * 24 * 60 * 6e4 : defaultSessionTokenTtlMs;
|
|
23652
23868
|
initWebAccess(secret, sessionTokenTtlMs);
|
|
23653
|
-
webAuth = { enabled: true, secret };
|
|
23869
|
+
webAuth = { enabled: true, secret, sessionTokenTtlMs };
|
|
23654
23870
|
}
|
|
23655
23871
|
const webRoot = options.webRoot ?? fileURLToPath5(new URL("../public", import.meta.url));
|
|
23656
23872
|
const llm = new LlmEndpointService({ config: repos.config });
|
|
@@ -23664,7 +23880,8 @@ async function startLocalServer(options = {}) {
|
|
|
23664
23880
|
}) : void 0;
|
|
23665
23881
|
sourcesRouter = new SourceRouterService({ databaseUrl: config.databaseUrl, runtime, env, routerDriver, startRouterPoll: false });
|
|
23666
23882
|
const autostart = options.cliEntrypoint ? new AutostartService({ entrypoint: options.cliEntrypoint, env }) : void 0;
|
|
23667
|
-
|
|
23883
|
+
const shareLinks = new ShareLinkService({ host: config.host, port: config.port, secret: webAuth.secret ?? "", config: repos.config, env });
|
|
23884
|
+
app = await buildApp(runtime, { webRoot, webAuth, shareLinks, schedules: schedules2, sourcesRouter, ...selfUpdate ? { selfUpdate } : {}, ...autostart ? { system: { autostart } } : {}, llm: { service: llm }, lark: { env, config: repos.config, agents: repos.agents, cardMappings: repos.channelMappings, runtime, agentTools, listeningDisabled: env.AGENTDOCK_DISABLE_LARK_LISTENER === "true" } });
|
|
23668
23885
|
await app.listen(listenOptions(config));
|
|
23669
23886
|
schedules2.start();
|
|
23670
23887
|
sourcesRouter.start();
|
|
@@ -24069,11 +24286,11 @@ function padToWidth(text2, width) {
|
|
|
24069
24286
|
function formatAccessUrls(result, options = {}) {
|
|
24070
24287
|
const urls = result.webAccessUrls?.length ? result.webAccessUrls : result.webConsoleUrl ? [{ kind: "local", url: result.webConsoleUrl }] : [];
|
|
24071
24288
|
if (!urls.length) return [];
|
|
24072
|
-
const
|
|
24073
|
-
const width = Math.max(...
|
|
24289
|
+
const labels2 = urls.map((entry) => kindLabels[entry.kind]);
|
|
24290
|
+
const width = Math.max(...labels2.map(displayWidth));
|
|
24074
24291
|
return [
|
|
24075
24292
|
"\u63A7\u5236\u53F0\uFF08\u542B\u8BBF\u95EE\u51ED\u8BC1\uFF0C\u8BF7\u52FF\u5916\u4F20\uFF09\uFF1A",
|
|
24076
|
-
...urls.map((entry, index) => ` ${padToWidth(
|
|
24293
|
+
...urls.map((entry, index) => ` ${padToWidth(labels2[index], width)} ${options.interactive ? "\u2197 " : ""}${terminalLink(entry.url, options.interactive ?? false)}`)
|
|
24077
24294
|
];
|
|
24078
24295
|
}
|
|
24079
24296
|
function formatDaemonStatus(status, options = {}) {
|
|
@@ -24115,65 +24332,6 @@ function formatDaemonCommandResult(result, options = {}) {
|
|
|
24115
24332
|
return lines.join("\n");
|
|
24116
24333
|
}
|
|
24117
24334
|
|
|
24118
|
-
// src/access-urls.ts
|
|
24119
|
-
import { hostname as osHostname, networkInterfaces } from "node:os";
|
|
24120
|
-
function isLoopbackV4(address) {
|
|
24121
|
-
const parts = address.split(".");
|
|
24122
|
-
return parts.length === 4 && parts[0] === "127";
|
|
24123
|
-
}
|
|
24124
|
-
function isLoopbackV6(address) {
|
|
24125
|
-
return address.toLowerCase() === "::1";
|
|
24126
|
-
}
|
|
24127
|
-
function isLinkLocalV6(address) {
|
|
24128
|
-
return /^fe[89ab]/.test(address.toLowerCase().split(":")[0] ?? "");
|
|
24129
|
-
}
|
|
24130
|
-
function bindsDualStack(host) {
|
|
24131
|
-
return host === "0.0.0.0" || host === "::";
|
|
24132
|
-
}
|
|
24133
|
-
function bindsLan(host) {
|
|
24134
|
-
return bindsDualStack(host) || !isLoopbackV4(host) && !isLoopbackV6(host) && host !== "localhost";
|
|
24135
|
-
}
|
|
24136
|
-
function origin(host, port) {
|
|
24137
|
-
return `http://${host.includes(":") ? `[${host}]` : host}:${port}`;
|
|
24138
|
-
}
|
|
24139
|
-
function withCredential(url, token) {
|
|
24140
|
-
return token ? `${url}/?t=${encodeURIComponent(token)}` : `${url}/`;
|
|
24141
|
-
}
|
|
24142
|
-
function hostnameEntry(hostname) {
|
|
24143
|
-
const name = (hostname ?? osHostname()).trim().replace(/\.$/, "");
|
|
24144
|
-
if (!name || isLoopbackV4(name) || isLoopbackV6(name) || name.toLowerCase() === "localhost") return void 0;
|
|
24145
|
-
return name;
|
|
24146
|
-
}
|
|
24147
|
-
function accessUrls(input) {
|
|
24148
|
-
const { host, port, token } = input;
|
|
24149
|
-
const locals = [{ kind: "local", url: origin("127.0.0.1", port) }];
|
|
24150
|
-
if (bindsDualStack(host)) locals.push({ kind: "local", url: origin("::1", port) });
|
|
24151
|
-
const lanV4 = [];
|
|
24152
|
-
const lanV6 = [];
|
|
24153
|
-
let stableHostname;
|
|
24154
|
-
if (bindsLan(host)) {
|
|
24155
|
-
stableHostname = hostnameEntry(input.hostname);
|
|
24156
|
-
const seen = /* @__PURE__ */ new Set();
|
|
24157
|
-
const candidates = [bindsDualStack(host) ? "" : host, ...Object.values(input.interfaces ?? networkInterfaces()).flat().map((info) => info?.address ?? "")];
|
|
24158
|
-
for (const address of candidates) {
|
|
24159
|
-
if (!address || seen.has(address)) continue;
|
|
24160
|
-
seen.add(address);
|
|
24161
|
-
if (isLoopbackV4(address) || isLoopbackV6(address)) continue;
|
|
24162
|
-
if (address.includes(":")) {
|
|
24163
|
-
if (!isLinkLocalV6(address)) lanV6.push(address);
|
|
24164
|
-
} else {
|
|
24165
|
-
lanV4.push(address);
|
|
24166
|
-
}
|
|
24167
|
-
}
|
|
24168
|
-
}
|
|
24169
|
-
return [
|
|
24170
|
-
...locals.map((entry) => ({ ...entry, url: withCredential(entry.url, token) })),
|
|
24171
|
-
...stableHostname ? [{ kind: "hostname", url: withCredential(origin(stableHostname, port), token) }] : [],
|
|
24172
|
-
...lanV4.map((address) => ({ kind: "lan-v4", url: withCredential(origin(address, port), token) })),
|
|
24173
|
-
...lanV6.map((address) => ({ kind: "lan-v6", url: withCredential(origin(address, port), token) }))
|
|
24174
|
-
];
|
|
24175
|
-
}
|
|
24176
|
-
|
|
24177
24335
|
// src/cli.ts
|
|
24178
24336
|
init_daemon();
|
|
24179
24337
|
|