gost通讯改为ws

This commit is contained in:
qaq
2025-06-25 10:49:06 +08:00
parent f3e9b31ed3
commit f7e82cdc8b
47 changed files with 3528 additions and 1513 deletions
Regular → Executable
+211 -102
View File
@@ -1,113 +1,112 @@
#!/bin/bash
ARCH=$(uname -m)
if [[ "$ARCH" != "x86_64" ]]; then
echo "❌ 不支持的架构: $ARCH,仅支持 x86_64。"
exit 1
fi
# 下载地址
DOWNLOAD_URL="https://github.com/bqlpfy/forward-panel/releases/download/gost/gost"
DOWNLOAD_URL="https://ghproxy.com/https://github.com/bqlpfy/forward-panel/releases/download/gost/gost"
INSTALL_DIR="/etc/gost"
# 解析参数
while getopts "a:p:s:" opt; do
# 显示菜单
show_menu() {
echo "==============================================="
echo " 管理脚本"
echo "==============================================="
echo "请选择操作:"
echo "1. 安装"
echo "2. 更新"
echo "3. 卸载"
echo "4. 退出"
echo "==============================================="
}
# 获取用户输入的配置参数
get_config_params() {
if [[ -z "$SERVER_ADDR" || -z "$SECRET" ]]; then
echo "请输入配置参数:"
if [[ -z "$SERVER_ADDR" ]]; then
read -p "服务器地址: " SERVER_ADDR
fi
if [[ -z "$SECRET" ]]; then
read -p "密钥: " SECRET
fi
if [[ -z "$SERVER_ADDR" || -z "$SECRET" ]]; then
echo "❌ 参数不完整,操作取消。"
exit 1
fi
fi
}
# 解析命令行参数
while getopts "a:s:" opt; do
case $opt in
a) SERVER_ADDR="$OPTARG" ;;
p) PORT="$OPTARG" ;;
s) SECRET="$OPTARG" ;;
*) echo "❌ 无效参数"; exit 1 ;;
esac
done
if [[ -z "$SERVER_ADDR" || -z "$PORT" || -z "$SECRET" ]]; then
echo "用法: $0 -a 服务器地址 -p 端口 -s 密钥"
exit 1
fi
# 安装功能
install_gost() {
echo "🚀 开始安装 GOST..."
get_config_params
mkdir -p "$INSTALL_DIR"
INSTALL_DIR="/etc/gost"
mkdir -p "$INSTALL_DIR"
# 停止并禁用已有服务
if systemctl list-units --full -all | grep -Fq "gost.service"; then
echo "🔍 检测到已存在的gost服务"
systemctl stop gost 2>/dev/null && echo "🛑 停止服务"
systemctl disable gost 2>/dev/null && echo "🚫 禁用自启"
fi
# 停止并禁用已有服务
if systemctl list-units --full -all | grep -Fq "gost.service"; then
echo "🔍 检测到已存在的gost服务"
systemctl stop gost 2>/dev/null && echo "🛑 停止服务"
systemctl disable gost 2>/dev/null && echo "🚫 禁用自启"
fi
# 删除旧文件
[[ -f "$INSTALL_DIR/gost" ]] && echo "🧹 删除旧文件 gost" && rm -f "$INSTALL_DIR/gost"
# 删除旧文件
[[ -f "$INSTALL_DIR/gost" ]] && echo "🧹 删除旧文件 gost" && rm -f "$INSTALL_DIR/gost"
# 下载 gost
echo "⬇️ 下载 gost 中..."
curl -L "$DOWNLOAD_URL" -o "$INSTALL_DIR/gost"
if [[ ! -f "$INSTALL_DIR/gost" || ! -s "$INSTALL_DIR/gost" ]]; then
echo "❌ 下载失败,请检查网络或下载链接。"
exit 1
fi
chmod +x "$INSTALL_DIR/gost"
echo "✅ 下载完成"
# 下载 gost
echo "⬇️ 下载 gost 中..."
curl -L "$DOWNLOAD_URL" -o "$INSTALL_DIR/gost"
if [[ ! -f "$INSTALL_DIR/gost" || ! -s "$INSTALL_DIR/gost" ]]; then
echo "❌ 下载失败,请检查网络或下载链接。"
exit 1
fi
chmod +x "$INSTALL_DIR/gost"
echo "✅ 下载完成"
# 打印版本
echo "🔎 gost 版本:$($INSTALL_DIR/gost -V)"
# 打印版本
echo "🔎 gost 版本:$($INSTALL_DIR/gost -V)"
# 写入 config.json
CONFIG_FILE="$INSTALL_DIR/config.json"
if [[ -f "$CONFIG_FILE" ]]; then
echo "📝 更新配置: config.json"
sed -i.bak "s|\"addr\": \".*\"|\"addr\": \"$SERVER_ADDR\"|g" "$CONFIG_FILE"
sed -i.bak "s|\"secret\": \".*\"|\"secret\": \"$SECRET\"|g" "$CONFIG_FILE"
rm -f "$CONFIG_FILE.bak"
else
echo "📄 创建新配置: config.json"
cat > "$CONFIG_FILE" <<EOF
# 写入 config.json
CONFIG_FILE="$INSTALL_DIR/config.json"
if [[ -f "$CONFIG_FILE" ]]; then
echo "⏭️ 跳过配置文件: config.json (已存在)"
else
echo "📄 创建新配置: config.json"
cat > "$CONFIG_FILE" <<EOF
{
"addr": "$SERVER_ADDR",
"secret": "$SECRET"
}
EOF
fi
fi
# 写入 gost.json
GOST_CONFIG="$INSTALL_DIR/gost.json"
if [[ -f "$GOST_CONFIG" ]]; then
echo "📝 更新配置: gost.json"
sed -i.bak '/\"name\": \"web_api\"/,/}/ { s|\"addr\": \":.*\"|\"addr\": \":'"$PORT"'\"|g; }' "$GOST_CONFIG"
sed -i.bak '/\"name\": \"web_api\"/,/}/ { s|\"username\": \".*\"|\"username\": \"'"$SECRET"'\"|g; }' "$GOST_CONFIG"
sed -i.bak '/\"name\": \"web_api\"/,/}/ { s|\"password\": \".*\"|\"password\": \"'"$SECRET"'\"|g; }' "$GOST_CONFIG"
rm -f "$GOST_CONFIG.bak"
else
echo "📄 创建新配置: gost.json"
cat > "$GOST_CONFIG" <<EOF
{
"services": [
{
"name": "web_api",
"addr": ":$PORT",
"handler": {
"type": "api",
"auth": {
"username": "$SECRET",
"password": "$SECRET"
},
"metadata": {
"pathPrefix": "/api"
}
},
"listener": {
"type": "tls"
}
}
]
}
# 写入 gost.json
GOST_CONFIG="$INSTALL_DIR/gost.json"
if [[ -f "$GOST_CONFIG" ]]; then
echo "⏭️ 跳过配置文件: gost.json (已存在)"
else
echo "📄 创建新配置: gost.json"
cat > "$GOST_CONFIG" <<EOF
{}
EOF
fi
fi
# 加强权限
chmod 600 "$INSTALL_DIR"/*.json
# 加强权限
chmod 600 "$INSTALL_DIR"/*.json
# 创建 systemd 服务
SERVICE_FILE="/etc/systemd/system/gost.service"
cat > "$SERVICE_FILE" <<EOF
# 创建 systemd 服务
SERVICE_FILE="/etc/systemd/system/gost.service"
cat > "$SERVICE_FILE" <<EOF
[Unit]
Description=Gost Proxy Service
After=network.target
@@ -121,22 +120,132 @@ Restart=on-failure
WantedBy=multi-user.target
EOF
# 启动服务
systemctl daemon-reload
systemctl enable gost
systemctl start gost
# 启动服务
systemctl daemon-reload
systemctl enable gost
systemctl start gost
# 检查状态
echo "🔄 检查服务状态..."
if systemctl is-active --quiet gost; then
echo "✅ 安装完成,gost服务已启动并设置为开机启动。"
echo "📁 配置目录: $INSTALL_DIR"
echo "🔧 服务状态: $(systemctl is-active gost)"
else
echo "❌ gost服务启动失败,请执行以下命令查看日志:"
echo "journalctl -u gost -f"
fi
# 检查状态
echo "🔄 检查服务状态..."
if systemctl is-active --quiet gost; then
echo "✅ 安装完成,gost服务已启动并设置为开机启动。"
echo "📁 配置目录: $INSTALL_DIR"
echo "🔧 服务状态: $(systemctl is-active gost)"
else
echo "❌ gost服务启动失败,请执行以下命令查看日志:"
echo "journalctl -u gost -f"
fi
}
# 删除安装脚本自身
#echo "🧹 清理安装脚本..."
#rm -f "$0" 2>/dev/null && echo "✨ 安装脚本已自动清理" || echo "⚠️ 安装脚本清理失败,请手动删除"
# 更新功能
update_gost() {
echo "🔄 开始更新 GOST..."
if [[ ! -d "$INSTALL_DIR" ]]; then
echo "❌ GOST 未安装,请先选择安装。"
return 1
fi
# 先下载新版本
echo "⬇️ 下载最新版本..."
curl -L "$DOWNLOAD_URL" -o "$INSTALL_DIR/gost.new"
if [[ ! -f "$INSTALL_DIR/gost.new" || ! -s "$INSTALL_DIR/gost.new" ]]; then
echo "❌ 下载失败。"
return 1
fi
# 停止服务
if systemctl list-units --full -all | grep -Fq "gost.service"; then
echo "🛑 停止 gost 服务..."
systemctl stop gost
fi
# 替换文件
mv "$INSTALL_DIR/gost.new" "$INSTALL_DIR/gost"
chmod +x "$INSTALL_DIR/gost"
# 打印版本
echo "🔎 新版本:$($INSTALL_DIR/gost -V)"
# 重启服务
echo "🔄 重启服务..."
systemctl start gost
echo "✅ 更新完成,服务已重新启动。"
}
# 卸载功能
uninstall_gost() {
echo "🗑️ 开始卸载 GOST..."
read -p "确认卸载 GOST 吗?此操作将删除所有相关文件 (y/N): " confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
echo "❌ 取消卸载"
return 0
fi
# 停止并禁用服务
if systemctl list-units --full -all | grep -Fq "gost.service"; then
echo "🛑 停止并禁用服务..."
systemctl stop gost 2>/dev/null
systemctl disable gost 2>/dev/null
fi
# 删除服务文件
if [[ -f "/etc/systemd/system/gost.service" ]]; then
rm -f "/etc/systemd/system/gost.service"
echo "🧹 删除服务文件"
fi
# 删除安装目录
if [[ -d "$INSTALL_DIR" ]]; then
rm -rf "$INSTALL_DIR"
echo "🧹 删除安装目录: $INSTALL_DIR"
fi
# 重载 systemd
systemctl daemon-reload
echo "✅ 卸载完成"
}
# 主逻辑
main() {
# 如果提供了命令行参数,直接执行安装
if [[ -n "$SERVER_ADDR" && -n "$SECRET" ]]; then
install_gost
exit 0
fi
# 显示交互式菜单
while true; do
show_menu
read -p "请输入选项 (1-4): " choice
case $choice in
1)
install_gost
break
;;
2)
update_gost
break
;;
3)
uninstall_gost
break
;;
4)
echo "👋 退出脚本"
exit 0
;;
*)
echo "❌ 无效选项,请输入 1-4"
echo ""
;;
esac
done
}
# 执行主函数
main