From 6be178490348086e4e1952df4eb7b11bfccf3956 Mon Sep 17 00:00:00 2001 From: Vxrtrauter <101264710+Vxrtrauter@users.noreply.github.com> Date: Fri, 6 Feb 2026 17:50:42 +0100 Subject: [PATCH] feat: add functionality to retrieve and bind IP address of network interfaces --- core/network/interface.py | 10 +++++++++- core/network/libtorrent_int.py | 27 +++++++++++++++++++++++++-- core/utils/data/state.py | 1 + main.py | 12 +++++++----- 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/core/network/interface.py b/core/network/interface.py index e3a70eb..8845d9b 100644 --- a/core/network/interface.py +++ b/core/network/interface.py @@ -41,4 +41,12 @@ def list_interfaces() -> None: def init_interfaces(): state.interfaces = list(addrs.keys()) - state.active_interfaces = get_active_interfaces() \ No newline at end of file + state.active_interfaces = get_active_interfaces() + +def get_interface_ip(interface_name): + if interface_name in addrs: + for addr in addrs[interface_name]: + if addr.family == 2: # ipv4 + if not addr.address.startswith("127.") and not addr.address.startswith("169.254"): + return addr.address + return None \ No newline at end of file diff --git a/core/network/libtorrent_int.py b/core/network/libtorrent_int.py index fcb14f3..42357ec 100644 --- a/core/network/libtorrent_int.py +++ b/core/network/libtorrent_int.py @@ -1,5 +1,6 @@ import time from core.utils.general.wrappers import run_thread +from core.network.interface import get_interface_ip import threading import libtorrent as lt from core.utils.data.state import state @@ -15,6 +16,7 @@ def init_session(): state.dl_session = lt.session() + settings = { "upload_rate_limit": state.up_speed_limit, "download_rate_limit": state.down_speed_limit, @@ -26,8 +28,15 @@ def init_session(): "connections_limit": state.max_connections, "active_downloads": state.max_downloads } - + if state.bound_interface: + interface_ip = get_interface_ip(state.bound_interface) + if interface_ip: + settings["outgoing_interfaces"] = interface_ip + settings["listen_interfaces"] = f"{interface_ip}:6881" + consoleLog(f"Binding to Interface IP: {interface_ip}") + else: + consoleLog(f"Error finding IP for Interface {state.bound_interface}") state.dl_session.apply_settings(settings) @@ -45,7 +54,6 @@ def add_download(magnet_uri, dl_path=state.download_path): consoleLog("Skipping, download already running...") return - magnetdl = lt.parse_magnet_uri(magnet_uri) magnetdl.save_path = dl_path @@ -98,6 +106,21 @@ def update_settings(): "connections_limit": state.max_connections, "active_downloads": state.max_downloads } + + if state.bound_interface: + interface_ip = get_interface_ip(state.bound_interface) + if interface_ip: + settings["outgoing_interfaces"] = interface_ip + settings["listen_interfaces"] = f"{interface_ip}:6881" + consoleLog(f"Binding to Interface IP: {interface_ip}") + else: + consoleLog(f"Error finding IP for Interface {state.bound_interface}") state.dl_session.apply_settings(settings) +def update_bound_interface(): + + settings = { "outgoing_interfaces": state.bound_interface } + + state.dl_session.apply_settings(settings) + diff --git a/core/utils/data/state.py b/core/utils/data/state.py index e1c9240..b5052a1 100644 --- a/core/utils/data/state.py +++ b/core/utils/data/state.py @@ -29,6 +29,7 @@ class AppState(QObject): self.window_transparency: bool = False self.interfaces: List = [] self.active_interfaces: List = [] + self.bound_interface: str = None @property def image_path(self) -> str: diff --git a/main.py b/main.py index 1063251..f6823c0 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ from core.utils.general.shutdown import closehelper, shutdown_event from core.utils.general.wrappers import run_thread from core.utils.general.loghandler import split_data, check_completed from core.network.interface import list_interfaces, init_interfaces +from core.network.libtorrent_int import update_bound_interface from core.utils.config.config import read_config from PySide6 import QtWidgets from PySide6.QtCore import Qt @@ -46,16 +47,17 @@ if __name__ == "__main__": if args.debug: state.debug = args.debug # override of read_config signal.signal(signal.SIGINT, keyboardinterrupthandler) - run_thread(threading.Thread(target=send_notification, args=(shutdown_event,), daemon=True)) - consoleLog("Started send_notification thread") - run_thread(threading.Thread(target=update_log, args=(shutdown_event,), daemon=True)) - consoleLog("Started update_log thread") - run_thread(threading.Thread(target=check_completed, args=(downloads, state.autoresume))) consoleLog("Started check_completed thread") consoleLog("Fetching Network Interfaces...") list_interfaces() consoleLog("Initialiting Interface variables...") init_interfaces() + consoleLog(f"Current Bound: {state.bound_interface}") + run_thread(threading.Thread(target=send_notification, args=(shutdown_event,), daemon=True)) + consoleLog("Started send_notification thread") + run_thread(threading.Thread(target=update_log, args=(shutdown_event,), daemon=True)) + consoleLog("Started update_log thread") + run_thread(threading.Thread(target=check_completed, args=(downloads, state.autoresume))) consoleLog("Launching GUI") run_gui()