@@ -22,6 +22,7 @@ interface vscodeModule {
2222 window : typeof vscode . window ;
2323 env : typeof vscode . env ;
2424 commands : typeof vscode . commands ;
25+ workspace : typeof vscode . workspace ;
2526}
2627
2728export class Tailscale {
@@ -33,6 +34,7 @@ export class Tailscale {
3334 private childProcess ?: cp . ChildProcess ;
3435 private notifyExit ?: ( ) => void ;
3536 private socket ?: string ;
37+ private ws ?: WebSocket ;
3638
3739 constructor ( vscode : vscodeModule ) {
3840 this . _vscode = vscode ;
@@ -41,6 +43,18 @@ export class Tailscale {
4143 static async withInit ( vscode : vscodeModule ) : Promise < Tailscale > {
4244 const ts = new Tailscale ( vscode ) ;
4345 await ts . init ( ) ;
46+ vscode . workspace . onDidChangeConfiguration ( ( event ) => {
47+ if ( event . affectsConfiguration ( 'tailscale.portDiscovery.enabled' ) ) {
48+ if ( ts . portDiscoOn ( ) && ! ts . ws ) {
49+ Logger . debug ( 'running port disco' ) ;
50+ ts . runPortDisco ( ) ;
51+ } else if ( ! ts . portDiscoOn ( ) && ts . ws ) {
52+ Logger . debug ( 'turning off port disco' ) ;
53+ ts . ws . close ( ) ;
54+ ts . ws = undefined ;
55+ }
56+ }
57+ } ) ;
4458 return ts ;
4559 }
4660
@@ -168,6 +182,10 @@ export class Tailscale {
168182 } ) ;
169183 }
170184
185+ portDiscoOn ( ) {
186+ return vscode . workspace . getConfiguration ( EXTENSION_NS ) . get < boolean > ( 'portDiscovery.enabled' ) ;
187+ }
188+
171189 processStderr ( childProcess : cp . ChildProcess ) {
172190 if ( ! childProcess . stderr ) {
173191 Logger . error ( 'childProcess.stderr is null' , LOG_COMPONENT ) ;
@@ -306,35 +324,39 @@ export class Tailscale {
306324 if ( ! this . url ) {
307325 throw new Error ( 'uninitialized client' ) ;
308326 }
327+ if ( ! this . portDiscoOn ( ) ) {
328+ Logger . info ( 'port discovery is off' ) ;
329+ return ;
330+ }
309331
310- const ws = new WebSocket ( `ws://${ this . url . slice ( 'http://' . length ) } /portdisco` , {
332+ this . ws = new WebSocket ( `ws://${ this . url . slice ( 'http://' . length ) } /portdisco` , {
311333 headers : {
312334 Authorization : 'Basic ' + this . authkey ,
313335 } ,
314336 } ) ;
315- ws . on ( 'error' , ( e ) => {
337+ this . ws . on ( 'error' , ( e ) => {
316338 Logger . info ( `got ws error: ${ e } ` ) ;
317339 } ) ;
318- ws . on ( 'open' , ( ) => {
340+ this . ws . on ( 'open' , ( ) => {
319341 Logger . info ( 'websocket is open' ) ;
320342 this . _vscode . window . terminals . forEach ( async ( t ) => {
321343 const pid = await t . processId ;
322344 if ( ! pid ) {
323345 return ;
324346 }
325347 Logger . debug ( `adding initial termianl process: ${ pid } ` ) ;
326- ws . send (
348+ this . ws ? .send (
327349 JSON . stringify ( {
328350 type : 'addPID' ,
329351 pid : pid ,
330352 } )
331353 ) ;
332354 } ) ;
333355 } ) ;
334- ws . on ( 'close' , ( ) => {
356+ this . ws . on ( 'close' , ( ) => {
335357 Logger . info ( 'websocket is closed' ) ;
336358 } ) ;
337- ws . on ( 'message' , async ( data ) => {
359+ this . ws . on ( 'message' , async ( data ) => {
338360 Logger . info ( 'got message' ) ;
339361 const msg = JSON . parse ( data . toString ( ) ) ;
340362 Logger . info ( `msg is ${ msg . type } ` ) ;
@@ -357,7 +379,7 @@ export class Tailscale {
357379 return ;
358380 }
359381 Logger . info ( `pid is ${ pid } ` ) ;
360- ws . send (
382+ this . ws ? .send (
361383 JSON . stringify ( {
362384 type : 'addPID' ,
363385 pid : pid ,
@@ -370,7 +392,7 @@ export class Tailscale {
370392 if ( ! pid ) {
371393 return ;
372394 }
373- ws . send (
395+ this . ws ? .send (
374396 JSON . stringify ( {
375397 type : 'removePID' ,
376398 pid : pid ,
0 commit comments