-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaddy-domain.sh
More file actions
executable file
·161 lines (138 loc) · 4 KB
/
caddy-domain.sh
File metadata and controls
executable file
·161 lines (138 loc) · 4 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env bash
# Manage domains on a Caddy reverse proxy via its admin API.
#
# Configure via environment:
# CADDY_API (default: http://10.0.0.1:2019)
#
# Usage:
# caddy-domain add <domain> <backend> Add a domain route
# caddy-domain remove <domain> Remove a domain route
# caddy-domain list List all configured domains
#
# Examples:
# caddy-domain add app.example.com 10.0.0.200:8000
# caddy-domain add docs.example.com 10.0.0.300:3001
# caddy-domain remove app.example.com
# caddy-domain list
set -euo pipefail
CADDY_API="${CADDY_API:-http://10.0.0.1:2019}"
usage() {
echo "Usage:"
echo " caddy-domain add <domain> <backend> e.g. caddy-domain add app.example.com 10.0.0.200:8000"
echo " caddy-domain remove <domain> e.g. caddy-domain remove app.example.com"
echo " caddy-domain list List all configured domains"
exit 1
}
check_api() {
if ! curl -sf "$CADDY_API/config/" >/dev/null 2>&1; then
echo "Error: Cannot reach Caddy API at $CADDY_API" >&2
echo "Is Caddy running on the host with the admin API enabled?" >&2
exit 1
fi
}
cmd_add() {
local domain="$1"
local backend="$2"
check_api
local route_json
route_json=$(cat <<ENDJSON
{
"@id": "$domain",
"match": [{"host": ["$domain"]}],
"handle": [{
"handler": "reverse_proxy",
"upstreams": [{"dial": "$backend"}]
}],
"terminal": true
}
ENDJSON
)
# Try to update existing route by ID
local http_code
http_code=$(curl -sf -o /dev/null -w "%{http_code}" \
-X POST \
-H "Content-Type: application/json" \
-d "$route_json" \
"$CADDY_API/id/$domain" 2>/dev/null) || true
if [ "$http_code" = "200" ]; then
echo "Updated: $domain -> $backend"
return
fi
# If the id doesn't exist, POST to the routes array
local server_name
server_name=$(curl -sf "$CADDY_API/config/apps/http/servers/" 2>/dev/null \
| python3 -c "import sys,json; print(list(json.load(sys.stdin).keys())[0])" 2>/dev/null) || server_name="srv0"
http_code=$(curl -sf -o /dev/null -w "%{http_code}" \
-X POST \
-H "Content-Type: application/json" \
-d "$route_json" \
"$CADDY_API/config/apps/http/servers/$server_name/routes" 2>/dev/null) || true
if [ "$http_code" = "200" ]; then
echo "Added: $domain -> $backend"
else
echo "Error: Failed to add route (HTTP $http_code)" >&2
echo "You may need to set up the initial Caddy config on the host first." >&2
exit 1
fi
}
cmd_remove() {
local domain="$1"
check_api
local http_code
http_code=$(curl -sf -o /dev/null -w "%{http_code}" \
-X DELETE \
"$CADDY_API/id/$domain" 2>/dev/null) || true
if [ "$http_code" = "200" ]; then
echo "Removed: $domain"
else
echo "Error: Domain '$domain' not found or could not be removed (HTTP $http_code)" >&2
exit 1
fi
}
cmd_list() {
check_api
echo "Configured domains:"
echo ""
curl -sf "$CADDY_API/config/apps/http/servers/" 2>/dev/null \
| python3 -c "
import sys, json
def find_upstreams(handlers):
backends = []
for h in handlers:
if h.get('handler') == 'reverse_proxy':
for u in h.get('upstreams', []):
backends.append(u.get('dial', '?'))
if h.get('handler') == 'subroute':
for sub in h.get('routes', []):
backends.extend(find_upstreams(sub.get('handle', [])))
return backends
servers = json.load(sys.stdin)
for srv_name, srv in servers.items():
for r in srv.get('routes', []):
hosts = []
for m in r.get('match', []):
hosts.extend(m.get('host', []))
backends = find_upstreams(r.get('handle', []))
for host in hosts:
print(f' {host} -> {\", \".join(backends)}')
" 2>/dev/null || echo " (no routes configured or could not parse config)"
}
if [ $# -lt 1 ]; then
usage
fi
case "$1" in
add)
[ $# -ne 3 ] && usage
cmd_add "$2" "$3"
;;
remove)
[ $# -ne 2 ] && usage
cmd_remove "$2"
;;
list)
cmd_list
;;
*)
usage
;;
esac