@@ -14,7 +14,9 @@ import { RemoteTunnel } from "./remote-tunnel"
1414
1515export const Info = Schema . Struct ( {
1616 disabled : Schema . optional ( Schema . Boolean ) ,
17- remote : Schema . optional ( Schema . Boolean ) ,
17+ // Present when remote access is on. The route is generated, never user-set: the secret subdomain the service is
18+ // served on.
19+ remote : Schema . optional ( Schema . Struct ( { route : Schema . String } ) ) ,
1820 hostname : Schema . optional ( Schema . String ) ,
1921 port : Schema . optional ( Schema . Int . check ( Schema . isGreaterThanOrEqualTo ( 1 ) , Schema . isLessThanOrEqualTo ( 65_535 ) ) ) ,
2022 password : Schema . optional ( Schema . String ) ,
@@ -27,6 +29,10 @@ const keys = ["disabled", "remote", "hostname", "port", "password", "cors", "env
2729type Key = ( typeof keys ) [ number ]
2830
2931const decodeInfo = Schema . decodeUnknownEffect ( Schema . fromJsonString ( Info ) )
32+ // Earlier builds stored remote access as a boolean.
33+ const decodeLegacy = Schema . decodeUnknownOption (
34+ Schema . fromJsonString ( Schema . Struct ( { ...Info . fields , remote : Schema . Boolean } ) ) ,
35+ )
3036const decodeRegistration = Schema . decodeUnknownEffect ( Schema . fromJsonString ( Service . Info ) )
3137
3238export function filename ( channel = OPENCODE_CHANNEL ) {
@@ -130,12 +136,24 @@ export const options = Effect.fnUntraced(function* (input: { readonly checkVersi
130136export const read = Effect . fn ( "cli.service-config.read" ) ( function * ( ) {
131137 const { fs, configFile, legacyConfigFile } = yield * paths
132138 if ( legacyConfigFile ) yield * migrateConfig ( legacyConfigFile , configFile )
133- return yield * fs . readFileString ( configFile ) . pipe (
134- Effect . flatMap ( decodeInfo ) ,
135- Effect . orElseSucceed ( ( ) => ( { } ) as Info ) ,
136- )
139+ const text = yield * fs . readFileString ( configFile ) . pipe ( Effect . option )
140+ if ( Option . isNone ( text ) ) return { } as Info
141+ const info = yield * decodeInfo ( text . value ) . pipe ( Effect . option )
142+ if ( Option . isSome ( info ) ) return info . value
143+ const legacy = decodeLegacy ( text . value )
144+ if ( Option . isNone ( legacy ) ) return { } as Info
145+ // Repair the file in place so every reader sees the same route.
146+ const { remote : enabled , ...rest } = legacy . value
147+ const repaired : Info = enabled ? { ...rest , remote : { route : route ( ) } } : rest
148+ yield * write ( repaired )
149+ return repaired
137150} )
138151
152+ // 64 random bits as 16 hex characters, a valid DNS label.
153+ function route ( ) {
154+ return randomBytes ( 8 ) . toString ( "hex" )
155+ }
156+
139157const write = Effect . fn ( "cli.service-config.write" ) ( function * ( value : Info ) {
140158 const { fs, configFile } = yield * paths
141159 const temp = configFile + ".tmp"
@@ -155,10 +173,20 @@ export const password = Effect.fn("cli.service-config.password")(function* (valu
155173 return next
156174} )
157175
176+ // Turns remote access on and returns its route, created once and kept so the remote URL survives restarts.
177+ export const remote = Effect . fn ( "cli.service-config.remote" ) ( function * ( ) {
178+ const existing = yield * read ( )
179+ if ( existing . remote ) return existing . remote . route
180+ const next = route ( )
181+ yield * write ( { ...existing , remote : { route : next } } )
182+ return next
183+ } )
184+
158185export const get = Effect . fn ( "cli.service-config.get" ) ( function * ( key ?: string , name ?: string ) {
159186 if ( key === undefined ) {
160187 const { password : _password , ...safe } = yield * read ( )
161- return JSON . stringify ( safe , null , 2 )
188+ // The route is as sensitive as the password: it is the unguessable half of the remote address.
189+ return JSON . stringify ( safe . remote === undefined ? safe : { ...safe , remote : { } } , null , 2 )
162190 }
163191 const selected = configKey ( key )
164192 if ( selected !== "env" && name !== undefined ) throw new Error ( `Usage: opencode service get ${ selected } ` )
@@ -167,7 +195,7 @@ export const get = Effect.fn("cli.service-config.get")(function* (key?: string,
167195 return String ( ( yield * read ( ) ) . disabled ?? false )
168196 }
169197 case "remote" : {
170- return String ( ( yield * read ( ) ) . remote ?? false )
198+ return String ( ( yield * read ( ) ) . remote !== undefined )
171199 }
172200 case "hostname" : {
173201 return ( yield * read ( ) ) . hostname ?? ""
@@ -206,7 +234,13 @@ export const set = Effect.fn("cli.service-config.set")(function* (key: string, v
206234 // A tunnel that cannot be created leaves remote access off instead of a service that keeps retrying.
207235 if ( value === "true" ) yield * RemoteTunnel . ensure ( )
208236 yield * Service . stop ( yield * options ( ) )
209- yield * write ( { ...( yield * read ( ) ) , remote : value === "true" } )
237+ if ( value === "true" ) {
238+ yield * remote ( )
239+ return
240+ }
241+ // Disabling forgets the address, so enabling again issues a new unguessable one.
242+ const { remote : _remote , ...next } = yield * read ( )
243+ yield * write ( next )
210244 return
211245 }
212246 case "hostname" : {
0 commit comments