Init commit
[networtool-web.git] / web.py
1 import os
2 from package.bottle import *
3 import subprocess
4
5 # Refer https://segmentfault.com/a/1190000011579147
6
7 DIR = os.path.dirname(os.path.realpath(__file__))
8 CONF = '/usr/local/trackerpp/config/trackerpp.conf'
9 SERVICE = 'trackerpp.service'
10
11 TEMPLATE_PATH.insert(0, DIR + '/views/')
12
13
14 def getWlanName():
15     return subprocess.check_output("iw dev | grep Interface | awk '{print $2}'", shell=True).strip()
16
17 def getConnectedSSID():
18     return subprocess.check_output("iw dev | grep ssid | awk '{print $2}'", shell=True).strip()
19
20 def getIpAddr():
21     return subprocess.check_output("ip addr", shell=True).strip()
22
23 # return AP or 
24 def getCurrentMode():
25     return subprocess.check_output("iw dev | grep type | awk '{print $2}'", shell=True).strip()
26
27 def isAPmode():
28     return getCurrentMode() == 'AP'
29
30 wlan = getWlanName();
31
32 def getSSIDs():
33     cmd = "iw dev " + wlan + " scan | grep -i ssid | awk '{print $2}'"
34     return subprocess.check_output(cmd, shell=True).strip().split('\n')
35
36 def getTrackerPPConf(fname = CONF):
37     with open(fname, 'rt') as f:
38         return f.read()
39
40 def updateTrackerPPConf(content, fname = CONF):
41     with open(fname, 'wt') as f:
42         f.write(content)
43
44 def restartTrackerPPService():
45     subprocess.check_call("systemctl restart trackerpp.service", shell=True)
46
47 def getCurrentNetworkId():
48     return subprocess.check_output("wpa_cli -i wlan0 list_networks | grep -i current | awk '{print $1}'", shell=True).strip()
49
50 def connectWifi(ssid, passwd):
51     if isAPmode():
52         print 'stop hostapd first'
53         subprocess.check_output( DIR + "/script/stopHostapd.sh", shell=True)
54
55     cmd = "wpa_cli -i wlan0 add_network"
56     print cmd
57     id = subprocess.check_output(cmd, shell=True).strip()
58
59     cmd = "wpa_cli -i wlan0 set_network " + id + " ssid '\"" + ssid + "\"'"
60     print cmd
61     subprocess.check_output(cmd, shell=True)
62
63     cmd = "wpa_cli -i wlan0 set_network " + id + " psk '\"" + passwd + "\"'"
64     print cmd
65     subprocess.check_output(cmd, shell=True)
66
67     cmd = "wpa_cli -i wlan0 disable_network " + getCurrentNetworkId()
68     print cmd
69     subprocess.check_output(cmd, shell=True)
70
71     cmd = "wpa_cli -i wlan0 enable_network " + id
72     print cmd
73     subprocess.check_output(cmd, shell=True)
74
75 def resetWifi():
76     if not isAPmode():
77         cmd = "wpa_cli -i wlan0 disable_network " + getCurrentNetworkId()
78         print cmd
79         subprocess.check_output(cmd, shell=True)
80     subprocess.check_output(DIR + "/script/startHostapd.sh", shell=True)
81
82
83
84 @route('/')
85 @get('/login') # or @route('/login')
86 def login():
87     return template('login.tpl', username="username", password="password")
88
89 def check_login(username, password):
90     return True
91     #return username == 'admin' and password == 'admin123'
92
93 form_dict = {'ipaddr': getIpAddr(), 
94             'ssids': getSSIDs(), 
95             'usedSsid': getConnectedSSID(),
96             'mode' : ''}
97
98 @post('/main')
99 def do_main():
100     action = request.forms.get('action')
101     if action == "Login":
102         username = request.forms.get('username')
103         password = request.forms.get('password')
104         if not check_login(username, password):
105             return "<p>Login failed.</p>"
106         print 'lobin'
107         form_dict['ipaddr'] = getIpAddr()
108         form_dict['usedSsid'] = getConnectedSSID()
109         form_dict['mode'] = 'AP' if isAPmode() else 'WiFi'
110         print getConnectedSSID()
111         print form_dict['usedSsid']
112         form_dict['ssids'] = getSSIDs()
113     elif action == 'Scan':
114         form_dict['usedSsid'] = getConnectedSSID()
115         form_dict['ssids'] = getSSIDs()
116     elif action == 'Connect':
117         selectedSsid = request.forms.get('select')
118         password = request.forms.get('password')
119         return connectWifi(selectedSsid, password)
120     elif action == 'Reset':
121         return resetWifi()
122     return template('main.tpl', **form_dict)
123
124
125 run(host='0.0.0.0', port=80)