SOCKS5 Proxy Server

`install-socks5.sh` 脚本内容

以下是 SOCKS5 代理服务器的自动化安装脚本。您可以复制以下内容并在您的 Linux 服务器上执行,或者直接使用教程页面提供的推荐命令。

#!/bin/bash

# SOCKS5 Proxy Server Installation Script
# This script automates the installation, uninstallation, and management of the SOCKS5 proxy server.

# Configuration
SERVICE_NAME="socks5-proxy"
INSTALL_DIR="/usr/local/bin"
SOCKS5_BINARY_NAME="socks5"

# Public URL for binaries and this script
BASE_URL="https://pub-f49c78b9eb854878a5ee1d8e3c61aae8.r2.dev/socks5"

# Default credentials (can be overridden by command line arguments)
DEFAULT_USERNAME="mypix"
DEFAULT_PASSWORD="mypass550"
DEFAULT_PORT="58367"

# Function to generate a random filename for obfuscation
generate_random_filename() {
    head /dev/urandom | tr -dc a-z | head -c 4
}

# Function to detect OS and architecture
detect_os_arch() {
    OS=$(uname -s)
    ARCH=$(uname -m)

    case "$OS" in
        Linux)
            case "$ARCH" in
                x86_64) BINARY_ARCH="linux-amd64";; # 64-bit Linux
                i386|i686) BINARY_ARCH="linux-386";; # 32-bit Linux
                armv6l|armv7l) BINARY_ARCH="linux-arm";; # ARM Linux
                aarch64) BINARY_ARCH="linux-arm64";; # ARM64 Linux
                *) echo "Unsupported Linux architecture: $ARCH"; exit 1;;
            esac
            ;; # End Linux case
        Darwin)
            case "$ARCH" in
                x86_64) BINARY_ARCH="darwin-amd64";; # macOS Intel
                arm64) BINARY_ARCH="darwin-arm64";; # macOS Apple Silicon
                *) echo "Unsupported macOS architecture: $ARCH"; exit 1;;
            esac
            ;; # End Darwin case
        FreeBSD)
            case "$ARCH" in
                x86_64) BINARY_ARCH="freebsd-amd64";; # 64-bit FreeBSD
                *) echo "Unsupported FreeBSD architecture: $ARCH"; exit 1;;
            esac
            ;; # End FreeBSD case
        *)
            echo "Unsupported OS: $OS"; exit 1;;
    esac
}

# Function to get public IP address
get_public_ip() {
    curl -s checkip.amazonaws.com || curl -s ipinfo.io/ip || curl -s ifconfig.me
}

# Function to check if SOCKS5 service is running
is_service_installed() {
    systemctl is-active --quiet ${SERVICE_NAME} || systemctl is-enabled --quiet ${SERVICE_NAME}
}

# Function to install/reinstall SOCKS5 proxy
install_socks5() {
    echo "Detecting OS and architecture..."
    detect_os_arch
    echo "Detected: ${OS} - ${BINARY_ARCH}"

    DOWNLOAD_URL="${BASE_URL}/${SOCKS5_BINARY_NAME}-${BINARY_ARCH}"
    TEMP_BINARY="/tmp/${SOCKS5_BINARY_NAME}-${BINARY_ARCH}"
    
    # Handle Windows binaries (.exe)
    if [[ "${BINARY_ARCH}" == "windows-386" || "${BINARY_ARCH}" == "windows-amd64" ]]; then
        DOWNLOAD_URL="${DOWNLOAD_URL}.exe"
    }

    echo "Downloading SOCKS5 binary from ${DOWNLOAD_URL}..."
    if ! curl -sSL -o "${TEMP_BINARY}" "${DOWNLOAD_URL}"; then
        echo "Error: Failed to download SOCKS5 binary from ${DOWNLOAD_URL}"
        exit 1
    }

    chmod +x "${TEMP_BINARY}"

    # Generate a random filename for obfuscation (e.g., abcd.py)
    RANDOM_FILENAME="$(generate_random_filename).py"
    FINAL_BINARY_PATH="${INSTALL_DIR}/${RANDOM_FILENAME}"

    echo "Installing SOCKS5 binary to ${FINAL_BINARY_PATH}..."
    sudo mv "${TEMP_BINARY}" "${FINAL_BINARY_PATH}"

    # Create systemd service file
    SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
    
    # Get username and password from command line args or use defaults
    USERNAME=${1:-$DEFAULT_USERNAME}
    PASSWORD=${2:-$DEFAULT_PASSWORD}
    PORT=${3:-$DEFAULT_PORT}

    echo "Creating systemd service ${SERVICE_NAME}.service..."
    sudo bash -c "cat > ${SERVICE_FILE} < /dev/null; then
            echo "Proxy test: SUCCESS"
        else
            echo "Proxy test: FAILED (Check logs with 'sudo journalctl -u ${SERVICE_NAME}')"
        fi
    else
        echo "Error: SOCKS5 proxy server failed to start. Check logs with 'sudo journalctl -u ${SERVICE_NAME}'"
        exit 1
    fi
}

# Function to uninstall SOCKS5 proxy
uninstall_socks5() {
    echo "Stopping SOCKS5 proxy service..."
    sudo systemctl stop ${SERVICE_NAME}
    sudo systemctl disable ${SERVICE_NAME}
    echo "Removing SOCKS5 service file..."
    sudo rm -f "/etc/systemd/system/${SERVICE_NAME}.service"
    sudo systemctl daemon-reload
    echo "Removing SOCKS5 binary..."
    # Find the randomly named binary
    BINARY_TO_REMOVE=$(find ${INSTALL_DIR} -name "*.py" -type f -exec grep -l "SOCKS5 Proxy Server" {} + | head -n 1)
    if [ -n "$BINARY_TO_REMOVE" ]; then
        sudo rm -f "$BINARY_TO_REMOVE"
        echo "Removed binary: $BINARY_TO_REMOVE"
    else
        echo "Could not find SOCKS5 binary to remove in ${INSTALL_DIR}."
    }
    echo "SOCKS5 proxy server uninstalled."
}

# Function to check status
status_socks5() {
    systemctl status ${SERVICE_NAME}
}

# Main logic
case "$1" in
    install)
        install_socks5 "$2" "$3" "$4"
        ;;
    uninstall)
        uninstall_socks5
        ;;
    reinstall)
        uninstall_socks5
        install_socks5 "$2" "$3" "$4"
        ;;
    status)
        status_socks5
        ;;
    auto)
        if is_service_installed; then
            echo "SOCKS5 service detected. Reinstalling..."
            reinstall "$2" "$3" "$4"
        else
            echo "SOCKS5 service not detected. Installing..."
            install_socks5 "$2" "$3" "$4"
        }
        ;;
    *)
        echo "Usage: $0 {install|uninstall|reinstall|status|auto} [username] [password] [port]"
        echo "  install      - Install the SOCKS5 proxy server."
        echo "  uninstall    - Uninstall the SOCKS5 proxy server."
        echo "  reinstall    - Reinstall the SOCKS5 proxy server."
        echo "  status       - Show the status of the SOCKS5 proxy server."
        echo "  auto         - Auto-detects and installs/reinstalls the SOCKS5 proxy server."
        echo "  [username]   - Optional: Username for SOCKS5 authentication (default: ${DEFAULT_USERNAME})"
        echo "  [password]   - Optional: Password for SOCKS5 authentication (default: ${DEFAULT_PASSWORD})"
        echo "  [port]       - Optional: Port for SOCKS5 proxy (default: ${DEFAULT_PORT})"
        exit 1
        ;;
esac

exit 0