Launch
This commit is contained in:
135
install_bridge.sh
Normal file
135
install_bridge.sh
Normal file
@@ -0,0 +1,135 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
REPO_URL="${REPO_URL:-}"
|
||||
BRANCH="${BRANCH:-main}"
|
||||
PORT="${PORT:-6969}"
|
||||
OPEN_FIREWALL="${OPEN_FIREWALL:-yes}"
|
||||
INSTALL_DIR="/opt/dragoncore-bridge"
|
||||
CONFIG_DIR="/etc/dragoncore-bridge"
|
||||
BIN="/usr/local/bin/dragoncore-bridge"
|
||||
SERVICE="/etc/systemd/system/dragoncore-bridge.service"
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--repo) REPO_URL="$2"; shift 2 ;;
|
||||
--branch) BRANCH="$2"; shift 2 ;;
|
||||
--port) PORT="$2"; shift 2 ;;
|
||||
--open-firewall) OPEN_FIREWALL="$2"; shift 2 ;;
|
||||
*) echo "Unknown option: $1"; exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ "$(id -u)" != "0" ]; then
|
||||
echo "Run as root."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
need_cmd() { command -v "$1" >/dev/null 2>&1; }
|
||||
rand_hex() { openssl rand -hex "$1"; }
|
||||
|
||||
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
|
||||
if ! need_cmd go; then
|
||||
apt-get install -y golang-go
|
||||
fi
|
||||
elif need_cmd yum; then
|
||||
yum install -y ca-certificates curl wget git openssl gcc make golang
|
||||
elif need_cmd dnf; then
|
||||
dnf install -y ca-certificates curl wget git openssl gcc make golang
|
||||
fi
|
||||
|
||||
mkdir -p "$INSTALL_DIR" "$CONFIG_DIR"
|
||||
chmod 700 "$CONFIG_DIR"
|
||||
|
||||
if [ -n "$REPO_URL" ]; then
|
||||
rm -rf "$INSTALL_DIR/src"
|
||||
git clone --depth 1 --branch "$BRANCH" "$REPO_URL" "$INSTALL_DIR/src"
|
||||
else
|
||||
# Local install: useful if this script is executed from inside the repository.
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
rm -rf "$INSTALL_DIR/src"
|
||||
mkdir -p "$INSTALL_DIR/src"
|
||||
cp -a "$SCRIPT_DIR"/. "$INSTALL_DIR/src/"
|
||||
fi
|
||||
|
||||
cd "$INSTALL_DIR/src"
|
||||
go build -trimpath -ldflags "-s -w" -o "$BIN" .
|
||||
chmod 755 "$BIN"
|
||||
|
||||
USER_NAME="admin"
|
||||
PASSWORD="$(rand_hex 10)"
|
||||
TOKEN="$(rand_hex 24)"
|
||||
|
||||
if [ -f "$CONFIG_DIR/config.json" ]; then
|
||||
echo "Existing config found: $CONFIG_DIR/config.json"
|
||||
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)"
|
||||
fi
|
||||
|
||||
cat > "$CONFIG_DIR/config.json" <<EOF
|
||||
{
|
||||
"listen": ":$PORT",
|
||||
"username": "$USER_NAME",
|
||||
"password": "$PASSWORD",
|
||||
"token": "$TOKEN",
|
||||
"data_dir": "$CONFIG_DIR"
|
||||
}
|
||||
EOF
|
||||
chmod 600 "$CONFIG_DIR/config.json"
|
||||
|
||||
cat > "$SERVICE" <<EOF
|
||||
[Unit]
|
||||
Description=DragonCore Generic Bridge API
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=$BIN -config $CONFIG_DIR/config.json
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
User=root
|
||||
LimitNOFILE=1048576
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now dragoncore-bridge
|
||||
|
||||
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
|
||||
ufw allow "$PORT"/tcp || true
|
||||
fi
|
||||
if need_cmd firewall-cmd && firewall-cmd --state >/dev/null 2>&1; then
|
||||
firewall-cmd --permanent --add-port="$PORT/tcp" || true
|
||||
firewall-cmd --reload || true
|
||||
fi
|
||||
if need_cmd iptables; then
|
||||
iptables -C INPUT -p tcp --dport "$PORT" -j ACCEPT 2>/dev/null || iptables -I INPUT -p tcp --dport "$PORT" -j ACCEPT || true
|
||||
if need_cmd netfilter-persistent; then netfilter-persistent save || true; fi
|
||||
if need_cmd iptables-save && [ -d /etc/iptables ]; then iptables-save > /etc/iptables/rules.v4 || true; fi
|
||||
fi
|
||||
fi
|
||||
|
||||
PUBLIC_IP="$(curl -fsS --max-time 4 https://api.ipify.org 2>/dev/null || hostname -I | awk '{print $1}')"
|
||||
|
||||
echo
|
||||
printf '%s\n' '============================================================'
|
||||
printf '%s\n' 'DragonCore Bridge installed.'
|
||||
printf '%s\n' 'Use these values in the DragonCore Panel:'
|
||||
printf 'API Type : %s\n' 'Bridge Generic'
|
||||
printf 'IP : %s\n' "$PUBLIC_IP"
|
||||
printf 'API Port : %s\n' "$PORT"
|
||||
printf 'User : %s\n' "$USER_NAME"
|
||||
printf 'Password : %s\n' "$PASSWORD"
|
||||
printf 'Legacy token/Senha header: %s\n' "$TOKEN"
|
||||
printf '%s\n' 'Service commands:'
|
||||
printf '%s\n' ' systemctl status dragoncore-bridge'
|
||||
printf '%s\n' ' journalctl -u dragoncore-bridge -f'
|
||||
printf '%s\n' '============================================================'
|
||||
Reference in New Issue
Block a user