mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 21:24:31 +00:00
TUI: honor gateway bind mode for local connection URL
This commit is contained in:
@@ -2,6 +2,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|||||||
|
|
||||||
const loadConfig = vi.fn();
|
const loadConfig = vi.fn();
|
||||||
const resolveGatewayPort = vi.fn();
|
const resolveGatewayPort = vi.fn();
|
||||||
|
const pickPrimaryTailnetIPv4 = vi.fn();
|
||||||
|
const pickPrimaryLanIPv4 = vi.fn();
|
||||||
|
|
||||||
const originalEnvToken = process.env.OPENCLAW_GATEWAY_TOKEN;
|
const originalEnvToken = process.env.OPENCLAW_GATEWAY_TOKEN;
|
||||||
const originalEnvPassword = process.env.OPENCLAW_GATEWAY_PASSWORD;
|
const originalEnvPassword = process.env.OPENCLAW_GATEWAY_PASSWORD;
|
||||||
@@ -15,13 +17,25 @@ vi.mock("../config/config.js", async (importOriginal) => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
vi.mock("../infra/tailnet.js", () => ({
|
||||||
|
pickPrimaryTailnetIPv4,
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../gateway/net.js", () => ({
|
||||||
|
pickPrimaryLanIPv4,
|
||||||
|
}));
|
||||||
|
|
||||||
const { resolveGatewayConnection } = await import("./gateway-chat.js");
|
const { resolveGatewayConnection } = await import("./gateway-chat.js");
|
||||||
|
|
||||||
describe("resolveGatewayConnection", () => {
|
describe("resolveGatewayConnection", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
loadConfig.mockReset();
|
loadConfig.mockReset();
|
||||||
resolveGatewayPort.mockReset();
|
resolveGatewayPort.mockReset();
|
||||||
|
pickPrimaryTailnetIPv4.mockReset();
|
||||||
|
pickPrimaryLanIPv4.mockReset();
|
||||||
resolveGatewayPort.mockReturnValue(18789);
|
resolveGatewayPort.mockReturnValue(18789);
|
||||||
|
pickPrimaryTailnetIPv4.mockReturnValue(undefined);
|
||||||
|
pickPrimaryLanIPv4.mockReturnValue(undefined);
|
||||||
delete process.env.OPENCLAW_GATEWAY_TOKEN;
|
delete process.env.OPENCLAW_GATEWAY_TOKEN;
|
||||||
delete process.env.OPENCLAW_GATEWAY_PASSWORD;
|
delete process.env.OPENCLAW_GATEWAY_PASSWORD;
|
||||||
});
|
});
|
||||||
@@ -77,4 +91,24 @@ describe("resolveGatewayConnection", () => {
|
|||||||
password: "explicit-password",
|
password: "explicit-password",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("uses tailnet host when local bind is tailnet", () => {
|
||||||
|
loadConfig.mockReturnValue({ gateway: { mode: "local", bind: "tailnet" } });
|
||||||
|
resolveGatewayPort.mockReturnValue(18800);
|
||||||
|
pickPrimaryTailnetIPv4.mockReturnValue("100.64.0.1");
|
||||||
|
|
||||||
|
const result = resolveGatewayConnection({});
|
||||||
|
|
||||||
|
expect(result.url).toBe("ws://100.64.0.1:18800");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses lan host when local bind is lan", () => {
|
||||||
|
loadConfig.mockReturnValue({ gateway: { mode: "local", bind: "lan" } });
|
||||||
|
resolveGatewayPort.mockReturnValue(18800);
|
||||||
|
pickPrimaryLanIPv4.mockReturnValue("192.168.1.42");
|
||||||
|
|
||||||
|
const result = resolveGatewayConnection({});
|
||||||
|
|
||||||
|
expect(result.url).toBe("ws://192.168.1.42:18800");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { loadConfig, resolveGatewayPort } from "../config/config.js";
|
import { loadConfig } from "../config/config.js";
|
||||||
import { ensureExplicitGatewayAuth, resolveExplicitGatewayAuth } from "../gateway/call.js";
|
import {
|
||||||
|
buildGatewayConnectionDetails,
|
||||||
|
ensureExplicitGatewayAuth,
|
||||||
|
resolveExplicitGatewayAuth,
|
||||||
|
} from "../gateway/call.js";
|
||||||
import { GatewayClient } from "../gateway/client.js";
|
import { GatewayClient } from "../gateway/client.js";
|
||||||
import { GATEWAY_CLIENT_CAPS } from "../gateway/protocol/client-info.js";
|
import { GATEWAY_CLIENT_CAPS } from "../gateway/protocol/client-info.js";
|
||||||
import {
|
import {
|
||||||
@@ -227,7 +231,6 @@ export function resolveGatewayConnection(opts: GatewayConnectionOptions) {
|
|||||||
const remote = isRemoteMode ? config.gateway?.remote : undefined;
|
const remote = isRemoteMode ? config.gateway?.remote : undefined;
|
||||||
const authToken = config.gateway?.auth?.token;
|
const authToken = config.gateway?.auth?.token;
|
||||||
|
|
||||||
const localPort = resolveGatewayPort(config);
|
|
||||||
const urlOverride =
|
const urlOverride =
|
||||||
typeof opts.url === "string" && opts.url.trim().length > 0 ? opts.url.trim() : undefined;
|
typeof opts.url === "string" && opts.url.trim().length > 0 ? opts.url.trim() : undefined;
|
||||||
const explicitAuth = resolveExplicitGatewayAuth({ token: opts.token, password: opts.password });
|
const explicitAuth = resolveExplicitGatewayAuth({ token: opts.token, password: opts.password });
|
||||||
@@ -236,12 +239,10 @@ export function resolveGatewayConnection(opts: GatewayConnectionOptions) {
|
|||||||
auth: explicitAuth,
|
auth: explicitAuth,
|
||||||
errorHint: "Fix: pass --token or --password when using --url.",
|
errorHint: "Fix: pass --token or --password when using --url.",
|
||||||
});
|
});
|
||||||
const url =
|
const url = buildGatewayConnectionDetails({
|
||||||
urlOverride ||
|
config,
|
||||||
(typeof remote?.url === "string" && remote.url.trim().length > 0
|
...(urlOverride ? { url: urlOverride } : {}),
|
||||||
? remote.url.trim()
|
}).url;
|
||||||
: undefined) ||
|
|
||||||
`ws://127.0.0.1:${localPort}`;
|
|
||||||
|
|
||||||
const token =
|
const token =
|
||||||
explicitAuth.token ||
|
explicitAuth.token ||
|
||||||
|
|||||||
Reference in New Issue
Block a user