import type {
RelayEnvironmentLinkProofPayload,
RelayLinkProofRequest,
RelayManagedEndpointOrigin,
RelayManagedEndpointRuntimeConfig,
} from "@t3tools/contracts/relay";
import type { HttpServerRequest } from "effect/http";
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 firstForwardedHeaderValue(value: string | undefined): string | undefined {
const first = value?.split(",")[0]?.trim();
return first && first.length > 0 ? first : undefined;
}
export function requestAbsoluteUrl(request: HttpServerRequest.HttpServerRequest): string | null {
try {
return new URL(request.originalUrl).href;
} catch {
const host = firstForwardedHeaderValue(request.headers.host) ?? "127.0.0.1";
try {
return new URL(request.originalUrl, `http://${host}`).href;
} catch {
return null;
}
}
}
export function hasForwardedAuthorityHeaders(
request: HttpServerRequest.HttpServerRequest,
): boolean {
return (
firstForwardedHeaderValue(request.headers["x-forwarded-host"]) !== undefined ||
firstForwardedHeaderValue(request.headers["x-forwarded-proto"]) !== undefined
);
}
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 isAllowedEndpointOrigin(input: {
readonly origin: RelayManagedEndpointOrigin;
readonly requestUrl: string;
readonly allowNonLoopbackRequestHost?: boolean;
}): boolean {
if (!isLoopbackHostname(input.origin.localHttpHost)) {
return false;
}
const url = new URL(input.requestUrl);
if (!input.allowNonLoopbackRequestHost && !isLoopbackHostname(url.hostname)) {
return false;
}
return input.origin.localHttpPort === endpointRequestPort(url);
}
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
);
}