1- import * as NodeOS from "node:os" ;
2-
31import * as Context from "effect/Context" ;
42import * as Duration from "effect/Duration" ;
53import * as Effect from "effect/Effect" ;
@@ -15,6 +13,7 @@ import { buildRemoteNodeEnvScript } from "@t3tools/ssh/tunnel";
1513import { satisfiesSemverRange } from "@t3tools/shared/semver" ;
1614
1715import * as DesktopEnvironment from "../app/DesktopEnvironment.ts" ;
16+ import * as DesktopNetworkInterfaces from "../backend/DesktopNetworkInterfaces.ts" ;
1817import { parseWslDistroList , type WslDistro } from "./wslPathParsing.ts" ;
1918
2019const PROCESS_TERMINATE_GRACE = Duration . seconds ( 1 ) ;
@@ -723,7 +722,7 @@ export const parseDistroIpCandidates = (stdout: string): ReadonlyArray<string> =
723722
724723export interface WindowsIpv4Interface {
725724 readonly address : string ;
726- readonly netmask : string ;
725+ readonly netmask : string | undefined ;
727726}
728727
729728const ipv4ToInt = ( ip : string ) : number | null => {
@@ -759,31 +758,36 @@ export const pickDistroIp = (
759758 for ( const candidate of candidates ) {
760759 for ( const iface of windowsInterfaces ) {
761760 if ( candidate === iface . address ) return candidate ;
762- if ( inSameSubnet ( candidate , iface . address , iface . netmask ) ) return candidate ;
761+ if ( iface . netmask !== undefined && inSameSubnet ( candidate , iface . address , iface . netmask ) ) {
762+ return candidate ;
763+ }
763764 }
764765 }
765766 return candidates [ 0 ] ?? null ;
766767} ;
767768
768- const windowsIpv4Interfaces = ( ) : ReadonlyArray < WindowsIpv4Interface > => {
769- const interfaces : WindowsIpv4Interface [ ] = [ ] ;
770- for ( const list of Object . values ( NodeOS . networkInterfaces ( ) ) ) {
769+ export const windowsIpv4Interfaces = (
770+ interfaces : DesktopNetworkInterfaces . NetworkInterfaces ,
771+ ) : ReadonlyArray < WindowsIpv4Interface > => {
772+ const flattened : WindowsIpv4Interface [ ] = [ ] ;
773+ for ( const list of Object . values ( interfaces ) ) {
771774 if ( ! list ) continue ;
772775 for ( const entry of list ) {
773776 // Same family normalization as isLocalHostIpv4 in
774777 // DesktopBackendConfiguration: Electron's Node reports the string
775778 // "IPv4", some Node builds report the numeric 4.
776779 const family = String ( entry . family ) ;
777780 if ( family === "IPv4" || family === "4" ) {
778- interfaces . push ( { address : entry . address , netmask : entry . netmask } ) ;
781+ flattened . push ( { address : entry . address , netmask : entry . netmask } ) ;
779782 }
780783 }
781784 }
782- return interfaces ;
785+ return flattened ;
783786} ;
784787
785788const getDistroIpImpl = (
786789 distro : string | null ,
790+ readNetworkInterfaces : Effect . Effect < DesktopNetworkInterfaces . NetworkInterfaces > ,
787791) : Effect . Effect < Option . Option < string > , never , ChildProcessSpawner . ChildProcessSpawner > =>
788792 Effect . scoped (
789793 Effect . gen ( function * ( ) {
@@ -805,7 +809,8 @@ const getDistroIpImpl = (
805809 if ( ( exitCode as unknown as number ) !== 0 ) return Option . none < string > ( ) ;
806810 const raw = decodeUtf8 ( concatChunks ( stdoutBytes ) ) . trim ( ) ;
807811 const candidates = parseDistroIpCandidates ( raw ) ;
808- const chosen = pickDistroIp ( candidates , windowsIpv4Interfaces ( ) ) ;
812+ const interfaces = yield * readNetworkInterfaces ;
813+ const chosen = pickDistroIp ( candidates , windowsIpv4Interfaces ( interfaces ) ) ;
809814 yield * Effect . log (
810815 `[wsl] distro IP probe chose ${ chosen ?? "none" } from candidates [${ candidates . join ( ", " ) } ]` ,
811816 ) ;
@@ -914,6 +919,7 @@ export const layer = Layer.effect(
914919 const environment = yield * DesktopEnvironment . DesktopEnvironment ;
915920 const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
916921 const fileSystem = yield * FileSystem . FileSystem ;
922+ const networkInterfaces = yield * DesktopNetworkInterfaces . DesktopNetworkInterfaces ;
917923 const windir = process . env . WINDIR ?? "C:\\Windows" ;
918924
919925 const provideSpawner = < A , E > (
@@ -960,7 +966,9 @@ export const layer = Layer.effect(
960966 } ) . pipe ( Effect . withSpan ( "desktop.wsl.getUserHome" ) ) ;
961967
962968 const getDistroIp = ( distro : string | null ) =>
963- provideSpawner ( getDistroIpImpl ( distro ) ) . pipe ( Effect . withSpan ( "desktop.wsl.getDistroIp" ) ) ;
969+ provideSpawner ( getDistroIpImpl ( distro , networkInterfaces . read ) ) . pipe (
970+ Effect . withSpan ( "desktop.wsl.getDistroIp" ) ,
971+ ) ;
964972
965973 const probeDistros = provideSpawner ( probeWslDistros ) . pipe (
966974 Effect . withSpan ( "desktop.wsl.probeDistros" ) ,
0 commit comments