From 748d0bf2bdf88580203b011f8d5eb0637e01251f Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Thu, 24 Apr 2025 20:23:31 +0330 Subject: [PATCH] add init_paths to handle sys.path setup for importing shared modules like paths.py --- core/scripts/hysteria2/init_paths.py | 7 +++++++ core/scripts/hysteria2/kickuser.py | 5 +++-- core/scripts/hysteria2/show_user_uri.py | 11 +++-------- core/scripts/hysteria2/version.py | 6 ++---- core/scripts/hysteria2/wrapper_uri.py | 6 ++++-- core/scripts/paths.py | 21 +++++++++++++++++++++ 6 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 core/scripts/hysteria2/init_paths.py create mode 100644 core/scripts/paths.py diff --git a/core/scripts/hysteria2/init_paths.py b/core/scripts/hysteria2/init_paths.py new file mode 100644 index 0000000..118c17b --- /dev/null +++ b/core/scripts/hysteria2/init_paths.py @@ -0,0 +1,7 @@ +import sys +from pathlib import Path + +core_scripts_dir = Path(__file__).resolve().parents[1] + +if str(core_scripts_dir) not in sys.path: + sys.path.append(str(core_scripts_dir)) diff --git a/core/scripts/hysteria2/kickuser.py b/core/scripts/hysteria2/kickuser.py index 465e86c..140fd14 100644 --- a/core/scripts/hysteria2/kickuser.py +++ b/core/scripts/hysteria2/kickuser.py @@ -6,8 +6,9 @@ import sys import os from hysteria2_api import Hysteria2Client, Hysteria2Error -CONFIG_FILE = '/etc/hysteria/config.json' -API_BASE_URL = 'http://127.0.0.1:25413' +from init_paths import * +from paths import * + def get_api_secret(config_path: str) -> str: if not os.path.exists(config_path): diff --git a/core/scripts/hysteria2/show_user_uri.py b/core/scripts/hysteria2/show_user_uri.py index ff11d52..11f142b 100644 --- a/core/scripts/hysteria2/show_user_uri.py +++ b/core/scripts/hysteria2/show_user_uri.py @@ -7,13 +7,8 @@ import subprocess import argparse import re from typing import Tuple, Optional, Dict, List, Any - -CORE_DIR = "/etc/hysteria" -CONFIG_FILE = f"{CORE_DIR}/config.json" -USERS_FILE = f"{CORE_DIR}/users.json" -HYSTERIA2_ENV = f"{CORE_DIR}/.configs.env" -SINGBOX_ENV = f"{CORE_DIR}/core/scripts/singbox/.env" -NORMALSUB_ENV = f"{CORE_DIR}/core/scripts/normalsub/.env" +from init_paths import * +from paths import * def load_env_file(env_file: str) -> Dict[str, str]: """Load environment variables from a file into a dictionary.""" @@ -29,7 +24,7 @@ def load_env_file(env_file: str) -> Dict[str, str]: def load_hysteria2_env() -> Dict[str, str]: """Load Hysteria2 environment variables.""" - return load_env_file(HYSTERIA2_ENV) + return load_env_file(CONFIG_ENV) def load_hysteria2_ips() -> Tuple[str, str, str]: """Load Hysteria2 IPv4 and IPv6 addresses from environment.""" diff --git a/core/scripts/hysteria2/version.py b/core/scripts/hysteria2/version.py index b17cc02..ac623c7 100644 --- a/core/scripts/hysteria2/version.py +++ b/core/scripts/hysteria2/version.py @@ -4,10 +4,8 @@ import os import sys import requests from pathlib import Path - -LOCALVERSION = "/etc/hysteria/VERSION" -LATESTVERSION = "https://raw.githubusercontent.com/ReturnFI/Blitz/main/VERSION" -LASTESTCHANGE = "https://raw.githubusercontent.com/ReturnFI/Blitz/main/changelog" +from init_paths import * +from paths import * def version_greater_equal(version1, version2): version1_parts = [int(part) for part in version1.strip().split('.')] diff --git a/core/scripts/hysteria2/wrapper_uri.py b/core/scripts/hysteria2/wrapper_uri.py index 4f3342d..f898f9d 100644 --- a/core/scripts/hysteria2/wrapper_uri.py +++ b/core/scripts/hysteria2/wrapper_uri.py @@ -4,12 +4,14 @@ import re import json import sys -SHOW_URI_SCRIPT = "/etc/hysteria/core/scripts/hysteria2/show_user_uri.py" +from init_paths import * +from paths import * + DEFAULT_ARGS = ["-a", "-n", "-s"] def run_show_uri(username): try: - cmd = ["python3", SHOW_URI_SCRIPT, "-u", username] + DEFAULT_ARGS + cmd = ["python3", CLI_PATH, "show-user-uri", "-u", username] + DEFAULT_ARGS result = subprocess.run(cmd, capture_output=True, text=True, check=True) output = result.stdout if "Invalid username" in output: diff --git a/core/scripts/paths.py b/core/scripts/paths.py new file mode 100644 index 0000000..850499e --- /dev/null +++ b/core/scripts/paths.py @@ -0,0 +1,21 @@ +from pathlib import Path + +BASE_DIR = Path("/etc/hysteria") + +CLI_PATH = BASE_DIR / "core/cli.py" +USERS_FILE = BASE_DIR / "users.json" +TRAFFIC_FILE = BASE_DIR / "traffic_data.json" +CONFIG_FILE = BASE_DIR / "config.json" +CONFIG_ENV = BASE_DIR / ".configs.env" +TELEGRAM_ENV = BASE_DIR / "core/scripts/telegrambot/.env" +SINGBOX_ENV = BASE_DIR / "core/scripts/singbox/.env" +NORMALSUB_ENV = BASE_DIR / "core/scripts/normalsub/.env" +WEBPANEL_ENV = BASE_DIR / "core/scripts/webpanel/.env" +API_BASE_URL = "http://127.0.0.1:25413" +ONLINE_API_URL = "http://127.0.0.1:25413/online" +LOCALVERSION = BASE_DIR / "VERSION" +LATESTVERSION = "https://raw.githubusercontent.com/ReturnFI/Blitz/main/VERSION" +LASTESTCHANGE = "https://raw.githubusercontent.com/ReturnFI/Blitz/main/changelog" +CONNECTIONS_FILE = BASE_DIR / "hysteria_connections.json" +BLOCK_LIST = Path("/tmp/hysteria_blocked_ips.txt") +SCRIPT_PATH = BASE_DIR / "core/scripts/hysteria2/limit.sh"