Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
import "server-only";
import { createLogger } from "@rallly/logger";
import { Agent, EnvHttpProxyAgent, setGlobalDispatcher } from "undici";
const logger = createLogger("outbound-proxy");
const PROXY_ENV_VARS = [
"HTTP_PROXY",
"HTTPS_PROXY",
"http_proxy",
"https_proxy",
] as const;
// Proxy URLs may carry credentials (http://user:pass@proxy) — never log them.
function sanitize(value: string) {
try {
const url = new URL(value);
url.username = "";
url.password = "";
return url.href;
} catch {
return "<invalid URL>";
}
}
/** Whether outbound fetches are routed through a proxy from the environment. */
export function isOutboundProxyConfigured() {
return PROXY_ENV_VARS.some((name) => process.env[name]);
}
/**
* Loopback traffic is never proxied, regardless of NO_PROXY: a CONNECT to
* "localhost" through a remote proxy reaches the proxy's loopback, never this
* container's, so proxying it is categorically wrong. Go's
* ProxyFromEnvironment applies the same rule. The undici matcher compares
* entries literally (no CIDR, no name/IP resolution), so both name and
* address forms are listed. "localhost" also covers *.localhost via the
* matcher's subdomain rule.
*/
const LOOPBACK_NO_PROXY = "localhost,127.0.0.1,[::1]";
/**
* Node's built-in fetch ignores proxy environment variables, so on networks
* with no direct egress (self-hosted behind a corporate proxy) outbound calls
* never reach the proxy at all. EnvHttpProxyAgent honors HTTP_PROXY /
* HTTPS_PROXY / NO_PROXY in both cases. Node consults the global dispatcher
* on every fetch, but anything fetched before this runs goes direct — call it
* first at boot.
*
* Node 24's experimental NODE_USE_ENV_PROXY does the same thing natively;
* when it stabilizes, this module can be deleted in its favor.
*/
export function setupOutboundProxy() {
const configured = PROXY_ENV_VARS.filter((name) => process.env[name]);
if (configured.length === 0) {
return;
}
const dispatcher = createOutboundDispatcher();
setGlobalDispatcher(dispatcher);
logger.info(
{
proxies: Object.fromEntries(
configured.map((name) => [name, sanitize(process.env[name] as string)]),
),
noProxy: getNoProxy(),
},
"Outbound proxy enabled: fetch will route through the configured proxy",
);
}
function getNoProxy() {
return [process.env.no_proxy ?? process.env.NO_PROXY, LOOPBACK_NO_PROXY]
.filter(Boolean)
.join(",");
}
/**
* A dispatcher that follows the proxy environment the same way the global
* one does, so a caller that needs its own connection options (a pinned DNS
* lookup, say) keeps the proxy routing. `connect` options apply to direct
* connections only: proxied ones are made by the proxy, and hosts exempted
* by NO_PROXY come back through the direct path.
*/
export function createOutboundDispatcher(options: Agent.Options = {}) {
if (isOutboundProxyConfigured()) {
return new EnvHttpProxyAgent({ ...options, noProxy: getNoProxy() });
}
return new Agent(options);
}