diff --git a/core/services/encrypted_proxy/hysteria_service.py b/core/services/encrypted_proxy/hysteria_service.py index 4dbb430..e49f1fd 100644 --- a/core/services/encrypted_proxy/hysteria_service.py +++ b/core/services/encrypted_proxy/hysteria_service.py @@ -1,5 +1,4 @@ import socket -import subprocess import time from pathlib import Path from core.Constants import Constants @@ -7,24 +6,21 @@ from core.utils.encrypted_proxy.net import get_real_ip, verify_ip from core.utils.encrypted_proxy.singbox import SingboxRunner from core.utils.encrypted_proxy.dns import wait_for_tun, set_dns_on_tun, revert_dns_on_tun from core.utils.encrypted_proxy import killswitch -from core.services.encrypted_proxy.disable_service import get_public_ip + def _resolve(host: str) -> str: return socket.gethostbyname(host) + def build_hysteria_config(username: str, password: str, - server_host: str, socks5_port: int) -> dict: - server_ip = _resolve(server_host) + server_host: str, socks5_port: int, + server_ip: str = None) -> dict: + if server_ip is None: + server_ip = _resolve(server_host) return { "dns": { - "servers": [ - { - "tag": "tunnel-dns", - "type": "udp", - "server": "9.9.9.9", - }, - ], + "servers": [{"tag": "tunnel-dns", "type": "udp", "server": "9.9.9.9"}], "final": "tunnel-dns", "strategy": "ipv4_only", "independent_cache": True, @@ -83,17 +79,14 @@ def _cleanup_all() -> None: SingboxRunner().stop() except Exception: pass - try: revert_dns_on_tun() except Exception: pass - try: killswitch.disarm() except Exception: pass - time.sleep(1) @@ -113,7 +106,8 @@ def enable_hysteria(username: str, password: str, server_host: str, runner = SingboxRunner() runner.stop() config_path = Path(Constants.SINGBOX_CONFIG_DIR) / f"{username}-sing-box.json" - config = build_hysteria_config(username, password, server_host, socks5_port) + config = build_hysteria_config(username, password, server_host, socks5_port, + server_ip=server_ip) try: runner.write_config(config_path, config) @@ -156,12 +150,11 @@ def enable_hysteria(username: str, password: str, server_host: str, }) return True + def disable_hysteria(observer=None) -> bool: revert_dns_on_tun() SingboxRunner().stop() killswitch.disarm() - time.sleep(1) - ip = get_public_ip() if observer: - observer.notify("disconnected", {"ip": ip}) + observer.notify("disconnected", {}) return True \ No newline at end of file diff --git a/core/services/encrypted_proxy/vless_service.py b/core/services/encrypted_proxy/vless_service.py index 517579e..9930d55 100644 --- a/core/services/encrypted_proxy/vless_service.py +++ b/core/services/encrypted_proxy/vless_service.py @@ -1,9 +1,7 @@ from urllib.parse import unquote from pathlib import Path import socket -import subprocess import time -from core.services.encrypted_proxy.disable_service import get_public_ip from core.Constants import Constants from core.utils.encrypted_proxy.net import get_real_ip, verify_ip from core.utils.encrypted_proxy.singbox import SingboxRunner @@ -36,18 +34,13 @@ def parse_vless_link(link: str) -> dict: } -def build_vless_config(vless: dict, socks5_port: int) -> dict: - server_ip = socket.gethostbyname(vless["host"]) +def build_vless_config(vless: dict, socks5_port: int, server_ip: str = None) -> dict: + if server_ip is None: + server_ip = socket.gethostbyname(vless["host"]) return { "dns": { - "servers": [ - { - "tag": "tunnel-dns", - "type": "udp", - "server": "9.9.9.9", - }, - ], + "servers": [{"tag": "tunnel-dns", "type": "udp", "server": "9.9.9.9"}], "final": "tunnel-dns", "strategy": "ipv4_only", "independent_cache": True, @@ -111,17 +104,14 @@ def _cleanup_all() -> None: SingboxRunner().stop() except Exception: pass - try: revert_dns_on_tun() except Exception: pass - try: killswitch.disarm() except Exception: pass - time.sleep(1) @@ -142,7 +132,7 @@ def enable_vless(vless_link: str, username: str, runner = SingboxRunner() runner.stop() config_path = Path(Constants.SINGBOX_CONFIG_DIR) / f"{username}-sing-box.json" - config = build_vless_config(vless, socks5_port) + config = build_vless_config(vless, socks5_port, server_ip=server_ip) try: runner.write_config(config_path, config) @@ -190,8 +180,6 @@ def disable_vless(observer=None) -> bool: revert_dns_on_tun() SingboxRunner().stop() killswitch.disarm() - time.sleep(1) - ip = get_public_ip() if observer: - observer.notify("disconnected", {"ip": ip}) + observer.notify("disconnected", {}) return True \ No newline at end of file