File size: 2,129 Bytes
913eba3
 
 
 
 
 
 
 
 
5dbd0ed
 
 
 
913eba3
5dbd0ed
 
 
913eba3
5dbd0ed
913eba3
 
 
5dbd0ed
913eba3
 
754cd70
913eba3
 
5dbd0ed
 
 
754cd70
 
 
 
5dbd0ed
754cd70
5dbd0ed
 
 
 
 
913eba3
 
 
754cd70
5dbd0ed
 
 
754cd70
913eba3
 
 
 
 
 
 
ca0f8a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env bash
set -euo pipefail

SSH_USER="blendersb.turn"
SSH_HOST="blendersb-45318.portmap.host"
SSH_REMOTE_PORT=45318
LOCAL_TARGET_PORT=7860
SSH_LOG="/home/appuser/ssh-tunnel.log"

SSH_DIR="/home/appuser/.ssh"
mkdir -p "$SSH_DIR"
chmod 700 "$SSH_DIR"
chown appuser:appuser "$SSH_DIR" 2>/dev/null || true

KEY_PATH="$SSH_DIR/private.pem"

if [ -n "${PORTMAP_SECRET:-}" ]; then
    printf '%b' "$PORTMAP_SECRET" > "$KEY_PATH"
    chmod 600 "$KEY_PATH"
    chown appuser:appuser "$KEY_PATH" 2>/dev/null || true
    echo "Wrote private key to $KEY_PATH (from env var)."
else
    echo "No PORTMAP_SECRET provided; skipping PEM creation."
fi

SSH_PID=0

start_ssh_tunnel() {
    if [ -f "$KEY_PATH" ]; then
        echo "Starting SSH reverse tunnel to ${SSH_HOST}:${SSH_REMOTE_PORT} -> localhost:${LOCAL_TARGET_PORT}"
        nohup ssh -i "$KEY_PATH" \
          -o StrictHostKeyChecking=no \
          -o UserKnownHostsFile=/dev/null \
          -o ServerAliveInterval=30 \
          -o ServerAliveCountMax=3 \
          "${SSH_USER}@${SSH_HOST}" \
          -N -R "${SSH_REMOTE_PORT}:127.0.0.1:${LOCAL_TARGET_PORT}" >"$SSH_LOG" 2>&1 &
        SSH_PID=$!
        echo "SSH tunnel started (pid=${SSH_PID}); logging to $SSH_LOG"
    else
        echo "No private key found; skipping SSH tunnel."
    fi
}

stop_ssh_tunnel() {
    if [ "$SSH_PID" -ne 0 ] && kill -0 "$SSH_PID" >/dev/null 2>&1; then
        echo "Stopping SSH tunnel (pid=${SSH_PID})..."
        kill "$SSH_PID" || true
        sleep 1
        kill -9 "$SSH_PID" 2>/dev/null || true
    fi
}

trap 'echo "Received SIGTERM/SIGINT, shutting down..."; stop_ssh_tunnel; exit 0' TERM INT

start_ssh_tunnel

exec turnserver \
#     -c /etc/coturn/turnserver.conf \
#     --listening-ip=0.0.0.0 \
     --listening-port=7860 \
     --tls-listening-port=5349 \
     --external-ip="tcp://blendersb-45318.portmap.host:45318" \
#     --cert=/etc/turn/certs/turn_server_cert.pem \
#     --pkey=/etc/turn/certs/turn_server_pkey.pem \
     --user=myuser:mypassword \
#     --realm="$TURN_PUBLIC_ADDR" \
     --log-file=stdout \
     --simple-log \
     --no-cli &
TURN_PID=$!