diff --git a/pwnagotchi/bettercap.py b/pwnagotchi/bettercap.py index 8616c26a..d1cf0cbb 100644 --- a/pwnagotchi/bettercap.py +++ b/pwnagotchi/bettercap.py @@ -3,8 +3,9 @@ import requests import websockets from requests.auth import HTTPBasicAuth -import asyncio # Add asyncio for async functionality -from time import sleep # Add sleep function +from time import sleep + +requests.adapters.DEFAULT_RETRIES = 5 # increase retries number def decode(r, verbose_errors=True): @@ -22,8 +23,7 @@ def decode(r, verbose_errors=True): class Client(object): - def __init__(self, hostname='localhost', scheme='http', port=8081, username='user', password='pass', - max_websockets=5): + def __init__(self, hostname='localhost', scheme='http', port=8081, username='user', password='pass'): self.hostname = hostname self.scheme = scheme self.port = port @@ -32,39 +32,54 @@ class Client(object): self.url = "%s://%s:%d/api" % (scheme, hostname, port) self.websocket = "ws://%s:%s@%s:%d/api" % (username, password, hostname, port) self.auth = HTTPBasicAuth(username, password) - self.max_websockets = max_websockets - self.websocket_semaphore = asyncio.Semaphore(max_websockets) - # session takes optional argument to pull a sub-dictionary - # ex.: "session/wifi", "session/ble" - def session(self, sess="session"): - r = requests.get("%s/%s" % (self.url, sess), auth=self.auth) + def session(self): + r = requests.get("%s/session" % self.url, auth=self.auth) return decode(r) async def start_websocket(self, consumer): s = "%s/events" % self.websocket - while True: - try: - async with websockets.connect(s, ping_interval=60, ping_timeout=90) as ws: + # while True: + # try: + # async with websockets.connect(s, ping_interval=60, ping_timeout=90) as ws: + # async for msg in ws: + # try: + # await consumer(msg) + # except Exception as ex: + # logging.debug("Error while parsing event (%s)", ex) + # except websockets.exceptions.ConnectionClosedError: + # logging.debug("Lost websocket connection. Reconnecting...") + # except websockets.exceptions.WebSocketException as wex: + # logging.debug("Websocket exception (%s)", wex) + # except OSError as e: + # logging.debug("Websocket OSError exception (%s) with parameter %s", e, s) + # except Exception as e: + # logging.debug("Other exception (%s) with parameter %s", e, s) + + async with websockets.connect(s, ping_interval=60, ping_timeout=90) as ws: + while True: + try: async for msg in ws: try: await consumer(msg) except Exception as ex: - logging.debug("Error while parsing event (%s)", ex) - except websockets.ConnectionClosedError: - logging.debug("Lost websocket connection. Reconnecting...") - except websockets.WebSocketException as wex: - logging.debug("Websocket exception (%s)", wex) - except Exception as e: - logging.exception("Other error while opening websocket (%s) with parameter %s", e, s) + logging.debug("Error while parsing event (%s)", ex) + except websockets.exceptions.ConnectionClosedError: + logging.error("Lost websocket connection. Reconnecting...") + except websockets.exceptions.WebSocketException as wex: + logging.error("Websocket exception (%s)", wex) + except OSError as e: + logging.error("Websocket OSError exception (%s) with parameter %s", e, s) + except Exception as e: + logging.error("Other exception (%s) with parameter %s", e, s) def run(self, command, verbose_errors=True): - for _ in range(0, 2): + for _ in range(0,2): try: r = requests.post("%s/session" % self.url, auth=self.auth, json={'cmd': command}) except requests.exceptions.ConnectionError as e: logging.exception("Request connection error (%s) while running command (%s)", e, command) - sleep(1) # Sleep for 1-s before trying a second time + sleep(1) # Sleep for 1-s before trying a second time else: break