-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharfoo.py
173 lines (134 loc) · 5.22 KB
/
sharfoo.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from tippiLink import TippiLink
import rumps
import subprocess
from subprocess import Popen, PIPE
import os.path
import json
import re
rumps.debug_mode(True)
app = None
_CONF_LOCATION = os.path.join(os.path.expanduser("~"), ".sharfoo.conf")
_TEMPLATE = """{
"username": "admin",
"password": "admin",
"host": "192.168.0.1"
}
"""
def _send_os_notification(message, title="", subtitle=""):
# Workaround to send notificaitons because notifications in rumps are broken
# https://github.com/jaredks/rumps/issues/59
cmd = b"""display notification "%s" with title "%s" Subtitle "%s" """ % (message, title, subtitle)
Popen(["osascript", '-'], stdin=PIPE, stdout=PIPE).communicate(cmd)
def _get_bssid():
output = subprocess.check_output(
["/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport", "-I"]
)
matches = re.match('.*BSSID: (.*?)\n', output, re.DOTALL)
is_on = True
if output == "AirPort: Off\n":
is_on = False
bssid = None
else:
bssid = matches.group(1)
return is_on, bssid
def _read_admin_credentials():
with open(_CONF_LOCATION) as f:
x = f.read()
creds = json.loads(x)
return creds["username"], creds["password"], creds["host"], creds["bssid"] if "bssid" in creds else None
def _update_config_file(username, password, host, bssid):
d = {
"username": username,
"password": password,
"host": host,
"bssid": bssid
}
with open(_CONF_LOCATION, "w") as f:
f.write(json.dumps(d, sort_keys=True, indent=4, separators=(',', ': ')))
def _create_template_file():
if os.path.isfile(_CONF_LOCATION):
return False
with open(_CONF_LOCATION, "w") as f:
f.write(_TEMPLATE)
return True
def _are_credentials_valid(username, password, host):
tl = TippiLink(username, password, host)
try:
tl.get_connected_clients()
return True
except:
return False
class Sharfoo(rumps.App):
def __init__(self):
super(Sharfoo, self).__init__("Initializing")
self.menu = ["Restart router"]
self.connected_mac = set()
self.mac_to_name = dict()
self.mac_to_ip = dict()
if _create_template_file():
rumps.alert("Created config file",
"Please enter router admin credentials in %s and press OK" % _CONF_LOCATION)
username, password, host, self.bssid = _read_admin_credentials()
if self.bssid is None:
while True:
if _are_credentials_valid(username, password, host):
break
else:
rumps.alert("Invalid credentials, please update and click OK")
username, password, host, _ = _read_admin_credentials()
_, self.bssid = _get_bssid()
_update_config_file(username, password, host, self.bssid)
self.tl = TippiLink(username, password, host)
def _clear_data(self):
for conn in self.connected_mac:
del self.menu[self.mac_to_name[conn]]
self.connected_mac = set()
self.mac_to_ip = dict()
self.mac_to_name = dict()
@rumps.timer(5)
def update_title(self, _):
try:
is_on, connected_bssid = _get_bssid()
if not is_on:
self.title = "Wifi Off"
self._clear_data()
return
if connected_bssid != self.bssid:
self.title = "Roaming"
self._clear_data()
return
try:
clients = self.tl.get_connected_clients()
except:
self.title = "No Access"
return
# TODO: Parse MAC adresses to identify make and model
total = len(clients)
print "Connected: %d" % total
self.title = "Connected: %d" % total
for client in clients:
self.mac_to_name[client['mac_address']] = client['client_name']
self.mac_to_ip[client['mac_address']] = client['ip']
if len(self.connected_mac) > 0: # Skip on first run
current_macs = {client['mac_address'] for client in clients}
new_joined = current_macs - self.connected_mac
new_left = self.connected_mac - current_macs
for joined in new_joined:
self.menu.add(self.mac_to_name[joined])
_send_os_notification(self.mac_to_name[joined] + "\n" + self.mac_to_ip[joined], "Joined Wifi")
for left in new_left:
del self.menu[self.mac_to_name[left]]
_send_os_notification(self.mac_to_name[left] + "\n" + self.mac_to_ip[left], "Left Wifi")
else: # Run only first time
self.menu = [client['client_name'] for client in clients]
self.connected_mac = {client["mac_address"] for client in clients}
except Exception as e:
print e
self.title = "<ERROR 101>"
@rumps.clicked("Restart router")
def restart_router(self, _):
print "Restarting router"
self.title = "Restarting router"
self.tl.restart_router()
if __name__ == "__main__":
app = Sharfoo().run()