mirror of
https://github.com/jayofelony/pwnagotchi.git
synced 2025-07-01 18:37:27 -04:00
BT-Tether: add a check to DNS config
This commit is contained in:
@ -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}$"
|
||||
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):
|
||||
__author__ = "Jayofelony, modified my fmatray"
|
||||
__version__ = "1.4"
|
||||
@ -138,8 +139,7 @@ class BTTether(plugins.Plugin):
|
||||
@staticmethod
|
||||
def exec_cmd(cmd, args, pattern=None):
|
||||
try:
|
||||
result = subprocess.run([cmd] + args,
|
||||
check=True, capture_output=True, text=True)
|
||||
result = subprocess.run([cmd] + args, check=True, capture_output=True, text=True)
|
||||
if pattern:
|
||||
return result.stdout.find(pattern)
|
||||
return result
|
||||
@ -152,7 +152,7 @@ class BTTether(plugins.Plugin):
|
||||
|
||||
def nmcli(self, args, pattern=None):
|
||||
return self.exec_cmd("nmcli", args, pattern)
|
||||
|
||||
|
||||
def on_loaded(self):
|
||||
logging.info("[BT-Tether] plugin loaded.")
|
||||
|
||||
@ -162,7 +162,7 @@ class BTTether(plugins.Plugin):
|
||||
return
|
||||
if not ("mac" in self.options and re.match(MAC_PTTRN, self.options["mac"])):
|
||||
logging.error("[BT-Tether] Error with mac address")
|
||||
return
|
||||
return
|
||||
|
||||
if not ("phone" in self.options and self.options["phone"].lower() in ["android", "ios"]):
|
||||
logging.error("[BT-Tether] Phone type not supported")
|
||||
@ -181,23 +181,30 @@ class BTTether(plugins.Plugin):
|
||||
self.mac = self.options["mac"]
|
||||
dns = self.options.get("dns", "8.8.8.8 1.1.1.1")
|
||||
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
|
||||
dns = re.sub("[\s,;]+", " ", dns).strip() # DNS cleaning
|
||||
dns = re.sub("[\s,;]+", " ", dns).strip() # DNS cleaning
|
||||
|
||||
try:
|
||||
# Configure connection. Metric is set to 200 to prefer connection over USB
|
||||
self.nmcli(["connection", "modify", f"{self.phone_name}",
|
||||
"connection.type", "bluetooth",
|
||||
"bluetooth.type", "panu",
|
||||
"bluetooth.bdaddr", f"{self.mac}",
|
||||
"connection.autoconnect", "yes",
|
||||
"connection.autoconnect-retries", "0",
|
||||
"ipv4.method", "manual",
|
||||
"ipv4.dns", f"{dns}",
|
||||
"ipv4.addresses", f"{address}/24",
|
||||
"ipv4.gateway", f"{gateway}",
|
||||
"ipv4.route-metric", "200" ])
|
||||
self.nmcli(
|
||||
[
|
||||
"connection", "modify", f"{self.phone_name}",
|
||||
"connection.type", "bluetooth",
|
||||
"bluetooth.type", "panu",
|
||||
"bluetooth.bdaddr", f"{self.mac}",
|
||||
"connection.autoconnect", "yes",
|
||||
"connection.autoconnect-retries", "0",
|
||||
"ipv4.method", "manual",
|
||||
"ipv4.dns", f"{dns}",
|
||||
"ipv4.addresses", f"{address}/24",
|
||||
"ipv4.gateway", f"{gateway}",
|
||||
"ipv4.route-metric", "200",
|
||||
]
|
||||
)
|
||||
self.nmcli(["connection", "reload"])
|
||||
self.ready = True
|
||||
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}")
|
||||
return
|
||||
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}"])
|
||||
except Exception as 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):
|
||||
with ui._lock:
|
||||
ui.add_element('bluetooth', LabeledValue(color=BLACK, label='BT', value='-',
|
||||
position=(ui.width() / 2 - 10, 0),
|
||||
label_font=fonts.Bold, text_font=fonts.Medium))
|
||||
ui.add_element(
|
||||
"bluetooth",
|
||||
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):
|
||||
if not self.ready:
|
||||
return
|
||||
@ -240,17 +256,26 @@ class BTTether(plugins.Plugin):
|
||||
status = ""
|
||||
try:
|
||||
# Checking connection
|
||||
if self.nmcli(["-w", "0", "-g", "GENERAL.STATE", "connection", "show", self.phone_name],
|
||||
"activated") != -1:
|
||||
if (
|
||||
self.nmcli(["-w", "0", "-g", "GENERAL.STATE", "connection", "show", self.phone_name],
|
||||
"activated",
|
||||
)
|
||||
!= -1
|
||||
):
|
||||
ui.set("bluetooth", "U")
|
||||
return
|
||||
else:
|
||||
ui.set("bluetooth", "D")
|
||||
status = "BT Conn. down"
|
||||
|
||||
|
||||
# Checking device
|
||||
if self.nmcli(["-w", "0", "-g", "GENERAL.STATE", "device", "show", self.mac],
|
||||
"(connected)") != -1:
|
||||
if (
|
||||
self.nmcli(
|
||||
["-w", "0", "-g", "GENERAL.STATE", "device", "show", self.mac],
|
||||
"(connected)",
|
||||
)
|
||||
!= -1
|
||||
):
|
||||
ui.set("bluetooth", "C")
|
||||
status += "\nBT dev conn."
|
||||
else:
|
||||
@ -269,26 +294,28 @@ class BTTether(plugins.Plugin):
|
||||
if path == "/" or not path:
|
||||
try:
|
||||
bluetooth = self.bluetoothctl(["info", self.mac])
|
||||
bluetooth = bluetooth.stdout.replace('\n', '<br>')
|
||||
bluetooth = bluetooth.stdout.replace("\n", "<br>")
|
||||
except Exception as e:
|
||||
bluetooth = "Error while checking bluetoothctl"
|
||||
|
||||
try:
|
||||
device = self.nmcli(["-w", "0","device", "show", self.mac])
|
||||
device = device.stdout.replace('\n', '<br>')
|
||||
|
||||
try:
|
||||
device = self.nmcli(["-w", "0", "device", "show", self.mac])
|
||||
device = device.stdout.replace("\n", "<br>")
|
||||
except Exception as e:
|
||||
device = "Error while checking nmcli device"
|
||||
|
||||
|
||||
try:
|
||||
connection = self.nmcli(["-w", "0","connection", "show", self.phone_name])
|
||||
connection = connection.stdout.replace('\n', '<br>')
|
||||
connection = self.nmcli(["-w", "0", "connection", "show", self.phone_name])
|
||||
connection = connection.stdout.replace("\n", "<br>")
|
||||
except Exception as e:
|
||||
connection = "Error while checking nmcli connection"
|
||||
|
||||
logging.debug(device)
|
||||
return render_template_string(TEMPLATE,
|
||||
title="BT-Tether",
|
||||
bluetooth=bluetooth,
|
||||
device=device,
|
||||
connection=connection)
|
||||
abort(404)
|
||||
return render_template_string(
|
||||
TEMPLATE,
|
||||
title="BT-Tether",
|
||||
bluetooth=bluetooth,
|
||||
device=device,
|
||||
connection=connection,
|
||||
)
|
||||
abort(404)
|
||||
|
Reference in New Issue
Block a user