Ejemplo n.º 1
0
Archivo: server.py Proyecto: sbuss/wssh
    def _forward_outbound(self, channel):
        """ Forward outbound traffic (ssh -> websockets) """
        try:
            data = b''
            while True:
                wait_read(channel.fileno())
                recv = channel.recv(1024)
                if not len(recv):
                    return

                data += recv
                try:
                    self._websocket.send(json.dumps({'data': encode(data)}))
                    data = b''
                except UnicodeDecodeError:
                    pass
        finally:
            self.close()
Ejemplo n.º 2
0
    def _forward_outbound(self, channel):
        """ Forward outbound traffic (ssh -> websockets) """
        try:
            data = b''
            while True:
                wait_read(channel.fileno())
                recv = channel.recv(1024)
                if not len(recv):
                    return

                data += recv
                try:
                    self._websocket.send(json.dumps({'data': encode(data)}))
                    data = b''
                except UnicodeDecodeError:
                    pass
        finally:
            self.close()
Ejemplo n.º 3
0
def invoke_shell(endpoint):
    ssh = websocket.create_connection(endpoint)
    _resize(ssh)
    oldtty = termios.tcgetattr(sys.stdin)
    old_handler = signal.getsignal(signal.SIGWINCH)

    def on_term_resize(signum, frame):
        _resize(ssh)
    signal.signal(signal.SIGWINCH, on_term_resize)

    try:
        tty.setraw(sys.stdin.fileno())
        tty.setcbreak(sys.stdin.fileno())

        rows, cols = _pty_size()
        ssh.send(json.dumps({'resize': {'width': cols, 'height': rows}}))

        while True:
            try:
                r, w, e = select.select([ssh.sock, sys.stdin], [], [])
                if ssh.sock in r:
                    data = ssh.recv()
                    if not data:
                        break
                    message = json.loads(data)
                    if 'error' in message:
                        raise ConnectionError(message['error'])
                    sys.stdout.buffer.write(decode(message['data']))
                    sys.stdout.flush()
                if sys.stdin in r:
                    x = sys.stdin.read(1)
                    if len(x) == 0:
                        break
                    ssh.send(json.dumps({'data': encode(x)}))
            except (select.error, IOError) as e:
                if e.args and e.args[0] == errno.EINTR:
                    pass
                else:
                    raise
    except websocket.WebSocketException:
        raise
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
        signal.signal(signal.SIGWINCH, old_handler)