@@ -14,10 +14,16 @@ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
1414import * as NodeNet from "node:net" ;
1515import * as NodeOS from "node:os" ;
1616
17- import { isPrivateNetworkHost , isTailnetHost } from "@t3tools/shared/hostClassification" ;
17+ import { isTailnetHost } from "@t3tools/shared/hostClassification" ;
1818
1919import * as ServerConfig from "../config.ts" ;
20- import { formatHostForUrl , isLoopbackHost , isWildcardHost } from "../startupAccess.ts" ;
20+ import {
21+ formatHostForUrl ,
22+ isLoopbackHost ,
23+ isPrivateNetworkAddress ,
24+ isWildcardHost ,
25+ VIRTUAL_INTERFACE ,
26+ } from "../startupAccess.ts" ;
2127
2228type NetworkInterfacesMap = ReturnType < typeof NodeOS . networkInterfaces > ;
2329
@@ -40,19 +46,7 @@ export class DirectEndpoints extends Context.Service<
4046 * (`server.local`) can resolve to a different machine on each client's network.
4147 */
4248const isAdvertisableAddress = ( address : string ) : boolean =>
43- NodeNet . isIPv4 ( address ) &&
44- ! address . startsWith ( "127." ) &&
45- ! address . startsWith ( "169.254." ) &&
46- isPrivateNetworkHost ( address ) ;
47-
48- /**
49- * Container and VM networks (Docker, libvirt, VMware, VirtualBox, Hyper-V, and
50- * macOS's `bridge100`+) have private addresses only this machine reaches. A
51- * host bridge such as `br0` or Proxmox's `vmbr0` carries the LAN address, so
52- * it stays listed.
53- */
54- const VIRTUAL_INTERFACE =
55- / ^ ( d o c k e r | b r - | v e t h | v i r b r | v m n e t | v b o x n e t | v E t h e r n e t | p o d m a n | c n i | f l a n n e l | c a l i | l x c b r | l x d b r | b r i d g e 1 \d \d ) / ;
49+ NodeNet . isIPv4 ( address ) && ! address . startsWith ( "127." ) && isPrivateNetworkAddress ( address ) ;
5650
5751/**
5852 * Tailscale's own interface: `tailscale0` on Linux, "Tailscale" on Windows.
@@ -71,6 +65,8 @@ export function resolveBoundEndpoints(input: {
7165 readonly host : string | undefined ;
7266 readonly port : number ;
7367 readonly interfaces : NetworkInterfacesMap ;
68+ /** A cloud VM's private network is its provider's, which no client shares. */
69+ readonly onCloudVm ?: boolean ;
7470} ) : ReadonlyArray < ServerDirectEndpoint > {
7571 if ( isLoopbackHost ( input . host ) ) return [ ] ;
7672 const tailscaleAddresses = new Set (
@@ -87,24 +83,61 @@ export function resolveBoundEndpoints(input: {
8783 )
8884 . map ( ( entry ) => entry . address )
8985 : [ input . host ! ] . filter ( isAdvertisableAddress ) ;
90- return [ ...new Set ( addresses ) ] . map ( ( address ) => ( {
91- kind : tailscaleAddresses . has ( address ) ? "tailnet" : "lan" ,
92- httpBaseUrl : `http://${ formatHostForUrl ( address ) } :${ input . port } /` ,
93- } ) ) ;
86+ return [ ...new Set ( addresses ) ]
87+ . filter ( ( address ) => ! input . onCloudVm || tailscaleAddresses . has ( address ) )
88+ . map ( ( address ) => ( {
89+ kind : tailscaleAddresses . has ( address ) ? "tailnet" : "lan" ,
90+ httpBaseUrl : `http://${ formatHostForUrl ( address ) } :${ input . port } /` ,
91+ } ) ) ;
9492}
9593
94+ /**
95+ * AWS, GCP, Azure, DigitalOcean, Hetzner, Linode, Vultr, and Oracle all serve
96+ * instance metadata at this address. A plain TCP connect skips HTTP proxies,
97+ * which would otherwise answer for a machine that is not in a cloud.
98+ */
99+ const CLOUD_METADATA_ADDRESS = "169.254.169.254" ;
100+ const CLOUD_METADATA_TIMEOUT_MS = 300 ;
101+
102+ const probeCloudMetadata = Effect . callback < boolean > ( ( resume ) => {
103+ const socket = NodeNet . connect ( {
104+ host : CLOUD_METADATA_ADDRESS ,
105+ port : 80 ,
106+ timeout : CLOUD_METADATA_TIMEOUT_MS ,
107+ } ) ;
108+ let settled = false ;
109+ const settle = ( found : boolean ) => {
110+ if ( settled ) return ;
111+ settled = true ;
112+ socket . destroy ( ) ;
113+ resume ( Effect . succeed ( found ) ) ;
114+ } ;
115+ socket . once ( "connect" , ( ) => settle ( true ) ) ;
116+ socket . once ( "timeout" , ( ) => settle ( false ) ) ;
117+ socket . once ( "error" , ( ) => settle ( false ) ) ;
118+ return Effect . sync ( ( ) => socket . destroy ( ) ) ;
119+ } ) ;
120+
96121/** @public Service construction is part of the canonical Effect module API. */
97122export const make = Effect . gen ( function * ( ) {
98123 const config = yield * ServerConfig . ServerConfig ;
99124 const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
100125 const httpClient = yield * HttpClient . HttpClient ;
126+ // Only the service default listens on every interface without being asked,
127+ // so only it checks once whether it runs in a cloud. The check starts now so
128+ // the first client does not wait for its timeout.
129+ const onCloudVm = config . privateNetworkOnly
130+ ? yield * Effect . cached ( probeCloudMetadata )
131+ : Effect . succeed ( false ) ;
132+ yield * Effect . forkDetach ( onCloudVm ) ;
101133
102134 const resolve = Effect . gen ( function * ( ) {
103135 const endpoints = [
104136 ...resolveBoundEndpoints ( {
105137 host : config . host ,
106138 port : config . port ,
107139 interfaces : NodeOS . networkInterfaces ( ) ,
140+ onCloudVm : yield * onCloudVm ,
108141 } ) ,
109142 ] ;
110143 // Tailscale Serve terminates HTTPS on the tailnet name and forwards to
0 commit comments