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..3f686f7 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,33 +442,52 @@ def on_ui_update(ui): INTERVAL.update() bt = BTNap(OPTIONS['mac']) - if bt.connect(): - btnap_iface = IfaceWrapper('bnep0') - if btnap_iface.exists(): - # check ip - addr = f"{OPTIONS['ip']}/{OPTIONS['netmask']}" - - if not btnap_iface.set_addr(addr): - ui.set('bluetooth', 'ERR1') - logging.error("Could not set ip of bnep0 to %s", addr) - return - - # change route if sharking - if OPTIONS['share_internet']: - 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') + 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') 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):