Improved systemwide speed, by Turning Wireguard subprocess commands into to recycled generic_run_commands that re-use the terminal
This commit is contained in:
parent
ed055f338d
commit
0e64351b5a
3 changed files with 54 additions and 12 deletions
|
|
@ -117,6 +117,12 @@ class ConfigurationController:
|
|||
configuration.firewall = new_value
|
||||
configuration.save()
|
||||
|
||||
@staticmethod
|
||||
def get_firewall_setting():
|
||||
configuration = ConfigurationController.get_or_new()
|
||||
return configuration.firewall
|
||||
|
||||
|
||||
@staticmethod
|
||||
def change_dns(new_value):
|
||||
configuration = ConfigurationController.get_or_new()
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
from core.services.networking.general_connection_tools.testing_evaluating import await_connection, system_uses_wireguard_interface, terminate_tor_connection
|
||||
from core.services.keys_and_verifications.endpoint_verification import verify_wireguard_endpoint
|
||||
from core.models.Result import Result, ResultError
|
||||
from core.services.networking.systemwide.systemwide_utils import extract_wg_interface_name, check_system_prereqs, get_firewall_setting, get_dns_setting
|
||||
from core.services.networking.systemwide.systemwide_utils import extract_wg_interface_name, check_system_prereqs, get_dns_setting
|
||||
# , get_firewall_setting
|
||||
# from core.services.networking.general_connection_tools.config_tools import extract_endpoint_ip, extract_dns
|
||||
from core.services.networking.systemwide import killswitch
|
||||
from core.services.networking.systemwide.dns_tools.process_dns_result import orchestrate_dns_check
|
||||
|
|
@ -10,6 +11,7 @@ from core.services.networking.systemwide.wireguard.nmcli_tools import setup_nmcl
|
|||
from core.services.networking.systemwide.wireguard.wg_firewall_dns import set_dns, revert_dns, turn_on_firewall, check_and_kill_firewall, enable_firewall_with_retry
|
||||
from core.errors.logger import logger
|
||||
from core.errors.exceptions import FirewallError
|
||||
from core.utils.run_commands import run_generic_command
|
||||
|
||||
from core.Constants import Constants
|
||||
from core.Errors import ConnectionTerminationError, CommandNotFoundError
|
||||
|
|
@ -35,7 +37,7 @@ def terminate_system_connection(
|
|||
|
||||
# ====== PREREQS =======
|
||||
if firewall_setting is None:
|
||||
firewall_setting = get_firewall_setting()
|
||||
firewall_setting = ConfigurationController.get_firewall_setting()
|
||||
|
||||
if dns_setting is None:
|
||||
dns_setting = get_dns_setting()
|
||||
|
|
@ -53,12 +55,22 @@ def terminate_system_connection(
|
|||
|
||||
# ====== NMCLI =======
|
||||
if SystemStateController.exists():
|
||||
process = subprocess.Popen(('nmcli', 'connection', 'delete', 'wg'), stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
|
||||
completed_successfully = not bool(os.waitpid(process.pid, 0)[1] >> 8)
|
||||
command = ['nmcli', 'connection', 'delete', 'wg']
|
||||
human_readable_goal = "Deleting NMCLI wg interface"
|
||||
delete_completed = run_generic_command(command, human_readable_goal, timeout=8)
|
||||
|
||||
if completed_successfully or not system_uses_wireguard_interface():
|
||||
# legacy version:
|
||||
# process = subprocess.Popen((), stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
|
||||
# completed_successfully = not bool(os.waitpid(process.pid, 0)[1] >> 8)
|
||||
# if completed_successfully or not system_uses_wireguard_interface():
|
||||
|
||||
if delete_completed.valid or not system_uses_wireguard_interface():
|
||||
command = ['nmcli', 'connection', 'delete', 'hv-ipv6-sink']
|
||||
human_readable_goal = "Deleting NMCLI IPv6 Sink"
|
||||
sinkhole_deleted = run_generic_command(command, human_readable_goal, timeout=8)
|
||||
if not sinkhole_deleted.valid:
|
||||
logger.error(f"IPv6 Sinkhole Delete Error: {sinkhole_deleted.error_type} {sinkhole_deleted.message}")
|
||||
|
||||
subprocess.run(('nmcli', 'connection', 'delete', 'hv-ipv6-sink'), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
terminate_tor_connection()
|
||||
|
||||
# confirm firewall is off.
|
||||
|
|
@ -69,7 +81,9 @@ def terminate_system_connection(
|
|||
SystemState.dissolve()
|
||||
|
||||
else:
|
||||
raise ConnectionTerminationError('The connection could not be terminated.')
|
||||
error_log = f"Error with deleting the connection: {delete_completed.error_type} {delete_completed.message}"
|
||||
logger.error(error_log)
|
||||
raise ConnectionTerminationError(f'The connection could not be terminated. {error_log}')
|
||||
|
||||
|
||||
|
||||
|
|
@ -245,7 +259,7 @@ def establish_system_connection(
|
|||
verify_wireguard_endpoint(profile, ignore=ignore)
|
||||
|
||||
# ================= SETTINGS =================
|
||||
firewall_setting = get_firewall_setting()
|
||||
firewall_setting = ConfigurationController.get_firewall_setting()
|
||||
dns_setting = get_dns_setting()
|
||||
|
||||
# ================= CONNECT WG ================
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
from core.utils.run_commands import run_generic_command
|
||||
from core.models.Result import Result, ResultError
|
||||
from subprocess import CalledProcessError
|
||||
import os
|
||||
|
|
@ -8,8 +9,13 @@ import subprocess
|
|||
|
||||
def setup_nmcli_connection(wg_config_path: str) -> Result:
|
||||
try:
|
||||
process_output = subprocess.check_output(('nmcli', 'connection', 'import', '--temporary', 'type', 'wireguard', 'file', wg_config_path), text=True)
|
||||
return Result(valid=True, data=process_output)
|
||||
command = ['nmcli', 'connection', 'import', '--temporary', 'type', 'wireguard', 'file', wg_config_path]
|
||||
human_readable_goal = "nmcli connection setup via import"
|
||||
return run_generic_command(command, human_readable_goal, timeout=7)
|
||||
|
||||
# legacy:
|
||||
# process_output = subprocess.check_output(('nmcli', 'connection', 'import', '--temporary', 'type', 'wireguard', 'file', wg_config_path), text=True)
|
||||
# return Result(valid=True, data=process_output)
|
||||
|
||||
except CalledProcessError as e:
|
||||
return Result(valid=False, error_type=ResultError.NMCLI, message=f"Called Process error with nmcli: {e}")
|
||||
|
|
@ -20,14 +26,30 @@ def setup_ipv6_sinkhole(process_output: str) -> Result:
|
|||
error_info = "with nmcli setting up the IPv6 sinkhole, error is "
|
||||
try:
|
||||
connection_id = (m := re.search(r'(?<=\()([a-f0-9-]+?)(?=\))', process_output)) and m.group(1)
|
||||
ipv6_method = subprocess.check_output(('nmcli', '-g', 'ipv6.method', 'connection', 'show', connection_id), text=True).strip()
|
||||
|
||||
ipv6_command = ['nmcli', '-g', 'ipv6.method', 'connection', 'show', connection_id]
|
||||
human_readable_goal = "IPv6 nmcli connection show"
|
||||
sinkhole_output = run_generic_command(ipv6_command, human_readable_goal, timeout=7)
|
||||
if sinkhole_output.valid:
|
||||
ipv6_method = sinkhole_output.data.strip()
|
||||
else:
|
||||
logger.error(f"Running {human_readable_goal} gave the error {sinkhole_output.error_type} with {sinkhole_output.message}")
|
||||
raise ConnectionError('The connection could not be established.')
|
||||
|
||||
# legacy:
|
||||
# ipv6_method = subprocess.check_output(('nmcli', '-g', 'ipv6.method', 'connection', 'show', connection_id), text=True).strip()
|
||||
|
||||
|
||||
except CalledProcessError as e:
|
||||
return Result(valid=False, error_type=ResultError.NMCLI, message=f"Called Process error {error_info}: {e}")
|
||||
# raise ConnectionError('The connection could not be established.')
|
||||
|
||||
if ipv6_method in ('disabled', 'ignore'):
|
||||
try:
|
||||
subprocess.run(('dbus-send', '--system', '--print-reply', '--dest=org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager', 'org.freedesktop.DBus.Properties.Set', 'string:org.freedesktop.NetworkManager', 'string:ConnectivityCheckEnabled', 'variant:boolean:false'), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)
|
||||
command = ['dbus-send', '--system', '--print-reply', '--dest=org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager', 'org.freedesktop.DBus.Properties.Set', 'string:org.freedesktop.NetworkManager', 'string:ConnectivityCheckEnabled', 'variant:boolean:false']
|
||||
human_readable_goal = "DBUS-send for IPv6 Sinkhole"
|
||||
dbus_sinkhole = run_generic_command(command, human_readable_goal, timeout=7)
|
||||
|
||||
except CalledProcessError as e:
|
||||
return Result(valid=False, error_type=ResultError.NMCLI, message=f"Called Process error {error_info}: {e}")
|
||||
# raise ConnectionError('The connection could not be established.')
|
||||
|
|
|
|||
Loading…
Reference in a new issue