





















@@ -37,7 +37,13 @@ export function resolveGatewayDiscoveryEndpoint(
3737): GatewayDiscoveryResolvedEndpoint | null {
3838const host = beacon.host?.trim();
3939const port = beacon.port;
40-if (!host || typeof port !== "number" || !Number.isFinite(port) || port <= 0) {
40+if (
41+!host ||
42+typeof port !== "number" ||
43+!Number.isSafeInteger(port) ||
44+port <= 0 ||
45+port > MAX_TCP_PORT
46+) {
4147return null;
4248}
4349const gatewayTls = beacon.gatewayTls === true;
@@ -70,6 +76,7 @@ export type GatewayBonjourDiscoverOpts = {
70767177const DEFAULT_TIMEOUT_MS = 2000;
7278const GATEWAY_SERVICE_TYPE = "_openclaw-gw._tcp";
79+const MAX_TCP_PORT = 65_535;
73807481function decodeDnsSdEscapes(value: string): string {
7582let decoded = false;
@@ -146,9 +153,9 @@ function parseDigSrv(stdout: string): { host: string; port: number } | null {
146153if (parts.length < 4) {
147154return null;
148155}
149-const port = Number.parseInt(parts[2] ?? "", 10);
156+const port = parsePortOrUndefined(parts[2]);
150157const hostRaw = parts[3] ?? "";
151-if (!Number.isFinite(port) || port <= 0) {
158+if (port === undefined) {
152159return null;
153160}
154161const host = hostRaw.replace(/\.$/, "");
@@ -193,11 +200,12 @@ function parseTailscaleStatusIPv4s(stdout: string): string[] {
193200return uniqueStrings(out);
194201}
195202196-function parseIntOrNull(value: string | undefined): number | undefined {
203+function parsePortOrUndefined(value: string | undefined): number | undefined {
197204if (!value) {
198205return undefined;
199206}
200-return parseStrictInteger(value);
207+const parsed = parseStrictInteger(value);
208+return parsed !== undefined && parsed > 0 && parsed <= MAX_TCP_PORT ? parsed : undefined;
201209}
202210203211function parseTxtTokens(tokens: string[]): Record<string, string> {
@@ -246,12 +254,12 @@ function parseDnsSdResolve(stdout: string, instanceName: string): GatewayBonjour
246254}
247255248256if (line.includes("can be reached at")) {
249-const match = line.match(/can be reached at\s+([^\s:]+):(\d+)/i);
257+const match = line.match(/can be reached at\s+([^\s:]+):([^\s]+)/i);
250258if (match?.[1]) {
251259beacon.host = match[1].replace(/\.$/, "");
252260}
253261if (match?.[2]) {
254-beacon.port = parseIntOrNull(match[2]);
262+beacon.port = parsePortOrUndefined(match[2]);
255263}
256264continue;
257265}
@@ -275,8 +283,8 @@ function parseDnsSdResolve(stdout: string, instanceName: string): GatewayBonjour
275283if (txt.cliPath) {
276284beacon.cliPath = txt.cliPath;
277285}
278-beacon.gatewayPort = parseIntOrNull(txt.gatewayPort);
279-beacon.sshPort = parseIntOrNull(txt.sshPort);
286+beacon.gatewayPort = parsePortOrUndefined(txt.gatewayPort);
287+beacon.sshPort = parsePortOrUndefined(txt.sshPort);
280288if (txt.gatewayTls) {
281289const raw = normalizeOptionalLowercaseString(txt.gatewayTls);
282290beacon.gatewayTls = raw === "1" || raw === "true" || raw === "yes";
@@ -450,8 +458,8 @@ async function discoverWideAreaViaTailnetDns(
450458host: srvParsed.host,
451459port: srvParsed.port,
452460txt: Object.keys(txtMap).length ? txtMap : undefined,
453-gatewayPort: parseIntOrNull(txtMap.gatewayPort),
454-sshPort: parseIntOrNull(txtMap.sshPort),
461+gatewayPort: parsePortOrUndefined(txtMap.gatewayPort),
462+sshPort: parsePortOrUndefined(txtMap.sshPort),
455463tailnetDns: txtMap.tailnetDns || undefined,
456464cliPath: txtMap.cliPath || undefined,
457465};
@@ -516,7 +524,7 @@ function parseAvahiBrowse(stdout: string): GatewayBonjourBeacon[] {
516524if (trimmed.startsWith("port =")) {
517525const match = trimmed.match(/port\s*=\s*\[(\d+)\]/);
518526if (match?.[1]) {
519-current.port = parseIntOrNull(match[1]);
527+current.port = parsePortOrUndefined(match[1]);
520528}
521529continue;
522530}
@@ -537,8 +545,8 @@ function parseAvahiBrowse(stdout: string): GatewayBonjourBeacon[] {
537545if (txt.cliPath) {
538546current.cliPath = txt.cliPath;
539547}
540-current.gatewayPort = parseIntOrNull(txt.gatewayPort);
541-current.sshPort = parseIntOrNull(txt.sshPort);
548+current.gatewayPort = parsePortOrUndefined(txt.gatewayPort);
549+current.sshPort = parsePortOrUndefined(txt.sshPort);
542550if (txt.gatewayTls) {
543551const raw = normalizeOptionalLowercaseString(txt.gatewayTls);
544552current.gatewayTls = raw === "1" || raw === "true" || raw === "yes";
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。