#!/usr/bin/env bash # ============================================================================== # Hackergate (hg) - Unified Edge WAF & Control Plane Installer # Progressive Innovation LAB, 2026 Hungary # # Usage: # curl -fsSL https://hackergate.hu/install.sh | bash # curl -fsSL https://hackergate.hu/install.sh | bash -s -- --bin-dir=/usr/local/bin # ============================================================================== set -euo pipefail # Visual formatting BOLD="$(tput bold 2>/dev/null || echo '')" RESET="$(tput sgr0 2>/dev/null || echo '')" GREEN="$(tput setaf 2 2>/dev/null || echo '')" CYAN="$(tput setaf 6 2>/dev/null || echo '')" RED="$(tput setaf 1 2>/dev/null || echo '')" YELLOW="$(tput setaf 3 2>/dev/null || echo '')" BLUE="$(tput setaf 4 2>/dev/null || echo '')" BANNER() { cat << "EOF" _ _ _ _____ _ | | | | | | / ____| | | | |__| | __ _ ___| | _____ _ __ __ _ | | __ __ _| |_ ___ | __ |/ _` |/ __| |/ / _ \ '__/ _` || | |_ |/ _` | __/ _ \ | | | | (_| | (__| < __/ | | (_| || |__| | (_| | || __/ |_| |_|\__,_|\___|_|\_\___|_| \__, (_)_____|\__,_|\__\___| __/ | |___/ Next-Era Edge WAF, API Gateway & Threat Gossip Mesh EOF } info() { echo -e "${CYAN}[*]${RESET} ${BOLD}$1${RESET}" } success() { echo -e "${GREEN}[✓]${RESET} ${BOLD}$1${RESET}" } warn() { echo -e "${YELLOW}[!]${RESET} ${BOLD}$1${RESET}" } error() { echo -e "${RED}[✗]${RESET} ${BOLD}$1${RESET}" >&2 } fatal() { error "$1" exit 1 } # Defaults BIN_DIR="${BIN_DIR:-/usr/local/bin}" CONFIG_DIR="${CONFIG_DIR:-/etc/hackergate}" DATA_DIR="${DATA_DIR:-/var/lib/hackergate}" LOG_DIR="${LOG_DIR:-/var/log/hackergate}" RUN_DIR="${RUN_DIR:-/run/hackergate}" INSTALL_SYSTEMD="${INSTALL_SYSTEMD:-true}" DRY_RUN="${DRY_RUN:-false}" SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." 2>/dev/null && pwd || echo "")" # Parse CLI arguments while [[ $# -gt 0 ]]; do case "$1" in --bin-dir=*) BIN_DIR="${1#*=}" shift ;; --config-dir=*) CONFIG_DIR="${1#*=}" shift ;; --no-systemd) INSTALL_SYSTEMD="false" shift ;; --dry-run) DRY_RUN="true" shift ;; --help|-h) cat << EOF Hackergate Installer Options: --bin-dir= Binary destination (default: /usr/local/bin) --config-dir= Config root destination (default: /etc/hackergate) --no-systemd Skip systemd service registration --dry-run Print installation plan without modifications -h, --help Show this help message EOF exit 0 ;; *) warn "Unknown argument ignored: $1" shift ;; esac done BANNER echo "" info "Initializing Hackergate installation..." # OS & Architecture Detection OS="$(uname -s | tr '[:upper:]' '[:lower:]')" ARCH="$(uname -m)" case "$ARCH" in x86_64|amd64) TARGET_ARCH="amd64" ;; arm64|aarch64) TARGET_ARCH="arm64" ;; *) fatal "Unsupported hardware architecture: $ARCH" ;; esac case "$OS" in linux) TARGET_OS="linux" ;; darwin) TARGET_OS="darwin" INSTALL_SYSTEMD="false" ;; *) fatal "Unsupported operating system: $OS" ;; esac success "Detected platform: ${BOLD}${TARGET_OS}/${TARGET_ARCH}${RESET}" # Elevation check SUDO="" if [[ $EUID -ne 0 ]]; then if command -v sudo >/dev/null 2>&1; then SUDO="sudo" info "Running with elevated privileges via sudo" else warn "Running as non-root user without sudo. Custom local paths recommended." if [[ "$BIN_DIR" == "/usr/local/bin" ]]; then BIN_DIR="$HOME/.local/bin" CONFIG_DIR="$HOME/.config/hackergate" DATA_DIR="$HOME/.local/share/hackergate" LOG_DIR="$HOME/.local/log/hackergate" RUN_DIR="$HOME/.local/run/hackergate" INSTALL_SYSTEMD="false" fi fi fi info "Installation targets:" echo " • Binaries: ${BIN_DIR}" echo " • Configurations: ${CONFIG_DIR}" echo " • State & DB: ${DATA_DIR}" echo " • Runtime logs: ${LOG_DIR}" echo " • Sockets & PID: ${RUN_DIR}" echo "" if [[ "$DRY_RUN" == "true" ]]; then success "Dry run complete. Exiting without changes." exit 0 fi # Directory Creation info "Creating directory structure..." $SUDO mkdir -p "${BIN_DIR}" $SUDO mkdir -p "${CONFIG_DIR}/vhosts.d" $SUDO mkdir -p "${CONFIG_DIR}/vhosts" $SUDO mkdir -p "${CONFIG_DIR}/certs" $SUDO mkdir -p "${DATA_DIR}" $SUDO mkdir -p "${LOG_DIR}/vhosts" $SUDO mkdir -p "${RUN_DIR}" BINARIES=("hg" "hggateway" "hg-controlplane" "hgctl" "firewalld" "simulator") # Binary Discovery, Package Download, or Compilation info "Locating Hackergate binaries..." INSTALLED_COUNT=0 REPO_BASE_URL="${REPO_BASE_URL:-https://repo.pilab.hu}" # 1. Package Download from repo.pilab.hu if [[ "$TARGET_OS" == "linux" ]]; then # Debian/Ubuntu (.deb) if [[ -f /etc/debian_version ]] || command -v dpkg >/dev/null 2>&1; then DEB_URL="${REPO_BASE_URL}/apt/pool/main/h/hackergate/hackergate_latest_${TARGET_ARCH}.deb" info "Attempting deb package install from: ${DEB_URL}" TMP_DEB="/tmp/hackergate_latest_${TARGET_ARCH}.deb" if curl -fsSL -o "$TMP_DEB" "$DEB_URL" 2>/dev/null; then if $SUDO dpkg -i "$TMP_DEB" 2>/dev/null || $SUDO apt-get install -f -y 2>/dev/null; then success "Installed Hackergate via Debian/Ubuntu package" INSTALLED_COUNT=${#BINARIES[@]} fi rm -f "$TMP_DEB" fi # RHEL / Fedora / CentOS / Rocky (.rpm) elif [[ -f /etc/redhat-release ]] || command -v rpm >/dev/null 2>&1; then RPM_URL="${REPO_BASE_URL}/rpm/Packages/hackergate-latest.${TARGET_ARCH}.rpm" info "Attempting RPM package install from: ${RPM_URL}" TMP_RPM="/tmp/hackergate_latest.${TARGET_ARCH}.rpm" if curl -fsSL -o "$TMP_RPM" "$RPM_URL" 2>/dev/null; then if $SUDO rpm -Uvh "$TMP_RPM" 2>/dev/null || $SUDO dnf install -y "$TMP_RPM" 2>/dev/null; then success "Installed Hackergate via RPM package" INSTALLED_COUNT=${#BINARIES[@]} fi rm -f "$TMP_RPM" fi fi fi # 2. Universal Tarball Fallback from repo.pilab.hu if [[ $INSTALLED_COUNT -lt ${#BINARIES[@]} ]]; then TAR_URL="${REPO_BASE_URL}/releases/hackergate_latest_${TARGET_OS}_${TARGET_ARCH}.tar.gz" info "Attempting release archive download from: ${TAR_URL}" TMP_TAR="/tmp/hackergate_latest_${TARGET_OS}_${TARGET_ARCH}.tar.gz" if curl -fsSL -o "$TMP_TAR" "$TAR_URL" 2>/dev/null; then if $SUDO tar -xzf "$TMP_TAR" -C "${BIN_DIR}" 2>/dev/null; then success "Extracted release archive into ${BIN_DIR}" for bin in "${BINARIES[@]}"; do if [[ -f "${BIN_DIR}/${bin}" ]]; then $SUDO chmod 755 "${BIN_DIR}/${bin}" ((INSTALLED_COUNT++)) fi done fi rm -f "$TMP_TAR" fi fi # 3. Local Workspace Fallback: compiled binaries if [[ $INSTALLED_COUNT -lt ${#BINARIES[@]} && -n "$SOURCE_DIR" && -d "$SOURCE_DIR/bin" ]]; then info "Found compiled binaries in workspace: $SOURCE_DIR/bin" for bin in "${BINARIES[@]}"; do if [[ -f "$SOURCE_DIR/bin/$bin" ]]; then $SUDO cp -f "$SOURCE_DIR/bin/$bin" "${BIN_DIR}/${bin}" $SUDO chmod 755 "${BIN_DIR}/${bin}" success "Installed ${bin} -> ${BIN_DIR}/${bin}" ((INSTALLED_COUNT++)) fi done fi # 4. Fallback: Compile from Go source if in workspace if [[ $INSTALLED_COUNT -lt ${#BINARIES[@]} && -n "$SOURCE_DIR" && -f "$SOURCE_DIR/go.mod" ]] && command -v go >/dev/null 2>&1; then info "Building binaries from source via Go..." ( cd "$SOURCE_DIR" CGO_ENABLED=0 go build -o "${BIN_DIR}/hg" ./apps/hg/ CGO_ENABLED=0 go build -o "${BIN_DIR}/hggateway" ./apps/gateway/ CGO_ENABLED=0 go build -o "${BIN_DIR}/hg-controlplane" ./apps/hg-controlplane/ CGO_ENABLED=0 go build -o "${BIN_DIR}/hgctl" ./apps/hgctl/ CGO_ENABLED=0 go build -o "${BIN_DIR}/firewalld" ./apps/firewalld/ CGO_ENABLED=0 go build -o "${BIN_DIR}/simulator" ./apps/simulator/ ) $SUDO chmod 755 "${BIN_DIR}/hg" "${BIN_DIR}/hggateway" "${BIN_DIR}/hg-controlplane" "${BIN_DIR}/hgctl" "${BIN_DIR}/firewalld" "${BIN_DIR}/simulator" success "All core binaries compiled and installed." INSTALLED_COUNT=${#BINARIES[@]} fi # 5. Fallback: symlinks if binaries exist elsewhere if [[ $INSTALLED_COUNT -eq 0 ]]; then warn "Binaries not found locally. Searching current PATH..." for bin in "${BINARIES[@]}"; do if command -v "$bin" >/dev/null 2>&1; then success "Found existing ${bin} in $(command -v "$bin")" ((INSTALLED_COUNT++)) fi done fi # Configuration Generation info "Configuring default parameters in ${CONFIG_DIR}..." if [[ ! -f "${CONFIG_DIR}/hackergate.toml" ]]; then $SUDO tee "${CONFIG_DIR}/hackergate.toml" > /dev/null << 'EOF' [general] socket_dir = "/run/hackergate" data_dir = "/var/lib/hackergate" log_dir = "/var/log/hackergate" grpc_address = "localhost:50051" log_level = "info" [service] grpc_listen_address = "localhost:50051" http_listen_address = "localhost:8080" database_path = "/var/lib/hackergate/hackergate.db" vhosts_include_dirs = ["vhosts"] log_level = "info" [service.nats] store_path = "/var/lib/hackergate/nats" port = 4222 embedded = true [reputation] block_threshold = 10 suspicious_threshold = 50 EOF success "Created ${CONFIG_DIR}/hackergate.toml" else info "Preserved existing ${CONFIG_DIR}/hackergate.toml" fi if [[ ! -f "${CONFIG_DIR}/gateway.toml" ]]; then $SUDO tee "${CONFIG_DIR}/gateway.toml" > /dev/null << 'EOF' [gateway] listen = ":8443" listen_http = ":80" http_enabled = true protected_by = "hackergate-enterprise" [logging] level = "info" [metrics] enabled = true listen = ":9091" [grpc] address = "localhost:50051" [coraza] enabled = true block_status = 403 paranoia_level = 2 rule_set = "builtin" [bot] enabled = true challenge_threshold = 30.0 block_threshold = 15.0 block_scanners = true block_impersonators = true allow_verified_crawlers = true [challenge] enabled = true difficulty = 16 require_browser_integrity = true [bola] enabled = true action = "block" detect_sequential_enumeration = true [llm_shield] enabled = true action = "block" inspect_paths = ["/v1/chat/completions", "/api/chat", "/chat"] strip_delimiter_escapes = true [gossip] enabled = true secret = "hackergate-mesh-threat-secret-change-me" peers = [] [vhosts] enabled = true default_upstream = "http://localhost:8080" include_dirs = ["/etc/hackergate/vhosts.d", "/etc/hackergate/vhosts"] EOF success "Created ${CONFIG_DIR}/gateway.toml" else info "Preserved existing ${CONFIG_DIR}/gateway.toml" fi if [[ ! -f "${CONFIG_DIR}/vhosts.d/default.yaml" ]]; then $SUDO tee "${CONFIG_DIR}/vhosts.d/default.yaml" > /dev/null << 'EOF' domain: localhost upstream: http://127.0.0.1:8080 EOF success "Created default vhost: ${CONFIG_DIR}/vhosts.d/default.yaml" fi if [[ ! -f "${CONFIG_DIR}/vhosts/default.yaml" ]]; then $SUDO cp -f "${CONFIG_DIR}/vhosts.d/default.yaml" "${CONFIG_DIR}/vhosts/default.yaml" 2>/dev/null || true fi # Service Integration (Linux systemd & macOS launchd) if [[ "$TARGET_OS" == "linux" && "$INSTALL_SYSTEMD" == "true" && -d "/etc/systemd/system" ]]; then info "Installing systemd services..." # 1. All-in-One Free Tier Service $SUDO tee /etc/systemd/system/hackergate.service > /dev/null << EOF [Unit] Description=Hackergate Unified Free Tier Edge WAF & Threat Mesh After=network.target Documentation=https://hackergate.hu [Service] Type=simple ExecStart=${BIN_DIR}/hg run --config ${CONFIG_DIR}/gateway.toml Restart=always RestartSec=3 LimitNOFILE=1048576 [Install] WantedBy=multi-user.target EOF # 2. Standalone Control Plane Service $SUDO tee /etc/systemd/system/hg-controlplane.service > /dev/null << EOF [Unit] Description=Hackergate Central Control Plane After=network.target Documentation=https://hackergate.hu [Service] Type=simple ExecStart=${BIN_DIR}/hg-controlplane -config ${CONFIG_DIR}/hackergate.toml Restart=always RestartSec=3 LimitNOFILE=65536 [Install] WantedBy=multi-user.target EOF # 3. Standalone Edge Gateway Service $SUDO tee /etc/systemd/system/hggateway.service > /dev/null << EOF [Unit] Description=Hackergate Edge WAF & Reverse Proxy Gateway After=network.target hg-controlplane.service Wants=hg-controlplane.service Documentation=https://hackergate.hu [Service] Type=simple ExecStart=${BIN_DIR}/hggateway -c ${CONFIG_DIR}/gateway.toml Restart=always RestartSec=3 LimitNOFILE=1048576 [Install] WantedBy=multi-user.target EOF $SUDO systemctl daemon-reload success "Systemd services installed: hackergate.service, hg-controlplane.service, hggateway.service" elif [[ "$TARGET_OS" == "darwin" ]]; then info "Configuring macOS launchd service..." PLIST_DIR="/Library/LaunchDaemons" if [[ $EUID -ne 0 ]]; then PLIST_DIR="$HOME/Library/LaunchAgents" mkdir -p "$PLIST_DIR" fi PLIST_PATH="${PLIST_DIR}/hu.hackergate.plist" $SUDO tee "$PLIST_PATH" > /dev/null << EOF Label hu.hackergate ProgramArguments ${BIN_DIR}/hg run --config ${CONFIG_DIR}/gateway.toml RunAtLoad KeepAlive StandardErrorPath ${LOG_DIR}/hackergate-error.log StandardOutPath ${LOG_DIR}/hackergate.log EOF success "macOS launchd plist registered: ${PLIST_PATH}" fi echo "" echo "========================================================================" success "${BOLD}Hackergate successfully installed!${RESET}" echo "========================================================================" echo "" echo "Quick Start Commands:" if [[ "$TARGET_OS" == "linux" && "$INSTALL_SYSTEMD" == "true" ]]; then echo " 1. Start All-in-One Service (Free Tier):" echo " ${CYAN}sudo systemctl enable --now hackergate${RESET}" echo " 2. Or Start Microservices Architecture:" echo " ${CYAN}sudo systemctl enable --now hg-controlplane hggateway${RESET}" echo " 3. Query Real-Time Status & Threat Telemetry:" echo " ${CYAN}hg status --detailed${RESET}" elif [[ "$TARGET_OS" == "darwin" ]]; then echo " 1. Start All-in-One Daemon in Background:" echo " ${CYAN}${BIN_DIR}/hg run &${RESET}" echo " 2. Or Load macOS Background Service:" echo " ${CYAN}launchctl load ${PLIST_PATH}${RESET}" echo " 3. Query Real-Time Status & Threat Telemetry:" echo " ${CYAN}hg status --detailed${RESET}" else echo " 1. Start All-in-One Engine:" echo " ${CYAN}${BIN_DIR}/hg run &${RESET}" echo " 2. Query System Status:" echo " ${CYAN}hg status --detailed${RESET}" fi echo " 4. List & Inspect Virtual Hosts:" echo " ${CYAN}hg vhosts list${RESET}" echo " 5. Pre-flight Validate VHost Config:" echo " ${CYAN}hg test-config ${CONFIG_DIR}/vhosts.d/default.yaml${RESET}" echo " 6. Inspect Threat Gossip Mesh Peers:" echo " ${CYAN}hg mesh peers${RESET}" echo "" echo "Documentation & Guides: https://hackergate.hu" echo "========================================================================"