Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor: hoist isPrivateNetworkEndpoint into server/lib/localEndpoint.js (#6818)

[._.]/ Adam Eivybf81612d55ec
diff --git a/client/src/utils/providerEndpoints.js b/client/src/utils/providerEndpoints.js
index 75169f6d5b..b06052a29f 100644
--- a/client/src/utils/providerEndpoints.js
+++ b/client/src/utils/providerEndpoints.js
@@ -5,10 +5,12 @@
* names, so callers can fold in live-installed models or decide whether a
* missing API key is a real gap.
*
- * Browser MIRROR of `localBackendForProvider` / `isLocalInstanceEndpoint` in
- * `server/lib/localProviderRuntime.js` and `isPrivateNetworkEndpoint` in
- * `server/lib/providerPrerequisites.js` — keep in lockstep. The server copies
- * are authoritative and stricter (they gate actions); these only label UI.
+ * `isPrivateNetworkEndpoint` is re-exported directly from
+ * `server/lib/localEndpoint.js` — the authoritative implementation, not a
+ * browser copy. Browser MIRROR of `localBackendForProvider` in
+ * `server/lib/localProviderRuntime.js` and `isLocalInstanceEndpoint` in
+ * `server/lib/localEndpoint.js` — keep those two in lockstep; the server
+ * copies are authoritative and stricter (they gate actions), these only label UI.
*
* Re-exported by `./providers.js` for existing `utils/providers` imports.
*/
@@ -64,78 +66,20 @@ const LOCAL_ENDPOINT_RE = /^(https?:\/\/)?(localhost|127\.\d{1,3}\.\d{1,3}\.\d{1
export const isLocalEndpoint = (endpoint) =>
typeof endpoint === 'string' && LOCAL_ENDPOINT_RE.test(endpoint.trim());
-// Hosts inside the trust boundary, where an unauthenticated OpenAI-compatible
-// server is a normal setup rather than a misconfiguration: RFC1918 LAN ranges,
-// link-local, and the Tailscale CGNAT range 100.64.0.0/10 (PortOS is a
-// tailnet-first product — an API provider pointed at another machine's Ollama
-// is a first-class configuration, not an edge case).
-const PRIVATE_IP_RE = /^(?:10\.|192\.168\.|172\.(?:1[6-9]|2\d|3[01])\.|169\.254\.|100\.(?:6[4-9]|[7-9]\d|1[01]\d|12[0-7])\.)/;
-
-/**
- * IPv6 counterpart to {@link PRIVATE_IP_RE}: unique-local (`fc00::/7`) and
- * link-local (`fe80::/10`). Tailscale hands out a ULA address alongside the
- * CGNAT v4 one, so without this a tailnet peer reached over IPv6 read as a
- * public host and its keyless provider was blocked on a missing API key.
- *
- * Gated on the host being an IPv6 literal (it contains a `:`) and compared
- * NUMERICALLY on the leading hextet — a bare `/^f[cd]/` prefix test would also
- * claim hostnames like `fdrive.example.com`, and `fd::1` expands to a leading
- * hextet of `0x00fd`, which is not in `fc00::/7` at all.
- */
-/**
- * Gate for {@link PRIVATE_IP_RE}: is this host an IPv4 literal at all?
- *
- * The range test above matches a PREFIX, which on its own also claims DNS names
- * that merely start like one — `10.evil.example` — and would report a keyless
- * PUBLIC endpoint as needing no key. Hosts arriving there have already been
- * through `URL`, which canonicalizes any IPv4 spelling to a dotted quad.
- * Mirror of the server helper in server/lib/providerPrerequisites.js.
- */
-const isIpv4Literal = (host) => /^\d{1,3}(?:\.\d{1,3}){3}$/.test(host);
-
-const isPrivateIpv6 = (host) => {
- if (!host.includes(':')) return false;
- const first = host.split(':')[0];
- if (!/^[0-9a-f]{1,4}$/.test(first)) return false; // '' for `::1` — loopback, already matched above
- const n = parseInt(first, 16);
- return (n >= 0xfc00 && n <= 0xfdff) || (n >= 0xfe80 && n <= 0xfebf);
-};
-
-/**
- * Is this endpoint inside the private network — loopback, a LAN/tailnet address,
- * a `.local`/`.ts.net`/`.internal` name, or a bare single-label host?
- *
- * Used to decide whether a missing API key is actually a missing prerequisite.
- * The server only attaches an `Authorization` header when a key is stored, so a
- * keyless call to a private OpenAI-compatible server (LM Studio on the desk
- * machine, Ollama on a tailnet peer) works exactly as configured — reporting it
- * as "needs setup" would be a false alarm on a supported deployment. A public
- * endpoint with no key stays flagged: that one really is misconfigured.
- *
- * A host that cannot be parsed reads as NOT private, keeping the stricter of
- * the two answers for input we don't understand.
- */
-export const isPrivateNetworkEndpoint = (endpoint) => {
- if (isLocalEndpoint(endpoint)) return true;
- if (typeof endpoint !== 'string' || !endpoint.trim()) return false;
- const trimmed = endpoint.trim();
- // A scheme-less endpoint ("192.168.1.5:1234/v1") is still a host — give the
- // parser one so it doesn't read the leading segment as a scheme.
- const candidate = /^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed) ? trimmed : `http://${trimmed}`;
- if (!URL.canParse(candidate)) return false;
- const host = new URL(candidate).hostname.toLowerCase().replace(/^\[|\]$/g, '');
- if (isIpv4Literal(host) && PRIVATE_IP_RE.test(host)) return true;
- if (isPrivateIpv6(host)) return true;
- if (/\.(local|internal|lan|home\.arpa|ts\.net)$/.test(host)) return true;
- // A single-label host resolves only inside the local network (`http://nas:11434`).
- return !host.includes('.') && !host.includes(':');
-};
+// THE private-network classification (RFC1918 LAN ranges, link-local, the
+// Tailscale CGNAT range, `.local`/`.ts.net`/`.internal` names, single-label
+// hosts) — imported directly from the server leaf instead of a browser copy,
+// so the two documented edge cases (`http://127.1`, a bracketless IPv6
+// literal) can't disagree between client and server again. (Imported, not
+// re-exported directly, because isFleetProvider below reads it too.)
+import { isPrivateNetworkEndpoint } from '../../../server/lib/localEndpoint.js';
+export { isPrivateNetworkEndpoint };
/**
* Does this provider talk to a daemon on THIS machine?
*
* Client mirror of `isLocalInstanceEndpoint` in
- * server/lib/localProviderRuntime.js, and the guard for anything that explains
+ * server/lib/localEndpoint.js, and the guard for anything that explains
* a provider by inspecting the machine PortOS runs on — "is `lms` installed
* here?", "start it from Models → LLMs". A provider named for LM Studio
* but pointed at another box on the tailnet matches
diff --git a/server/lib/localEndpoint.js b/server/lib/localEndpoint.js
index 66248cdbb7..0bcb9b32f4 100644
--- a/server/lib/localEndpoint.js
+++ b/server/lib/localEndpoint.js
@@ -38,3 +38,87 @@ export function localEndpointPort(endpoint) {
if (!url || !isLocalInstanceHost(url.hostname)) return null;
return url.port || (url.protocol === 'https:' ? '443' : '80');
}
+
+/**
+ * Hosts inside the trust boundary, where an unauthenticated OpenAI-compatible
+ * server is a normal setup rather than a misconfiguration: RFC1918 LAN ranges,
+ * link-local, and the Tailscale CGNAT range 100.64.0.0/10 (PortOS is a
+ * tailnet-first product — an API provider pointed at another machine's Ollama
+ * is a first-class configuration, not an edge case).
+ */
+const PRIVATE_IP_RE = /^(?:10\.|192\.168\.|172\.(?:1[6-9]|2\d|3[01])\.|169\.254\.|100\.(?:6[4-9]|[7-9]\d|1[01]\d|12[0-7])\.)/;
+
+/**
+ * Gate for {@link PRIVATE_IP_RE}: is this host an IPv4 literal at all?
+ *
+ * The range test above matches a PREFIX, which on its own also claims DNS names
+ * that merely start like one — `10.evil.example`, `172.16.evil.example` — and
+ * would report a keyless PUBLIC endpoint as needing no key. Hosts arriving here
+ * have already been through `URL`, which canonicalizes any IPv4 spelling to a
+ * dotted quad, so this is the exact shape a real address takes.
+ */
+const isIpv4Literal = (host) => /^\d{1,3}(?:\.\d{1,3}){3}$/.test(host);
+
+/**
+ * IPv6 counterpart to {@link PRIVATE_IP_RE}: unique-local (`fc00::/7`) and
+ * link-local (`fe80::/10`). Tailscale hands out a ULA address alongside the
+ * CGNAT v4 one, so without this a tailnet peer reached over IPv6 reads as a
+ * public host and its keyless provider is blocked on a missing API key.
+ *
+ * Gated on the host being an IPv6 literal (it contains a `:`) and compared
+ * NUMERICALLY on the leading hextet — a bare `/^f[cd]/` prefix test would also
+ * claim hostnames like `fdrive.example.com`, and `fd::1` expands to a leading
+ * hextet of `0x00fd`, which is not in `fc00::/7` at all.
+ */
+const isPrivateIpv6 = (host) => {
+ if (!host.includes(':')) return false;
+ const first = host.split(':')[0];
+ if (!/^[0-9a-f]{1,4}$/.test(first)) return false; // '' for `::1` — loopback, matched by isLocalInstanceHost
+ const n = parseInt(first, 16);
+ return (n >= 0xfc00 && n <= 0xfdff) || (n >= 0xfe80 && n <= 0xfebf);
+};
+
+/** The hostname of an endpoint (scheme optional), lowercased and de-bracketed; `null` when unparseable. */
+const endpointHost = (endpoint) => {
+ if (typeof endpoint !== 'string' || endpoint.trim() === '') return null;
+ const trimmed = endpoint.trim();
+ // A scheme-less endpoint ("192.0.2.10:1234/v1") is still a host — give the
+ // parser one so it doesn't read the leading segment as a scheme.
+ const candidate = /^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed) ? trimmed : `http://${trimmed}`;
+ if (!URL.canParse(candidate)) return null;
+ return new URL(candidate).hostname.toLowerCase().replace(/^\[|\]$/g, '');
+};
+
+/**
+ * Is this endpoint inside the private network — loopback, a LAN/tailnet
+ * address, a `.local`/`.ts.net`/`.internal` name, or a bare single-label host?
+ *
+ * Decides whether a missing API key is actually a missing prerequisite. The
+ * server attaches an `Authorization` header only when a key is stored, so a
+ * keyless call to a private OpenAI-compatible server (LM Studio on the desk
+ * machine, Ollama on a tailnet peer) works exactly as configured — treating it
+ * as un-runnable would take a supported deployment out of the fallback chain.
+ * A public endpoint with no key stays flagged: that one really is misconfigured.
+ *
+ * THE implementation — `server/lib/providerPrerequisites.js` imports this
+ * rather than keeping its own copy, and `client/src/utils/providerEndpoints.js`
+ * re-exports it directly rather than carrying a browser mirror. Two edge cases
+ * that used to divide a cheap client regex from this URL-based parse (both
+ * settled now that there is only one answer):
+ * - `http://127.1` — `URL` expands the compact form to `127.0.0.1`, so this
+ * calls it private. Node's own fetch expands it the same way.
+ * - `http://::1:11434` — an IPv6 literal without brackets, which `URL`
+ * rejects, so this reads it as NOT private; nothing can connect to that
+ * endpoint anyway. A host that cannot be parsed reads as NOT private,
+ * keeping the stricter answer for input we don't understand.
+ */
+export const isPrivateNetworkEndpoint = (endpoint) => {
+ const host = endpointHost(endpoint);
+ if (host === null) return false;
+ if (isLocalInstanceHost(host)) return true;
+ if (isIpv4Literal(host) && PRIVATE_IP_RE.test(host)) return true;
+ if (isPrivateIpv6(host)) return true;
+ if (/\.(local|internal|lan|home\.arpa|ts\.net)$/.test(host)) return true;
+ // A single-label host resolves only inside the local network (`http://nas:11434`).
+ return !host.includes('.') && !host.includes(':');
+};
diff --git a/server/lib/providerPrerequisites.js b/server/lib/providerPrerequisites.js
index 02feabd002..16713ac1f2 100644
--- a/server/lib/providerPrerequisites.js
+++ b/server/lib/providerPrerequisites.js
@@ -38,7 +38,7 @@
import { isProcessProvider } from './providerTypes.js';
import { PROVIDER_TYPES } from './aiToolkit/constants.js';
import { CODEX_ACCOUNT_STATUS, isCodexSubscriptionProvider } from './codexAccount.js';
-import { isLocalInstanceHost } from './localProviderRuntime.js';
+import { isPrivateNetworkEndpoint } from './localEndpoint.js';
import {
CODEX_OSS_LOCAL_PROVIDERS,
CODEX_OSS_MIN_VERSION,
@@ -49,92 +49,12 @@ import {
} from './providerModels.js';
import { gatewayForProvider } from './providerGateways.js';
-/**
- * Hosts inside the trust boundary, where an unauthenticated OpenAI-compatible
- * server is a normal setup rather than a misconfiguration: RFC1918 LAN ranges,
- * link-local, and the Tailscale CGNAT range 100.64.0.0/10 (PortOS is a
- * tailnet-first product — an API provider pointed at another machine's Ollama
- * is a first-class configuration, not an edge case).
- */
-const PRIVATE_IP_RE = /^(?:10\.|192\.168\.|172\.(?:1[6-9]|2\d|3[01])\.|169\.254\.|100\.(?:6[4-9]|[7-9]\d|1[01]\d|12[0-7])\.)/;
-
-/**
- * Gate for {@link PRIVATE_IP_RE}: is this host an IPv4 literal at all?
- *
- * The range test above matches a PREFIX, which on its own also claims DNS names
- * that merely start like one — `10.evil.example`, `172.16.evil.example` — and
- * would report a keyless PUBLIC endpoint as needing no key. Hosts arriving here
- * have already been through `URL`, which canonicalizes any IPv4 spelling to a
- * dotted quad, so this is the exact shape a real address takes.
- */
-const isIpv4Literal = (host) => /^\d{1,3}(?:\.\d{1,3}){3}$/.test(host);
-
-/**
- * IPv6 counterpart to {@link PRIVATE_IP_RE}: unique-local (`fc00::/7`) and
- * link-local (`fe80::/10`). Tailscale hands out a ULA address alongside the
- * CGNAT v4 one, so without this a tailnet peer reached over IPv6 reads as a
- * public host and its keyless provider is blocked on a missing API key.
- *
- * Gated on the host being an IPv6 literal (it contains a `:`) and compared
- * NUMERICALLY on the leading hextet — a bare `/^f[cd]/` prefix test would also
- * claim hostnames like `fdrive.example.com`, and `fd::1` expands to a leading
- * hextet of `0x00fd`, which is not in `fc00::/7` at all.
- */
-const isPrivateIpv6 = (host) => {
- if (!host.includes(':')) return false;
- const first = host.split(':')[0];
- if (!/^[0-9a-f]{1,4}$/.test(first)) return false; // '' for `::1` — loopback, matched by isLocalInstanceHost
- const n = parseInt(first, 16);
- return (n >= 0xfc00 && n <= 0xfdff) || (n >= 0xfe80 && n <= 0xfebf);
-};
-
-/** The hostname of an endpoint (scheme optional), lowercased and de-bracketed; `null` when unparseable. */
-const endpointHost = (endpoint) => {
- if (typeof endpoint !== 'string' || endpoint.trim() === '') return null;
- const trimmed = endpoint.trim();
- // A scheme-less endpoint ("192.0.2.10:1234/v1") is still a host — give the
- // parser one so it doesn't read the leading segment as a scheme.
- const candidate = /^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed) ? trimmed : `http://${trimmed}`;
- if (!URL.canParse(candidate)) return null;
- return new URL(candidate).hostname.toLowerCase().replace(/^\[|\]$/g, '');
-};
-
-/**
- * Is this endpoint inside the private network — loopback, a LAN/tailnet
- * address, a `.local`/`.ts.net`/`.internal` name, or a bare single-label host?
- *
- * Decides whether a missing API key is actually a missing prerequisite. The
- * server attaches an `Authorization` header only when a key is stored, so a
- * keyless call to a private OpenAI-compatible server (LM Studio on the desk
- * machine, Ollama on a tailnet peer) works exactly as configured — treating it
- * as un-runnable would take a supported deployment out of the fallback chain.
- * A public endpoint with no key stays flagged: that one really is misconfigured.
- *
- * MIRROR of `isPrivateNetworkEndpoint` in client/src/utils/providerEndpoints.js — keep
- * in lockstep. A host that cannot be parsed reads as NOT private, keeping the
- * stricter of the two answers for input we don't understand.
- *
- * The two disagree only where the client's cheap regex and a real URL parser
- * read a host differently, and in both directions THIS answer is the one that
- * reaches the card (the client consumes it when the server publishes, falling
- * back to its own regex only against a server too old to publish):
- * - `http://127.1` — `URL` expands the compact form to `127.0.0.1` and this
- * calls it private; the client's four-octet regex does not. Node's own
- * fetch expands it the same way, so private is the correct answer.
- * - `http://::1:11434` — an IPv6 literal without brackets, which `URL`
- * rejects and the client regex accepts. Unparseable reads as NOT private
- * here, per the rule above; nothing can connect to that endpoint anyway.
- */
-export const isPrivateNetworkEndpoint = (endpoint) => {
- const host = endpointHost(endpoint);
- if (host === null) return false;
- if (isLocalInstanceHost(host)) return true;
- if (isIpv4Literal(host) && PRIVATE_IP_RE.test(host)) return true;
- if (isPrivateIpv6(host)) return true;
- if (/\.(local|internal|lan|home\.arpa|ts\.net)$/.test(host)) return true;
- // A single-label host resolves only inside the local network (`http://nas:11434`).
- return !host.includes('.') && !host.includes(':');
-};
+// The private-network / loopback classification (RFC1918, Tailscale CGNAT,
+// `.local`/`.ts.net`, single-label hosts) lives in `server/lib/localEndpoint.js`
+// now, alongside the other endpoint-locality predicates it composes with
+// (`isLocalInstanceHost`) — `client/src/utils/providerEndpoints.js` re-exports
+// the same function rather than keeping a browser mirror.
+export { isPrivateNetworkEndpoint };
/**
* The key a CLI/TUI provider's runtime is published under in the runtimes map