Fix test and online

This commit is contained in:
2026-05-29 10:22:24 -03:00
parent 096a8275be
commit 8c27a7f5d9
3 changed files with 382 additions and 32 deletions

View File

@@ -4,6 +4,7 @@ set -euo pipefail
REPO_URL="${REPO_URL:-}"
BRANCH="${BRANCH:-main}"
PORT="${PORT:-6969}"
PORT_SET=0
OPEN_FIREWALL="${OPEN_FIREWALL:-yes}"
INSTALL_DIR="/opt/dragoncore-bridge"
CONFIG_DIR="/etc/dragoncore-bridge"
@@ -14,7 +15,7 @@ while [ $# -gt 0 ]; do
case "$1" in
--repo) REPO_URL="$2"; shift 2 ;;
--branch) BRANCH="$2"; shift 2 ;;
--port) PORT="$2"; shift 2 ;;
--port) PORT="$2"; PORT_SET=1; shift 2 ;;
--open-firewall) OPEN_FIREWALL="$2"; shift 2 ;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
@@ -28,6 +29,51 @@ fi
need_cmd() { command -v "$1" >/dev/null 2>&1; }
rand_hex() { openssl rand -hex "$1"; }
extract_listen_port() {
cfg="$1"
[ -f "$cfg" ] || return 1
listen="$(grep -o '"listen"[[:space:]]*:[[:space:]]*"[^"]*"' "$cfg" | head -1 | cut -d '"' -f4 || true)"
port="${listen##*:}"
case "$port" in
''|*[!0-9]*) return 1 ;;
*) printf '%s\n' "$port" ;;
esac
}
port_in_use() {
p="$1"
if need_cmd ss; then
ss -ltn 2>/dev/null | awk '{print $4}' | grep -Eq "[:.]${p}$"
return $?
fi
if need_cmd netstat; then
netstat -ltn 2>/dev/null | awk '{print $4}' | grep -Eq "[:.]${p}$"
return $?
fi
if need_cmd lsof; then
lsof -iTCP:"$p" -sTCP:LISTEN -Pn >/dev/null 2>&1
return $?
fi
return 1
}
choose_free_port() {
base="$1"
case "$base" in ''|*[!0-9]*) base=6969 ;; esac
i=0
while [ "$i" -le 100 ]; do
cand=$((base + i))
if ! port_in_use "$cand"; then
printf '%s\n' "$cand"
return 0
fi
i=$((i + 1))
done
echo "No free TCP port found from $base to $((base + 100))." >&2
exit 1
}
# The bridge source is intentionally Go 1.13-compatible so old VPS images
# such as Ubuntu 20.04 can compile it with their distro package.
# If the distro Go is older than 1.13, install a known-good official Go.
@@ -78,14 +124,14 @@ install_official_go() {
export DEBIAN_FRONTEND=noninteractive
if need_cmd apt-get; then
apt-get update -y
apt-get install -y ca-certificates curl wget git openssl build-essential
apt-get install -y ca-certificates curl wget git openssl build-essential sqlite3 lsof iproute2
if ! need_cmd go; then
apt-get install -y golang-go || true
fi
elif need_cmd yum; then
yum install -y ca-certificates curl wget git openssl gcc make golang || yum install -y ca-certificates curl wget git openssl gcc make
yum install -y ca-certificates curl wget git openssl gcc make golang sqlite lsof iproute || yum install -y ca-certificates curl wget git openssl gcc make sqlite lsof iproute
elif need_cmd dnf; then
dnf install -y ca-certificates curl wget git openssl gcc make golang || dnf install -y ca-certificates curl wget git openssl gcc make
dnf install -y ca-certificates curl wget git openssl gcc make golang sqlite lsof iproute || dnf install -y ca-certificates curl wget git openssl gcc make sqlite lsof iproute
fi
if go_version_ok; then
@@ -114,6 +160,7 @@ else
fi
cd "$INSTALL_DIR/src"
if need_cmd systemctl; then systemctl stop dragoncore-bridge 2>/dev/null || true; fi
"$GO_BIN" build -trimpath -ldflags "-s -w" -o "$BIN" .
chmod 755 "$BIN"
@@ -126,6 +173,16 @@ if [ -f "$CONFIG_DIR/config.json" ]; then
USER_NAME="$(grep -o '"username"[[:space:]]*:[[:space:]]*"[^"]*"' "$CONFIG_DIR/config.json" | head -1 | cut -d '"' -f4 || echo admin)"
PASSWORD="$(grep -o '"password"[[:space:]]*:[[:space:]]*"[^"]*"' "$CONFIG_DIR/config.json" | head -1 | cut -d '"' -f4 || rand_hex 10)"
TOKEN="$(grep -o '"token"[[:space:]]*:[[:space:]]*"[^"]*"' "$CONFIG_DIR/config.json" | head -1 | cut -d '"' -f4 || rand_hex 24)"
EXISTING_PORT="$(extract_listen_port "$CONFIG_DIR/config.json" || true)"
if [ "$PORT_SET" = "0" ] && [ -n "$EXISTING_PORT" ]; then
PORT="$EXISTING_PORT"
fi
fi
REQUESTED_PORT="$PORT"
PORT="$(choose_free_port "$PORT")"
if [ "$PORT" != "$REQUESTED_PORT" ]; then
echo "Port $REQUESTED_PORT is already in use. Using free port $PORT instead."
fi
cat > "$CONFIG_DIR/config.json" <<EOF
@@ -159,6 +216,12 @@ EOF
systemctl daemon-reload
systemctl enable --now dragoncore-bridge
sleep 1
if ! systemctl is-active --quiet dragoncore-bridge; then
echo "dragoncore-bridge failed to start. Last logs:" >&2
journalctl -u dragoncore-bridge -n 40 --no-pager >&2 || true
exit 1
fi
if [ "$OPEN_FIREWALL" = "yes" ] || [ "$OPEN_FIREWALL" = "true" ] || [ "$OPEN_FIREWALL" = "1" ]; then
if need_cmd ufw && ufw status 2>/dev/null | grep -qi "Status: active"; then