Improve logging and add already_connected check

This commit is contained in:
dadav 2019-10-16 09:28:29 +02:00
parent c449c77ef9
commit ebeb22081b
2 changed files with 50 additions and 1 deletions

View File

@ -73,6 +73,7 @@ main:
ip: '192.168.44.44' # ip from which your pwnagotchi should be reachable ip: '192.168.44.44' # ip from which your pwnagotchi should be reachable
netmask: 24 netmask: 24
interval: 1 # check every x minutes for device interval: 1 # check every x minutes for device
share_internet: false
# monitor interface to use # monitor interface to use
iface: mon0 iface: mon0
# command to run to bring the mon interface up in case it's not up already # command to run to bring the mon interface up in case it's not up already

View File

@ -154,7 +154,40 @@ class BTNap:
return None 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 Wait for device
@ -409,20 +442,35 @@ def on_ui_update(ui):
INTERVAL.update() INTERVAL.update()
bt = BTNap(OPTIONS['mac']) 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(): if bt.connect():
logging.debug('BT-TETHER: Successfuly connected')
btnap_iface = IfaceWrapper('bnep0') btnap_iface = IfaceWrapper('bnep0')
logging.debug('BT-TETHER: Check interface')
if btnap_iface.exists(): if btnap_iface.exists():
logging.debug('BT-TETHER: Interface found')
# check ip # check ip
addr = f"{OPTIONS['ip']}/{OPTIONS['netmask']}" addr = f"{OPTIONS['ip']}/{OPTIONS['netmask']}"
logging.debug('BT-TETHER: Try to set ADDR to interface')
if not btnap_iface.set_addr(addr): if not btnap_iface.set_addr(addr):
ui.set('bluetooth', 'ERR1') ui.set('bluetooth', 'ERR1')
logging.error("Could not set ip of bnep0 to %s", addr) logging.error("Could not set ip of bnep0 to %s", addr)
return return
else:
logging.debug('BT-TETHER: Set ADDR to interface')
# change route if sharking # change route if sharking
if OPTIONS['share_internet']: 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 IfaceWrapper.set_route(".".join(OPTIONS['ip'].split('.')[:-1] + ['1'])) # im not proud about that
# fix resolv.conf; dns over https ftw! # fix resolv.conf; dns over https ftw!
with open('/etc/resolv.conf', 'r+') as resolv: with open('/etc/resolv.conf', 'r+') as resolv: