-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
64 lines (57 loc) · 2.22 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from bottle import static_file, template, post, request, route, run, auth_basic
import ConfigParser
import subprocess
def check_login(username, password):
config = ConfigParser.ConfigParser()
config.read('server.conf')
u = config.get('vpncweb', 'username')
p = config.get('vpncweb', 'password')
if ( u == username and p == password ):
return True
else:
return False
@route('/static/<filename>')
def server_static(filename):
return static_file(filename, root='/opt/vpncwebctl/static')
@route('/initctl', method='POST')
@auth_basic(check_login)
def initctl():
username = request.forms.get('username')
password = request.forms.get('password')
action = request.forms.get('initctl')
config = ConfigParser.ConfigParser()
config.read('server.conf')
profile = config.get('vpnc', 'profile')
if (action == "start"):
p = subprocess.Popen(["vpnc-connect", profile], stdout=subprocess.PIPE)
p_output, p_err = p.communicate()
return template('initctl_template', action=action, output=p_output)
elif (action == "stop"):
p = subprocess.Popen(["vpnc-disconnect"], stdout=subprocess.PIPE)
p_output, p_err = p.communicate()
return template('initctl_template', action=action, output=p_output)
else:
return "<p>Unknown Error</p>"
@route('/')
@route('/status')
@auth_basic(check_login)
def status():
config = ConfigParser.ConfigParser()
config.read('server.conf')
remote_ip = config.get('vpnc', 'remote_ip')
p1 = subprocess.Popen(["pgrep","vpnc"], stdout=subprocess.PIPE)
p1_output, p1_err = p1.communicate()
p2 = subprocess.Popen(["ip","addr","show","tun0"], stdout=subprocess.PIPE)
p2_output, p2_err = p2.communicate()
p3 = subprocess.Popen(["ping", "-q", "-c", "1", remote_ip], stdout=subprocess.PIPE)
p3_output, p3_err = p3.communicate()
if '1 received' in p3_output:
status = "Connected"
else:
status = "Disconnected"
return template('status_template', status=status, pid=p1_output, tun=p2_output, ping=p3_output)
if __name__ == '__main__':
config = ConfigParser.ConfigParser()
config.read('server.conf')
port = config.get('vpncweb', 'port')
run(host='0.0.0.0',port=port)