ssh2 icon indicating copy to clipboard operation
ssh2 copied to clipboard

how to pipe ssh stream

Open nunuqq1 opened this issue 5 years ago • 2 comments

here's three part, client side(A), ssh tunnel server(B), ssh dest server(C) now client need to ssh connect to C without create linux user account in B so here's the solution A: ssh ${C_ip}${username}${expireTimestamp}@{B_ip} B: start a ssh server, auth A's request, accept, then ssh connect to C, pipe A and C stream

works fine with code bellow: const conn = new ssh2.Server({ hostKeys: [hostPrivateKey] }, fromClient => { let toClient const uuid = commonUtil.uuid() fromClient.on('authentication', ctx => { if (ctx.method !== 'password') { ctx.reject(); return } const authInfo = authUtils.getAuthInfo(ctx.username) if (authInfo === undefined) { ctx.reject(); return } authUtils.verifySignature(ctx.username, authInfo.expireTs, ctx.password).then(ok => { log.info(Tunnel createSshForward:: ${uuid} on verify: ok:${ok})

      if (!ok) { ctx.reject(); return }
      toClient = new Client()
      toClient.on('ready', () => {
        fromClient.on('session', (accept, reject) => {
          const fromSession = accept()
          fromSession.once('pty', (accept, reject, info) => {
            const ptyInfo = info
            fromSession.once('shell', accept => {
              const fromStream = accept()
              toClient.shell(ptyInfo, {}, (err, toStream) => {
                if (err) { log.info(`Tunnel createSshForward:: ${uuid} on to client shell error:  ${err}`); closeSocket(fromClient); closeSocket(toClient) }
                fromSession.on('window-change', (accept, reject, info) => {
                  accept && accept()
                  toStream.setWindow(info.rows, info.cols, info.height, info.width)
                })
                fromStream.pipe(toStream).pipe(fromStream)
              })
            })
            accept && accept()
          })
        })
        ctx.accept()
      })
      toClient.connect({ host: authInfo.ip, port, username, password, privateKey })
    })
  })
  fromClient.on('ready', _ => { log.info(`Tunnel createSshForward:: ${uuid} on ready`) })
})
conn.listen(listenPort, '0.0.0.0', () => { log.info('Tunnel createSshForward:: Listening on port ' + listenPort) })

the problem is:

  1. as u can see, there's a lot event need to be translated
  2. only accept client ssh request from a console shell, hard for client A to coding with

so i'm trying code bellow:

const conn = new ssh2.Server({ hostKeys: [hostPrivateKey] }, fromClient => { let toClient const uuid = commonUtil.uuid() fromClient.on('authentication', ctx => { if (ctx.method !== 'password') { ctx.reject(); return } const authInfo = authUtils.getAuthInfo(ctx.username) authUtils.verifySignature(ctx.username, authInfo.expireTs, ctx.password).then(ok => { if (!ok) { ctx.reject(); return } toClient = new Client() toClient.on('ready', () => { ctx.accept() fromClient._sock.pipe(toClient._sock).pipe(fromClient._sock) }) const destIp = authInfo.ip toClient.connect({ host: destIp, 22,'root', password: undefinded, privateKey: 'xxx' }) }) }) fromClient.on('ready', _ => { log.info(Tunnel createSshForward:: ${uuid} on ready) }) }) conn.listen(listenPort, '0.0.0.0', () => { log.info('Tunnel createSshForward:: Listening on port ' + listenPort) })

client get error: channel 0: open failed: administratively prohibited:

this is definitely not a ssh2 issue, just because of my poor understanding with ssh protocol can anybody tell my how to achieve the goal

nunuqq1 avatar Dec 11 '20 07:12 nunuqq1

did you manage to solve it?

Kreijstal avatar Jan 07 '21 01:01 Kreijstal

There is no trivial solution because there is no way to simply pipe a stream, especially while also interacting with it and also because of the nature of the ssh protocol. You will need to manually handle whatever it is you want to proxy.

mscdex avatar Jan 07 '21 01:01 mscdex