telegram-shop/wg/start.sh
2025-03-06 16:13:11 +00:00

192 lines
5.2 KiB
Bash

#!/bin/sh
# Функция для отображения разделителя
print_separator() {
echo "════════════════════════════════════════════════════════════════════════════════"
}
# Функция для отображения заголовка этапа
print_stage() {
echo "║ 🚀 $1"
print_separator
}
# Функция для отображения результата
print_result() {
local status=$?
local message=$1
local action=$2
if [ -n "$action" ]; then
case "$action" in
"created")
echo "║ 🆕 $message"
;;
"exists")
echo "║ ✅ $message"
;;
*)
if [ $status -eq 0 ]; then
echo "║ ✅ $message"
else
echo "║ ❌ $message"
fi
;;
esac
else
if [ $status -eq 0 ]; then
echo "║ ✅ $message"
else
echo "║ ❌ $message"
fi
fi
print_separator
}
# Проверка включения WireGuard
if [ "$WG_ENABLED" = "false" ]; then
print_stage "WireGuard is disabled"
print_result "Skipping WireGuard setup"
print_stage "Starting application"
echo "║ Application is starting..."
exec node src/index.js
exit 0
fi
# Проверка наличия /etc/resolv.conf
print_stage "Checking /etc/resolv.conf"
if [ ! -f /etc/resolv.conf ]; then
echo "║ /etc/resolv.conf not found. Creating it..."
echo "nameserver 1.1.1.1" > /etc/resolv.conf
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
if [ $? -eq 0 ]; then
print_result "/etc/resolv.conf created successfully." "created"
else
print_result "Failed to create /etc/resolv.conf"
exit 1
fi
else
print_result "/etc/resolv.conf already exists." "exists"
fi
# Проверка наличия конфига WireGuard
print_stage "Checking WireGuard config"
if [ ! -f /etc/wireguard/wg0.conf ]; then
echo "║ Error: WireGuard config not found!"
exit 1
else
if [ -r /etc/wireguard/wg0.conf ]; then
print_result "WireGuard config found and readable." "exists"
else
print_result "WireGuard config found but not readable!"
exit 1
fi
fi
# Проверка сети ДО включения WireGuard
print_stage "Testing connectivity BEFORE WireGuard"
echo "║ Pinging 1.1.1.1..."
ping -c 4 1.1.1.1 > /tmp/ping.log 2>&1
if [ $? -eq 0 ]; then
echo "║ Ping successful."
cat /tmp/ping.log | sed 's/^/║ /'
else
echo "║ Ping failed."
fi
print_separator
# Извлекаем DNS из конфига WireGuard
WG_DNS=$(awk -F= '/DNS/ {print $2}' /etc/wireguard/wg0.conf | xargs)
# Настройка DNS
print_stage "Configuring DNS"
if [ -n "$WG_DNS" ]; then
echo "║ Using DNS from WireGuard config: $WG_DNS"
echo "nameserver $WG_DNS" > /etc/resolv.conf
if [ $? -eq 0 ]; then
print_result "DNS configured using WireGuard settings." "created"
else
print_result "Failed to configure DNS!"
exit 1
fi
else
echo "║ Using fallback DNS: 1.1.1.1, 8.8.8.8"
echo "nameserver 1.1.1.1" > /etc/resolv.conf
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
if [ $? -eq 0 ]; then
print_result "DNS configured using fallback settings." "created"
else
print_result "Failed to configure DNS!"
exit 1
fi
fi
# Проверка включения WireGuard
print_stage "Checking WireGuard status"
if [ "$WG_ENABLED" = "true" ]; then
echo "║ WireGuard is enabled. Starting..."
# Запуск WireGuard
wg-quick up wg0 2>&1 | tee /tmp/wg.log
wg_status=$?
if [ $wg_status -eq 0 ]; then
echo "║ WireGuard started successfully."
print_result "WireGuard interface activated successfully."
else
echo "║ WireGuard failed to start. Logs:"
cat /tmp/wg.log | sed 's/^/║ /'
print_result "Failed to start WireGuard interface!"
exit 1
fi
# Проверка маршрутизации после запуска WireGuard
print_stage "Routing table AFTER WireGuard"
ip route | sed 's/^/║ /'
print_separator
# Проверка сети ПОСЛЕ включения WireGuard
print_stage "Testing connectivity AFTER WireGuard"
echo "║ Pinging 1.1.1.1..."
ping -c 4 1.1.1.1 > /tmp/ping.log 2>&1
if [ $? -eq 0 ]; then
echo "║ Ping successful."
cat /tmp/ping.log | sed 's/^/║ /'
else
echo "║ Ping failed."
fi
print_separator
else
echo "║ WireGuard is disabled. Skipping..."
print_result "WireGuard is disabled in configuration."
fi
# Проверка DNS
print_stage "Testing DNS"
nslookup api.ipify.org > /tmp/dns.log 2>&1
if [ $? -eq 0 ]; then
echo "║ DNS lookup successful."
cat /tmp/dns.log | sed 's/^/║ /'
else
echo "║ DNS lookup failed."
fi
print_separator
# Проверка подключения через icanhazip.com
print_stage "Testing external connectivity (icanhazip.com)"
echo "║ Fetching external IP..."
curl -s https://icanhazip.com > /tmp/curl.log 2>&1
if [ $? -eq 0 ]; then
echo "║ Connection successful."
echo "║ External IP: $(cat /tmp/curl.log)"
else
echo "║ Connection failed."
fi
print_separator
# Запуск приложения
print_stage "Starting application"
echo "║ Application is starting..."
exec node src/index.js