2021-05-24 17:50:46 -04:00
|
|
|
from signal import SIGTERM, signal
|
|
|
|
from subprocess import CalledProcessError, check_output
|
|
|
|
from sys import exit
|
|
|
|
from time import sleep
|
|
|
|
|
2021-09-16 16:36:42 -04:00
|
|
|
INITIAL_DELAY_IN_SECONDS = 40
|
2021-05-24 17:50:46 -04:00
|
|
|
PING_INTERVAL_IN_SECONDS = 80
|
|
|
|
|
|
|
|
|
2021-05-25 11:59:19 -04:00
|
|
|
class WifiKeepalive:
|
2021-05-24 17:50:46 -04:00
|
|
|
|
2021-05-25 11:59:19 -04:00
|
|
|
def __init__(self):
|
2021-05-25 13:30:11 -04:00
|
|
|
self.defaultRoute = None
|
2021-09-16 16:36:42 -04:00
|
|
|
self.initialDelay = INITIAL_DELAY_IN_SECONDS
|
2021-05-25 11:59:19 -04:00
|
|
|
self.pingInterval = PING_INTERVAL_IN_SECONDS
|
2021-05-25 13:30:11 -04:00
|
|
|
self.isConnected = False
|
2021-05-25 11:59:19 -04:00
|
|
|
self.pingCommand = 'ping -c 1 -W 2 -n'
|
2021-05-25 13:30:11 -04:00
|
|
|
self.connectedMessage = 'Connected to Wifi'
|
|
|
|
self.disconnectedMessage = 'No Wifi Connection'
|
2021-05-24 17:50:46 -04:00
|
|
|
|
2021-05-25 11:59:19 -04:00
|
|
|
def run(self):
|
2021-05-25 13:30:11 -04:00
|
|
|
self.prepare()
|
2021-05-24 17:50:46 -04:00
|
|
|
|
2021-05-25 13:30:11 -04:00
|
|
|
while True:
|
2021-05-25 11:59:19 -04:00
|
|
|
self.keepalive()
|
|
|
|
sleep(self.pingInterval)
|
2021-05-24 17:50:46 -04:00
|
|
|
|
2021-05-25 13:30:11 -04:00
|
|
|
def prepare(self):
|
2021-09-16 16:36:42 -04:00
|
|
|
sleep(self.initialDelay)
|
2021-05-25 13:30:11 -04:00
|
|
|
self.updateDefaultWifiRoute()
|
2021-05-24 17:50:46 -04:00
|
|
|
|
2021-05-25 11:59:19 -04:00
|
|
|
if self.defaultRoute:
|
2021-05-25 13:30:11 -04:00
|
|
|
self.connect(preparing=True)
|
2021-05-25 11:59:19 -04:00
|
|
|
else:
|
2021-05-25 13:30:11 -04:00
|
|
|
self.disconnect(preparing=True)
|
2021-05-24 17:50:46 -04:00
|
|
|
|
2021-05-25 13:30:11 -04:00
|
|
|
def keepalive(self):
|
|
|
|
if self.defaultRoute:
|
|
|
|
self.connect()
|
|
|
|
self.ping() or self.updateDefaultWifiRoute()
|
|
|
|
else:
|
|
|
|
self.disconnect()
|
|
|
|
self.updateDefaultWifiRoute()
|
2021-05-25 11:59:19 -04:00
|
|
|
|
2021-05-25 13:30:11 -04:00
|
|
|
def connect(self, preparing=False):
|
|
|
|
if not self.isConnected or preparing:
|
|
|
|
print(self.connectedMessage, flush=True)
|
|
|
|
self.isConnected = True
|
2021-05-25 11:59:19 -04:00
|
|
|
|
2021-05-25 13:30:11 -04:00
|
|
|
def disconnect(self, preparing=False):
|
|
|
|
if self.isConnected or preparing:
|
|
|
|
print(self.disconnectedMessage, flush=True)
|
|
|
|
self.isConnected = False
|
2021-05-25 11:59:19 -04:00
|
|
|
|
|
|
|
def ping(self):
|
|
|
|
try:
|
|
|
|
check_output([*self.pingCommand.split(), self.defaultRoute])
|
|
|
|
except CalledProcessError as e:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2021-05-25 13:30:11 -04:00
|
|
|
def updateDefaultWifiRoute(self):
|
2021-05-25 11:59:19 -04:00
|
|
|
defaults = [
|
|
|
|
r['gateway'] for r in self.getRoutes() if self.isDefaultWifiRoute(r)
|
|
|
|
]
|
|
|
|
|
2021-05-25 13:30:11 -04:00
|
|
|
self.defaultRoute = defaults[0] if defaults else None
|
2021-05-25 11:59:19 -04:00
|
|
|
|
|
|
|
def getRoutes(self):
|
|
|
|
outputLines = check_output(
|
|
|
|
['route', '-n']
|
|
|
|
).decode('utf-8').splitlines()[2:]
|
|
|
|
|
|
|
|
routes = map(lambda r: self.parseRoute(r), outputLines)
|
|
|
|
|
|
|
|
return routes
|
|
|
|
|
|
|
|
def parseRoute(self, routeLine):
|
|
|
|
destination, gateway, _, _, _, _, _, interface = routeLine.split()
|
|
|
|
|
|
|
|
return {'destination': destination, 'gateway': gateway, 'interface': interface}
|
|
|
|
|
|
|
|
def isDefaultWifiRoute(self, route):
|
|
|
|
return route['destination'] == '0.0.0.0' and route['interface'].startswith('w')
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
signal(SIGTERM, lambda s, f: exit(0))
|
|
|
|
WifiKeepalive().run()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|