mirror of
https://github.com/hanzoai/dev.git
synced 2026-11-21 09:59:11 +00:00
103 lines
3.0 KiB
TypeScript
103 lines
3.0 KiB
TypeScript
import type {
|
|
RelayEnvironmentLinkProofPayload,
|
|
RelayLinkProofRequest,
|
|
RelayManagedEndpointOrigin,
|
|
RelayManagedEndpointRuntimeConfig,
|
|
} from "@hanzodev/contracts/relay";
|
|
|
|
const CLOUD_PROOF_MAX_LIFETIME_SECONDS = 5 * 60;
|
|
const CLOUD_PROOF_CLOCK_SKEW_SECONDS = 60;
|
|
const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]);
|
|
|
|
function normalizeHostname(hostname: string): string {
|
|
return hostname
|
|
.trim()
|
|
.toLowerCase()
|
|
.replace(/^\[(.*)\]$/, "$1");
|
|
}
|
|
|
|
function isLoopbackHostname(hostname: string): boolean {
|
|
return LOOPBACK_HOSTNAMES.has(normalizeHostname(hostname));
|
|
}
|
|
|
|
function endpointRequestPort(url: URL): number {
|
|
return Number(url.port || (url.protocol === "https:" ? 443 : 80));
|
|
}
|
|
|
|
export function parseManagedEndpointLocalOrigin(localOrigin: string) {
|
|
const url = new URL(localOrigin);
|
|
if (
|
|
localOrigin !== localOrigin.trim() ||
|
|
(url.protocol !== "http:" && url.protocol !== "https:") ||
|
|
url.username !== "" ||
|
|
url.password !== "" ||
|
|
url.pathname !== "/" ||
|
|
url.search !== "" ||
|
|
url.hash !== "" ||
|
|
localOrigin.includes("?") ||
|
|
localOrigin.includes("#")
|
|
) {
|
|
throw new Error("Invalid local origin");
|
|
}
|
|
const wsUrl = new URL(url.origin);
|
|
wsUrl.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
return {
|
|
httpBaseUrl: url.origin,
|
|
wsBaseUrl: wsUrl.origin,
|
|
origin: {
|
|
localHttpHost: url.hostname,
|
|
localHttpPort: endpointRequestPort(url),
|
|
} satisfies RelayManagedEndpointOrigin,
|
|
};
|
|
}
|
|
|
|
export function isLoopbackEndpointOrigin(origin: RelayManagedEndpointOrigin): boolean {
|
|
return isLoopbackHostname(origin.localHttpHost);
|
|
}
|
|
|
|
export function isSupportedLinkProviderKind(request: RelayLinkProofRequest): boolean {
|
|
return (
|
|
request.endpoint.providerKind === "cloudflare_tunnel" ||
|
|
request.endpoint.providerKind === "manual"
|
|
);
|
|
}
|
|
|
|
export function linkProofScopes(
|
|
request: RelayLinkProofRequest,
|
|
): RelayEnvironmentLinkProofPayload["scopes"] {
|
|
return request.endpoint.providerKind === "cloudflare_tunnel"
|
|
? ["agent_activity_notifications", "managed_tunnels"]
|
|
: ["agent_activity_notifications"];
|
|
}
|
|
|
|
export function hasExactScope(input: {
|
|
readonly scopes: ReadonlyArray<string>;
|
|
readonly expected: string;
|
|
}): boolean {
|
|
return input.scopes.length === 1 && input.scopes[0] === input.expected;
|
|
}
|
|
|
|
export function hasBoundedCloudProofLifetime(input: {
|
|
readonly iat: number;
|
|
readonly exp: number;
|
|
readonly nowSeconds: number;
|
|
}): boolean {
|
|
return (
|
|
input.exp > input.iat &&
|
|
input.exp - input.iat <= CLOUD_PROOF_MAX_LIFETIME_SECONDS &&
|
|
input.iat <= input.nowSeconds + CLOUD_PROOF_CLOCK_SKEW_SECONDS
|
|
);
|
|
}
|
|
|
|
export function managedEndpointRuntimeConfigsMatch(
|
|
left: RelayManagedEndpointRuntimeConfig,
|
|
right: RelayManagedEndpointRuntimeConfig,
|
|
): boolean {
|
|
return (
|
|
left.providerKind === right.providerKind &&
|
|
left.connectorToken === right.connectorToken &&
|
|
left.tunnelId === right.tunnelId &&
|
|
left.tunnelName === right.tunnelName
|
|
);
|
|
}
|