From ebeb22081b3a1ea538f65c196c35544da2046188 Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Wed, 16 Oct 2019 09:28:29 +0200 Subject: [PATCH 1/2] Improve logging and add already_connected check --- pwnagotchi/defaults.yml | 1 + pwnagotchi/plugins/default/bt-tether.py | 50 ++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/pwnagotchi/defaults.yml b/pwnagotchi/defaults.yml index 5034d49..e207f87 100644 --- a/pwnagotchi/defaults.yml +++ b/pwnagotchi/defaults.yml @@ -73,6 +73,7 @@ main: ip: '192.168.44.44' # ip from which your pwnagotchi should be reachable netmask: 24 interval: 1 # check every x minutes for device + share_internet: false # monitor interface to use iface: mon0 # command to run to bring the mon interface up in case it's not up already diff --git a/pwnagotchi/plugins/default/bt-tether.py b/pwnagotchi/plugins/default/bt-tether.py index 5614f25..067b31e 100644 --- a/pwnagotchi/plugins/default/bt-tether.py +++ b/pwnagotchi/plugins/default/bt-tether.py @@ -154,7 +154,40 @@ class BTNap: return None - def wait_for_device(self, timeout=30): + def is_connected(self): + """ + Check if already connected + """ + bt_dev = self.power(True) + + if not bt_dev: + return False + + try: + dev_remote = BTNap.find_device(self._mac, bt_dev) + return bool(BTNap.prop_get(dev_remote, 'Connected')) + except BTError: + pass + return False + + + def is_paired(self): + """ + Check if already connected + """ + bt_dev = self.power(True) + + if not bt_dev: + return False + + try: + dev_remote = BTNap.find_device(self._mac, bt_dev) + return bool(BTNap.prop_get(dev_remote, 'Paired')) + except BTError: + pass + return False + + def wait_for_device(self, timeout=15): """ Wait for device @@ -409,20 +442,35 @@ def on_ui_update(ui): INTERVAL.update() bt = BTNap(OPTIONS['mac']) + + logging.debug('BT-TETHER: Check if already connected and paired') + if bt.is_connected() and bt.is_paired(): + logging.debug('BT-TETHER: Already connected and paired') + ui.set('bluetooth', 'CON') + return + + logging.debug('BT-TETHER: Try to connect to mac') if bt.connect(): + logging.debug('BT-TETHER: Successfuly connected') btnap_iface = IfaceWrapper('bnep0') + logging.debug('BT-TETHER: Check interface') if btnap_iface.exists(): + logging.debug('BT-TETHER: Interface found') # check ip addr = f"{OPTIONS['ip']}/{OPTIONS['netmask']}" + logging.debug('BT-TETHER: Try to set ADDR to interface') if not btnap_iface.set_addr(addr): ui.set('bluetooth', 'ERR1') logging.error("Could not set ip of bnep0 to %s", addr) return + else: + logging.debug('BT-TETHER: Set ADDR to interface') # change route if sharking if OPTIONS['share_internet']: + logging.debug('BT-TETHER: Set routing and change resolv.conf') IfaceWrapper.set_route(".".join(OPTIONS['ip'].split('.')[:-1] + ['1'])) # im not proud about that # fix resolv.conf; dns over https ftw! with open('/etc/resolv.conf', 'r+') as resolv: From 5987f9300901067552913640f05298a4f5cfb200 Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Wed, 16 Oct 2019 09:41:47 +0200 Subject: [PATCH 2/2] Refracture code --- pwnagotchi/plugins/default/bt-tether.py | 76 +++++++++++++------------ 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/pwnagotchi/plugins/default/bt-tether.py b/pwnagotchi/plugins/default/bt-tether.py index 067b31e..3f686f7 100644 --- a/pwnagotchi/plugins/default/bt-tether.py +++ b/pwnagotchi/plugins/default/bt-tether.py @@ -447,43 +447,47 @@ def on_ui_update(ui): if bt.is_connected() and bt.is_paired(): logging.debug('BT-TETHER: Already connected and paired') ui.set('bluetooth', 'CON') - return - - logging.debug('BT-TETHER: Try to connect to mac') - if bt.connect(): - logging.debug('BT-TETHER: Successfuly connected') - btnap_iface = IfaceWrapper('bnep0') - - logging.debug('BT-TETHER: Check interface') - if btnap_iface.exists(): - logging.debug('BT-TETHER: Interface found') - # check ip - addr = f"{OPTIONS['ip']}/{OPTIONS['netmask']}" - - logging.debug('BT-TETHER: Try to set ADDR to interface') - if not btnap_iface.set_addr(addr): - ui.set('bluetooth', 'ERR1') - logging.error("Could not set ip of bnep0 to %s", addr) - return - else: - logging.debug('BT-TETHER: Set ADDR to interface') - - # change route if sharking - if OPTIONS['share_internet']: - logging.debug('BT-TETHER: Set routing and change resolv.conf') - IfaceWrapper.set_route(".".join(OPTIONS['ip'].split('.')[:-1] + ['1'])) # im not proud about that - # fix resolv.conf; dns over https ftw! - with open('/etc/resolv.conf', 'r+') as resolv: - nameserver = resolv.read() - if 'nameserver 9.9.9.9' not in nameserver: - resolv.seek(0) - resolv.write(nameserver + 'nameserver 9.9.9.9\n') - - ui.set('bluetooth', 'CON') - else: - ui.set('bluetooth', 'ERR2') else: - ui.set('bluetooth', 'NF') + logging.debug('BT-TETHER: Try to connect to mac') + if bt.connect(): + logging.info('BT-TETHER: Successfuly connected') + else: + logging.error('BT-TETHER: Could not connect') + ui.set('bluetooth', 'NF') + return + + btnap_iface = IfaceWrapper('bnep0') + logging.debug('BT-TETHER: Check interface') + if btnap_iface.exists(): + logging.debug('BT-TETHER: Interface found') + + # check ip + addr = f"{OPTIONS['ip']}/{OPTIONS['netmask']}" + + logging.debug('BT-TETHER: Try to set ADDR to interface') + if not btnap_iface.set_addr(addr): + ui.set('bluetooth', 'ERR1') + logging.error("BT-TETHER: Could not set ip of bnep0 to %s", addr) + return + else: + logging.debug('BT-TETHER: Set ADDR to interface') + + # change route if sharking + if OPTIONS['share_internet']: + logging.debug('BT-TETHER: Set routing and change resolv.conf') + IfaceWrapper.set_route(".".join(OPTIONS['ip'].split('.')[:-1] + ['1'])) # im not proud about that + # fix resolv.conf; dns over https ftw! + with open('/etc/resolv.conf', 'r+') as resolv: + nameserver = resolv.read() + if 'nameserver 9.9.9.9' not in nameserver: + logging.info('BT-TETHER: Added nameserver') + resolv.seek(0) + resolv.write(nameserver + 'nameserver 9.9.9.9\n') + + ui.set('bluetooth', 'CON') + else: + logging.error('BT-TETHER: bnep0 not found') + ui.set('bluetooth', 'ERR2') def on_ui_setup(ui):