|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Per-machine client certs → $XDG_DATA_HOME/.../agent-gateway; then sidecar + claude. Dev: --gateway-ca certs/server-ca.pem |
| 3 | + |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +LISTEN="127.0.0.1:3128" |
| 7 | +EXTENSION_VALUE="agent-alpha" |
| 8 | +SIDECAR_BIN="" |
| 9 | +REGENERATE_CERTS=false |
| 10 | +GATEWAY="" |
| 11 | +GATEWAY_CA="" |
| 12 | + |
| 13 | +usage() { |
| 14 | + cat <<'EOF' |
| 15 | +Usage: connect.sh [OPTIONS] |
| 16 | +
|
| 17 | +Required: |
| 18 | + --gateway HOST:PORT Gateway address |
| 19 | + --gateway-ca PATH CA that issued tls_cert_path (dev: certs/server-ca.pem) |
| 20 | +
|
| 21 | +Optional: |
| 22 | + --extension-value VALUE (default: agent-alpha) |
| 23 | + --listen ADDR:PORT |
| 24 | + --sidecar-bin PATH |
| 25 | + --regenerate-certs |
| 26 | + -h, --help |
| 27 | +EOF |
| 28 | + exit "${1:-0}" |
| 29 | +} |
| 30 | + |
| 31 | +need_arg() { [[ $# -ge 2 ]] || { echo "error: $1 requires a value" >&2; exit 1; }; } |
| 32 | + |
| 33 | +while [[ $# -gt 0 ]]; do |
| 34 | + case "$1" in |
| 35 | + --gateway) need_arg "$@"; GATEWAY="$2"; shift 2 ;; |
| 36 | + --gateway-ca) need_arg "$@"; GATEWAY_CA="$2"; shift 2 ;; |
| 37 | + --extension-value) need_arg "$@"; EXTENSION_VALUE="$2"; shift 2 ;; |
| 38 | + --listen) need_arg "$@"; LISTEN="$2"; shift 2 ;; |
| 39 | + --sidecar-bin) need_arg "$@"; SIDECAR_BIN="$2"; shift 2 ;; |
| 40 | + --regenerate-certs) REGENERATE_CERTS=true; shift ;; |
| 41 | + -h|--help) usage 0 ;; |
| 42 | + *) echo "Unknown option: $1" >&2; usage 1 ;; |
| 43 | + esac |
| 44 | +done |
| 45 | + |
| 46 | +[[ -n "$GATEWAY" ]] || { echo "error: --gateway is required" >&2; usage 1; } |
| 47 | +[[ -n "$GATEWAY_CA" ]] || { echo "error: --gateway-ca is required" >&2; usage 1; } |
| 48 | +[[ -f "$GATEWAY_CA" ]] || { echo "error: gateway CA file not found: $GATEWAY_CA" >&2; exit 1; } |
| 49 | + |
| 50 | +CERT_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/agent-gateway" |
| 51 | +mkdir -p "$CERT_DIR" |
| 52 | + |
| 53 | +der_utf8string() { |
| 54 | + local val="$1" |
| 55 | + local len=${#val} |
| 56 | + local result |
| 57 | + result=$(printf '0c:%02x' "$len") |
| 58 | + for (( i=0; i<len; i++ )); do |
| 59 | + result=$(printf '%s:%02x' "$result" "'${val:$i:1}") |
| 60 | + done |
| 61 | + printf '%s' "$result" |
| 62 | +} |
| 63 | + |
| 64 | +generate_certs() { |
| 65 | + echo "==> Generating per-machine client CA" |
| 66 | + openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \ |
| 67 | + -keyout "$CERT_DIR/machine-client-ca-key.pem" -out "$CERT_DIR/machine-client-ca.pem" \ |
| 68 | + -days 365 -nodes -subj "/CN=agent-gateway machine client CA" 2>/dev/null |
| 69 | + |
| 70 | + echo "==> Generating machine client certificate (extension_value=$EXTENSION_VALUE)" |
| 71 | + local der_hex |
| 72 | + der_hex=$(der_utf8string "$EXTENSION_VALUE") |
| 73 | + openssl req -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \ |
| 74 | + -keyout "$CERT_DIR/machine-client-key.pem" -out "$CERT_DIR/machine-client.csr" \ |
| 75 | + -nodes -subj "/CN=machine-client" 2>/dev/null |
| 76 | + openssl x509 -req -in "$CERT_DIR/machine-client.csr" \ |
| 77 | + -CA "$CERT_DIR/machine-client-ca.pem" -CAkey "$CERT_DIR/machine-client-ca-key.pem" -CAcreateserial \ |
| 78 | + -out "$CERT_DIR/machine-client.pem" -days 365 \ |
| 79 | + -extfile <(printf '1.3.6.1.4.1.57264.1.1=DER:%s' "$der_hex") 2>/dev/null |
| 80 | + rm -f "$CERT_DIR/machine-client.csr" "$CERT_DIR/machine-client-ca.srl" |
| 81 | +} |
| 82 | + |
| 83 | +needs_certs() { |
| 84 | + [[ "$REGENERATE_CERTS" == "true" ]] && return 0 |
| 85 | + for f in machine-client-ca.pem machine-client-ca-key.pem machine-client.pem machine-client-key.pem; do |
| 86 | + [[ -f "$CERT_DIR/$f" ]] || return 0 |
| 87 | + done |
| 88 | + return 1 |
| 89 | +} |
| 90 | + |
| 91 | +if needs_certs; then |
| 92 | + generate_certs |
| 93 | +fi |
| 94 | + |
| 95 | +echo "Append $CERT_DIR/machine-client-ca.pem to client_ca_path; restart gateway." |
| 96 | +read -r -p "Press Enter when done. " _ |
| 97 | + |
| 98 | +find_sidecar() { |
| 99 | + if [[ -n "$SIDECAR_BIN" ]]; then |
| 100 | + if [[ ! -x "$SIDECAR_BIN" ]]; then |
| 101 | + echo "error: sidecar binary not found or not executable: $SIDECAR_BIN" >&2 |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + printf '%s' "$SIDECAR_BIN" |
| 105 | + return |
| 106 | + fi |
| 107 | + |
| 108 | + if command -v agent_gateway_sidecar &>/dev/null; then |
| 109 | + command -v agent_gateway_sidecar |
| 110 | + return |
| 111 | + fi |
| 112 | + |
| 113 | + local candidates=( |
| 114 | + "./target/release/agent_gateway_sidecar" |
| 115 | + "./target/debug/agent_gateway_sidecar" |
| 116 | + ) |
| 117 | + for candidate in "${candidates[@]}"; do |
| 118 | + if [[ -x "$candidate" ]]; then |
| 119 | + printf '%s' "$candidate" |
| 120 | + return |
| 121 | + fi |
| 122 | + done |
| 123 | + |
| 124 | + echo "error: could not find agent_gateway_sidecar binary" >&2 |
| 125 | + echo "hint: build with 'cargo build -p agent_gateway_sidecar' or pass --sidecar-bin" >&2 |
| 126 | + exit 1 |
| 127 | +} |
| 128 | + |
| 129 | +SIDECAR="$(find_sidecar)" |
| 130 | + |
| 131 | +SIDECAR_PID="" |
| 132 | + |
| 133 | +cleanup() { |
| 134 | + if [[ -n "$SIDECAR_PID" ]]; then |
| 135 | + kill "$SIDECAR_PID" 2>/dev/null || true |
| 136 | + wait "$SIDECAR_PID" 2>/dev/null || true |
| 137 | + fi |
| 138 | +} |
| 139 | + |
| 140 | +trap cleanup EXIT |
| 141 | + |
| 142 | +"$SIDECAR" \ |
| 143 | + --listen "$LISTEN" \ |
| 144 | + --gateway "$GATEWAY" \ |
| 145 | + --client-cert "$CERT_DIR/machine-client.pem" \ |
| 146 | + --client-key "$CERT_DIR/machine-client-key.pem" \ |
| 147 | + --ca-cert "$GATEWAY_CA" & |
| 148 | +SIDECAR_PID=$! |
| 149 | + |
| 150 | +parse_listen_addr() { |
| 151 | + local addr="$1" |
| 152 | + if [[ "$addr" == \[* ]]; then |
| 153 | + LISTEN_HOST="${addr%%\]:*}" |
| 154 | + LISTEN_HOST="${LISTEN_HOST#\[}" |
| 155 | + LISTEN_PORT="${addr##*\]:}" |
| 156 | + else |
| 157 | + LISTEN_HOST="${addr%:*}" |
| 158 | + LISTEN_PORT="${addr##*:}" |
| 159 | + fi |
| 160 | +} |
| 161 | + |
| 162 | +parse_listen_addr "$LISTEN" |
| 163 | + |
| 164 | +echo "Waiting for sidecar on $LISTEN..." |
| 165 | +for _ in $(seq 1 50); do |
| 166 | + if (echo > "/dev/tcp/$LISTEN_HOST/$LISTEN_PORT") 2>/dev/null; then |
| 167 | + break |
| 168 | + fi |
| 169 | + if ! kill -0 "$SIDECAR_PID" 2>/dev/null; then |
| 170 | + echo "error: sidecar exited unexpectedly" >&2 |
| 171 | + wait "$SIDECAR_PID" 2>/dev/null || true |
| 172 | + SIDECAR_PID="" |
| 173 | + exit 1 |
| 174 | + fi |
| 175 | + sleep 0.1 |
| 176 | +done |
| 177 | + |
| 178 | +if ! (echo > "/dev/tcp/$LISTEN_HOST/$LISTEN_PORT") 2>/dev/null; then |
| 179 | + echo "error: sidecar did not become ready within 5 seconds" >&2 |
| 180 | + exit 1 |
| 181 | +fi |
| 182 | + |
| 183 | +echo "Sidecar ready on $LISTEN" |
| 184 | + |
| 185 | +PROXY_URL="http://$LISTEN" |
| 186 | + |
| 187 | +HTTP_PROXY="$PROXY_URL" \ |
| 188 | +HTTPS_PROXY="$PROXY_URL" \ |
| 189 | + claude |
0 commit comments