JavaScript's URL API omits .port for standard ports (443 for wss:, 80 for ws:) even when explicitly specified. This caused valid URLs like wss://host.ts.net:443 to fail validation with 'Gateway URL must include an explicit port.' Fix by checking default ports when url.port is empty. Closes #148
65 lines
2.2 KiB
TypeScript
65 lines
2.2 KiB
TypeScript
import { gatewaysStatusApiV1GatewaysStatusGet } from "@/api/generated/gateways/gateways";
|
|
|
|
export const DEFAULT_WORKSPACE_ROOT = "~/.openclaw";
|
|
|
|
export type GatewayCheckStatus = "idle" | "checking" | "success" | "error";
|
|
|
|
export const validateGatewayUrl = (value: string) => {
|
|
const trimmed = value.trim();
|
|
if (!trimmed) return "Gateway URL is required.";
|
|
try {
|
|
const url = new URL(trimmed);
|
|
if (url.protocol !== "ws:" && url.protocol !== "wss:") {
|
|
return "Gateway URL must start with ws:// or wss://.";
|
|
}
|
|
// url.port is empty for default ports (80 for ws:, 443 for wss:) — allow those
|
|
const defaultPorts: Record<string, string> = { "ws:": "80", "wss:": "443" };
|
|
const effectivePort = url.port || defaultPorts[url.protocol] || "";
|
|
if (!effectivePort) {
|
|
return "Gateway URL must include an explicit port.";
|
|
}
|
|
return null;
|
|
} catch {
|
|
return "Enter a valid gateway URL including port.";
|
|
}
|
|
};
|
|
|
|
export async function checkGatewayConnection(params: {
|
|
gatewayUrl: string;
|
|
gatewayToken: string;
|
|
gatewayDisableDevicePairing: boolean;
|
|
gatewayAllowInsecureTls: boolean;
|
|
}): Promise<{ ok: boolean; message: string }> {
|
|
try {
|
|
const requestParams: {
|
|
gateway_url: string;
|
|
gateway_token?: string;
|
|
gateway_disable_device_pairing: boolean;
|
|
gateway_allow_insecure_tls: boolean;
|
|
} = {
|
|
gateway_url: params.gatewayUrl.trim(),
|
|
gateway_disable_device_pairing: params.gatewayDisableDevicePairing,
|
|
gateway_allow_insecure_tls: params.gatewayAllowInsecureTls,
|
|
};
|
|
if (params.gatewayToken.trim()) {
|
|
requestParams.gateway_token = params.gatewayToken.trim();
|
|
}
|
|
|
|
const response = await gatewaysStatusApiV1GatewaysStatusGet(requestParams);
|
|
if (response.status !== 200) {
|
|
return { ok: false, message: "Unable to reach gateway." };
|
|
}
|
|
const data = response.data;
|
|
if (!data.connected) {
|
|
return { ok: false, message: data.error ?? "Unable to reach gateway." };
|
|
}
|
|
return { ok: true, message: "Gateway reachable." };
|
|
} catch (error) {
|
|
return {
|
|
ok: false,
|
|
message:
|
|
error instanceof Error ? error.message : "Unable to reach gateway.",
|
|
};
|
|
}
|
|
}
|