Add python client implementation
This commit is contained in:
parent
151b904d36
commit
7f1a92f611
2 changed files with 46 additions and 4 deletions
|
@ -15,7 +15,3 @@ rm README.md
|
||||||
|
|
||||||
echo Enable Caddy to bind low ports
|
echo Enable Caddy to bind low ports
|
||||||
sudo setcap 'cap_net_bind_service=+ep' caddy
|
sudo setcap 'cap_net_bind_service=+ep' caddy
|
||||||
|
|
||||||
echo Download sirtunnel binary
|
|
||||||
curl -s -O -L https://github.com/anderspitman/SirTunnel/releases/download/${sirtunnelVersion}/sirtunnel
|
|
||||||
chmod +x sirtunnel
|
|
||||||
|
|
46
sirtunnel
Executable file
46
sirtunnel
Executable file
|
@ -0,0 +1,46 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import time
|
||||||
|
from urllib import request
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
tunnel_id = sys.argv[1]
|
||||||
|
host, port = tunnel_id.split(':')
|
||||||
|
|
||||||
|
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)
|
||||||
|
request.urlopen(req, body)
|
||||||
|
|
||||||
|
print("Tunnel created successfully")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
time.sleep(1)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
|
||||||
|
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)
|
||||||
|
break
|
Loading…
Reference in a new issue