@@ -84,6 +84,75 @@ const parseCatalogId = (id: string) => {
8484const platformVersion = ( platform : DevicePlatform , runtime : string | undefined ) =>
8585 `${ platform === "ios" ? "iOS" : "Android" } ${ runtime ? ` ${ runtime } ` : "" } ` ;
8686
87+ const AGENT_DEVICE_PATH = "/v1/agent-device" ;
88+ const sameOrigin = ( left : string , right : string ) => {
89+ try {
90+ return new URL ( left ) . origin === new URL ( right ) . origin ;
91+ } catch {
92+ return false ;
93+ }
94+ } ;
95+
96+ /**
97+ * The gateway and token this machine's agent-device uses, from agent-device's own remote-daemon
98+ * variables, which Simlock tells its clients to set. Null unless they point at a Simlock gateway.
99+ */
100+ export const machineSimlockToken = ( environment : NodeJS . ProcessEnv ) => {
101+ const base = environment . AGENT_DEVICE_DAEMON_BASE_URL ?. trim ( ) . replace ( / \/ + $ / , "" ) ;
102+ const agentToken = environment . AGENT_DEVICE_DAEMON_AUTH_TOKEN ?. trim ( ) ;
103+ if ( ! base ?. endsWith ( AGENT_DEVICE_PATH ) || ! agentToken ) return null ;
104+ const gatewayUrl = base . slice ( 0 , - AGENT_DEVICE_PATH . length ) ;
105+ return URL . canParse ( gatewayUrl ) ? { gatewayUrl, agentToken } : null ;
106+ } ;
107+
108+ /** The saved token, else this machine's agent-device token, but only for the gateway it targets. */
109+ export const resolveSimlockToken = (
110+ gatewayUrl : string ,
111+ saved : string ,
112+ environment : NodeJS . ProcessEnv ,
113+ ) => {
114+ if ( saved ) return { agentToken : saved , source : "settings" as const } ;
115+ const machine = machineSimlockToken ( environment ) ;
116+ return machine && sameOrigin ( machine . gatewayUrl , gatewayUrl )
117+ ? { agentToken : machine . agentToken , source : "env" as const }
118+ : null ;
119+ } ;
120+
121+ /** What went wrong talking to a gateway, without the gateway's own text. */
122+ export const describeGatewayFailure = ( cause : unknown , gatewayUrl : string , tokenOwner : string ) => {
123+ const status = HttpClientError . isHttpClientError ( cause ) ? cause . response ?. status : undefined ;
124+ return status === 401 || status === 403
125+ ? `The Simlock gateway refused the agent token for ${ tokenOwner } .`
126+ : status !== undefined
127+ ? `The Simlock gateway at ${ gatewayUrl } answered HTTP ${ status } .`
128+ : `Could not reach the Simlock gateway at ${ gatewayUrl } .` ;
129+ } ;
130+
131+ const decodeIdentity = Schema . decodeUnknownEffect (
132+ Schema . Struct ( { name : Schema . String , role : Schema . String } ) ,
133+ ) ;
134+
135+ /** Who the gateway says a token is, via GET /v1/identity. */
136+ export const simlockIdentity = Effect . fn ( "SimlockDeviceHost.identity" ) ( function * (
137+ gatewayUrl : string ,
138+ agentToken : string ,
139+ ) {
140+ const http = yield * HttpClient . HttpClient ;
141+ return yield * http
142+ . execute (
143+ HttpClientRequest . get ( `${ gatewayUrl . replace ( / \/ $ / , "" ) } /v1/identity` ) . pipe (
144+ HttpClientRequest . bearerToken ( agentToken ) ,
145+ ) ,
146+ )
147+ . pipe (
148+ Effect . flatMap ( HttpClientResponse . filterStatusOk ) ,
149+ Effect . flatMap ( ( response ) => response . json ) ,
150+ Effect . flatMap ( decodeIdentity ) ,
151+ Effect . scoped ,
152+ Effect . timeout ( REQUEST_TIMEOUT ) ,
153+ ) ;
154+ } ) ;
155+
87156/** T3's own agent-device CLI, installed on first use. */
88157export type AgentDeviceCli = Effect . Effect <
89158 { readonly nodePath : string ; readonly entryPath : string } ,
@@ -103,6 +172,8 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
103172 const environment = yield * HostProcess . Environment ;
104173 const hostId = config . id ;
105174 const gateway = config . gatewayUrl . replace ( / \/ $ / , "" ) ;
175+ const agentToken =
176+ resolveSimlockToken ( config . gatewayUrl , config . agentToken , environment ) ?. agentToken ?? "" ;
106177 const agentDeviceUrl = `${ gateway } /v1/agent-device` ;
107178 const hostDir = path . join ( server . stateDir , "device" , "simlock" , hostId ) ;
108179 const leases = new Map < string , Lease > ( ) ;
@@ -142,7 +213,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
142213 HttpClientRequest . bodyJsonUnsafe ( body ?? { } ) ,
143214 )
144215 : HttpClientRequest . delete ( `${ gateway } ${ route } ` )
145- ) . pipe ( HttpClientRequest . bearerToken ( config . agentToken ) ) ,
216+ ) . pipe ( HttpClientRequest . bearerToken ( agentToken ) ) ,
146217 )
147218 . pipe (
148219 Effect . flatMap ( HttpClientResponse . filterStatusOk ) ,
@@ -203,24 +274,17 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
203274 const fetchCatalog = Effect . gen ( function * ( ) {
204275 const now = yield * Clock . currentTimeMillis ;
205276 if ( catalog && now - catalog . at < CATALOG_TTL_MS ) return catalog . devices ;
206- if ( ! config . agentToken ) {
277+ if ( ! agentToken ) {
207278 return yield * unavailable ( `Add an agent token for ${ config . label } in Settings.` ) ;
208279 }
209280 const decoded = yield * request ( "GET" , "/v1/catalog" ) . pipe (
210281 Effect . flatMap ( decodeCatalog ) ,
211- Effect . mapError ( ( cause ) => {
212- const status = HttpClientError . isHttpClientError ( cause )
213- ? cause . response ?. status
214- : undefined ;
215- return unavailable (
216- status === 401 || status === 403
217- ? `The Simlock gateway refused the agent token for ${ config . label } . Check it in Settings.`
218- : status !== undefined
219- ? `The Simlock gateway at ${ gateway } answered HTTP ${ status } .`
220- : `Could not reach the Simlock gateway at ${ gateway } .` ,
282+ Effect . mapError ( ( cause ) =>
283+ unavailable (
284+ `${ describeGatewayFailure ( cause , gateway , config . label ) } Check it in Settings.` ,
221285 cause ,
222- ) ;
223- } ) ,
286+ ) ,
287+ ) ,
224288 ) ;
225289 const devices = decoded . platforms . flatMap ( ( entry ) => {
226290 if ( entry . platform !== "ios" && entry . platform !== "android" ) return [ ] ;
@@ -354,7 +418,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
354418 const configPath = path . join ( dir , "agent-device.json" ) ;
355419 yield * writeAgentDeviceConfig ( configPath , {
356420 baseUrl : agentDeviceUrl ,
357- token : config . agentToken ,
421+ token : agentToken ,
358422 stateDir : dir ,
359423 } ) . pipe (
360424 Effect . provide ( services ) ,
@@ -480,7 +544,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
480544 onSome : ( lease ) =>
481545 Effect . succeed ( {
482546 baseUrl : agentDeviceUrl ,
483- token : config . agentToken ,
547+ token : agentToken ,
484548 agentDeviceId : null ,
485549 session : lease . session ,
486550 stateDir : lease . dir ,
@@ -511,7 +575,7 @@ export const make = Effect.fn("SimlockDeviceHost.make")(function* (
511575 if ( ! lease ) return null ;
512576 return {
513577 url : `${ gateway } /v1/leases/${ encodeURIComponent ( lease . leaseId ) } /${ stream ? "stream" : "input" } ` ,
514- headers : { authorization : `Bearer ${ config . agentToken } ` } ,
578+ headers : { authorization : `Bearer ${ agentToken } ` } ,
515579 protocols : [ ] ,
516580 } ;
517581 } ) ;
0 commit comments