Refactor paths

This commit is contained in:
Sarina
2024-07-22 15:46:16 +03:30
parent 1985a171e9
commit ba9ff87b8c
11 changed files with 133 additions and 96 deletions

View File

@ -1,9 +1,14 @@
#!/bin/bash
# Source the path.sh script to load the necessary variables
source /etc/hysteria/core/scripts/path.sh
source /etc/hysteria/core/scripts/utils.sh
define_colors
# Function to add a new user to the configuration
add_user() {
if [ $# -ne 3 ]; then
echo "Usage: $0 <username> <traffic_limit_GB> <expiration_days>"
if [ $# -ne 3 ] && [ $# -ne 5 ]; then
echo "Usage: $0 <username> <traffic_limit_GB> <expiration_days> [password] [creation_date]"
exit 1
fi
@ -11,11 +16,11 @@ add_user() {
traffic_gb=$2
expiration_days=$3
password=$4
creation_date=$5
if [ -z "$password" ]; then
password=$(pwgen -s 32 1)
fi
creation_date=$5
# Check if the creation_date is empty
if [ -z "$creation_date" ]; then
creation_date=$(date +%Y-%m-%d)
else
@ -33,25 +38,24 @@ add_user() {
# Validate the username
if ! [[ "$username" =~ ^[a-z0-9]+$ ]]; then
echo -e "\033[0;31mError:\033[0m Username can only contain lowercase letters and numbers."
echo -e "${red}Error:${NC} Username can only contain lowercase letters and numbers."
exit 1
fi
# Convert GB to bytes (1 GB = 1073741824 bytes)
traffic=$((traffic_gb * 1073741824))
if [ ! -f "/etc/hysteria/users.json" ]; then
echo "{}" > /etc/hysteria/users.json
if [ ! -f "$USERS_FILE" ]; then
echo "{}" > "$USERS_FILE"
fi
jq --arg username "$username" --arg password "$password" --argjson traffic "$traffic" --argjson expiration_days "$expiration_days" --arg creation_date "$creation_date" \
'.[$username] = {password: $password, max_download_bytes: $traffic, expiration_days: $expiration_days, account_creation_date: $creation_date, blocked: false}' \
/etc/hysteria/users.json > /etc/hysteria/users_temp.json && mv /etc/hysteria/users_temp.json /etc/hysteria/users.json
"$USERS_FILE" > "${USERS_FILE}.temp" && mv "${USERS_FILE}.temp" "$USERS_FILE"
python3 /etc/hysteria/core/cli.py restart-hysteria2 > /dev/null 2>&1
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
echo -e "\033[0;32mUser $username added successfully.\033[0m"
echo -e "${green}User $username added successfully.${NC}"
}
# Call the function with the provided arguments

View File

@ -1,3 +1,8 @@
#!/bin/bash
# Source the path.sh script to load the CONFIG_FILE variable
source /etc/hysteria/core/scripts/path.sh
# Function to update port number in configuration
update_port() {
local port=$1
@ -9,12 +14,12 @@ update_port() {
fi
# Check if the config file exists and update the port number
if [ -f "/etc/hysteria/config.json" ]; then
jq --arg port "$port" '.listen = ":" + $port' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json
python3 /etc/hysteria/core/cli.py restart-hysteria2 > /dev/null 2>&1
if [ -f "$CONFIG_FILE" ]; then
jq --arg port "$port" '.listen = ":" + $port' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE"
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
echo "Port changed successfully to $port."
else
echo "Error: Config file /etc/hysteria/config.json not found."
echo "Error: Config file $CONFIG_FILE not found."
return 1
fi
}

View File

@ -1,9 +1,13 @@
#!/bin/bash
# Source the path.sh script to load the necessary variables
source /etc/hysteria/core/scripts/path.sh
source /etc/hysteria/core/scripts/utils.sh
define_colors
install_hysteria() {
local port=$1
# Step 1: Install Hysteria2
echo "Installing Hysteria2..."
bash <(curl -fsSL https://get.hy2.sh/) >/dev/null 2>&1
@ -34,21 +38,12 @@ install_hysteria() {
sha256=$(python3 generate.py)
# WE CLONED THE REPO, SO WE HAVE THE config.json ALREADY
# Step 6: Download the config.json file
# echo "Downloading config.json..."
# wget https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/config.json -O /etc/hysteria/config.json >/dev/null 2>&1
# echo
# Step 7: Ask for the port number and validate input
port=$1
if [[ $port =~ ^[0-9]+$ ]] && (( port >= 1 && port <= 65535 )); then
# Check if the port is in use
if ss -tuln | grep -q ":$port\b"; then
echo -e "\e[91mPort $port is already in use. Please choose another port.\e[0m"
echo -e "${red}Port $port is already in use. Please choose another port.${NC}"
exit 1
else
break
fi
else
echo "Invalid port number. Please enter a number between 1 and 65535."
@ -85,12 +80,12 @@ install_hysteria() {
.tls.pinSHA256 = $sha256 |
.obfs.salamander.password = $obfspassword |
.trafficStats.secret = $UUID |
.outbounds[0].direct.bindDevice = $networkdef' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json
.outbounds[0].direct.bindDevice = $networkdef' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE"
# Step 11: Modify the systemd service file to use config.json
echo "Updating hysteria-server.service to use config.json..."
sed -i 's|(config.yaml)||' /etc/systemd/system/hysteria-server.service
sed -i 's|/etc/hysteria/config.yaml|/etc/hysteria/config.json|' /etc/systemd/system/hysteria-server.service
sed -i "s|/etc/hysteria/config.yaml|$CONFIG_FILE|" /etc/systemd/system/hysteria-server.service
rm /etc/hysteria/config.yaml
sleep 1
@ -103,7 +98,7 @@ install_hysteria() {
# Step 13: Check if the hysteria-server.service is active
if systemctl is-active --quiet hysteria-server.service; then
echo "${cyan}Hysteria2${green} has been successfully install."
echo "${cyan}Hysteria2${green} has been successfully installed."
else
echo "${red}Error:${NC} hysteria-server.service is not active."
exit 1
@ -118,13 +113,6 @@ install_hysteria() {
(crontab -l ; echo "*/1 * * * * /etc/hysteria/core/scripts/hysteria2/kick.sh >/dev/null 2>&1") | crontab -
}
if systemctl is-active --quiet hysteria-server.service; then
echo -e "${red}Error:${NC} Hysteria2 is already installed and running."
echo
@ -140,4 +128,3 @@ else
echo -e "${red}Error:${NC} Hysteria2 service is not active. Please check the logs for more details."
fi
fi

View File

@ -1,5 +1,10 @@
#!/bin/bash
# Source the path.sh script to load the necessary variables
source /etc/hysteria/core/scripts/path.sh
source /etc/hysteria/core/scripts/utils.sh
define_colors
# Function to remove a user from the configuration
remove_user() {
if [ $# -ne 1 ]; then
@ -9,22 +14,22 @@ remove_user() {
username=$1
if [ -f "/etc/hysteria/users.json" ]; then
if [ -f "$USERS_FILE" ]; then
# Check if the username exists in the users.json file
if jq -e "has(\"$username\")" /etc/hysteria/users.json > /dev/null; then
jq --arg username "$username" 'del(.[$username])' /etc/hysteria/users.json > /etc/hysteria/users_temp.json && mv /etc/hysteria/users_temp.json /etc/hysteria/users.json
if jq -e "has(\"$username\")" "$USERS_FILE" > /dev/null; then
jq --arg username "$username" 'del(.[$username])' "$USERS_FILE" > "${USERS_FILE}.temp" && mv "${USERS_FILE}.temp" "$USERS_FILE"
if [ -f "/etc/hysteria/traffic_data.json" ]; then
jq --arg username "$username" 'del(.[$username])' /etc/hysteria/traffic_data.json > /etc/hysteria/traffic_data_temp.json && mv /etc/hysteria/traffic_data_temp.json /etc/hysteria/traffic_data.json
if [ -f "$TRAFFIC_FILE" ]; then
jq --arg username "$username" 'del(.[$username])' "$TRAFFIC_FILE" > "${TRAFFIC_FILE}.temp" && mv "${TRAFFIC_FILE}.temp" "$TRAFFIC_FILE"
fi
python3 /etc/hysteria/core/cli.py restart-hysteria2 > /dev/null 2>&1
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
echo "User $username removed successfully."
else
echo -e "\033[0;31mError:\033[0m User $username not found."
echo -e "${red}Error:${NC} User $username not found."
fi
else
echo -e "\033[0;31mError:\033[0m Config file /etc/hysteria/users.json not found."
echo -e "${red}Error:${NC} Config file $USERS_FILE not found."
fi
}

View File

@ -1,8 +1,11 @@
#!/bin/bash
# Source the path.sh script to load the configuration variables
source /etc/hysteria/core/scripts/path.sh
# Function to show URI if Hysteria2 is installed and active
show_uri() {
if [ -f "/etc/hysteria/users.json" ]; then
if [ -f "$USERS_FILE" ]; then
if systemctl is-active --quiet hysteria-server.service; then
# Check if the username is provided as an argument
if [ -z "$1" ]; then
@ -13,12 +16,12 @@ show_uri() {
username=$1
# Validate the username
if jq -e "has(\"$username\")" /etc/hysteria/users.json > /dev/null; then
if jq -e "has(\"$username\")" "$USERS_FILE" > /dev/null; then
# Get the selected user's details
authpassword=$(jq -r ".\"$username\".password" /etc/hysteria/users.json)
port=$(jq -r '.listen' /etc/hysteria/config.json | cut -d':' -f2)
sha256=$(jq -r '.tls.pinSHA256' /etc/hysteria/config.json)
obfspassword=$(jq -r '.obfs.salamander.password' /etc/hysteria/config.json)
authpassword=$(jq -r ".\"$username\".password" "$USERS_FILE")
port=$(jq -r '.listen' "$CONFIG_FILE" | cut -d':' -f2)
sha256=$(jq -r '.tls.pinSHA256' "$CONFIG_FILE")
obfspassword=$(jq -r '.obfs.salamander.password' "$CONFIG_FILE")
# Get IP addresses
IP=$(curl -s -4 ip.gs)
@ -56,7 +59,7 @@ show_uri() {
echo -e "\033[0;31mError:\033[0m Hysteria2 is not active."
fi
else
echo -e "\033[0;31mError:\033[0m Config file /etc/hysteria/users.json not found."
echo -e "\033[0;31mError:\033[0m Config file $USERS_FILE not found."
fi
}

View File

@ -1,39 +1,44 @@
#!/bin/bash
# Source the path.sh script to load the CONFIG_FILE variable
source /etc/hysteria/core/scripts/path.sh
echo "Starting the update process for Hysteria2..."
echo "Backing up the current configuration..."
cp /etc/hysteria/config.json /etc/hysteria/config_backup.json
cp "$CONFIG_FILE" /etc/hysteria/config_backup.json
if [ $? -ne 0 ]; then
echo "${red}Error:${NC} Failed to back up configuration. Aborting update."
echo "Error: Failed to back up configuration. Aborting update."
return 1
fi
echo "Downloading and installing the latest version of Hysteria2..."
bash <(curl -fsSL https://get.hy2.sh/) >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "${red}Error:${NC} Failed to download or install the latest version. Restoring backup configuration."
mv /etc/hysteria/config_backup.json /etc/hysteria/config.json
python3 /etc/hysteria/core/cli.py restart-hysteria2 > /dev/null 2>&1
echo "Error: Failed to download or install the latest version. Restoring backup configuration."
mv /etc/hysteria/config_backup.json "$CONFIG_FILE"
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
return 1
fi
echo "Restoring configuration from backup..."
mv /etc/hysteria/config_backup.json /etc/hysteria/config.json
mv /etc/hysteria/config_backup.json "$CONFIG_FILE"
if [ $? -ne 0 ]; then
echo "${red}Error:${NC} Failed to restore configuration from backup."
echo "Error: Failed to restore configuration from backup."
return 1
fi
echo "Modifying systemd service to use config.json..."
sed -i 's|/etc/hysteria/config.yaml|/etc/hysteria/config.json|' /etc/systemd/system/hysteria-server.service
sed -i "s|/etc/hysteria/config.yaml|$CONFIG_FILE|" /etc/systemd/system/hysteria-server.service
if [ $? -ne 0 ]; then
echo "${red}Error:${NC} Failed to modify systemd service."
echo "Error: Failed to modify systemd service."
return 1
fi
rm /etc/hysteria/config.yaml
systemctl daemon-reload >/dev/null 2>&1
python3 /etc/hysteria/core/cli.py restart-hysteria2 > /dev/null 2>&1
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "${red}Error:${NC} Failed to restart Hysteria2 service."
echo "Error: Failed to restart Hysteria2 service."
return 1
fi

View File

@ -4,26 +4,30 @@ ADDR="$1"
AUTH="$2"
TX="$3"
USERS_FILE="/etc/hysteria/users.json"
TRAFFIC_FILE="/etc/hysteria/traffic_data.json"
CONFIG_FILE="/etc/hysteria/config.json"
# Source the path.sh script to load variables
source /etc/hysteria/core/scripts/path.sh
# Extract username and password from AUTH
IFS=':' read -r USERNAME PASSWORD <<< "$AUTH"
# Retrieve stored user data
STORED_PASSWORD=$(jq -r --arg user "$USERNAME" '.[$user].password' "$USERS_FILE")
MAX_DOWNLOAD_BYTES=$(jq -r --arg user "$USERNAME" '.[$user].max_download_bytes' "$USERS_FILE")
EXPIRATION_DAYS=$(jq -r --arg user "$USERNAME" '.[$user].expiration_days' "$USERS_FILE")
ACCOUNT_CREATION_DATE=$(jq -r --arg user "$USERNAME" '.[$user].account_creation_date' "$USERS_FILE")
BLOCKED=$(jq -r --arg user "$USERNAME" '.[$user].blocked' "$USERS_FILE")
# Check if the user is blocked
if [ "$BLOCKED" == "true" ]; then
exit 1
fi
# Check if the provided password matches the stored password
if [ "$STORED_PASSWORD" != "$PASSWORD" ]; then
exit 1
fi
# Check if the user's account has expired
CURRENT_DATE=$(date +%s)
EXPIRATION_DATE=$(date -d "$ACCOUNT_CREATION_DATE + $EXPIRATION_DAYS days" +%s)
@ -32,6 +36,7 @@ if [ "$CURRENT_DATE" -ge "$EXPIRATION_DATE" ]; then
exit 1
fi
# Check if the user's download limit has been exceeded
CURRENT_DOWNLOAD_BYTES=$(jq -r --arg user "$USERNAME" '.[$user].download_bytes' "$TRAFFIC_FILE")
if [ "$CURRENT_DOWNLOAD_BYTES" -ge "$MAX_DOWNLOAD_BYTES" ]; then
@ -43,5 +48,6 @@ if [ "$CURRENT_DOWNLOAD_BYTES" -ge "$MAX_DOWNLOAD_BYTES" ]; then
exit 1
fi
# If all checks pass, print the username and exit successfully
echo "$USERNAME"
exit 0

View File

@ -1,11 +1,15 @@
# Check if wg-quick@wgcf.service is active
#!/bin/bash
# Source the path.sh script to load the CONFIG_FILE and CLI_PATH variables
source /etc/hysteria/core/scripts/path.sh
# Check if wg-quick@wgcf.service is active
if ! systemctl is-active --quiet wg-quick@wgcf.service; then
echo "WARP is not active. Please install WARP before configuring."
return
exit 1
fi
CONFIG_FILE="/etc/hysteria/config.json"
# Check if the config file exists
if [ -f "$CONFIG_FILE" ]; then
# Check the current status of WARP configurations
warp_all_status=$(jq -r 'if .acl.inline | index("warps(all)") then "WARP active" else "Direct" end' "$CONFIG_FILE")
@ -62,13 +66,13 @@ if [ -f "$CONFIG_FILE" ]; then
fi
;;
5)
return
exit 0
;;
*)
echo "Invalid option. Please try again."
;;
esac
python3 /etc/hysteria/core/cli.py restart-hysteria2 > /dev/null 2>&1
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
else
echo "${red}Error:${NC} Config file $CONFIG_FILE not found."
echo "Error: Config file $CONFIG_FILE not found."
fi

View File

@ -1,3 +1,8 @@
#!/bin/bash
# Source the path.sh script to load the CONFIG_FILE variable
source /etc/hysteria/core/scripts/path.sh
# Check if wg-quick@wgcf.service is active
if systemctl is-active --quiet wg-quick@wgcf.service; then
echo "WARP is already active. Skipping installation and configuration update."
@ -6,13 +11,13 @@ else
bash <(curl -fsSL git.io/warp.sh) wgx
# Check if the config file exists
if [ -f "/etc/hysteria/config.json" ]; then
if [ -f "$CONFIG_FILE" ]; then
# Add the outbound configuration to the config.json file
jq '.outbounds += [{"name": "warps", "type": "direct", "direct": {"mode": 4, "bindDevice": "wgcf"}}]' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json
jq '.outbounds += [{"name": "warps", "type": "direct", "direct": {"mode": 4, "bindDevice": "wgcf"}}]' "$CONFIG_FILE" > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json "$CONFIG_FILE"
# Restart the hysteria-server service
python3 /etc/hysteria/core/cli.py restart-hysteria2 > /dev/null 2>&1
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
echo "WARP installed and outbound added to config.json."
else
echo "${red}Error:${NC} Config file /etc/hysteria/config.json not found."
echo "Error: Config file $CONFIG_FILE not found."
fi
fi

View File

@ -1,8 +1,15 @@
#!/bin/bash
# Source the path.sh script to load the CONFIG_FILE and CLI_PATH variables
source /etc/hysteria/core/scripts/path.sh
# Check if WARP is active
if systemctl is-active --quiet wg-quick@wgcf.service; then
echo "Uninstalling WARP..."
bash <(curl -fsSL git.io/warp.sh) dwg
if [ -f "/etc/hysteria/config.json" ]; then
# Check if the config file exists
if [ -f "$CONFIG_FILE" ]; then
default_config='["reject(geosite:ir)", "reject(geoip:ir)", "reject(geosite:category-ads-all)", "reject(geoip:private)", "reject(geosite:google@ads)"]'
jq --argjson default_config "$default_config" '
@ -17,13 +24,14 @@ if systemctl is-active --quiet wg-quick@wgcf.service; then
.
end
) | .acl.inline |= ($default_config + (. - $default_config | map(select(. != "direct"))))
' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json
jq 'del(.outbounds[] | select(.name == "warps" and .type == "direct" and .direct.mode == 4 and .direct.bindDevice == "wgcf"))' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json
' "$CONFIG_FILE" > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json "$CONFIG_FILE"
python3 /etc/hysteria/core/cli.py restart-hysteria2 > /dev/null 2>&1
jq 'del(.outbounds[] | select(.name == "warps" and .type == "direct" and .direct.mode == 4 and .direct.bindDevice == "wgcf"))' "$CONFIG_FILE" > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json "$CONFIG_FILE"
python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
echo "WARP uninstalled and configurations reset to default."
else
echo "${red}Error:${NC} Config file /etc/hysteria/config.json not found."
echo "Error: Config file $CONFIG_FILE not found."
fi
else
echo "WARP is not active. Skipping uninstallation."

View File

@ -3,15 +3,21 @@ import subprocess
import json
import os
# Define static variables for paths and URLs
CONFIG_FILE = '/etc/hysteria/config.json'
TRAFFIC_FILE = '/etc/hysteria/traffic_data.json'
TRAFFIC_API_URL = 'http://127.0.0.1:25413/traffic?clear=1'
ONLINE_API_URL = 'http://127.0.0.1:25413/online'
def traffic_status():
green = '\033[0;32m'
cyan = '\033[0;36m'
NC = '\033[0m'
try:
secret = subprocess.check_output(['jq', '-r', '.trafficStats.secret', '/etc/hysteria/config.json']).decode().strip()
secret = subprocess.check_output(['jq', '-r', '.trafficStats.secret', CONFIG_FILE]).decode().strip()
except subprocess.CalledProcessError as e:
print(f"Error: Failed to read secret from config.json. Details: {e}")
print(f"Error: Failed to read secret from {CONFIG_FILE}. Details: {e}")
return
if not secret:
@ -19,7 +25,7 @@ def traffic_status():
return
try:
response = subprocess.check_output(['curl', '-s', '-H', f'Authorization: {secret}', 'http://127.0.0.1:25413/traffic?clear=1']).decode().strip()
response = subprocess.check_output(['curl', '-s', '-H', f'Authorization: {secret}', TRAFFIC_API_URL]).decode().strip()
except subprocess.CalledProcessError as e:
print(f"Error: Failed to fetch traffic data. Details: {e}")
return
@ -29,7 +35,7 @@ def traffic_status():
return
try:
online_response = subprocess.check_output(['curl', '-s', '-H', f'Authorization: {secret}', 'http://127.0.0.1:25413/online']).decode().strip()
online_response = subprocess.check_output(['curl', '-s', '-H', f'Authorization: {secret}', ONLINE_API_URL]).decode().strip()
except subprocess.CalledProcessError as e:
print(f"Error: Failed to fetch online status data. Details: {e}")
return
@ -55,9 +61,9 @@ def traffic_status():
}
existing_data = {}
if os.path.exists('/etc/hysteria/traffic_data.json'):
if os.path.exists(TRAFFIC_FILE):
try:
with open('/etc/hysteria/traffic_data.json', 'r') as json_file:
with open(TRAFFIC_FILE, 'r') as json_file:
existing_data = json.load(json_file)
except json.JSONDecodeError:
print("Error: Failed to parse existing traffic data JSON file.")
@ -71,7 +77,7 @@ def traffic_status():
else:
existing_data[user] = data
with open('/etc/hysteria/traffic_data.json', 'w') as json_file:
with open(TRAFFIC_FILE, 'w') as json_file:
json.dump(existing_data, json_file, indent=4)
display_traffic_data(existing_data, green, cyan, NC)
@ -108,4 +114,3 @@ def format_bytes(bytes):
return f"{bytes / 1073741824:.2f}GB"
else:
return f"{bytes / 1099511627776:.2f}TB"