#!/bin/bash

echo "=== 代理程序安装脚本 ==="

# 检测系统类型
if [ -f /etc/debian_version ]; then
    echo "检测到 Debian/Ubuntu 系统"
    PKG_MANAGER="apt-get"
elif [ -f /etc/redhat-release ]; then
    echo "检测到 CentOS/RHEL 系统"
    PKG_MANAGER="yum"
else
    echo "未知系统类型，请手动安装"
    exit 1
fi

# 安装依赖
echo "安装依赖包..."
sudo $PKG_MANAGER update
sudo $PKG_MANAGER install -y wget curl unzip

# 下载 goproxy
echo "下载 goproxy..."
cd /tmp
wget https://github.com/snail007/goproxy/releases/download/v13.3/proxy-linux-amd64.tar.gz
tar -xzf proxy-linux-amd64.tar.gz
sudo mv proxy /usr/local/bin/
sudo chmod +x /usr/local/bin/proxy

# 创建配置文件
echo "创建配置文件..."
sudo mkdir -p /etc/goproxy
sudo tee /etc/goproxy/config.json > /dev/null <<EOF
{
    "proxy": {
        "http": {
            "port": 80,
            "target": "https://andys-dandy-site-4c8fd6.webflow.io",
            "replace": {
                "andys-dandy-site-4c8fd6.webflow.io": "pog.cxhy.cn",
                "https://andys-dandy-site-4c8fd6.webflow.io": "https://pog.cxhy.cn"
            }
        }
    }
}
EOF

# 创建systemd服务
echo "创建系统服务..."
sudo tee /etc/systemd/system/goproxy.service > /dev/null <<EOF
[Unit]
Description=GoProxy Service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/proxy http -t tcp -p "0.0.0.0:80" --forever
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

# 启动服务
echo "启动服务..."
sudo systemctl daemon-reload
sudo systemctl enable goproxy
sudo systemctl start goproxy

# 检查状态
echo "检查服务状态..."
sudo systemctl status goproxy

echo "=== 安装完成 ==="
echo "代理服务已启动在端口 80"
echo "目标网站: andys-dandy-site-4c8fd6.webflow.io"
echo "显示域名: pog.cxhy.cn"
echo ""
echo "管理命令："
echo "启动: sudo systemctl start goproxy"
echo "停止: sudo systemctl stop goproxy"
echo "重启: sudo systemctl restart goproxy"
echo "状态: sudo systemctl status goproxy"
echo "日志: sudo journalctl -u goproxy -f" 