@@ -608,20 +608,59 @@ ipcMain.handle('backendStatus', async (_event, { target = 'local' } = {}) => {
608608 if ( target === 'remote' ) {
609609 const tunnel = getTunnelState ( )
610610 const localExpressPort = tunnel && tunnel . localExpressPort
611- // First try: use existing local→remote forwarding to /status
612- if ( localExpressPort ) {
611+ const conn = getActiveTunnel && getActiveTunnel ( )
612+ if ( ! conn ) return { success : false , error : 'no-active-ssh' }
613+
614+ const normalizeHost = ( value ) => String ( value || '' ) . trim ( ) . toLowerCase ( ) . replace ( / \. + $ / , '' )
615+ const shortHost = ( value ) => normalizeHost ( value ) . split ( '.' ) [ 0 ]
616+ const isValidStatusPayload = ( payload ) => {
617+ return ! ! ( payload && typeof payload === 'object' && payload . success === true )
618+ }
619+
620+ const remoteOS = await detectRemoteOS ( )
621+ const remoteHostName = await new Promise ( ( resolve ) => {
622+ try {
623+ const hostCmd = remoteOS === 'win32'
624+ ? `powershell -NoProfile -Command "$env:COMPUTERNAME"`
625+ : `bash -lc "hostname -f 2>/dev/null || hostname 2>/dev/null || uname -n"`
626+ conn . exec ( hostCmd , ( err , stream ) => {
627+ if ( err ) return resolve ( null )
628+ let out = ''
629+ stream . on ( 'data' , ( d ) => { out += d . toString ( ) } )
630+ stream . stderr . on ( 'data' , ( ) => { } )
631+ stream . on ( 'close' , ( ) => resolve ( ( out || '' ) . trim ( ) || null ) )
632+ } )
633+ } catch {
634+ resolve ( null )
635+ }
636+ } )
637+
638+ // First try: use existing local→remote forwarding to /status only when the express
639+ // tunnel is already known to be active, and verify identity against remote host.
640+ const tunnels = Array . isArray ( tunnel ?. tunnels ) ? tunnel . tunnels : [ ]
641+ const expressForward = tunnels . find ( ( t ) => t ?. name === 'express' )
642+ const hasActiveExpressForward = ! ! (
643+ localExpressPort &&
644+ expressForward &&
645+ expressForward . status === 'forwarding' &&
646+ Number ( expressForward . localPort ) === Number ( localExpressPort )
647+ )
648+ if ( hasActiveExpressForward ) {
613649 try {
614650 const res = await axios . get ( `http://127.0.0.1:${ localExpressPort } /status` , { timeout : 3000 } )
615- if ( res && res . data ) return res . data
651+ const data = res && res . data
652+ const reportedHost = data && data . serverIdentity && data . serverIdentity . hostName
653+ const matchesRemoteHost = remoteHostName && reportedHost && (
654+ normalizeHost ( remoteHostName ) === normalizeHost ( reportedHost ) ||
655+ shortHost ( remoteHostName ) === shortHost ( reportedHost )
656+ )
657+ if ( isValidStatusPayload ( data ) && matchesRemoteHost ) return data
616658 } catch { }
617659 }
618660
619- // Fallback: sweep remote ports 5000–8000 to discover an Express server
620- const conn = getActiveTunnel && getActiveTunnel ( )
621- if ( ! conn ) return { success : false , error : 'no-active-ssh' }
661+ // Fallback: sweep remote ports 5000-8000 to discover an Express server
622662
623663 // Single-shot list of listening ports on remote for performance
624- const remoteOS = await detectRemoteOS ( )
625664 const listCmd = remoteOS === 'win32'
626665 ? `netstat -an | findstr LISTEN`
627666 : `bash -c "command -v ss >/dev/null 2>&1 && ss -ltn || netstat -an | grep LISTEN"`
@@ -692,7 +731,7 @@ ipcMain.handle('backendStatus', async (_event, { target = 'local' } = {}) => {
692731
693732 for ( const rp of ports ) {
694733 const found = await tryPortStatus ( rp )
695- if ( found && found . data ) {
734+ if ( found && isValidStatusPayload ( found . data ) ) {
696735 // Persistently start Express forward using discovered remote port
697736 try {
698737 await startExpressForward ( { remoteExpressPort : rp } )
0 commit comments