11import { createAdvertisedEndpoint } from "@t3tools/shared/advertisedEndpoint" ;
2- import type { AdvertisedEndpoint , AdvertisedEndpointProvider } from "@t3tools/contracts" ;
2+ import {
3+ ExecutionEnvironmentDescriptor ,
4+ type AdvertisedEndpoint ,
5+ type AdvertisedEndpointProvider ,
6+ } from "@t3tools/contracts" ;
37import {
48 buildTailscaleHttpsBaseUrl ,
59 isTailscaleIpv4Address ,
@@ -11,6 +15,7 @@ import {
1115import * as Effect from "effect/Effect" ;
1216import * as Option from "effect/Option" ;
1317import * as HttpClient from "effect/http/HttpClient" ;
18+ import * as HttpClientResponse from "effect/http/HttpClientResponse" ;
1419import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
1520
1621import type { NetworkInterfaces } from "./DesktopNetworkInterfaces.ts" ;
@@ -71,10 +76,20 @@ function resolveTailscaleIpAdvertisedEndpoints(input: {
7176 return endpoints ;
7277}
7378
79+ // The backend may run elsewhere (WSL), so ask it for its id instead of reading the state dir.
80+ const probeServesThisBackend = ( localHttpUrl : string ) => ( baseUrl : string ) =>
81+ HttpClient . get ( new URL ( "/.well-known/t3/environment" , localHttpUrl ) ) . pipe (
82+ Effect . flatMap ( HttpClientResponse . filterStatusOk ) ,
83+ Effect . flatMap ( HttpClientResponse . schemaBodyJson ( ExecutionEnvironmentDescriptor ) ) ,
84+ Effect . flatMap ( ( { environmentId } ) => probeTailscaleHttpsEndpoint ( { baseUrl, environmentId } ) ) ,
85+ Effect . orElseSucceed ( ( ) => false ) ,
86+ ) ;
87+
7488const resolveTailscaleMagicDnsAdvertisedEndpoint = Effect . fn (
7589 "resolveTailscaleMagicDnsAdvertisedEndpoint" ,
7690) ( function * ( input : {
7791 readonly dnsName : string | null ;
92+ readonly localHttpUrl : string ;
7893 readonly serveEnabled : boolean ;
7994 readonly servePort ?: number ;
8095 readonly probe ?: ( baseUrl : string ) => Effect . Effect < boolean , never , HttpClient . HttpClient > ;
@@ -87,11 +102,7 @@ const resolveTailscaleMagicDnsAdvertisedEndpoint = Effect.fn(
87102 magicDnsName : input . dnsName ,
88103 ...( input . servePort === undefined ? { } : { servePort : input . servePort } ) ,
89104 } ) ;
90- const probe =
91- input . probe ?.( httpBaseUrl ) ??
92- probeTailscaleHttpsEndpoint ( {
93- baseUrl : httpBaseUrl ,
94- } ) ;
105+ const probe = ( input . probe ?? probeServesThisBackend ( input . localHttpUrl ) ) ( httpBaseUrl ) ;
95106 const isReachable = input . serveEnabled ? yield * probe : false ;
96107
97108 return Option . some (
@@ -140,6 +151,7 @@ export const resolveTailscaleAdvertisedEndpoints = Effect.fn("resolveTailscaleAd
140151 const ipEndpoints = resolveTailscaleIpAdvertisedEndpoints ( { ...input , status } ) ;
141152 const magicDnsEndpoint = yield * resolveTailscaleMagicDnsAdvertisedEndpoint ( {
142153 dnsName : status ?. magicDnsName ?? null ,
154+ localHttpUrl : `http://127.0.0.1:${ input . port } ` ,
143155 serveEnabled : input . serveEnabled === true ,
144156 ...( input . servePort === undefined ? { } : { servePort : input . servePort } ) ,
145157 ...( input . probe === undefined ? { } : { probe : input . probe } ) ,
0 commit comments