Paramiko服务器模式端口转发

6
我需要使用paramiko实现一个ssh服务器,只处理像这样的“-R”端口转发请求:
ssh -N -T -R 40005:destination_host:22 user@example.com

就我所了解的,我需要实现ServerInterface.check_port_forward_request,并在某个时刻创建一个套接字并监听指定端口。通过信道/连接传输的任何数据将分别进入连接/信道。

class Server (paramiko.ServerInterface):
    .
    .
    .
    def check_port_forward_request(self, address, port):
        'Check if the requested port forward is allowed'
        ...
        return port

def handler(chan, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('', port))
    sock.listen(1)
    conn, addr = s.accept()    

    while True:
        r, w, x = select.select([conn, chan], [], [])
        if conn in r:
            data = conn.recv(1024)
            if len(data) == 0:
                break
            chan.send(data)
        if chan in r:
            data = chan.recv(1024)
            if len(data) == 0:
                break
            conn.send(data)
    chan.close()
    conn.close()
    verbose('Tunnel closed from %r' % (chan.origin_addr,))

thr = threading.Thread(target=handler, args=(chan,server_port))
thr.setDaemon(True)
thr.start()

这是实现服务器端paramiko ssh端口转发的一般思路吗?我应该在check_port_forward_request内启动线程,还是其他地方?
1个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接