-
Notifications
You must be signed in to change notification settings - Fork 4
/
hctunnel.py
executable file
·125 lines (98 loc) · 3.87 KB
/
hctunnel.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
import pycos, socket, sys, ssl, os, signal, time
import websocket
import ConfigParser
# Function to get dictionary with the values of one section of the configuration file
def config_section_map(section):
dict1 = {}
options = config.options(section)
for option in options:
try:
dict1[option] = config.get(section, option)
except:
dict1[option] = None
return dict1
def ws_send(conn,ws, task=None):
task.set_daemon()
thread_pool = pycos.AsyncThreadPool(1)
while True:
try:
line = yield thread_pool.async_task(ws.recv)
except Exception as ex:
print 'Error in server tunnel: %s'%(str(ex))
break
if not line:
break
yield conn.send(line)
print('End of server tunnel!')
os.kill(os.getpid(), signal.SIGTERM)
def client_send(conn,ws, task=None):
task.set_daemon()
while True:
try:
line = yield conn.recv(1024)
except Exception as ex:
print 'Error in client tunnel: %s'%(str(ex))
break
if not line:
break
ws.send_binary(line)
print('End of client tunnel!')
os.kill(os.getpid(), signal.SIGTERM)
def hcwst(host, port,repeater_ws,proxy_host,proxy_port,proxy_username,proxy_password, ssl_verify, task=None):
task.set_daemon()
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
sock = pycos.AsyncSocket(sock)
sock.bind((host, int(port)))
sock.listen(1)
print('Tunnel listening at %s' % str(sock.getsockname()))
if ssl_verify:
ssl_defaults = ssl.get_default_verify_paths()
sslopt_ca_certs = {'ca_cert_path': ssl_defaults.capath}
ws = websocket.WebSocket(sslopt=sslopt_ca_certs
else:
ws = websocket.WebSocket(sslopt={"cert_reqs": ssl.CERT_NONE})
if not proxy_host:
ws.connect(repeater_ws, subprotocols=["binary"],sockopt=(socket.IPPROTO_TCP, socket.TCP_NODELAY))
else:
ws.connect(repeater_ws,http_proxy_host=proxy_host,http_proxy_port=proxy_port,http_proxy_auth=proxy_auth, subprotocols=["binary"],sockopt=(socket.IPPROTO_TCP, socket.TCP_NODELAY))
print('Tunnel connected to %s'% repeater_ws )
conn, _ = yield sock.accept()
pycos.Task(client_send, conn,ws)
pycos.Task(ws_send, conn,ws)
if __name__ == '__main__':
config = ConfigParser.ConfigParser()
config.read("/etc/helpchannel.conf")
server_config = config_section_map("ServerConfig")
proxy_host = None
if 'proxy_host' in server_config:
proxy_host = server_config['proxy_host']
proxy_port = None
if 'proxy_port' in server_config:
proxy_port = server_config['proxy_port']
proxy_username = None
if 'proxy_username' in server_config:
proxy_username = server_config['proxy_username']
proxy_password = None
if 'proxy_password' in server_config:
proxy_password = server_config['proxy_password']
repeater_ws = config_section_map("TunnelConfig")['tunnel_url']
local_tunnel_port = config_section_map("TunnelConfig")['local_port']
ssl_verify = (config_section_map("TunnelConfig")['ssl_verify'] in
['True', 'true', '1', 't', 'y', 'yes'])
proxy_auth=(proxy_username, proxy_password)
host, port = '127.0.0.1', int(local_tunnel_port)
if len(sys.argv) > 1:
host = sys.argv[1]
if len(sys.argv) > 2:
port = int(sys.argv[2])
pycos.Task(hcwst, host, local_tunnel_port,repeater_ws,proxy_host,proxy_port,proxy_username,proxy_password, ssl_verify)
if sys.version_info.major > 2:
read_input = input
else:
read_input = raw_input
while True:
# Sleep for one minute
time.sleep(60)