Isolate change_port

This commit is contained in:
Sarina
2024-07-21 18:09:00 +03:30
parent 749f524490
commit d685307280
3 changed files with 145 additions and 187 deletions

View File

@ -0,0 +1,16 @@
while true; do
read -p "Enter the new port number you want to use: " port
if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then
echo "Invalid port number. Please enter a number between 1 and 65535."
else
break
fi
done
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
restart_hysteria_service >/dev/null 2>&1
echo "Port changed successfully to $port."
else
echo "${red}Error:${NC} Config file /etc/hysteria/config.json not found."
fi

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
define_colors() { define_colors() {
green='\033[0;32m' green='\033[0;32m'
cyan='\033[0;36m' cyan='\033[0;36m'
@ -7,43 +8,45 @@ define_colors() {
LPurple='\033[1;35m' LPurple='\033[1;35m'
NC='\033[0m' NC='\033[0m'
} }
# Step 1: Install Hysteria2
echo "Installing Hysteria2..."
bash <(curl -fsSL https://get.hy2.sh/) >/dev/null 2>&1
# Step 2: Create hysteria directory and navigate into it install_hysteria() {
mkdir -p /etc/hysteria && cd /etc/hysteria/ # Step 1: Install Hysteria2
echo "Installing Hysteria2..."
bash <(curl -fsSL https://get.hy2.sh/) >/dev/null 2>&1
# Step 3: Generate CA key and certificate and download geo data # Step 2: Create hysteria directory and navigate into it
echo "Generating CA key and certificate..." mkdir -p /etc/hysteria && cd /etc/hysteria/
openssl ecparam -genkey -name prime256v1 -out ca.key >/dev/null 2>&1
openssl req -new -x509 -days 36500 -key ca.key -out ca.crt -subj "/CN=bts.com" >/dev/null 2>&1
echo "Downloading geo data..."
wget -O /etc/hysteria/geosite.dat https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geosite.dat >/dev/null 2>&1
wget -O /etc/hysteria/geoip.dat https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geoip.dat >/dev/null 2>&1
# Step 4: Extract the SHA-256 fingerprint # Step 3: Generate CA key and certificate and download geo data
fingerprint=$(openssl x509 -noout -fingerprint -sha256 -inform pem -in ca.crt | sed 's/.*=//;s/://g') echo "Generating CA key and certificate..."
openssl ecparam -genkey -name prime256v1 -out ca.key >/dev/null 2>&1
openssl req -new -x509 -days 36500 -key ca.key -out ca.crt -subj "/CN=bts.com" >/dev/null 2>&1
echo "Downloading geo data..."
wget -O /etc/hysteria/geosite.dat https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geosite.dat >/dev/null 2>&1
wget -O /etc/hysteria/geoip.dat https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geoip.dat >/dev/null 2>&1
# Step 5: Generate the base64 encoded SHA-256 fingerprint # Step 4: Extract the SHA-256 fingerprint
echo "Generating base64 encoded SHA-256 fingerprint..." fingerprint=$(openssl x509 -noout -fingerprint -sha256 -inform pem -in ca.crt | sed 's/.*=//;s/://g')
echo "import re, base64, binascii
hex_string = \"$fingerprint\" # Step 5: Generate the base64 encoded SHA-256 fingerprint
binary_data = binascii.unhexlify(hex_string) echo "Generating base64 encoded SHA-256 fingerprint..."
base64_encoded = base64.b64encode(binary_data).decode('utf-8') echo "import re, base64, binascii
print(\"sha256/\" + base64_encoded)" > generate.py hex_string = \"$fingerprint\"
binary_data = binascii.unhexlify(hex_string)
base64_encoded = base64.b64encode(binary_data).decode('utf-8')
sha256=$(python3 generate.py) print(\"sha256/\" + base64_encoded)" > generate.py
# Step 6: Download the config.json file sha256=$(python3 generate.py)
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 # Step 6: Download the config.json file
while true; do 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
while true; do
read -p "Enter the port number you want to use (1-65535): " port read -p "Enter the port number you want to use (1-65535): " port
if [[ $port =~ ^[0-9]+$ ]] && (( port >= 1 && port <= 65535 )); then if [[ $port =~ ^[0-9]+$ ]] && (( port >= 1 && port <= 65535 )); then
# Check if the port is in use # Check if the port is in use
@ -57,28 +60,28 @@ while true; do
else else
echo "Invalid port number. Please enter a number between 1 and 65535." echo "Invalid port number. Please enter a number between 1 and 65535."
fi fi
done done
# Step 8: Generate required passwords and UUID # Step 8: Generate required passwords and UUID
echo "Generating passwords and UUID..." echo "Generating passwords and UUID..."
obfspassword=$(pwgen -s 32 1) obfspassword=$(pwgen -s 32 1)
UUID=$(uuidgen) UUID=$(uuidgen)
# Step 9: Adjust file permissions for Hysteria service # Step 9: Adjust file permissions for Hysteria service
chown hysteria:hysteria /etc/hysteria/ca.key /etc/hysteria/ca.crt chown hysteria:hysteria /etc/hysteria/ca.key /etc/hysteria/ca.crt
chmod 640 /etc/hysteria/ca.key /etc/hysteria/ca.crt chmod 640 /etc/hysteria/ca.key /etc/hysteria/ca.crt
# Create hysteria user without login permissions # Create hysteria user without login permissions
if ! id -u hysteria &> /dev/null; then if ! id -u hysteria &> /dev/null; then
useradd -r -s /usr/sbin/nologin hysteria useradd -r -s /usr/sbin/nologin hysteria
fi fi
# Get the default network interface # Get the default network interface
networkdef=$(ip route | grep "^default" | awk '{print $5}') networkdef=$(ip route | grep "^default" | awk '{print $5}')
# Step 10: Customize the config.json file # Step 10: Customize the config.json file
echo "Customizing config.json..." echo "Customizing config.json..."
jq --arg port "$port" \ jq --arg port "$port" \
--arg sha256 "$sha256" \ --arg sha256 "$sha256" \
--arg obfspassword "$obfspassword" \ --arg obfspassword "$obfspassword" \
--arg UUID "$UUID" \ --arg UUID "$UUID" \
@ -91,36 +94,61 @@ jq --arg port "$port" \
.trafficStats.secret = $UUID | .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' /etc/hysteria/config.json > /etc/hysteria/config_temp.json && mv /etc/hysteria/config_temp.json /etc/hysteria/config.json
# Step 11: Modify the systemd service file to use config.json # Step 11: Modify the systemd service file to use config.json
echo "Updating hysteria-server.service 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|(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|/etc/hysteria/config.json|' /etc/systemd/system/hysteria-server.service
rm /etc/hysteria/config.yaml rm /etc/hysteria/config.yaml
sleep 1 sleep 1
# Step 12: Start and enable the Hysteria service # Step 12: Start and enable the Hysteria service
echo "Starting and enabling Hysteria service..." echo "Starting and enabling Hysteria service..."
systemctl daemon-reload >/dev/null 2>&1 systemctl daemon-reload >/dev/null 2>&1
systemctl start hysteria-server.service >/dev/null 2>&1 systemctl start hysteria-server.service >/dev/null 2>&1
systemctl enable hysteria-server.service >/dev/null 2>&1 systemctl enable hysteria-server.service >/dev/null 2>&1
systemctl restart hysteria-server.service >/dev/null 2>&1 systemctl restart hysteria-server.service >/dev/null 2>&1
# Step 13: Check if the hysteria-server.service is active # Step 13: Check if the hysteria-server.service is active
if systemctl is-active --quiet hysteria-server.service; then if systemctl is-active --quiet hysteria-server.service; then
echo "${cyan}Hysteria2${green} has been successfully install." echo "${cyan}Hysteria2${green} has been successfully install."
else else
echo "${red}Error:${NC} hysteria-server.service is not active." echo "${red}Error:${NC} hysteria-server.service is not active."
fi
# Step 15: wget Traffic/user/kick script
wget https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/traffic.py -O /etc/hysteria/traffic.py >/dev/null 2>&1
mkdir -p /etc/hysteria/users
wget https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/user.sh -O /etc/hysteria/users/user.sh >/dev/null 2>&1
wget https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/kick.sh -O /etc/hysteria/users/kick.sh >/dev/null 2>&1
wget https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/modify.py -O /etc/hysteria/users/modify.py >/dev/null 2>&1
chmod +x /etc/hysteria/users/user.sh
chmod +x /etc/hysteria/users/kick.sh
# Add the commands to the crontab
(crontab -l ; echo "*/1 * * * * python3 /etc/hysteria/traffic.py >/dev/null 2>&1") | crontab -
(crontab -l ; echo "*/1 * * * * /etc/hysteria/users/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
echo "If you need to update the core, please use the 'Update Core' option."
else
echo "Installing and configuring Hysteria2..."
install_hysteria
echo -e "\n"
if systemctl is-active --quiet hysteria-server.service; then
echo "Installation and configuration complete."
else
echo -e "${red}Error:${NC} Hysteria2 service is not active. Please check the logs for more details."
fi
fi fi
# Step 15: wget Traffic/user/kick script
wget https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/traffic.py -O /etc/hysteria/traffic.py >/dev/null 2>&1
mkdir -p /etc/hysteria/users
wget https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/user.sh -O /etc/hysteria/users/user.sh >/dev/null 2>&1
wget https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/kick.sh -O /etc/hysteria/users/kick.sh >/dev/null 2>&1
wget https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/modify.py -O /etc/hysteria/users/modify.py >/dev/null 2>&1
chmod +x /etc/hysteria/users/user.sh
chmod +x /etc/hysteria/users/kick.sh
# Add the commands to the crontab
(crontab -l ; echo "*/1 * * * * python3 /etc/hysteria/traffic.py >/dev/null 2>&1") | crontab -
(crontab -l ; echo "*/1 * * * * /etc/hysteria/users/kick.sh >/dev/null 2>&1") | crontab -

86
menu.sh
View File

@ -37,92 +37,6 @@ get_system_info() {
RAM=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }') RAM=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')
} }
# Function to install and configure Hysteria2
install_and_configure() {
if systemctl is-active --quiet hysteria-server.service; then
echo -e "${red}Error:${NC} Hysteria2 is already installed and running."
echo
echo "If you need to update the core, please use the 'Update Core' option."
else
echo "Installing and configuring Hysteria2..."
bash <(curl -s https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/install.sh)
echo -e "\n"
if systemctl is-active --quiet hysteria-server.service; then
echo "Installation and configuration complete."
else
echo -e "${red}Error:${NC} Hysteria2 service is not active. Please check the logs for more details."
fi
fi
}
# TODO: remove
# Function to update Hysteria2
update_core() {
echo "Starting the update process for Hysteria2..."
echo "Backing up the current configuration..."
cp /etc/hysteria/config.json /etc/hysteria/config_backup.json
if [ $? -ne 0 ]; then
echo "${red}Error:${NC} 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
restart_hysteria_service >/dev/null 2>&1
return 1
fi
echo "Restoring configuration from backup..."
mv /etc/hysteria/config_backup.json /etc/hysteria/config.json
if [ $? -ne 0 ]; then
echo "${red}Error:${NC} 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
if [ $? -ne 0 ]; then
echo "${red}Error:${NC} Failed to modify systemd service."
return 1
fi
rm /etc/hysteria/config.yaml
systemctl daemon-reload >/dev/null 2>&1
restart_hysteria_service >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "${red}Error:${NC} Failed to restart Hysteria2 service."
return 1
fi
echo "Hysteria2 has been successfully updated."
echo ""
return 0
}
# Function to change port
change_port() {
while true; do
read -p "Enter the new port number you want to use: " port
if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then
echo "Invalid port number. Please enter a number between 1 and 65535."
else
break
fi
done
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
restart_hysteria_service >/dev/null 2>&1
echo "Port changed successfully to $port."
else
echo "${red}Error:${NC} Config file /etc/hysteria/config.json not found."
fi
}
# Function to show URI if Hysteria2 is installed and active # Function to show URI if Hysteria2 is installed and active
show_uri() { show_uri() {
if [ -f "/etc/hysteria/users/users.json" ]; then if [ -f "/etc/hysteria/users/users.json" ]; then