mirror of
https://github.com/jayofelony/pwnagotchi.git
synced 2025-07-01 18:37:27 -04:00
Cleaner connection method for bt-tether
Signed-off-by: Jeroen Oudshoorn <oudshoorn.jeroen@gmail.com>
This commit is contained in:
@ -7,7 +7,7 @@ from pwnagotchi.ui.view import BLACK
|
||||
|
||||
class BTTether(plugins.Plugin):
|
||||
__author__ = 'Jayofelony'
|
||||
__version__ = '1.1'
|
||||
__version__ = '1.2'
|
||||
__license__ = 'GPL3'
|
||||
__description__ = 'A new BT-Tether plugin'
|
||||
|
||||
@ -15,11 +15,13 @@ class BTTether(plugins.Plugin):
|
||||
self.ready = False
|
||||
self.options = dict()
|
||||
self.status = '-'
|
||||
self._config = dict()
|
||||
|
||||
def on_loaded(self):
|
||||
logging.info("[BT-Tether] plugin loaded.")
|
||||
|
||||
def on_config_changed(self, config):
|
||||
self._config = config
|
||||
ip = config['main']['plugins']['bt-tether']['ip']
|
||||
phone_name = config['main']['plugins']['bt-tether']['phone-name'] + ' Network'
|
||||
if config['main']['plugins']['bt-tether']['phone'].lower() == 'android':
|
||||
@ -33,8 +35,7 @@ class BTTether(plugins.Plugin):
|
||||
return
|
||||
try:
|
||||
subprocess.run(['nmcli', 'connection', 'modify', f'{phone_name}', 'ipv4.addresses', f'{address}', 'ipv4.gateway',f'{gateway}', 'ipv4.route-metric', '100'], check=True)
|
||||
subprocess.run(['nmcli', 'connection', 'reload'], check=True)
|
||||
subprocess.run(['systemctl', 'restart', 'NetworkManager'], check=True)
|
||||
subprocess.run(['nmcli', 'connection', 'up', f'{phone_name}'], check=True)
|
||||
except Exception as e:
|
||||
logging.error(f"[BT-Tether] Failed to connect to device: {e}")
|
||||
self.ready = True
|
||||
@ -49,23 +50,26 @@ class BTTether(plugins.Plugin):
|
||||
label_font=fonts.Bold, text_font=fonts.Medium))
|
||||
|
||||
def on_ui_update(self, ui):
|
||||
phone_name = self._config['main']['plugins']['bt-tether']['phone-name'] + ' Network'
|
||||
if (subprocess.run(['bluetoothctl', 'info'], capture_output=True, text=True)).stdout.find('Connected: yes') != -1:
|
||||
self.status = 'C'
|
||||
else:
|
||||
self.status = '-'
|
||||
try:
|
||||
mac = self.options['mac']
|
||||
subprocess.run(['nmcli', 'device', 'connect', f'{mac}'], check=True)
|
||||
subprocess.run(['nmcli', 'connection', 'up', f'{phone_name}'], check=True)
|
||||
except Exception as e:
|
||||
logging.error(f"[BT-Tether] Failed to connect to device: {e}")
|
||||
ui.set('bluetooth', self.status)
|
||||
|
||||
def on_unload(self, ui):
|
||||
phone_name = self._config['main']['plugins']['bt-tether']['phone-name'] + ' Network'
|
||||
with ui._lock:
|
||||
ui.remove_element('bluetooth')
|
||||
try:
|
||||
mac = self.options['mac']
|
||||
subprocess.run(['nmcli', 'device', 'disconnect', f'{mac}'], check=True)
|
||||
logging.info(f"[BT-Tether] Disconnected from device with MAC: {mac}")
|
||||
if (subprocess.run(['bluetoothctl', 'info'], capture_output=True, text=True)).stdout.find('Connected: yes') != -1:
|
||||
subprocess.run(['nmcli', 'connection', 'down', f'{phone_name}'], check=True)
|
||||
logging.info(f"[BT-Tether] Disconnected from device with name: {phone_name}")
|
||||
else:
|
||||
logging.info(f"[BT-Tether] Device with name {phone_name} is not connected, not disconnecting")
|
||||
except Exception as e:
|
||||
logging.error(f"[BT-Tether] Failed to disconnect from device: {e}")
|
Reference in New Issue
Block a user