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
@ -181,13 +181,18 @@ 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", "modify", f"{self.phone_name}",
"connection.type", "bluetooth", "connection.type", "bluetooth",
"bluetooth.type", "panu", "bluetooth.type", "panu",
"bluetooth.bdaddr", f"{self.mac}", "bluetooth.bdaddr", f"{self.mac}",
@ -197,7 +202,9 @@ class BTTether(plugins.Plugin):
"ipv4.dns", f"{dns}", "ipv4.dns", f"{dns}",
"ipv4.addresses", f"{address}/24", "ipv4.addresses", f"{address}/24",
"ipv4.gateway", f"{gateway}", "ipv4.gateway", f"{gateway}",
"ipv4.route-metric", "200" ]) "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")
@ -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(
"bluetooth",
LabeledValue(
color=BLACK,
label="BT",
value="-",
position=(ui.width() / 2 - 10, 0), position=(ui.width() / 2 - 10, 0),
label_font=fonts.Bold, text_font=fonts.Medium)) 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,8 +256,12 @@ 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:
@ -249,8 +269,13 @@ class BTTether(plugins.Plugin):
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(
TEMPLATE,
title="BT-Tether", title="BT-Tether",
bluetooth=bluetooth, bluetooth=bluetooth,
device=device, device=device,
connection=connection) connection=connection,
)
abort(404) abort(404)