Merge pull request #29 from ReturnFI/Dev

Fix Backup and Telegram BOT
This commit is contained in:
Whispering Wind
2024-09-04 13:16:06 +03:30
committed by GitHub
4 changed files with 46 additions and 31 deletions

View File

@ -1,6 +1,5 @@
#!/bin/bash #!/bin/bash
# Source the path.sh script to load the necessary variables
source /etc/hysteria/core/scripts/path.sh source /etc/hysteria/core/scripts/path.sh
source /etc/hysteria/core/scripts/utils.sh source /etc/hysteria/core/scripts/utils.sh
define_colors define_colors
@ -8,14 +7,11 @@ define_colors
install_hysteria() { install_hysteria() {
local port=$1 local port=$1
# Step 1: Install Hysteria2
echo "Installing Hysteria2..." echo "Installing Hysteria2..."
bash <(curl -fsSL https://get.hy2.sh/) >/dev/null 2>&1 bash <(curl -fsSL https://get.hy2.sh/) >/dev/null 2>&1
# Step 2: Create hysteria directory and navigate into it
mkdir -p /etc/hysteria && cd /etc/hysteria/ mkdir -p /etc/hysteria && cd /etc/hysteria/
# Step 3: Generate CA key and certificate and download geo data
echo "Generating CA key and certificate..." echo "Generating CA key and certificate..."
openssl ecparam -genkey -name prime256v1 -out ca.key >/dev/null 2>&1 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 openssl req -new -x509 -days 36500 -key ca.key -out ca.crt -subj "/CN=bts.com" >/dev/null 2>&1
@ -23,10 +19,8 @@ install_hysteria() {
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/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 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
fingerprint=$(openssl x509 -noout -fingerprint -sha256 -inform pem -in ca.crt | sed 's/.*=//;s/://g') fingerprint=$(openssl x509 -noout -fingerprint -sha256 -inform pem -in ca.crt | sed 's/.*=//;s/://g')
# Step 5: Generate the base64 encoded SHA-256 fingerprint
echo "Generating base64 encoded SHA-256 fingerprint..." echo "Generating base64 encoded SHA-256 fingerprint..."
cat <<EOF > generate.py cat <<EOF > generate.py
import base64 import base64
@ -45,12 +39,9 @@ base64_encoded = base64.b64encode(binary_data).decode('utf-8')
print('sha256/' + base64_encoded) print('sha256/' + base64_encoded)
EOF EOF
# Execute the Python script and capture the output
sha256=$(python3 generate.py) sha256=$(python3 generate.py)
# Step 7: Ask for the port number and validate input
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
if ss -tuln | grep -q ":$port\b"; then if ss -tuln | grep -q ":$port\b"; then
echo -e "${red}Port $port is already in use. Please choose another port.${NC}" echo -e "${red}Port $port is already in use. Please choose another port.${NC}"
exit 1 exit 1
@ -60,24 +51,19 @@ EOF
exit 1 exit 1
fi fi
# 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
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
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
networkdef=$(ip route | grep "^default" | awk '{print $5}') networkdef=$(ip route | grep "^default" | awk '{print $5}')
# 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" \
@ -92,21 +78,18 @@ EOF
.trafficStats.secret = $UUID | .trafficStats.secret = $UUID |
.outbounds[0].direct.bindDevice = $networkdef' "$CONFIG_FILE" > "${CONFIG_FILE}.temp" && mv "${CONFIG_FILE}.temp" "$CONFIG_FILE" .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..." 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|$CONFIG_FILE|" /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 rm /etc/hysteria/config.yaml
sleep 1 sleep 1
# 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
if systemctl is-active --quiet hysteria-server.service; then if systemctl is-active --quiet hysteria-server.service; then
echo -e "${cyan}Hysteria2${NC} has been successfully installed." echo -e "${cyan}Hysteria2${NC} has been successfully installed."
else else
@ -114,12 +97,11 @@ EOF
exit 1 exit 1
fi fi
# Step 15: Give right permissions to scripts
chmod +x /etc/hysteria/core/scripts/hysteria2/user.sh chmod +x /etc/hysteria/core/scripts/hysteria2/user.sh
chmod +x /etc/hysteria/core/scripts/hysteria2/kick.sh chmod +x /etc/hysteria/core/scripts/hysteria2/kick.sh
# Add the scripts to the crontab
(crontab -l ; echo "*/1 * * * * /bin/bash -c 'source /etc/hysteria/hysteria2_venv/bin/activate && python3 /etc/hysteria/core/cli.py traffic-status' >/dev/null 2>&1") | crontab - (crontab -l ; echo "*/1 * * * * /bin/bash -c 'source /etc/hysteria/hysteria2_venv/bin/activate && python3 /etc/hysteria/core/cli.py traffic-status' >/dev/null 2>&1") | crontab -
(crontab -l ; echo "0 */6 * * * /bin/bash -c 'source /etc/hysteria/hysteria2_venv/bin/activate && python3 /etc/hysteria/core/cli.py backup-hysteria' >/dev/null 2>&1") | crontab -
(crontab -l ; echo "*/1 * * * * /etc/hysteria/core/scripts/hysteria2/kick.sh >/dev/null 2>&1") | crontab - (crontab -l ; echo "*/1 * * * * /etc/hysteria/core/scripts/hysteria2/kick.sh >/dev/null 2>&1") | crontab -
} }

View File

@ -14,6 +14,7 @@ load_dotenv()
API_TOKEN = os.getenv('API_TOKEN') API_TOKEN = os.getenv('API_TOKEN')
ADMIN_USER_IDS = json.loads(os.getenv('ADMIN_USER_IDS')) ADMIN_USER_IDS = json.loads(os.getenv('ADMIN_USER_IDS'))
CLI_PATH = '/etc/hysteria/core/cli.py' CLI_PATH = '/etc/hysteria/core/cli.py'
BACKUP_DIRECTORY = '/opt/hysbackup'
bot = telebot.TeleBot(API_TOKEN) bot = telebot.TeleBot(API_TOKEN)
def run_cli_command(command): def run_cli_command(command):
@ -28,6 +29,7 @@ def create_main_markup():
markup = types.ReplyKeyboardMarkup(resize_keyboard=True) markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
markup.row('Add User', 'Show User') markup.row('Add User', 'Show User')
markup.row('Delete User', 'Server Info') markup.row('Delete User', 'Server Info')
markup.row('Backup Server')
return markup return markup
def is_admin(user_id): def is_admin(user_id):
@ -88,10 +90,11 @@ def process_add_user_step3(message, username, traffic_limit):
lower_username = username.lower() lower_username = username.lower()
command = f"python3 {CLI_PATH} add-user -u {username} -t {traffic_limit} -e {expiration_days}" command = f"python3 {CLI_PATH} add-user -u {username} -t {traffic_limit} -e {expiration_days}"
result = run_cli_command(command) result = run_cli_command(command)
qr_command = f"python3 {CLI_PATH} show-user-uri -u {lower_username} -ip 4" qr_command = f"python3 {CLI_PATH} show-user-uri -u {lower_username} -ip 4"
qr_result = run_cli_command(qr_command) qr_result = run_cli_command(qr_command).replace("IPv4:\n", "").strip()
if qr_result.strip() == "": if not qr_result:
bot.reply_to(message, "Failed to generate QR code.") bot.reply_to(message, "Failed to generate QR code.")
return return
@ -99,9 +102,9 @@ def process_add_user_step3(message, username, traffic_limit):
bio_v4 = io.BytesIO() bio_v4 = io.BytesIO()
qr_v4.save(bio_v4, 'PNG') qr_v4.save(bio_v4, 'PNG')
bio_v4.seek(0) bio_v4.seek(0)
caption = f"{result}" caption = f"{result}\n\n`{qr_result}`"
bot.send_photo(message.chat.id, photo=bio_v4, caption=caption) bot.send_photo(message.chat.id, photo=bio_v4, caption=caption, parse_mode="Markdown")
except ValueError: except ValueError:
bot.reply_to(message, "Invalid expiration days. Please enter a number.") bot.reply_to(message, "Invalid expiration days. Please enter a number.")
@ -300,6 +303,32 @@ def process_delete_user(message):
result = run_cli_command(command) result = run_cli_command(command)
bot.reply_to(message, result) bot.reply_to(message, result)
@bot.message_handler(func=lambda message: is_admin(message.from_user.id) and message.text == 'Backup Server')
def backup_server(message):
bot.reply_to(message, "Starting backup. This may take a few moments...")
backup_command = f"python3 {CLI_PATH} backup-hysteria"
result = run_cli_command(backup_command)
if "Error" in result:
bot.reply_to(message, f"Backup failed: {result}")
return
try:
files = [f for f in os.listdir(BACKUP_DIRECTORY) if f.endswith('.zip')]
files.sort(key=lambda x: os.path.getctime(os.path.join(BACKUP_DIRECTORY, x)), reverse=True)
latest_backup_file = files[0] if files else None
except Exception as e:
bot.reply_to(message, f"Failed to locate the backup file: {str(e)}")
return
if latest_backup_file:
backup_file_path = os.path.join(BACKUP_DIRECTORY, latest_backup_file)
with open(backup_file_path, 'rb') as f:
bot.send_document(message.chat.id, f, caption=f"Backup completed: {latest_backup_file}")
else:
bot.reply_to(message, "No backup file found after the backup process.")
@bot.inline_handler(lambda query: is_admin(query.from_user.id)) @bot.inline_handler(lambda query: is_admin(query.from_user.id))
def handle_inline_query(query): def handle_inline_query(query):
command = f"python3 {CLI_PATH} list-users" command = f"python3 {CLI_PATH} list-users"

View File

@ -1,6 +1,6 @@
pyTelegramBotAPI pyTelegramBotAPI==4.22.1
qrcode qrcode==7.4.2
python-dotenv python-dotenv==1.0.1
requests requests==2.32.3
aiohttp aiohttp==3.10.5
click click==8.1.7

View File

@ -2,6 +2,10 @@
cd /root/ cd /root/
if ! command -v zip &> /dev/null; then
apt install -y zip
fi
TEMP_DIR=$(mktemp -d) TEMP_DIR=$(mktemp -d)
FILES=( FILES=(