#!/bin/bash

# =============================================
# SOCKS5代理客户端配置脚本 (北京服务器)
# 功能：配置北京服务器通过香港SOCKS5代理访问外网
# =============================================

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 配置参数 (请根据实际情况修改)
HK_SERVER="154.94.238.27"  # 请替换为实际的香港服务器IP
SOCKS_PORT="1080"
PROFILE_FILE="$HOME/.bashrc"

log() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1"
    exit 1
}

validate_connection() {
    log "验证与香港服务器的连接..."
    
    if ! ping -c 2 -W 2 "$HK_SERVER" &> /dev/null; then
        warn "无法ping通香港服务器，继续尝试端口连接..."
    fi
    
    if ! nc -z -w 3 "$HK_SERVER" "$SOCKS_PORT" &> /dev/null; then
        error "无法连接到香港服务器的SOCKS5端口 $SOCKS_PORT，请检查:"
        echo "  1. 香港服务器IP是否正确: $HK_SERVER"
        echo "  2. SOCKS5服务是否正常运行"
        echo "  3. 防火墙是否开放端口"
    else
        log "✅ 成功连接到香港SOCKS5代理"
    fi
}

setup_temporary_proxy() {
    log "设置临时代理环境变量..."
    
    export http_proxy=socks5://$HK_SERVER:$SOCKS_PORT
    export https_proxy=socks5://$HK_SERVER:$SOCKS_PORT
    export HTTP_PROXY=socks5://$HK_SERVER:$SOCKS_PORT
    export HTTPS_PROXY=socks5://$HK_SERVER:$SOCKS_PORT
    export ALL_PROXY=socks5://$HK_SERVER:$SOCKS_PORT
    export SOCKS_SERVER=$HK_SERVER:$SOCKS_PORT
    
    echo "✅ 临时代理设置完成"
    echo "   代理地址: socks5://$HK_SERVER:$SOCKS_PORT"
}

setup_permanent_proxy() {
    log "配置永久代理设置..."
    
    # 备份原有配置文件
    if [ -f "$PROFILE_FILE" ]; then
        cp "$PROFILE_FILE" "${PROFILE_FILE}.backup.$(date +%Y%m%d%H%M%S)"
    fi
    
    # 添加代理配置到bashrc
    cat >> "$PROFILE_FILE" << EOF

# SOCKS5代理配置 (自动添加)
export http_proxy=socks5://$HK_SERVER:$SOCKS_PORT
export https_proxy=socks5://$HK_SERVER:$SOCKS_PORT
export HTTP_PROXY=socks5://$HK_SERVER:$SOCKS_PORT
export HTTPS_PROXY=socks5://$HK_SERVER:$SOCKS_PORT
export ALL_PROXY=socks5://$HK_SERVER:$SOCKS_PORT
export SOCKS_SERVER=$HK_SERVER:$SOCKS_PORT
EOF

    log "✅ 永久代理配置已添加到: $PROFILE_FILE"
    echo "   重新登录或执行 'source $PROFILE_FILE' 后生效"
}

configure_apt_proxy() {
    if [ -d /etc/apt/apt.conf.d ]; then
        log "配置APT包管理器代理..."
        
        cat > /etc/apt/apt.conf.d/95socks5proxy << EOF
Acquire::http::Proxy "socks5h://$HK_SERVER:$SOCKS_PORT";
Acquire::https::Proxy "socks5h://$HK_SERVER:$SOCKS_PORT";
EOF
        
        log "✅ APT代理配置完成"
    else
        warn "未找到APT配置目录，跳过APT代理配置"
    fi
}

test_proxy_connection() {
    log "测试代理连接..."
    
    echo "1. 测试环境变量..."
    echo "   http_proxy=$http_proxy"
    
    echo "2. 测试网络连通性..."
    if command -v curl &> /dev/null; then
        echo "通过代理获取公网IP:"
        curl -x socks5://$HK_SERVER:$SOCKS_PORT --connect-timeout 10 -s https://api.ip.sb/ip
        if [ $? -eq 0 ]; then
            log "✅ 代理测试成功"
        else
            warn "⚠️ 代理测试失败，但环境变量已设置"
        fi
    else
        warn "未找到curl，跳过网络测试"
        echo "安装curl: apt-get update && apt-get install -y curl"
    fi
}

show_usage() {
    cat << EOF

💡 代理使用说明:

1. 当前会话已启用代理
2. 支持的用法:
   - 命令行工具: curl, wget, git 等
   - 应用程序: 设置 http_proxy 环境变量

3. 测试命令:
   curl -x socks5://$HK_SERVER:$SOCKS_PORT https://www.google.com
   wget -e use_proxy=yes -e http_proxy=socks5://$HK_SERVER:$SOCKS_PORT https://www.example.com

4. 为单个命令设置代理:
   http_proxy=socks5://$HK_SERVER:$SOCKS_PORT curl https://api.ip.sb/ip

5. 取消代理:
   unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY SOCKS_SERVER

EOF
}

main() {
    echo -e "${GREEN}🚀 开始配置SOCKS5代理客户端...${NC}"
    echo "目标代理: socks5://$HK_SERVER:$SOCKS_PORT"
    echo ""
    
    # 检查输入参数
    if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
        echo "用法: $0 [香港服务器IP] [端口]"
        echo "示例: $0 192.168.1.100 1080"
        exit 0
    fi
    
    # 允许命令行参数覆盖配置
    if [ -n "$1" ]; then
        HK_SERVER="$1"
    fi
    if [ -n "$2" ]; then
        SOCKS_PORT="$2"
    fi
    
    if [ -z "$HK_SERVER" ] || [ "$HK_SERVER" = "香港服务器IP" ]; then
        error "请编辑脚本设置香港服务器IP，或使用: $0 <香港服务器IP> [端口]"
    fi
    
    validate_connection
    setup_temporary_proxy
    setup_permanent_proxy
    configure_apt_proxy
    test_proxy_connection
    show_usage
}

# 执行主函数
main "$@"