#!/usr/bin/env bash set -Eeuo pipefail REPO="${NG_REPO:-rogue/NexusGate}" BRANCH="${NG_BRANCH:-main}" # 自建 Gitea 分发基址(源码归档由此拼装) GIT_BASE="https://git.881218.xyz/${REPO}" DOMAIN="" EMAIL="" die() { printf '错误:%s\n' "$*" >&2; exit 1; } info() { printf '\033[1;36m[NexusGate]\033[0m %s\n' "$*"; } ask() { local prompt="$1" value="" if [[ -r /dev/tty ]]; then read -r -p "$prompt" value /dev/null || groupadd --system nexusgate id nexusgate >/dev/null 2>&1 || useradd --system --gid nexusgate --home-dir /var/lib/nexusgate --shell /usr/sbin/nologin nexusgate install -d -o root -g root -m 0755 /opt/nexusgate cp -a "${src_dir}/." /opt/nexusgate/ install -d -o nexusgate -g nexusgate -m 0700 "$DATA_DIR" fi if $IS_OPENWRT && [[ -z "$DOMAIN" ]]; then # 无域名:直接监听内网(OpenWrt 防火墙默认拦截 WAN 入站),纯 HTTP NG_HOST=0.0.0.0 NG_COOKIE_SECURE=false else NG_HOST=127.0.0.1 NG_COOKIE_SECURE=true fi admin_password="$(openssl rand -base64 24 | tr -d '\n')" session_secret="$(openssl rand -hex 32)" cat > /etc/nexusgate.env < /etc/caddy/Caddyfile.d/nexusgate.caddy <> /etc/caddy/Caddyfile.d/nexusgate.caddy; fi printf '}\n' >> /etc/caddy/Caddyfile.d/nexusgate.caddy if ! grep -qF 'import /etc/caddy/Caddyfile.d/*' /etc/caddy/Caddyfile; then printf '\nimport /etc/caddy/Caddyfile.d/*\n' >> /etc/caddy/Caddyfile fi caddy fmt --overwrite /etc/caddy/Caddyfile caddy fmt --overwrite /etc/caddy/Caddyfile.d/nexusgate.caddy caddy validate --config /etc/caddy/Caddyfile fi if $IS_OPENWRT; then /etc/init.d/nexusgate enable /etc/init.d/nexusgate start else systemctl daemon-reload systemctl enable --now nexusgate.service fi healthy=false for _ in {1..30}; do if curl -fsS http://127.0.0.1:8787/healthz >/dev/null 2>&1; then healthy=true; break; fi sleep 1 done if [[ "$healthy" != true ]]; then if $IS_OPENWRT; then logread 2>/dev/null | grep -a nexusgate | tail -n 100 || true else systemctl status nexusgate --no-pager || true journalctl -u nexusgate -n 100 --no-pager || true fi die "控制面启动失败,诊断信息已输出" fi if $IS_OPENWRT && [[ "$NG_HOST" == "0.0.0.0" ]] && ! netstat -tln 2>/dev/null | grep -q "0.0.0.0:8787"; then die "控制面未监听 0.0.0.0:8787(环境变量未正确传递),请检查 /etc/init.d/nexusgate" fi sed -i '/^NG_ADMIN_PASSWORD=/d' /etc/nexusgate.env if [[ -n "$DOMAIN" ]]; then if $IS_OPENWRT; then /etc/init.d/caddy enable /etc/init.d/caddy restart else systemctl enable --now caddy systemctl reload caddy fi fi printf '\n\033[1;32m安装完成\033[0m\n' if [[ -n "$DOMAIN" ]]; then printf '地址:https://%s\n' "$DOMAIN" else printf '地址:http://<路由器 LAN IP>:8787(内网访问)\n' fi printf '账号:admin\n密码:%s\n' "$admin_password" printf '管理命令:nexusgate 或 ng\n请立即登录并妥善保存密码。\n'