add init_paths to handle sys.path setup for importing shared modules like paths.py

This commit is contained in:
Whispering Wind
2025-04-24 20:23:31 +03:30
committed by GitHub
parent ff194d0098
commit 748d0bf2bd
6 changed files with 40 additions and 16 deletions

View File

@ -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))

View File

@ -6,8 +6,9 @@ import sys
import os import os
from hysteria2_api import Hysteria2Client, Hysteria2Error from hysteria2_api import Hysteria2Client, Hysteria2Error
CONFIG_FILE = '/etc/hysteria/config.json' from init_paths import *
API_BASE_URL = 'http://127.0.0.1:25413' from paths import *
def get_api_secret(config_path: str) -> str: def get_api_secret(config_path: str) -> str:
if not os.path.exists(config_path): if not os.path.exists(config_path):

View File

@ -7,13 +7,8 @@ import subprocess
import argparse import argparse
import re import re
from typing import Tuple, Optional, Dict, List, Any from typing import Tuple, Optional, Dict, List, Any
from init_paths import *
CORE_DIR = "/etc/hysteria" from paths import *
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"
def load_env_file(env_file: str) -> Dict[str, str]: def load_env_file(env_file: str) -> Dict[str, str]:
"""Load environment variables from a file into a dictionary.""" """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]: def load_hysteria2_env() -> Dict[str, str]:
"""Load Hysteria2 environment variables.""" """Load Hysteria2 environment variables."""
return load_env_file(HYSTERIA2_ENV) return load_env_file(CONFIG_ENV)
def load_hysteria2_ips() -> Tuple[str, str, str]: def load_hysteria2_ips() -> Tuple[str, str, str]:
"""Load Hysteria2 IPv4 and IPv6 addresses from environment.""" """Load Hysteria2 IPv4 and IPv6 addresses from environment."""

View File

@ -4,10 +4,8 @@ import os
import sys import sys
import requests import requests
from pathlib import Path from pathlib import Path
from init_paths import *
LOCALVERSION = "/etc/hysteria/VERSION" from paths import *
LATESTVERSION = "https://raw.githubusercontent.com/ReturnFI/Blitz/main/VERSION"
LASTESTCHANGE = "https://raw.githubusercontent.com/ReturnFI/Blitz/main/changelog"
def version_greater_equal(version1, version2): def version_greater_equal(version1, version2):
version1_parts = [int(part) for part in version1.strip().split('.')] version1_parts = [int(part) for part in version1.strip().split('.')]

View File

@ -4,12 +4,14 @@ import re
import json import json
import sys 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"] DEFAULT_ARGS = ["-a", "-n", "-s"]
def run_show_uri(username): def run_show_uri(username):
try: 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) result = subprocess.run(cmd, capture_output=True, text=True, check=True)
output = result.stdout output = result.stdout
if "Invalid username" in output: if "Invalid username" in output:

21
core/scripts/paths.py Normal file
View File

@ -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"