-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.py
More file actions
57 lines (50 loc) · 1.57 KB
/
proxy.py
File metadata and controls
57 lines (50 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import paramiko
import threading
import select
import socket
import logging
logger = logging.getLogger('uvicorn.error')
def ssh_handler(chan, host, port):
sock = socket.socket()
try:
sock.connect((host, port))
except Exception:
chan.close()
return
while True:
r, w, x = select.select([sock, chan], [], [])
if sock in r:
data = sock.recv(1024)
if not data: break
chan.send(data)
if chan in r:
data = chan.recv(1024)
if not data: break
sock.send(data)
chan.close()
sock.close()
def start_reverse_tunnel(config):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_cfg = config['ssh']
try:
client.connect(
ssh_cfg['host'],
username=ssh_cfg['user'],
key_filename=ssh_cfg['key_path']
)
transport = client.get_transport()
transport.request_port_forward("", int(ssh_cfg['remote_port']))
logger.info(f"SSH Tunnel established: {ssh_cfg['host']}:{ssh_cfg['remote_port']} -> localhost:{ssh_cfg['local_port']}")
while True:
chan = transport.accept(1000)
if chan is None:
continue
thr = threading.Thread(
target=ssh_handler,
args=(chan, 'localhost', int(ssh_cfg['local_port'])),
daemon=True
)
thr.start()
except Exception as e:
logger.error(f"Failed to establish SSH tunnel: {e}")