-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·41 lines (29 loc) · 1.03 KB
/
server.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
#!/usr/bin/env python3
import subprocess
import wgconfig
from bottle import route, request, response, run, template
PORT=8080
CLIENT_SUBNET="24"
wc = wgconfig.WGConfig('./example.conf')
wc.read_file()
privkey = wc.interface['PrivateKey']
res = subprocess.run(["wg", "pubkey"], input=privkey, text=True, capture_output=True)
# TODO error check
pubkey = res.stdout
# TODO stub
def findNextClientIP():
return "100.70.0.1"
def addClientToConfig(clientPubKey, clientRequestIP="unknown IP"):
global CLIENT_SUBNET
clientIP = findNextClientIP()
wc.add_peer(clientPubKey, "# Added by " + clientRequestIP)
wc.add_attr(clientPubKey, "AllowedIPs", clientIP + "/" + CLIENT_SUBNET)
wc.write_file()
return clientIP
@route('/request', method=['POST'])
def index():
clientKey = request.params.key
clientIP = addClientToConfig(clientKey, request.remote_addr)
# TODO return text instead of HTML
return template('{{clientIP}}|{{serverPubkey}}', clientIP=clientIP, serverPubkey=pubkey)
run(host='localhost', port=8080)