Merge pull request #79 from ReturnFI/beta

Manage masquerade
This commit is contained in:
Whispering Wind
2025-01-18 23:55:26 +03:30
committed by GitHub
4 changed files with 100 additions and 9 deletions

View File

@ -1,3 +1 @@
1. Fix: Block users based on total data usage
2. Increasing the chunk size to 32768 bytes(GeoFiles)
3. Update Requirements
1. Add: Manage masquerade

View File

@ -31,6 +31,7 @@ class Command(Enum):
SHOW_USER_URI = os.path.join(SCRIPT_DIR, 'hysteria2', 'show_user_uri.sh')
IP_ADD = os.path.join(SCRIPT_DIR, 'hysteria2', 'ip.sh')
MANAGE_OBFS = os.path.join(SCRIPT_DIR, 'hysteria2', 'manage_obfs.sh')
MASQUERADE_SCRIPT = os.path.join(SCRIPT_DIR, 'hysteria2', 'masquerade.sh')
TRAFFIC_STATUS = 'traffic.py' # won't be call directly (it's a python module)
UPDATE_GEO = os.path.join(SCRIPT_DIR, 'hysteria2', 'update_geo.py')
LIST_USERS = os.path.join(SCRIPT_DIR, 'hysteria2', 'list_users.sh')
@ -304,6 +305,28 @@ def cli_update_geo(country):
except Exception as e:
print(f"An unexpected error occurred: {e}")
@cli.command('masquerade')
@click.option('--remove', '-r', is_flag=True, help="Remove 'masquerade' from config.json.")
@click.option('--enable', '-e', metavar="<domain>", type=str, help="Enable 'masquerade' in config.json with the specified domain.")
def masquerade(remove, enable):
"""Manage 'masquerade' in Hysteria2 configuration."""
if remove and enable:
click.echo("Error: You cannot use both --remove and --enable at the same time.")
return
elif remove:
click.echo("Removing 'masquerade' from config.json...")
run_cmd(['bash', Command.MASQUERADE_SCRIPT.value, '2'])
elif enable:
if not enable:
click.echo("Error: You must specify a domain with --enable.")
return
click.echo(f"Enabling 'masquerade' with URL: {enable}...")
run_cmd(['bash', Command.MASQUERADE_SCRIPT.value, '1', enable])
else:
click.echo("Error: Please specify either --remove or --enable.")
# endregion
# region advanced menu

View File

@ -0,0 +1,38 @@
#!/bin/bash
source /etc/hysteria/core/scripts/path.sh
function is_masquerade_enabled() {
jq -e '.masquerade' $CONFIG_FILE > /dev/null 2>&1
}
function enable_masquerade() {
if is_masquerade_enabled; then
echo "Masquerade is already enabled."
exit 0
fi
url="https://$1"
jq --arg url "$url" '. + {masquerade: {type: "proxy", proxy: {url: $url, rewriteHost: true}, listenHTTP: ":80", listenHTTPS: ":443", forceHTTPS: true}}' $CONFIG_FILE > tmp.json && mv tmp.json $CONFIG_FILE
echo "Masquerade enabled with URL: $url"
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
}
function remove_masquerade() {
if ! is_masquerade_enabled; then
echo "Masquerade is not enabled."
exit 0
fi
jq 'del(.masquerade)' $CONFIG_FILE > tmp.json && mv tmp.json $CONFIG_FILE
echo "Masquerade removed from config.json"
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
}
if [[ "$1" == "1" ]]; then
enable_masquerade "$2"
elif [[ "$1" == "2" ]]; then
remove_masquerade
else
echo "Usage: $0 {1|2} [domain]"
echo "1: Enable Masquerade [domain]"
echo "2: Remove Masquerade"
exit 1
fi

44
menu.sh
View File

@ -640,6 +640,36 @@ geo_update_handler() {
esac
}
masquerade_handler() {
while true; do
echo -e "${cyan}1.${NC} Enable Masquerade"
echo -e "${red}2.${NC} Remove Masquerade"
echo "0. Back"
read -p "Choose an option: " option
case $option in
1)
read -p "Enter the URL for rewriteHost: " url
if [ -z "$url" ]; then
echo "Error: URL cannot be empty. Please try again."
else
python3 $CLI_PATH masquerade -e "$url"
fi
;;
2)
python3 $CLI_PATH masquerade -r
;;
0)
break
;;
*)
echo "Invalid option. Please try again."
;;
esac
done
}
# Function to display the main menu
display_main_menu() {
clear
@ -761,9 +791,10 @@ display_advance_menu() {
echo -e "${cyan}[10] ${NC}↝ Manage OBFS"
echo -e "${cyan}[11] ${NC}↝ Change IPs(4-6)"
echo -e "${cyan}[12] ${NC}↝ Update geo Files"
echo -e "${cyan}[13] ${NC}Restart Hysteria2"
echo -e "${cyan}[14] ${NC}Update Core Hysteria2"
echo -e "${red}[15] ${NC}↝ Uninstall Hysteria2"
echo -e "${cyan}[13] ${NC}Manage Masquerade"
echo -e "${cyan}[14] ${NC}Restart Hysteria2"
echo -e "${cyan}[15] ${NC}↝ Update Core Hysteria2"
echo -e "${red}[16] ${NC}↝ Uninstall Hysteria2"
echo -e "${red}[0] ${NC}↝ Back to Main Menu"
echo -e "${LPurple}◇──────────────────────────────────────────────────────────────────────◇${NC}"
echo -ne "${yellow}➜ Enter your option: ${NC}"
@ -789,9 +820,10 @@ advance_menu() {
10) obfs_handler ;;
11) edit_ips ;;
12) geo_update_handler ;;
13) python3 $CLI_PATH restart-hysteria2 ;;
14) python3 $CLI_PATH update-hysteria2 ;;
15) python3 $CLI_PATH uninstall-hysteria2 ;;
13) masquerade_handler ;;
14) python3 $CLI_PATH restart-hysteria2 ;;
15) python3 $CLI_PATH update-hysteria2 ;;
16) python3 $CLI_PATH uninstall-hysteria2 ;;
0) return ;;
*) echo "Invalid option. Please try again." ;;
esac