From 07cb999259bf9b8ddb2d5b3db3865af18513c342 Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Fri, 2 May 2025 17:35:43 +0330 Subject: [PATCH] Refactor: Implement warp status in Python --- core/cli_api.py | 4 +- core/scripts/warp/status.py | 74 +++++++++++++++++++++++++++++++++++++ core/scripts/warp/status.sh | 34 ----------------- 3 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 core/scripts/warp/status.py delete mode 100644 core/scripts/warp/status.sh diff --git a/core/cli_api.py b/core/cli_api.py index 7245195..3a0ae5e 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -46,7 +46,7 @@ class Command(Enum): INSTALL_WARP = os.path.join(SCRIPT_DIR, 'warp', 'install.py') UNINSTALL_WARP = os.path.join(SCRIPT_DIR, 'warp', 'uninstall.py') CONFIGURE_WARP = os.path.join(SCRIPT_DIR, 'warp', 'configure.sh') - STATUS_WARP = os.path.join(SCRIPT_DIR, 'warp', 'status.sh') + STATUS_WARP = os.path.join(SCRIPT_DIR, 'warp', 'status.py') SERVICES_STATUS = os.path.join(SCRIPT_DIR, 'services_status.sh') VERSION = os.path.join(SCRIPT_DIR, 'hysteria2', 'version.py') LIMIT_SCRIPT = os.path.join(SCRIPT_DIR, 'hysteria2', 'limit.sh') @@ -471,7 +471,7 @@ def configure_warp(all: bool, popular_sites: bool, domestic_sites: bool, block_a def warp_status() -> str | None: '''Checks the status of WARP.''' - return run_cmd(['bash', Command.STATUS_WARP.value]) + return run_cmd(['python3', Command.STATUS_WARP.value]) def start_telegram_bot(token: str, adminid: str): diff --git a/core/scripts/warp/status.py b/core/scripts/warp/status.py new file mode 100644 index 0000000..0d49424 --- /dev/null +++ b/core/scripts/warp/status.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import json +from pathlib import Path +import sys + +core_scripts_dir = Path(__file__).resolve().parents[1] +if str(core_scripts_dir) not in sys.path: + sys.path.append(str(core_scripts_dir)) + +from paths import * + +colors = { + "cyan": "\033[96m", + "green": "\033[92m", + "red": "\033[91m", + "purple": "\033[95m", + "end": "\033[0m" +} + +def echo_status(label, is_active): + status = f"{colors['green']}Active{colors['end']}" if is_active else f"{colors['red']}Inactive{colors['end']}" + print(f"{colors['cyan']}{label}:{colors['end']} {status}") + +def check_warp_configuration(): + if not Path(CONFIG_FILE).exists(): + print(f"{colors['red']}Error: Config file not found at {CONFIG_FILE}{colors['end']}") + return + + with open(CONFIG_FILE, "r") as f: + config = json.load(f) + + acl_inline = config.get("acl", {}).get("inline", []) + + def contains_warp(rule_prefixes): + return any(rule.startswith(prefix) for rule in acl_inline for prefix in rule_prefixes) + + print("--------------------------------") + print(f"{colors['purple']}Current WARP Configuration:{colors['end']}") + + echo_status( + "All traffic", + contains_warp(["warps(all)"]) + ) + + echo_status( + "Popular sites (Google, Netflix, etc.)", + contains_warp([ + "warps(geosite:google)", + "warps(geoip:google)", + "warps(geosite:netflix)", + "warps(geosite:spotify)", + "warps(geosite:openai)", + "warps(geoip:openai)" + ]) + ) + + echo_status( + "Domestic sites (geosite:ir, geoip:ir)", + contains_warp([ + "warps(geosite:ir)", + "warps(geoip:ir)" + ]) + ) + + echo_status( + "Block adult content", + "reject(geosite:nsfw)" in acl_inline + ) + + print("--------------------------------") + +if __name__ == "__main__": + check_warp_configuration() diff --git a/core/scripts/warp/status.sh b/core/scripts/warp/status.sh deleted file mode 100644 index c019d46..0000000 --- a/core/scripts/warp/status.sh +++ /dev/null @@ -1,34 +0,0 @@ -source /etc/hysteria/core/scripts/utils.sh -source /etc/hysteria/core/scripts/path.sh - -check_warp_configuration() { - echo "--------------------------------" - echo -e "${LPurple}Current WARP Configuration: ${NC}" - - if jq -e '.acl.inline[]? | select(test("warps\\(all\\)"))' "$CONFIG_FILE" > /dev/null; then - echo -e "${cyan}All traffic:${NC} ${green}Active${NC}" - else - echo -e "${cyan}All traffic:${NC} ${red}Inactive${NC}" - fi - - if jq -e '.acl.inline[]? | select(test("warps\\(geosite:google\\)")) or select(test("warps\\(geoip:google\\)")) or select(test("warps\\(geosite:netflix\\)")) or select(test("warps\\(geosite:spotify\\)")) or select(test("warps\\(geosite:openai\\)")) or select(test("warps\\(geoip:openai\\)"))' "$CONFIG_FILE" > /dev/null; then - echo -e "${cyan}Popular sites (Google, Netflix, etc.):${NC} ${green}Active${NC}" - else - echo -e "${cyan}Popular sites (Google, Netflix, etc.):${NC} ${red}Inactive${NC}" - fi - - if jq -e '.acl.inline[]? | select(test("warps\\(geosite:ir\\)")) or select(test("warps\\(geoip:ir\\)"))' "$CONFIG_FILE" > /dev/null; then - echo -e "${cyan}Domestic sites (geosite:ir, geoip:ir):${NC} ${green}Active${NC}" - else - echo -e "${cyan}Domestic sites (geosite:ir, geoip:ir):${NC} ${red}Inactive${NC}" - fi - - if jq -e '.acl.inline[]? | select(test("reject\\(geosite:nsfw\\)"))' "$CONFIG_FILE" > /dev/null; then - echo -e "${cyan}Block adult content:${NC} ${green}Active${NC}" - else - echo -e "${cyan}Block adult content:${NC} ${red}Inactive${NC}" - fi - echo "--------------------------------" -} -define_colors -check_warp_configuration