-
Notifications
You must be signed in to change notification settings - Fork 4
/
manager.py
executable file
·117 lines (96 loc) · 3.96 KB
/
manager.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
#!/usr/bin/env python3
import time
import logging
import subprocess
from db_lounge import DBLoungeManager
from db_wifionice import DBWifiOnICEManager
from db_cdwifi import DBCDWiFiManager
from db_wifiatdb import DBWifiAtDBDecider
class DBManager:
managers = {
DBLoungeManager.SSID: DBLoungeManager,
DBWifiOnICEManager.SSID: DBWifiOnICEManager,
DBWifiOnICEManager.SSID_secondary: DBWifiOnICEManager,
DBCDWiFiManager.SSID: DBCDWiFiManager,
DBWifiAtDBDecider.SSID: DBWifiAtDBDecider
}
manager = None
def __init__(self, batch_mode=False, ssid=""):
self.batch_mode = batch_mode
if ssid:
manager = self.managers.get(ssid)
if manager:
self.manager = manager()
logging.basicConfig(level=logging.WARNING if batch_mode else logging.INFO)
self.is_online = None
self.new_api = None
def run(self):
if self.batch_mode:
self.manager = self.get_login_manager()
if not self.manager:
return
iteration_ = 5
while iteration_ > 0:
self.manager.update_online()
print('DB: !' if not self.manager.is_online else 'DB: {:.0%}'.format(self.manager.get_quota()))
if self.manager.is_online:
return
else:
self.manager.login()
time.sleep(1)
iteration_ -= 1
else:
while not time.sleep(1):
self.manager = self.get_login_manager()
if not self.manager:
continue
self.manager.update_online()
if self.manager.is_online:
quota = self.manager.get_quota()
if quota < 1:
print('Quota: {0:.0%}\r'.format(quota), end='', flush=True)
else:
print("Quota surpassed, your traffic is being slowed! MAC-Change suggested")
elif self.manager.is_online is None:
continue
else:
self.manager.login()
def get_login_manager(self):
if self.manager is not None:
return self.manager
interface_ssids = []
res = subprocess.run(['/bin/ip', 'a'], stdout=subprocess.PIPE)
up_interfaces = [l.split(b':')[1].strip() for l in res.stdout.split(b'\n') if b"state UP" in l]
for interface in up_interfaces:
res = subprocess.run(['/sbin/iw', 'dev', interface, 'link'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
iw_result = res.stdout.decode('utf-8')
for line in iw_result.split('\n'):
if line.startswith('command failed'):
break
if 'ssid' in line.lower():
interface_ssids.append(line.strip().split()[-1])
break
for ssid in interface_ssids:
manager = self.managers.get(ssid)
if manager is None:
continue
if hasattr(manager, "PROVIDERS"):
provider = manager()
manager = provider.get_specific_provider()
if type(manager) is type:
# If not yet in instanciated, do
manager = manager()
self.managers[ssid] = manager
return manager
if __name__ == '__main__':
import argparse
argparser = argparse.ArgumentParser(description="Keeps your Wifi logged into the various DB Wifis")
argparser.add_argument('-b', '--batch', action='store_true',
help='Just check status and login, if not yet.')
argparser.add_argument('ssid', nargs="?", type=str, help="If you already know the SSID and it's not gonna change")
args = argparser.parse_args()
db_manager = DBManager(batch_mode=args.batch, ssid=args.ssid)
try:
db_manager.run()
except (KeyboardInterrupt, EOFError):
pass