|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# GitHub repository for bridge releases |
| 5 | +REPO="vercel-eddie/bridge" |
| 6 | + |
| 7 | +# Install required tools |
| 8 | +install_dependencies() { |
| 9 | + if ! command -v curl &> /dev/null; then |
| 10 | + if command -v apt-get &> /dev/null; then |
| 11 | + apt-get update && apt-get install -y curl ca-certificates |
| 12 | + elif command -v apk &> /dev/null; then |
| 13 | + apk add --no-cache curl ca-certificates |
| 14 | + elif command -v yum &> /dev/null; then |
| 15 | + yum install -y curl ca-certificates |
| 16 | + fi |
| 17 | + fi |
| 18 | + |
| 19 | + # Install iptables if not present (needed for traffic interception) |
| 20 | + if ! command -v iptables &> /dev/null; then |
| 21 | + if command -v apt-get &> /dev/null; then |
| 22 | + apt-get update && apt-get install -y iptables |
| 23 | + elif command -v apk &> /dev/null; then |
| 24 | + apk add --no-cache iptables |
| 25 | + elif command -v yum &> /dev/null; then |
| 26 | + yum install -y iptables |
| 27 | + fi |
| 28 | + fi |
| 29 | +} |
| 30 | + |
| 31 | +# Detect architecture |
| 32 | +get_arch() { |
| 33 | + local arch=$(uname -m) |
| 34 | + case "$arch" in |
| 35 | + x86_64) echo "amd64" ;; |
| 36 | + aarch64|arm64) echo "arm64" ;; |
| 37 | + *) echo "Unsupported architecture: $arch" >&2; exit 1 ;; |
| 38 | + esac |
| 39 | +} |
| 40 | + |
| 41 | +# Get the latest release version from GitHub |
| 42 | +get_latest_version() { |
| 43 | + curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | \ |
| 44 | + grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' |
| 45 | +} |
| 46 | + |
| 47 | +# Download and install bridge binary |
| 48 | +install_bridge() { |
| 49 | + if [ -x /usr/local/bin/bridge ]; then |
| 50 | + echo "Bridge binary already installed, skipping download" |
| 51 | + return 0 |
| 52 | + fi |
| 53 | + |
| 54 | + local version="${BRIDGEVERSION:-edge}" |
| 55 | + |
| 56 | + # In dev mode the binary is expected to be provided via bind mount at runtime. |
| 57 | + if [ "$version" = "dev" ]; then |
| 58 | + echo "Dev mode: skipping binary download (expecting bind mount at runtime)" |
| 59 | + return 0 |
| 60 | + fi |
| 61 | + |
| 62 | + local arch=$(get_arch) |
| 63 | + local os="linux" |
| 64 | + |
| 65 | + # Resolve 'latest' to actual version |
| 66 | + if [ "$version" = "latest" ]; then |
| 67 | + version=$(get_latest_version) |
| 68 | + if [ -z "$version" ]; then |
| 69 | + echo "Failed to fetch latest version" >&2 |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | + fi |
| 73 | + |
| 74 | + echo "Installing bridge ${version} for ${os}-${arch}..." |
| 75 | + |
| 76 | + local binary_name="bridge-${os}-${arch}" |
| 77 | + local download_url="https://github.com/${REPO}/releases/download/${version}/${binary_name}" |
| 78 | + |
| 79 | + echo "Downloading from: ${download_url}" |
| 80 | + |
| 81 | + if ! curl -fsSL -o /usr/local/bin/bridge "${download_url}"; then |
| 82 | + echo "Failed to download bridge binary" >&2 |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | + |
| 86 | + chmod +x /usr/local/bin/bridge |
| 87 | + echo "Bridge ${version} installed successfully" |
| 88 | +} |
| 89 | + |
| 90 | +# Write environment configuration to /etc/profile.d (sourced by entrypoint) |
| 91 | +write_env_config() { |
| 92 | + cat > /etc/profile.d/bridge.sh << EOF |
| 93 | +export BRIDGE_SERVER_ADDR="${BRIDGESERVERADDR:-}" |
| 94 | +export APP_PORT="${APPPORT:-3000}" |
| 95 | +export FORWARD_DOMAINS="${FORWARDDOMAINS:-$FORWARD_DOMAINS}" |
| 96 | +EOF |
| 97 | +} |
| 98 | + |
| 99 | +# Create entrypoint script |
| 100 | +create_entrypoint() { |
| 101 | + # First part: bake the workspace path at install time (unquoted heredoc |
| 102 | + # so ${WORKSPACEPATH} is expanded now, not at runtime) |
| 103 | + cat > /usr/local/bin/bridge-entrypoint.sh << EOF |
| 104 | +#!/bin/bash |
| 105 | +# Baked at install time from feature option "workspacePath" |
| 106 | +BRIDGE_WORKSPACE_PATH="${WORKSPACEPATH:-}" |
| 107 | +EOF |
| 108 | + |
| 109 | + # Second part: runtime logic (quoted heredoc — no expansion at install time) |
| 110 | + cat >> /usr/local/bin/bridge-entrypoint.sh << 'RUNTIME' |
| 111 | +
|
| 112 | +# Source profile in case env vars aren't inherited |
| 113 | +[ -f /etc/profile.d/bridge.sh ] && source /etc/profile.d/bridge.sh |
| 114 | +
|
| 115 | +# Run bridge intercept as root (required for iptables) |
| 116 | +if [ -n "$BRIDGE_SERVER_ADDR" ]; then |
| 117 | + sudo BRIDGE_SERVER_ADDR="$BRIDGE_SERVER_ADDR" \ |
| 118 | + FORWARD_DOMAINS="$FORWARD_DOMAINS" \ |
| 119 | + KUBECONFIG="${KUBECONFIG:-}" \ |
| 120 | + /usr/local/bin/bridge intercept > /tmp/bridge-intercept.log 2>&1 & |
| 121 | +fi |
| 122 | +
|
| 123 | +exec "$@" |
| 124 | +RUNTIME |
| 125 | + chmod +x /usr/local/bin/bridge-entrypoint.sh |
| 126 | +} |
| 127 | + |
| 128 | +# Main installation |
| 129 | +main() { |
| 130 | + echo "Installing Bridge Tunnel Client..." |
| 131 | + |
| 132 | + install_dependencies |
| 133 | + install_bridge |
| 134 | + write_env_config |
| 135 | + create_entrypoint |
| 136 | + |
| 137 | + echo "Bridge Tunnel Client installation complete" |
| 138 | +} |
| 139 | + |
| 140 | +main |
0 commit comments