Files
gotelegram_pro/install.sh

123 lines
4.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Установка GoTelegram MTProxy Bot.
# Формат как kaskad: curl -sL -H "Authorization: token TOKEN" https://raw.githubusercontent.com/anten-ka/gotelegram_pro/main/install.sh -o /usr/local/bin/gotelegram && chmod +x /usr/local/bin/gotelegram && systemctl restart gotelegram-bot 2>/dev/null; GITHUB_TOKEN=TOKEN gotelegram
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Запустите с sudo.${NC}"
exit 1
fi
# Токен можно передать аргументом: gotelegram ВАШ_ТОКЕН (для приватного репо)
[ -n "$1" ] && export GITHUB_TOKEN="$1"
BOT_DIR="/opt/gotelegram-bot"
SERVICE_NAME="gotelegram-bot"
REPO_RAW="${GOTETELEGRAM_REPO_RAW:-https://raw.githubusercontent.com/anten-ka/gotelegram_pro/main}"
echo -e "${GREEN}[*] GoTelegram Bot — установка...${NC}"
# Зависимости
if ! command -v python3 &>/dev/null; then
if command -v apt-get &>/dev/null; then
apt-get update && apt-get install -y python3 python3-pip python3-venv curl
elif command -v dnf &>/dev/null; then
dnf install -y python3 python3-pip python3-virtualenv curl 2>/dev/null || dnf install -y python3 python3-pip curl
elif command -v yum &>/dev/null; then
yum install -y python3 python3-pip curl
else
echo -e "${RED}Установите python3 и curl.${NC}"
exit 1
fi
fi
if ! command -v docker &>/dev/null; then
echo -e "${YELLOW}[!] Docker не найден. Нужен для /install в боте.${NC}"
fi
mkdir -p "$BOT_DIR"
cd "$BOT_DIR"
# Скачивание файлов бота по raw URL (с токеном для приватного репо)
fetch_file() {
local f="$1"
if [ -n "$GITHUB_TOKEN" ]; then
curl -sL -f -H "Authorization: token $GITHUB_TOKEN" "$REPO_RAW/gotelegram-bot/$f" -o "$BOT_DIR/$f" 2>/dev/null && return 0
fi
curl -sL -f "$REPO_RAW/gotelegram-bot/$f" -o "$BOT_DIR/$f" 2>/dev/null && return 0
return 1
}
if [ ! -f "$BOT_DIR/bot.py" ]; then
echo -e "${GREEN}[*] Загрузка файлов из репозитория...${NC}"
OK=1
for f in bot.py requirements.txt config.example.env; do
fetch_file "$f" || { OK=0; break; }
done
if [ "$OK" -eq 0 ] && [ -n "$GITHUB_TOKEN" ] && command -v git &>/dev/null; then
echo -e "${GREEN}[*] Клонирование по токену...${NC}"
TMP="/tmp/gotelegram_pro_$$"
git clone --depth 1 --branch main "https://${GITHUB_TOKEN}@github.com/anten-ka/gotelegram_pro.git" "$TMP" 2>/dev/null && {
cp -r "$TMP/gotelegram-bot"/* "$BOT_DIR/"
rm -rf "$TMP"
}
fi
if [ ! -f "$BOT_DIR/bot.py" ]; then
echo -e "${RED}Не удалось загрузить файлы. Для приватного репо запустите с токеном:${NC}"
echo -e " ${YELLOW}gotelegram ВАШ_GITHUB_ТОКЕН${NC}"
echo -e " или: ${YELLOW}GITHUB_TOKEN=ВАШ_ТОКЕН gotelegram${NC}"
exit 1
fi
fi
# venv
if [ ! -d "$BOT_DIR/venv" ]; then
python3 -m venv "$BOT_DIR/venv"
fi
"$BOT_DIR/venv/bin/pip" install -r "$BOT_DIR/requirements.txt" -q
# Конфиг
if [ ! -f "$BOT_DIR/.env" ]; then
echo -e "${YELLOW}Введите BOT_TOKEN от @BotFather:${NC}"
TOKEN=""
while [ -z "$TOKEN" ]; do
read -r TOKEN
TOKEN=$(echo "$TOKEN" | tr -d '[:space:]')
[ -z "$TOKEN" ] && echo -e "${RED}Токен не может быть пустым.${NC}"
done
echo "BOT_TOKEN=$TOKEN" > "$BOT_DIR/.env"
[ -f "$BOT_DIR/config.example.env" ] && grep -v "^BOT_TOKEN=" "$BOT_DIR/config.example.env" | grep -v "^#" >> "$BOT_DIR/.env"
chmod 600 "$BOT_DIR/.env"
echo -e "${GREEN}[*] .env создан.${NC}"
fi
# systemd
cat > "/etc/systemd/system/${SERVICE_NAME}.service" << EOF
[Unit]
Description=GoTelegram MTProxy Bot
After=network.target docker.service
[Service]
Type=simple
WorkingDirectory=$BOT_DIR
ExecStart=$BOT_DIR/venv/bin/python $BOT_DIR/bot.py
Restart=always
RestartSec=5
Environment=PATH=$BOT_DIR/venv/bin:/usr/bin
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME" 2>/dev/null || systemctl start "$SERVICE_NAME"
echo -e "${GREEN}[*] Сервис $SERVICE_NAME запущен.${NC}"
echo -e "Проверка: systemctl status $SERVICE_NAME"
echo -e "Логи: journalctl -u $SERVICE_NAME -f"
exit 0