BT-Tether: add a check to DNS config

This commit is contained in:
frédéric
2025-02-12 14:10:33 +01:00
parent f20d4017bd
commit 1d635d955b

View File

@ -123,6 +123,7 @@ MAC_PTTRN = r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"
IP_PTTRN = r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$" IP_PTTRN = r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
DNS_PTTRN = r"^\s*((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*[ ,;]\s*)+((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*[ ,;]?\s*)$" DNS_PTTRN = r"^\s*((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*[ ,;]\s*)+((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*[ ,;]?\s*)$"
class BTTether(plugins.Plugin): class BTTether(plugins.Plugin):
__author__ = "Jayofelony, modified my fmatray" __author__ = "Jayofelony, modified my fmatray"
__version__ = "1.4" __version__ = "1.4"
@ -138,8 +139,7 @@ class BTTether(plugins.Plugin):
@staticmethod @staticmethod
def exec_cmd(cmd, args, pattern=None): def exec_cmd(cmd, args, pattern=None):
try: try:
result = subprocess.run([cmd] + args, result = subprocess.run([cmd] + args, check=True, capture_output=True, text=True)
check=True, capture_output=True, text=True)
if pattern: if pattern:
return result.stdout.find(pattern) return result.stdout.find(pattern)
return result return result
@ -152,7 +152,7 @@ class BTTether(plugins.Plugin):
def nmcli(self, args, pattern=None): def nmcli(self, args, pattern=None):
return self.exec_cmd("nmcli", args, pattern) return self.exec_cmd("nmcli", args, pattern)
def on_loaded(self): def on_loaded(self):
logging.info("[BT-Tether] plugin loaded.") logging.info("[BT-Tether] plugin loaded.")
@ -162,7 +162,7 @@ class BTTether(plugins.Plugin):
return return
if not ("mac" in self.options and re.match(MAC_PTTRN, self.options["mac"])): if not ("mac" in self.options and re.match(MAC_PTTRN, self.options["mac"])):
logging.error("[BT-Tether] Error with mac address") logging.error("[BT-Tether] Error with mac address")
return return
if not ("phone" in self.options and self.options["phone"].lower() in ["android", "ios"]): if not ("phone" in self.options and self.options["phone"].lower() in ["android", "ios"]):
logging.error("[BT-Tether] Phone type not supported") logging.error("[BT-Tether] Phone type not supported")
@ -181,23 +181,30 @@ class BTTether(plugins.Plugin):
self.mac = self.options["mac"] self.mac = self.options["mac"]
dns = self.options.get("dns", "8.8.8.8 1.1.1.1") dns = self.options.get("dns", "8.8.8.8 1.1.1.1")
if not re.match(DNS_PTTRN, dns): if not re.match(DNS_PTTRN, dns):
logging.error(f"[BT-Tether] DNS error: {dns}") if dns == "":
logging.error(f"[BT-Tether] Empty DNS setting")
else:
logging.error(f"[BT-Tether] Wrong DNS setting: '{dns}'")
return return
dns = re.sub("[\s,;]+", " ", dns).strip() # DNS cleaning dns = re.sub("[\s,;]+", " ", dns).strip() # DNS cleaning
try: try:
# Configure connection. Metric is set to 200 to prefer connection over USB # Configure connection. Metric is set to 200 to prefer connection over USB
self.nmcli(["connection", "modify", f"{self.phone_name}", self.nmcli(
"connection.type", "bluetooth", [
"bluetooth.type", "panu", "connection", "modify", f"{self.phone_name}",
"bluetooth.bdaddr", f"{self.mac}", "connection.type", "bluetooth",
"connection.autoconnect", "yes", "bluetooth.type", "panu",
"connection.autoconnect-retries", "0", "bluetooth.bdaddr", f"{self.mac}",
"ipv4.method", "manual", "connection.autoconnect", "yes",
"ipv4.dns", f"{dns}", "connection.autoconnect-retries", "0",
"ipv4.addresses", f"{address}/24", "ipv4.method", "manual",
"ipv4.gateway", f"{gateway}", "ipv4.dns", f"{dns}",
"ipv4.route-metric", "200" ]) "ipv4.addresses", f"{address}/24",
"ipv4.gateway", f"{gateway}",
"ipv4.route-metric", "200",
]
)
self.nmcli(["connection", "reload"]) self.nmcli(["connection", "reload"])
self.ready = True self.ready = True
logging.info(f"[BT-Tether] Connection {self.phone_name} configured") logging.info(f"[BT-Tether] Connection {self.phone_name} configured")
@ -205,7 +212,7 @@ class BTTether(plugins.Plugin):
logging.error(f"[BT-Tether] Error while configuring: {e}") logging.error(f"[BT-Tether] Error while configuring: {e}")
return return
try: try:
time.sleep(5) # Give some delay to configure before going up time.sleep(5) # Give some delay to configure before going up
self.nmcli(["connection", "up", f"{self.phone_name}"]) self.nmcli(["connection", "up", f"{self.phone_name}"])
except Exception as e: except Exception as e:
logging.error(f"[BT-Tether] Failed to connect to device: {e}") logging.error(f"[BT-Tether] Failed to connect to device: {e}")
@ -230,9 +237,18 @@ class BTTether(plugins.Plugin):
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='-', ui.add_element(
position=(ui.width() / 2 - 10, 0), "bluetooth",
label_font=fonts.Bold, text_font=fonts.Medium)) LabeledValue(
color=BLACK,
label="BT",
value="-",
position=(ui.width() / 2 - 10, 0),
label_font=fonts.Bold,
text_font=fonts.Medium,
),
)
def on_ui_update(self, ui): def on_ui_update(self, ui):
if not self.ready: if not self.ready:
return return
@ -240,17 +256,26 @@ class BTTether(plugins.Plugin):
status = "" status = ""
try: try:
# Checking connection # Checking connection
if self.nmcli(["-w", "0", "-g", "GENERAL.STATE", "connection", "show", self.phone_name], if (
"activated") != -1: self.nmcli(["-w", "0", "-g", "GENERAL.STATE", "connection", "show", self.phone_name],
"activated",
)
!= -1
):
ui.set("bluetooth", "U") ui.set("bluetooth", "U")
return return
else: else:
ui.set("bluetooth", "D") ui.set("bluetooth", "D")
status = "BT Conn. down" status = "BT Conn. down"
# Checking device # Checking device
if self.nmcli(["-w", "0", "-g", "GENERAL.STATE", "device", "show", self.mac], if (
"(connected)") != -1: self.nmcli(
["-w", "0", "-g", "GENERAL.STATE", "device", "show", self.mac],
"(connected)",
)
!= -1
):
ui.set("bluetooth", "C") ui.set("bluetooth", "C")
status += "\nBT dev conn." status += "\nBT dev conn."
else: else:
@ -269,26 +294,28 @@ class BTTether(plugins.Plugin):
if path == "/" or not path: if path == "/" or not path:
try: try:
bluetooth = self.bluetoothctl(["info", self.mac]) bluetooth = self.bluetoothctl(["info", self.mac])
bluetooth = bluetooth.stdout.replace('\n', '<br>') bluetooth = bluetooth.stdout.replace("\n", "<br>")
except Exception as e: except Exception as e:
bluetooth = "Error while checking bluetoothctl" bluetooth = "Error while checking bluetoothctl"
try: try:
device = self.nmcli(["-w", "0","device", "show", self.mac]) device = self.nmcli(["-w", "0", "device", "show", self.mac])
device = device.stdout.replace('\n', '<br>') device = device.stdout.replace("\n", "<br>")
except Exception as e: except Exception as e:
device = "Error while checking nmcli device" device = "Error while checking nmcli device"
try: try:
connection = self.nmcli(["-w", "0","connection", "show", self.phone_name]) connection = self.nmcli(["-w", "0", "connection", "show", self.phone_name])
connection = connection.stdout.replace('\n', '<br>') connection = connection.stdout.replace("\n", "<br>")
except Exception as e: except Exception as e:
connection = "Error while checking nmcli connection" connection = "Error while checking nmcli connection"
logging.debug(device) logging.debug(device)
return render_template_string(TEMPLATE, return render_template_string(
title="BT-Tether", TEMPLATE,
bluetooth=bluetooth, title="BT-Tether",
device=device, bluetooth=bluetooth,
connection=connection) device=device,
abort(404) connection=connection,
)
abort(404)