Create install_gost_socks5.sh

This commit is contained in:
Lsmoisu 2025-05-06 16:02:37 +08:00 committed by GitHub
parent bc62c22e2a
commit 8fd20cda80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

135
install_gost_socks5.sh Normal file
View File

@ -0,0 +1,135 @@
#!/bin/bash
# 颜色定义,用于美化输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 函数:检查 GOST 进程或文件是否存在
check_gost() {
if pgrep gost > /dev/null; then
echo -e "${YELLOW}警告GOST 进程正在运行。${NC}"
return 1 # 存在进程
fi
if [ -f /usr/local/bin/gost ]; then
echo -e "${YELLOW}警告GOST 文件已存在。${NC}"
return 1 # 存在文件
fi
return 0 # 不存在
}
# 函数:获取 GOST 最新版本
get_latest_version() {
latest_info=$(curl -s https://api.github.com/repos/ginuerzh/gost/releases/latest)
if [ $? -ne 0 ]; then
echo -e "${RED}错误无法获取最新版本信息从参考版本回退v2.12.0)。${NC}"
echo "v2.12.0" # 回退到参考版本
else
echo "$latest_info" | grep -oP '"tag_name":\s*"\K(.*)(?=")' # 解析 tag_name
fi
}
# 主菜单
echo -e "${GREEN}请选择操作:${NC}"
echo -e "${YELLOW}1. 安装 GOST 并启用 SOCKS5 (以 root 用户运行)${NC}"
echo -e "${YELLOW}2. 卸载 GOST${NC}"
read -p "输入选项 (1 或 2): " choice
case $choice in
1)
# 安装逻辑
if ! check_gost; then
echo -e "${RED}检测到 GOST 相关进程或文件,请先卸载或停止。${NC}"
exit 1
fi
read -p "输入用户名: " username
read -p "输入密码: " password # 使用 -s 隐藏输入
echo # 换行
read -p "输入端口 (例如: 12333): " port
# 验证端口是否为数字(简单检查)
if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then
echo -e "${RED}错误:端口必须是 1-65535 之间的数字。${NC}"
exit 1
fi
latest_version=$(get_latest_version)
cleaned_version=${latest_version#v}
asset_url="https://github.com/ginuerzh/gost/releases/download/${latest_version}/gost_${cleaned_version}_linux_amd64.tar.gz"
echo -e "${GREEN}正在下载 GOST 版本 (${latest_version}) ...${NC}"
if ! command -v wget > /dev/null; then
curl -s -L -o gost.tar.gz "$asset_url" # 使用 -s 隐藏输出
else
wget -q -O gost.tar.gz "$asset_url" # 使用 -q 隐藏输出
fi
if [ $? -ne 0 ]; then
echo -e "${RED}错误:下载失败。${NC}"
exit 1
fi
tar -xzf gost.tar.gz -C /tmp/
gost_file=$(find /tmp/ -name "gost" -type f | head -n 1)
if [ -z "$gost_file" ]; then
echo -e "${RED}错误:未找到 gost 文件。${NC}"
rm -f gost.tar.gz
exit 1
fi
mv "$gost_file" /usr/local/bin/gost
chmod +x /usr/local/bin/gost
rm -f gost.tar.gz
rm -rf /tmp/*gost*
# 创建 systemd 服务文件,并设置 User=root (无注释)
cat > /etc/systemd/system/gost.service << EOF
[Unit]
Description=GOST Proxy Service
After=network.target
[Service]
User=root
ExecStart=/usr/local/bin/gost -L "socks5://${username}:${password}@:${port}"
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable gost
systemctl start gost
# 自动检测服务状态
echo -e "${GREEN}正在检测服务状态...${NC}"
service_status=$(systemctl is-active gost) # 检查服务是否 active
if [ "$service_status" = "active" ]; then
echo -e "${GREEN}GOST 安装成功 (端口: ${port})。服务已启动。${NC}"
echo "管理命令:"
echo " systemctl status gost - 查看状态"
echo " systemctl stop gost - 停止服务"
echo " systemctl restart gost - 重启服务"
else
echo -e "${RED}错误:服务启动失败或未运行正常。请检查日志 (journalctl -u gost)。${NC}"
exit 1 # 退出脚本
fi
;;
2)
# 卸载逻辑
sudo systemctl stop gost
sudo systemctl disable gost
sudo rm /etc/systemd/system/gost.service
sudo rm /usr/local/bin/gost
sudo systemctl daemon-reload
echo -e "${GREEN}GOST 已卸载。${NC}"
;;
*)
echo -e "${RED}无效选项,请输入 1 或 2。${NC}"
exit 1
;;
esac