@@ -19,7 +19,7 @@ const tailscaleCommandForPlatform = (platform: NodeJS.Platform): "tailscale" | "
1919
2020const TailscaleCommandContext = {
2121 executable : Schema . Literals ( [ "tailscale" , "tailscale.exe" ] ) ,
22- subcommand : Schema . Literals ( [ "status" , "serve" ] ) ,
22+ subcommand : Schema . Literals ( [ "status" , "serve" , "debug" ] ) ,
2323 argumentCount : Schema . Number ,
2424} ;
2525
@@ -214,64 +214,93 @@ export const parseTailscaleStatus = (
214214 } ) ,
215215 ) ;
216216
217- export const readTailscaleStatus = Effect . gen ( function * ( ) {
218- const args = [ "status" , "--json" ] ;
219- const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
220- const hostPlatform = yield * HostProcess . Platform ;
221- const executable = tailscaleCommandForPlatform ( hostPlatform ) ;
222- const commandContext = {
223- executable,
224- subcommand : "status" as const ,
225- argumentCount : args . length ,
226- } ;
227- return yield * Effect . gen ( function * ( ) {
228- const child = yield * spawner . spawn ( ChildProcess . make ( executable , args ) ) . pipe (
229- Effect . mapError ( ( cause ) => new TailscaleCommandSpawnError ( { ...commandContext , cause } ) ) ,
230- // Spawning can also fail as a defect rather than a typed error - a
231- // non-directory entry on PATH makes node throw ENOTDIR synchronously.
232- // `mapError` never sees that, so it would escape as an uncaught error.
233- Effect . catchDefect ( ( cause ) =>
234- Effect . fail ( new TailscaleCommandSpawnError ( { ...commandContext , cause } ) ) ,
235- ) ,
236- ) ;
237- const [ stdout , stderr , exitCode ] = yield * Effect . all (
238- [
239- collectStdout ( child . stdout ) ,
240- collectStderr ( child . stderr ) ,
241- child . exitCode . pipe ( Effect . map ( Number ) ) ,
242- ] ,
243- { concurrency : "unbounded" } ,
244- ) . pipe (
245- Effect . mapError ( ( cause ) => new TailscaleCommandOutputError ( { ...commandContext , cause } ) ) ,
246- ) ;
247- if ( exitCode !== 0 ) {
248- return yield * new TailscaleCommandExitError ( {
249- ...commandContext ,
250- exitCode,
251- stdoutLength : stdout . length ,
252- stderrLength : stderr . length ,
253- ...( stderrDiagnosticOf ( stderr ) !== undefined
254- ? { stderrDiagnostic : stderrDiagnosticOf ( stderr ) }
255- : { } ) ,
256- } ) ;
257- }
258- return yield * parseTailscaleStatus ( stdout ) ;
259- } ) . pipe (
260- Effect . scoped ,
261- Effect . timeout ( TAILSCALE_STATUS_TIMEOUT ) ,
262- Effect . catchTags ( {
263- TimeoutError : ( cause ) =>
264- Effect . fail (
265- new TailscaleCommandTimeoutError ( {
266- ...commandContext ,
267- timeoutMs : Duration . toMillis ( TAILSCALE_STATUS_TIMEOUT ) ,
268- cause,
269- } ) ,
217+ /** Runs a read-only tailscale command and returns its stdout. */
218+ const readTailscaleOutput = ( subcommand : "status" | "debug" , args : readonly string [ ] ) =>
219+ Effect . gen ( function * ( ) {
220+ const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
221+ const hostPlatform = yield * HostProcess . Platform ;
222+ const executable = tailscaleCommandForPlatform ( hostPlatform ) ;
223+ const commandContext = {
224+ executable,
225+ subcommand,
226+ argumentCount : args . length ,
227+ } ;
228+ return yield * Effect . gen ( function * ( ) {
229+ const child = yield * spawner . spawn ( ChildProcess . make ( executable , [ ...args ] ) ) . pipe (
230+ Effect . mapError ( ( cause ) => new TailscaleCommandSpawnError ( { ...commandContext , cause } ) ) ,
231+ // Spawning can also fail as a defect rather than a typed error - a
232+ // non-directory entry on PATH makes node throw ENOTDIR synchronously.
233+ // `mapError` never sees that, so it would escape as an uncaught error.
234+ Effect . catchDefect ( ( cause ) =>
235+ Effect . fail ( new TailscaleCommandSpawnError ( { ...commandContext , cause } ) ) ,
270236 ) ,
271- } ) ,
272- ) ;
237+ ) ;
238+ const [ stdout , stderr , exitCode ] = yield * Effect . all (
239+ [
240+ collectStdout ( child . stdout ) ,
241+ collectStderr ( child . stderr ) ,
242+ child . exitCode . pipe ( Effect . map ( Number ) ) ,
243+ ] ,
244+ { concurrency : "unbounded" } ,
245+ ) . pipe (
246+ Effect . mapError ( ( cause ) => new TailscaleCommandOutputError ( { ...commandContext , cause } ) ) ,
247+ ) ;
248+ if ( exitCode !== 0 ) {
249+ return yield * new TailscaleCommandExitError ( {
250+ ...commandContext ,
251+ exitCode,
252+ stdoutLength : stdout . length ,
253+ stderrLength : stderr . length ,
254+ ...( stderrDiagnosticOf ( stderr ) !== undefined
255+ ? { stderrDiagnostic : stderrDiagnosticOf ( stderr ) }
256+ : { } ) ,
257+ } ) ;
258+ }
259+ return stdout ;
260+ } ) . pipe (
261+ Effect . scoped ,
262+ Effect . timeout ( TAILSCALE_STATUS_TIMEOUT ) ,
263+ Effect . catchTags ( {
264+ TimeoutError : ( cause ) =>
265+ Effect . fail (
266+ new TailscaleCommandTimeoutError ( {
267+ ...commandContext ,
268+ timeoutMs : Duration . toMillis ( TAILSCALE_STATUS_TIMEOUT ) ,
269+ cause,
270+ } ) ,
271+ ) ,
272+ } ) ,
273+ ) ;
274+ } ) ;
275+
276+ export const readTailscaleStatus = readTailscaleOutput ( "status" , [ "status" , "--json" ] ) . pipe (
277+ Effect . flatMap ( parseTailscaleStatus ) ,
278+ ) ;
279+
280+ const TailscalePrefsJson = Schema . Struct ( {
281+ RunSSH : Schema . optional ( Schema . Unknown ) ,
282+ WantRunning : Schema . optional ( Schema . Unknown ) ,
273283} ) ;
274284
285+ const decodeTailscalePrefsJson = Schema . decodeEffect ( Schema . fromJsonString ( TailscalePrefsJson ) ) ;
286+
287+ /**
288+ * Whether this node serves Tailscale SSH (`tailscale up --ssh`), read from its
289+ * prefs. `tailscale status --json` does not report this for the local node.
290+ * `tailscale down` clears WantRunning but keeps RunSSH, so check both.
291+ */
292+ export const parseTailscaleSshEnabled = (
293+ rawPrefsJson : string ,
294+ ) : Effect . Effect < boolean , TailscaleStatusParseError > =>
295+ decodeTailscalePrefsJson ( rawPrefsJson ) . pipe (
296+ Effect . mapError ( ( cause ) => new TailscaleStatusParseError ( { cause } ) ) ,
297+ Effect . map ( ( prefs ) => prefs . RunSSH === true && prefs . WantRunning !== false ) ,
298+ ) ;
299+
300+ export const readTailscaleSshEnabled = readTailscaleOutput ( "debug" , [ "debug" , "prefs" ] ) . pipe (
301+ Effect . flatMap ( parseTailscaleSshEnabled ) ,
302+ ) ;
303+
275304export function buildTailscaleHttpsBaseUrl ( input : {
276305 readonly magicDnsName : string ;
277306 readonly servePort ?: number ;
0 commit comments