-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaddy-setup.ts
More file actions
executable file
·394 lines (350 loc) · 11 KB
/
caddy-setup.ts
File metadata and controls
executable file
·394 lines (350 loc) · 11 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env bun
import { $ } from "bun"
import { existsSync } from "fs"
import { resolve } from "path"
const DOMAIN = "xtc.localhost"
const PORT = 443 // Use port 443 to coexist with BSD
const PROJECT_DIR = resolve(import.meta.dir)
const WEB_DIST_DIR = resolve(PROJECT_DIR, "zig-out/web-dist")
const SECURITY_HEADERS: Record<string, string[]> = {
"Cross-Origin-Embedder-Policy": ["require-corp"],
"Cross-Origin-Opener-Policy": ["same-origin"]
}
async function setup() {
console.log("🚀 XTC Web Development Setup")
console.log(`Domain: https://${DOMAIN}`)
console.log(`Serving: ${WEB_DIST_DIR}`)
console.log()
// Check if web-dist exists
if (!existsSync(WEB_DIST_DIR)) {
console.error("❌ Web distribution not found at", WEB_DIST_DIR)
console.log("Run 'zig build web-dist' first")
process.exit(1)
}
// Check if Caddy is running
try {
await $`curl -s http://localhost:2019/config/`.quiet()
console.log("✓ Caddy API is accessible")
} catch {
console.error("❌ Caddy API not accessible at http://localhost:2019")
console.log(
"Make sure Caddy is running (the BSD setup should have started it)"
)
process.exit(1)
}
// Check for mkcert certificates
const certFile = `${DOMAIN}+1.pem`
const keyFile = `${DOMAIN}+1-key.pem`
if (
!existsSync(resolve(PROJECT_DIR, certFile)) ||
!existsSync(resolve(PROJECT_DIR, keyFile))
) {
console.log("📜 Generating SSL certificate for", DOMAIN)
try {
await $`mkcert ${DOMAIN} localhost`.cwd(PROJECT_DIR)
console.log("✓ SSL certificate generated")
} catch (error) {
console.error(
"❌ Failed to generate certificate. Make sure mkcert is installed and CA is set up"
)
console.log("Run: brew install mkcert && mkcert -install")
process.exit(1)
}
} else {
console.log("✓ SSL certificates found")
}
console.log("Configuring Caddy...")
try {
// First, check if there's an existing server on port 443
const serversResponse = await fetch(
"http://localhost:2019/config/apps/http/servers"
)
const servers = await serversResponse.json()
let targetServer = null
let serverName = null
// Find the server listening on port 443
for (const [name, config] of Object.entries(servers)) {
if (config.listen && config.listen.includes(`:${PORT}`)) {
targetServer = config
serverName = name
console.log(`✓ Found existing server '${name}' on port ${PORT}`)
break
}
}
if (!targetServer) {
// Create a new server if none exists on port 443
console.log("Creating new server configuration...")
serverName = "xtc_server"
targetServer = {
listen: [`:${PORT}`],
routes: []
}
}
// Add or update XTC route
const xtcRoute = {
match: [
{
host: [DOMAIN]
}
],
handle: [
{
handler: "headers",
response: {
set: SECURITY_HEADERS
}
},
{
handler: "file_server",
root: WEB_DIST_DIR,
index_names: ["index.html"]
}
]
}
// Remove any existing XTC routes
if (targetServer.routes) {
targetServer.routes = targetServer.routes.filter((route) => {
const hosts = route.match?.[0]?.host || []
return !hosts.includes(DOMAIN)
})
} else {
targetServer.routes = []
}
// Add the new XTC route
targetServer.routes.push(xtcRoute)
// Update the server configuration
const response = await fetch(
`http://localhost:2019/config/apps/http/servers/${serverName}`,
{
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(targetServer)
}
)
if (!response.ok) {
const error = await response.text()
console.error("❌ Failed to configure server:", error)
process.exit(1)
}
// Configure TLS certificates
const tlsConfig = {
certificates: {
load_files: [
{
certificate: resolve(PROJECT_DIR, certFile),
key: resolve(PROJECT_DIR, keyFile)
}
]
}
}
// Get existing TLS config and merge
const tlsResponse = await fetch("http://localhost:2019/config/apps/tls")
if (tlsResponse.ok) {
const existingTls = await tlsResponse.json()
if (existingTls?.certificates?.load_files) {
// Check if our cert is already there
const certPath = resolve(PROJECT_DIR, certFile)
const alreadyHasCert = existingTls.certificates.load_files.some(
(cert) => cert.certificate === certPath
)
if (!alreadyHasCert) {
existingTls.certificates.load_files.push(
tlsConfig.certificates.load_files[0]
)
tlsConfig.certificates.load_files =
existingTls.certificates.load_files
} else {
console.log("✓ TLS certificate already configured")
}
}
}
const tlsUpdateResponse = await fetch(
"http://localhost:2019/config/apps/tls",
{
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(tlsConfig)
}
)
if (!tlsUpdateResponse.ok) {
const error = await tlsUpdateResponse.text()
console.error("⚠️ Warning: Failed to configure TLS:", error)
}
console.log("✓ Caddy configured successfully")
console.log()
console.log("🌐 Access your site at:")
console.log(` https://${DOMAIN}`)
console.log()
console.log("📊 Caddy admin: http://localhost:2019/config/")
} catch (error) {
console.error("❌ Error configuring Caddy:", error)
process.exit(1)
}
}
async function remove() {
console.log("Removing XTC configuration from Caddy...")
try {
// Get all servers
const serversResponse = await fetch(
"http://localhost:2019/config/apps/http/servers"
)
const servers = await serversResponse.json()
let found = false
// Find and update servers that have XTC routes
for (const [serverName, config] of Object.entries(servers)) {
if (config.routes) {
const originalLength = config.routes.length
config.routes = config.routes.filter((route) => {
const hosts = route.match?.[0]?.host || []
return !hosts.includes(DOMAIN)
})
if (config.routes.length < originalLength) {
found = true
console.log(`Removing XTC route from server '${serverName}'...`)
// Update the server without the XTC route
if (config.routes.length > 0) {
// Server has other routes, update it
const response = await fetch(
`http://localhost:2019/config/apps/http/servers/${serverName}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(config)
}
)
if (!response.ok) {
const error = await response.text()
console.error(
`❌ Failed to update server '${serverName}':`,
error
)
} else {
console.log(`✓ Removed XTC route from server '${serverName}'`)
}
} else if (serverName === "xtc_server") {
// This is our dedicated server with no other routes, delete it
const response = await fetch(
`http://localhost:2019/config/apps/http/servers/${serverName}`,
{
method: "DELETE"
}
)
if (response.ok) {
console.log(`✓ Removed dedicated XTC server '${serverName}'`)
}
}
}
}
}
if (!found) {
console.log("ℹ XTC was not configured")
} else {
console.log("✓ XTC configuration removed from Caddy")
}
} catch (error) {
console.error("❌ Error removing configuration:", error)
process.exit(1)
}
}
async function status() {
console.log("📊 XTC Caddy Server Status")
console.log("==========================")
try {
// Check if Caddy is running
const testResponse = await fetch("http://localhost:2019/config/")
if (!testResponse.ok) {
console.error("❌ Caddy API not accessible")
console.log("Make sure Caddy is running")
return
}
// Get all servers
const serversResponse = await fetch(
"http://localhost:2019/config/apps/http/servers"
)
const servers = await serversResponse.json()
let found = false
// Look for XTC configuration in all servers
for (const [serverName, config] of Object.entries(servers)) {
if (config.routes) {
for (const route of config.routes) {
const hosts = route.match?.[0]?.host || []
if (hosts.includes(DOMAIN)) {
found = true
console.log(`✓ XTC is configured in server '${serverName}'`)
console.log(` Listening on: ${config.listen.join(", ")}`)
console.log(` Serving from: ${route.handle[0].root}`)
console.log()
console.log(`🌐 https://${DOMAIN}`)
// Check certificate
const certFile = resolve(PROJECT_DIR, `${DOMAIN}+1.pem`)
if (existsSync(certFile)) {
console.log("✓ SSL certificate exists")
} else {
console.log(
"⚠️ SSL certificate not found - run setup to generate"
)
}
break
}
}
if (found) break
}
}
if (!found) {
console.log("ℹ XTC is not configured")
console.log("Run 'bun caddy-setup.ts' to configure")
}
// Show other configured hosts
console.log()
console.log("Other configured domains:")
const allHosts = new Set()
for (const config of Object.values(servers)) {
if (config.routes) {
for (const route of config.routes) {
const hosts = route.match?.[0]?.host || []
hosts.forEach((host) => {
if (host !== DOMAIN) allHosts.add(host)
})
}
}
}
if (allHosts.size > 0) {
for (const host of allHosts) {
console.log(` - https://${host}`)
}
} else {
console.log(" (none)")
}
} catch (error) {
console.error("❌ Caddy API not accessible")
console.log("Make sure Caddy is running")
}
}
// Parse command line arguments
const command = process.argv[2]
switch (command) {
case "remove":
await remove()
break
case "status":
await status()
break
case "setup":
case undefined:
await setup()
break
default:
console.log("Usage: bun caddy-setup.ts [setup|remove|status]")
console.log()
console.log("Commands:")
console.log(" setup - Configure Caddy to serve XTC web-dist (default)")
console.log(" remove - Remove XTC configuration from Caddy")
console.log(" status - Check current configuration status")
process.exit(1)
}