Signed-off-by: Jeroen Oudshoorn <oudshoorn.jeroen@gmail.com>
This commit is contained in:
Jeroen Oudshoorn
2023-09-14 22:59:55 +02:00
committed by Jeroen Oudshoorn
parent 52be2aa90f
commit bb4d962957
8 changed files with 61 additions and 32 deletions

2
.idea/misc.xml generated
View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (pwnagotchi)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (pwnagotchi)" project-jdk-type="Python SDK" />
</project> </project>

2
.idea/pwnagotchi.iml generated
View File

@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.11 (pwnagotchi)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="PyDocumentationSettings"> <component name="PyDocumentationSettings">

View File

@ -3,7 +3,6 @@ import logging
import time import time
import re import re
from pwnagotchi._version import __version__ from pwnagotchi._version import __version__
_name = None _name = None

View File

@ -1 +1 @@
__version__ = '2.3.8' __version__ = '2.3.9'

View File

@ -2,12 +2,19 @@ import json
import logging import logging
import requests import requests
import websockets import websockets
import asyncio
import random
from requests.auth import HTTPBasicAuth from requests.auth import HTTPBasicAuth
from time import sleep from time import sleep
requests.adapters.DEFAULT_RETRIES = 5 # increase retries number requests.adapters.DEFAULT_RETRIES = 5 # increase retries number
ping_timeout = 90
ping_interval = 60
max_sleep = 2.0
def decode(r, verbose_errors=True): def decode(r, verbose_errors=True):
try: try:
@ -40,34 +47,62 @@ class Client(object):
async def start_websocket(self, consumer): async def start_websocket(self, consumer):
s = "%s/events" % self.websocket s = "%s/events" % self.websocket
async with websockets.connect(s, ping_interval=60, ping_timeout=90) as ws:
# More modern version of the approach below
# logging.info("Creating new websocket...")
# async for ws in websockets.connect(s):
# try:
# 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:
# sleep_time = max_sleep*random.random()
# logger.warning('Retrying websocket connection in {} sec'.format(sleep_time))
# await asyncio.sleep(sleep_time)
# continue
# restarted every time the connection fails
while True:
logging.info("creating new websocket...")
try:
async with websockets.connect(s, ping_interval=ping_interval, ping_timeout=ping_timeout) as ws:
# listener loop
while True: while True:
try: try:
async for msg in ws: async for msg in ws:
try: try:
await consumer(msg) await consumer(msg)
except Exception as ex: except Exception as ex:
logging.debug("Error while parsing event (%s)", ex) logging.debug("error while parsing event (%s)", ex)
except websockets.ConnectionClosedError: except websockets.exceptions.ConnectionClosedError:
logging.error("Lost websocket connection. Reconnecting...") try:
pong = await ws.ping()
await asyncio.wait_for(pong, timeout=ping_timeout)
logging.warning('ping OK, keeping connection alive...')
continue continue
except websockets.WebSocketException as wex: except:
logging.error("Websocket exception (%s)", wex) sleep_time = max_sleep*random.random()
continue logging.warning('ping error - retrying connection in {} sec'.format(sleep_time))
except OSError as e: await asyncio.sleep(sleep_time)
logging.error("Websocket OSError exception (%s) with parameter %s", e, s) break
continue except ConnectionRefusedError:
except Exception as e: sleep_time = max_sleep*random.random()
logging.error("Other exception (%s) with parameter %s", e, s) logging.warning('nobody seems to listen to the bettercap endpoint...')
logging.warning('retrying connection in {} sec'.format(sleep_time))
await asyncio.sleep(sleep_time)
continue continue
def run(self, command, verbose_errors=True): def run(self, command, verbose_errors=True):
for _ in range(0,2): for _ in range(0, 2):
try: try:
r = requests.post("%s/session" % self.url, auth=self.auth, json={'cmd': command}) r = requests.post("%s/session" % self.url, auth=self.auth, json={'cmd': command})
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
sleep_time = max_sleep*random.random()
logging.exception("Request connection error (%s) while running command (%s)", e, command) logging.exception("Request connection error (%s) while running command (%s)", e, command)
sleep(1) # Sleep for 1-s before trying a second time logging.warning('Retrying run in {} sec'.format(sleep_time))
sleep(sleep_time)
else: else:
break break

View File

@ -76,6 +76,7 @@ def on(event_name, *args, **kwargs):
for plugin_name in loaded.keys(): for plugin_name in loaded.keys():
one(plugin_name, event_name, *args, **kwargs) one(plugin_name, event_name, *args, **kwargs)
def locked_cb(lock_name, cb, *args, **kwargs): def locked_cb(lock_name, cb, *args, **kwargs):
global locks global locks

View File

@ -423,7 +423,6 @@ class BTTether(plugins.Plugin):
__license__ = 'GPL3' __license__ = 'GPL3'
__description__ = 'This makes the display reachable over bluetooth' __description__ = 'This makes the display reachable over bluetooth'
def __init__(self): def __init__(self):
self.ready = False self.ready = False
self.options = dict() self.options = dict()
@ -432,7 +431,6 @@ class BTTether(plugins.Plugin):
self.running = True self.running = True
self.status = '-' self.status = '-'
def on_loaded(self): def on_loaded(self):
# new config # new config
if 'devices' in self.options: if 'devices' in self.options:
@ -574,18 +572,15 @@ class BTTether(plugins.Plugin):
if any_device_connected: if any_device_connected:
self.status = 'C' self.status = 'C'
def on_unload(self, ui): def on_unload(self, ui):
self.running = False self.running = False
with ui._lock: with ui._lock:
ui.remove_element('bluetooth') ui.remove_element('bluetooth')
def on_ui_setup(self, ui): def on_ui_setup(self, ui):
with ui._lock: with ui._lock:
ui.add_element('bluetooth', LabeledValue(color=BLACK, label='BT', value='-', position=(ui.width() / 2 - 15, 0), ui.add_element('bluetooth', LabeledValue(color=BLACK, label='BT', value='-', position=(ui.width() / 2 - 15, 0),
label_font=fonts.Bold, text_font=fonts.Medium)) label_font=fonts.Bold, text_font=fonts.Medium))
def on_ui_update(self, ui): def on_ui_update(self, ui):
ui.set('bluetooth', self.status) ui.set('bluetooth', self.status)

View File

@ -22,4 +22,3 @@ torch
torchvision torchvision
stable_baselines3 stable_baselines3
RPi.GPIO RPi.GPIO
backoff