-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist-tunnels
More file actions
executable file
·82 lines (69 loc) · 2.55 KB
/
list-tunnels
File metadata and controls
executable file
·82 lines (69 loc) · 2.55 KB
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
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TUNNEL_STATE_DIR="${HOME}/.cache/worm/tunnels"
SOCKET_DIR="${HOME}/.cache/worm/sockets"
CADDYFILE="${SCRIPT_DIR}/Caddyfile"
if [ ! -d "$TUNNEL_STATE_DIR" ] || [ -z "$(ls -A "$TUNNEL_STATE_DIR" 2>/dev/null)" ]; then
echo "No active tunnels"
exit 0
fi
echo "Active Tunnels:"
echo "==============="
echo ""
active=0
stale=0
for state_file in "$TUNNEL_STATE_DIR"/*.json; do
if [ -f "$state_file" ]; then
# `|| true` on each: grep exits 1 when a field is absent (e.g. legacy
# state files predating ssh_tunnel_pid) and set -e + pipefail would kill
# the script otherwise.
domain=$(grep -o '"domain": "[^"]*"' "$state_file" | cut -d'"' -f4 || true)
ssh_addr=$(grep -o '"ssh_addr": "[^"]*"' "$state_file" | cut -d'"' -f4 || true)
project=$(grep -o '"project": "[^"]*"' "$state_file" | cut -d'"' -f4 || true)
container=$(grep -o '"container_name": "[^"]*"' "$state_file" | cut -d'"' -f4 || true)
ssh_pid=$(grep -o '"ssh_tunnel_pid": [0-9]*' "$state_file" | awk '{print $2}' || true)
# Check if SSH tunnel process is still alive
if [ -n "$ssh_pid" ] && kill -0 "$ssh_pid" 2>/dev/null; then
echo "• http://${domain}"
echo " SSH: $ssh_addr | Project: $project | Container: $container"
echo ""
active=$((active + 1))
else
# Stale state file — tunnel is dead. Drop the socket too.
rm -f "$state_file" "${SOCKET_DIR}/${domain}.sock"
stale=$((stale + 1))
fi
fi
done
if [ $active -eq 0 ]; then
echo "No active tunnels"
fi
if [ $stale -gt 0 ]; then
echo "Cleaned up $stale stale tunnel(s)"
# Regenerate Caddyfile so Caddy stops proxying to the dead sockets.
# Keep in sync with container-tunnel-caddy:update_caddy_config.
{
cat <<'HEADER'
{
auto_https off
admin localhost:2019
}
HEADER
for state_file in "$TUNNEL_STATE_DIR"/*.json; do
[ -f "$state_file" ] || continue
domain=$(grep -o '"domain": "[^"]*"' "$state_file" | cut -d'"' -f4 || true)
[ -n "$domain" ] || continue
cat <<EOF
${domain}:80 {
reverse_proxy unix//sockets/${domain}.sock
respond /health 200
}
EOF
done
} > "$CADDYFILE"
if docker ps --filter "name=worm-caddy" --filter "status=running" -q 2>/dev/null | grep -q .; then
docker exec worm-caddy caddy reload --config /etc/caddy/Caddyfile 2>/dev/null || true
fi
fi
exit 0