@@ -373,31 +373,63 @@ function connectToHub(config: AgentConfig): Socket {
373373
374374 if ( data . pty ) {
375375 try {
376- const pty = await import ( 'node-pty' ) ;
377376 const shell = process . env . SHELL || '/bin/bash' ;
378- const ptyProcess = pty . spawn ( shell , [ ] , {
379- name : 'xterm-256color' ,
380- cols : 80 ,
381- rows : 24 ,
382- cwd : process . env . HOME || '/' ,
383- env : { ...process . env } as Record < string , string > ,
384- } ) ;
385-
386- connections . set ( data . connectionId , { socket : ptyProcess , connected : true , pty : ptyProcess } ) ;
387- console . log ( chalk . green ( ` [ok] PTY shell spawned (${ shell } )` ) ) ;
388- socket . emit ( 'dial_success' , { connectionId : data . connectionId } ) ;
377+ let ptyHandle : any ;
389378
390- ptyProcess . onData ( ( chunk : string ) => {
391- socket . emit ( 'data' , {
392- connectionId : data . connectionId ,
393- data : Buffer . from ( chunk , 'utf8' ) . toString ( 'base64' ) ,
379+ try {
380+ const pty = await import ( 'node-pty' ) ;
381+ const p = pty . spawn ( shell , [ ] , {
382+ name : 'xterm-256color' ,
383+ cols : 80 ,
384+ rows : 24 ,
385+ cwd : process . env . HOME || '/' ,
386+ env : { ...process . env } as Record < string , string > ,
394387 } ) ;
395- } ) ;
388+ ptyHandle = {
389+ type : 'node-pty' ,
390+ process : p ,
391+ write : ( d : string ) => p . write ( d ) ,
392+ resize : ( c : number , r : number ) => p . resize ( c , r ) ,
393+ kill : ( ) => p . kill ( ) ,
394+ } ;
395+ p . onData ( ( chunk : string ) => {
396+ socket . emit ( 'data' , { connectionId : data . connectionId , data : Buffer . from ( chunk , 'utf8' ) . toString ( 'base64' ) } ) ;
397+ } ) ;
398+ p . onExit ( ( ) => {
399+ socket . emit ( 'close' , { connectionId : data . connectionId } ) ;
400+ connections . delete ( data . connectionId ) ;
401+ } ) ;
402+ } catch {
403+ const { spawn : spawnChild } = await import ( 'child_process' ) ;
404+ const isLinux = process . platform === 'linux' ;
405+ const args = isLinux ? [ '-qc' , shell , '/dev/null' ] : [ '-q' , '/dev/null' , shell ] ;
406+ const child = spawnChild ( 'script' , args , {
407+ stdio : 'pipe' ,
408+ env : { ...process . env , TERM : 'xterm-256color' } ,
409+ cwd : process . env . HOME || '/' ,
410+ } ) ;
411+ ptyHandle = {
412+ type : 'script' ,
413+ process : child ,
414+ write : ( d : string ) => child . stdin ?. write ( d ) ,
415+ resize : ( ) => { } ,
416+ kill : ( ) => child . kill ( ) ,
417+ } ;
418+ child . stdout ?. on ( 'data' , ( chunk : Buffer ) => {
419+ socket . emit ( 'data' , { connectionId : data . connectionId , data : chunk . toString ( 'base64' ) } ) ;
420+ } ) ;
421+ child . stderr ?. on ( 'data' , ( chunk : Buffer ) => {
422+ socket . emit ( 'data' , { connectionId : data . connectionId , data : chunk . toString ( 'base64' ) } ) ;
423+ } ) ;
424+ child . on ( 'exit' , ( ) => {
425+ socket . emit ( 'close' , { connectionId : data . connectionId } ) ;
426+ connections . delete ( data . connectionId ) ;
427+ } ) ;
428+ }
396429
397- ptyProcess . onExit ( ( ) => {
398- socket . emit ( 'close' , { connectionId : data . connectionId } ) ;
399- connections . delete ( data . connectionId ) ;
400- } ) ;
430+ connections . set ( data . connectionId , { socket : ptyHandle , connected : true , pty : ptyHandle } ) ;
431+ console . log ( chalk . green ( ` [ok] PTY shell spawned (${ shell } ) via ${ ptyHandle . type } ` ) ) ;
432+ socket . emit ( 'dial_success' , { connectionId : data . connectionId } ) ;
401433 } catch ( error : unknown ) {
402434 const err = error as Error ;
403435 console . log ( chalk . red ( ` [x] PTY spawn failed: ${ err . message } ` ) ) ;
@@ -465,7 +497,7 @@ function connectToHub(config: AgentConfig): Socket {
465497 // Handle terminal resize from hub
466498 socket . on ( 'resize' , ( data : { connectionId : string ; cols : number ; rows : number } ) => {
467499 const conn = connections . get ( data . connectionId ) ;
468- if ( conn ?. pty ) {
500+ if ( conn ?. pty ?. resize ) {
469501 conn . pty . resize ( data . cols , data . rows ) ;
470502 }
471503 } ) ;
@@ -474,9 +506,9 @@ function connectToHub(config: AgentConfig): Socket {
474506 socket . on ( 'close' , ( data : { connectionId : string } ) => {
475507 const conn = connections . get ( data . connectionId ) ;
476508 if ( conn ) {
477- if ( conn . pty ) {
509+ if ( conn . pty ?. kill ) {
478510 conn . pty . kill ( ) ;
479- } else {
511+ } else if ( conn . socket ?. end ) {
480512 conn . socket . end ( ) ;
481513 }
482514 connections . delete ( data . connectionId ) ;
0 commit comments