forked from anderspitman/SirTunnel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsirtunnel.py
executable file
·66 lines (54 loc) · 1.62 KB
/
sirtunnel.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
#!/usr/bin/env python3
import sys
import json
import time
from urllib import request, error
def check_tunnel_health(tunnel_id):
check_url = 'http://127.0.0.1:2019/id/' + tunnel_id
try:
request.urlopen(check_url)
except error.HTTPError as e:
if e.code == 404: # Not Found
return False
except error.URLError:
return False
return True
def delete_tunnel(tunnel_id):
print("Cleaning up tunnel")
delete_url = 'http://127.0.0.1:2019/id/' + tunnel_id
req = request.Request(method='DELETE', url=delete_url)
request.urlopen(req)
if __name__ == '__main__':
host = sys.argv[1]
port = sys.argv[2]
tunnel_id = host + '-' + port
caddy_add_route_request = {
"@id": tunnel_id,
"match": [{
"host": [host],
}],
"handle": [{
"handler": "reverse_proxy",
"upstreams":[{
"dial": ':' + port
}]
}]
}
body = json.dumps(caddy_add_route_request).encode('utf-8')
headers = {
'Content-Type': 'application/json'
}
create_url = 'http://127.0.0.1:2019/config/apps/http/servers/sirtunnel/routes'
req = request.Request(method='POST', url=create_url, headers=headers, data=body)
request.urlopen(req)
print("Tunnel created successfully")
while True:
try:
if not check_tunnel_health(tunnel_id):
print("Tunnel disconnected unexpectedly")
delete_tunnel(tunnel_id)
break
time.sleep(1)
except KeyboardInterrupt:
delete_tunnel(tunnel_id)
break