feat(auth): Implement Go HTTP auth server for max performance

This commit is contained in:
Whispering Wind
2025-08-24 20:34:33 +03:30
committed by GitHub
parent 3ca2e6649e
commit dbff546523
4 changed files with 72 additions and 10 deletions

View File

@ -5,6 +5,30 @@ source /etc/hysteria/core/scripts/utils.sh
source /etc/hysteria/core/scripts/scheduler.sh source /etc/hysteria/core/scripts/scheduler.sh
define_colors define_colors
compile_auth_binary() {
echo "Compiling authentication binary..."
local auth_dir="/etc/hysteria/core/scripts/auth"
if [ -f "$auth_dir/user_auth.go" ]; then
(
cd "$auth_dir" || exit 1
go mod init hysteria-auth >/dev/null 2>&1
go mod tidy >/dev/null 2>&1
if go build -o user_auth .; then
chown hysteria:hysteria user_auth
chmod +x user_auth
echo "Authentication binary compiled successfully."
else
echo -e "${red}Error:${NC} Failed to compile the authentication binary."
exit 1
fi
)
else
echo -e "${red}Error:${NC} Go source file not found at $auth_dir/user_auth.go"
exit 1
fi
}
install_hysteria() { install_hysteria() {
local port=$1 local port=$1
@ -13,6 +37,8 @@ install_hysteria() {
mkdir -p /etc/hysteria && cd /etc/hysteria/ mkdir -p /etc/hysteria && cd /etc/hysteria/
compile_auth_binary
echo "Generating CA key and certificate..." echo "Generating CA key and certificate..."
openssl ecparam -genkey -name prime256v1 -out ca.key >/dev/null 2>&1 openssl ecparam -genkey -name prime256v1 -out ca.key >/dev/null 2>&1
openssl req -new -x509 -days 36500 -key ca.key -out ca.crt -subj "/CN=$sni" >/dev/null 2>&1 openssl req -new -x509 -days 36500 -key ca.key -out ca.crt -subj "/CN=$sni" >/dev/null 2>&1

View File

@ -39,20 +39,20 @@ check_scheduler_service() {
} }
setup_hysteria_auth_server() { setup_hysteria_auth_server() {
chmod +x /etc/hysteria/core/scripts/hysteria2/auth_server.py chmod +x /etc/hysteria/core/scripts/auth/user_auth
cat > /etc/systemd/system/hysteria-auth.service << 'EOF' cat > /etc/systemd/system/hysteria-auth.service << 'EOF'
[Unit] [Unit]
Description=Hysteria aiohttp Auth Server Description=Hysteria Auth Server
After=network.target After=network.target
[Service] [Service]
Type=simple Type=simple
User=root User=hysteria
WorkingDirectory=/etc/hysteria Group=hysteria
ExecStart=/etc/hysteria/hysteria2_venv/bin/python3 /etc/hysteria/core/scripts/hysteria2/auth_server.py ExecStart=/etc/hysteria/core/auth-server/auth_server
Restart=always Restart=always
RestartSec=10 RestartSec=5
StandardOutput=journal StandardOutput=journal
StandardError=journal StandardError=journal
SyslogIdentifier=hysteria-Auth SyslogIdentifier=hysteria-Auth

View File

@ -76,7 +76,7 @@ check_os_version() {
} }
install_packages() { install_packages() {
local REQUIRED_PACKAGES=("jq" "curl" "pwgen" "python3" "python3-pip" "python3-venv" "git" "bc" "zip" "cron" "lsof") local REQUIRED_PACKAGES=("jq" "curl" "pwgen" "python3" "python3-pip" "python3-venv" "git" "bc" "zip" "cron" "lsof" "golang-go")
local MISSING_PACKAGES=() local MISSING_PACKAGES=()
log_info "Checking required packages..." log_info "Checking required packages..."

View File

@ -6,6 +6,7 @@ trap 'echo -e "\n❌ An error occurred. Aborting."; exit 1' ERR
# ========== Variables ========== # ========== Variables ==========
HYSTERIA_INSTALL_DIR="/etc/hysteria" HYSTERIA_INSTALL_DIR="/etc/hysteria"
HYSTERIA_VENV_DIR="$HYSTERIA_INSTALL_DIR/hysteria2_venv" HYSTERIA_VENV_DIR="$HYSTERIA_INSTALL_DIR/hysteria2_venv"
AUTH_BINARY_DIR="$HYSTERIA_INSTALL_DIR/core/scripts/auth"
REPO_URL="https://github.com/ReturnFI/Blitz" REPO_URL="https://github.com/ReturnFI/Blitz"
REPO_BRANCH="auth" REPO_BRANCH="auth"
GEOSITE_URL="https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geosite.dat" GEOSITE_URL="https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geosite.dat"
@ -23,6 +24,38 @@ success() { echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] [OK] - ${RESET} $1";
warn() { echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] [WARN] - ${RESET} $1"; } warn() { echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] [WARN] - ${RESET} $1"; }
error() { echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] [ERROR] - ${RESET} $1"; } error() { echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] [ERROR] - ${RESET} $1"; }
# ========== New Function to Install Go and Compile Auth Binary ==========
install_go_and_compile_auth() {
info "Checking for Go and compiling authentication binary..."
if ! command -v go &>/dev/null; then
warn "Go is not installed. Attempting to install..."
apt-get update -y >/dev/null
apt-get install -y golang-go >/dev/null
success "Go installed successfully."
else
success "Go is already installed."
fi
if [[ -f "$AUTH_BINARY_DIR/user_auth.go" ]]; then
info "Found auth binary source. Compiling..."
(
cd "$AUTH_BINARY_DIR"
go mod init hysteria_auth >/dev/null 2>&1
go mod tidy >/dev/null 2>&1
if go build -o user_auth .; then
chown hysteria:hysteria user_auth
chmod +x user_auth
success "Authentication binary compiled successfully."
else
error "Failed to compile the authentication binary."
exit 1
fi
)
else
warn "Authentication binary source not found. Skipping compilation."
fi
}
# ========== Backup Files ========== # ========== Backup Files ==========
cd /root cd /root
TEMP_DIR=$(mktemp -d) TEMP_DIR=$(mktemp -d)
@ -81,7 +114,7 @@ info "Updating Hysteria configuration for HTTP authentication..."
auth_block='{"type": "http", "http": {"url": "http://127.0.0.1:28262/auth", "timeout": "5s"}}' auth_block='{"type": "http", "http": {"url": "http://127.0.0.1:28262/auth", "timeout": "5s"}}'
if [[ -f "$HYSTERIA_INSTALL_DIR/config.json" ]]; then if [[ -f "$HYSTERIA_INSTALL_DIR/config.json" ]]; then
jq --argjson auth_block "$auth_block" '.auth = $auth_block' "$HYSTERIA_INSTALL_DIR/config.json" > "$HYSTERIA_INSTALL_DIR/config.json.tmp" && mv "$HYSTERIA_INSTALL_DIR/config.json.tmp" "$HYSTERIA_INSTALL_DIR/config.json" jq --argjson auth_block "$auth_block" '.auth = $auth_block' "$HYSTERIA_INSTALL_DIR/config.json" > "$HYSTERIA_INSTALL_DIR/config.json.tmp" && mv "$HYSTERIA_INSTALL_DIR/config.json.tmp" "$HYSTERIA_INSTALL_DIR/config.json"
success "config.json updated to use aiohttp auth server." success "config.json updated to use auth server."
else else
warn "config.json not found after restore. Skipping auth update." warn "config.json not found after restore. Skipping auth update."
fi fi
@ -103,6 +136,9 @@ pip install --upgrade pip >/dev/null
pip install -r requirements.txt >/dev/null pip install -r requirements.txt >/dev/null
success "Python environment ready." success "Python environment ready."
# ========== Compile Go Binary ==========
install_go_and_compile_auth
# ========== Systemd Services ========== # ========== Systemd Services ==========
info "Ensuring systemd services are configured..." info "Ensuring systemd services are configured..."
if source "$HYSTERIA_INSTALL_DIR/core/scripts/scheduler.sh"; then if source "$HYSTERIA_INSTALL_DIR/core/scripts/scheduler.sh"; then