-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpark
More file actions
executable file
·1366 lines (1180 loc) · 45.9 KB
/
Copy pathpark
File metadata and controls
executable file
·1366 lines (1180 loc) · 45.9 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
# =============================================================
# proxypark – Local reverse-proxy & DNS manager for macOS
# https://github.com/McGo/proxypark
# =============================================================
# Traefik + dnsmasq + mkcert. Zero port conflicts.
# Uses file-provider only (no Docker socket required).
# =============================================================
VERSION="0.1.0"
PARK_DIR="$(cd "$(dirname "$(readlink -f "$0" 2>/dev/null || realpath "$0")")/.." && pwd)"
DATA_DIR="${PROXYPARK_DATA_DIR:-$HOME/.proxypark}"
CERTS_DIR="$DATA_DIR/certs"
DYNAMIC_DIR="$DATA_DIR/dynamic"
COMPOSE_FILE="$DATA_DIR/docker-compose.yml"
TLD="${PROXYPARK_TLD:-test}"
# Homebrew prefix (Apple Silicon vs Intel)
if [ -d "/opt/homebrew" ]; then
BREW_PREFIX="/opt/homebrew"
else
BREW_PREFIX="/usr/local"
fi
DNSMASQ_CONF_DIR="$BREW_PREFIX/etc/dnsmasq.d"
# ─── Colors ──────────────────────────────────────────────────
RED=$'\033[0;31m'
GREEN=$'\033[0;32m'
YELLOW=$'\033[1;33m'
BLUE=$'\033[0;34m'
CYAN=$'\033[0;36m'
BOLD=$'\033[1m'
DIM=$'\033[2m'
NC=$'\033[0m'
# ─── Helpers ─────────────────────────────────────────────────
info() { echo -e " ${BLUE}→${NC} $*"; }
success() { echo -e " ${GREEN}✔${NC} $*"; }
warn() { echo -e " ${YELLOW}⚠${NC} $*"; }
error() { echo -e " ${RED}✖${NC} $*" >&2; }
slugify() {
echo "$1" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9-]/-/g' | sed 's/--*/-/g' | sed 's/^-//;s/-$//'
}
ensure_running() {
if ! docker compose -f "$COMPOSE_FILE" ps --status running 2>/dev/null | grep -q proxypark; then
error "Traefik is not running. Start with: park start"
exit 1
fi
}
# ─── Certificate Management ─────────────────────────────────
# .test is on the Public Suffix List — browsers reject wildcard
# certs for PSL TLDs. We generate explicit certs for each
# linked domain and regenerate whenever links change.
# ─────────────────────────────────────────────────────────────
collect_domains() {
local domains=("localhost")
# Extract domains from all link-*.yml files
if compgen -G "$DYNAMIC_DIR/link-*.yml" > /dev/null 2>&1; then
for f in "$DYNAMIC_DIR"/link-*.yml; do
local domain
domain=$(sed -n "s/.*Host(\`\([^\`]*\).*/\1/p" "$f" 2>/dev/null | head -1)
if [ -n "$domain" ]; then
domains+=("$domain")
fi
done
fi
# Extract domains from valet catchall (skip, it's a regex)
# Deduplicate and sort
printf '%s\n' "${domains[@]}" | sort -u
}
regenerate_certs() {
local domains
domains=$(collect_domains)
if [ -z "$domains" ]; then
warn "No domains found, skipping cert generation."
return
fi
local domain_args=()
while IFS= read -r d; do
domain_args+=("$d")
done <<< "$domains"
info "Generating certs for: ${domain_args[*]}"
(cd "$CERTS_DIR" && mkcert \
-cert-file "wildcard.${TLD}.pem" \
-key-file "wildcard.${TLD}-key.pem" \
"${domain_args[@]}")
success "Certificates updated (${#domain_args[@]} domains)"
# Restart Traefik to pick up new certificate
docker compose -f "$COMPOSE_FILE" restart >/dev/null 2>&1 && \
success "Traefik restarted" || true
}
# ─── install ─────────────────────────────────────────────────
cmd_install() {
echo ""
echo -e " ${BOLD}${CYAN}proxypark v${VERSION}${NC} – Installation"
echo -e " ${DIM}────────────────────────────────────${NC}"
echo ""
# --- Homebrew check ---
if ! command -v brew &>/dev/null; then
error "Homebrew is required. Install from https://brew.sh"
exit 1
fi
# --- mkcert ---
info "Checking mkcert..."
if ! command -v mkcert &>/dev/null; then
info "Installing mkcert + nss (Firefox support)..."
brew install mkcert nss
fi
mkcert -install 2>/dev/null || true
success "mkcert & local CA ready"
# --- dnsmasq ---
info "Checking dnsmasq..."
if ! brew list dnsmasq &>/dev/null 2>&1; then
info "Installing dnsmasq..."
brew install dnsmasq
fi
# Ensure conf-dir include
local dnsmasq_main="$BREW_PREFIX/etc/dnsmasq.conf"
mkdir -p "$DNSMASQ_CONF_DIR"
if ! grep -q "conf-dir=$DNSMASQ_CONF_DIR" "$dnsmasq_main" 2>/dev/null; then
echo "conf-dir=$DNSMASQ_CONF_DIR/,*.conf" >> "$dnsmasq_main"
fi
# TLD config
cat > "$DNSMASQ_CONF_DIR/proxypark-${TLD}.conf" <<EOF
# proxypark – resolve *.${TLD} to localhost
address=/.${TLD}/127.0.0.1
EOF
sudo brew services restart dnsmasq
success "dnsmasq configured: *.${TLD} → 127.0.0.1"
# macOS resolver
info "Setting up macOS DNS resolver..."
sudo mkdir -p /etc/resolver
echo "nameserver 127.0.0.1" | sudo tee /etc/resolver/${TLD} > /dev/null
success "/etc/resolver/${TLD} created"
# --- Data directories ---
mkdir -p "$DATA_DIR" "$CERTS_DIR" "$DYNAMIC_DIR"
# --- Docker network ---
info "Creating Docker network 'proxy'..."
docker network create proxy 2>/dev/null \
&& success "Network 'proxy' created" \
|| success "Network 'proxy' already exists"
# --- Initial certificates ---
info "Generating initial certificates..."
if [ ! -f "$CERTS_DIR/wildcard.${TLD}.pem" ]; then
(cd "$CERTS_DIR" && mkcert \
-cert-file "wildcard.${TLD}.pem" \
-key-file "wildcard.${TLD}-key.pem" \
"localhost")
success "Initial certificate created"
else
success "Certificates already exist"
fi
# --- TLS dynamic config ---
cat > "$DYNAMIC_DIR/tls.yml" <<EOF
tls:
certificates:
- certFile: /etc/traefik/certs/wildcard.${TLD}.pem
keyFile: /etc/traefik/certs/wildcard.${TLD}-key.pem
stores:
default:
defaultCertificate:
certFile: /etc/traefik/certs/wildcard.${TLD}.pem
keyFile: /etc/traefik/certs/wildcard.${TLD}-key.pem
EOF
# --- Traefik compose ---
# File-provider only. No Docker socket needed.
cat > "$COMPOSE_FILE" <<EOF
services:
traefik:
image: traefik:v3.2
container_name: proxypark
restart: unless-stopped
command:
- --api.insecure=true
- --entrypoints.web.address=:80
- --entrypoints.web.http.redirections.entrypoint.to=websecure
- --entrypoints.websecure.address=:443
- --entrypoints.websecure.http.tls=true
- --providers.file.directory=/etc/traefik/dynamic
- --providers.file.watch=true
- --log.level=INFO
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- ${DYNAMIC_DIR}:/etc/traefik/dynamic:ro
- ${CERTS_DIR}:/etc/traefik/certs:ro
networks:
- proxy
extra_hosts:
- "host.docker.internal:host-gateway"
networks:
proxy:
name: proxy
external: true
EOF
# --- Start ---
echo ""
cmd_start
echo ""
echo -e " ${BOLD}${GREEN}Installation complete!${NC}"
echo ""
echo -e " Dashboard: ${CYAN}http://localhost:8080${NC}"
echo -e " DNS: *.${TLD} → 127.0.0.1"
echo -e " HTTPS: mkcert per-domain certificates"
echo ""
echo -e " Get started:"
echo -e " ${BOLD}park link myapp 8000${NC} → https://myapp.${TLD}"
echo -e " ${BOLD}park scaffold projekt${NC} → docker-compose.yml template"
echo -e " ${BOLD}park list${NC} → show all routes"
echo ""
}
# ─── uninstall ───────────────────────────────────────────────
cmd_uninstall() {
echo ""
echo -e " ${BOLD}${CYAN}Uninstall proxypark${NC}"
echo ""
warn "This removes Traefik, dnsmasq config, certs, and all links."
echo ""
read -p " Continue? (y/N) " -n 1 -r
echo ""
[[ $REPLY =~ ^[Yy]$ ]] || exit 0
if [ -f "$COMPOSE_FILE" ]; then
docker compose -f "$COMPOSE_FILE" down 2>/dev/null || true
success "Traefik stopped and removed"
fi
docker network rm proxy 2>/dev/null && success "Network 'proxy' removed" || true
rm -f "$DNSMASQ_CONF_DIR/proxypark-${TLD}.conf" 2>/dev/null
sudo brew services restart dnsmasq 2>/dev/null || true
success "dnsmasq config removed"
sudo rm -f "/etc/resolver/${TLD}" 2>/dev/null
success "DNS resolver removed"
rm -rf "$DATA_DIR"
success "~/.proxypark removed"
echo ""
echo -e " ${GREEN}Uninstall complete.${NC}"
echo ""
}
# ─── start / stop / restart ─────────────────────────────────
cmd_start() {
info "Starting Traefik..."
docker compose -f "$COMPOSE_FILE" up -d --quiet-pull
success "Traefik running – Dashboard: ${CYAN}http://localhost:8080${NC}"
}
cmd_stop() {
info "Stopping Traefik..."
docker compose -f "$COMPOSE_FILE" down
success "Traefik stopped"
}
cmd_restart() {
info "Restarting Traefik..."
docker compose -f "$COMPOSE_FILE" restart
success "Traefik restarted"
}
# ─── link ────────────────────────────────────────────────────
# ─── parse name into sub + domain ────────────────────────────
# "api.myapp" → sub=api, name=myapp
# "myapp" → sub="", name=myapp
parse_link_name() {
local input="$1"
if [[ "$input" == *.* ]]; then
LINK_SUB="$(slugify "${input%%.*}")"
LINK_NAME="$(slugify "${input#*.}")"
else
LINK_SUB=""
LINK_NAME="$(slugify "$input")"
fi
# Derived values
if [ -n "$LINK_SUB" ]; then
LINK_DOMAIN="${LINK_SUB}.${LINK_NAME}.${TLD}"
LINK_FILE_ID="${LINK_SUB}-${LINK_NAME}"
else
LINK_DOMAIN="${LINK_NAME}.${TLD}"
LINK_FILE_ID="${LINK_NAME}"
fi
}
# ─── link ────────────────────────────────────────────────────
cmd_link() {
local input="${1:-}"
local port="${2:-}"
if [ -z "$input" ] || [ -z "$port" ]; then
echo ""
echo " Usage: park link <name> <port>"
echo ""
echo " Links a local service. Use dot notation for subdomains."
echo ""
echo " Examples:"
echo " park link myshop 8000 → https://myshop.${TLD}"
echo " park link api.myshop 8001 → https://api.myshop.${TLD}"
echo ""
exit 1
fi
parse_link_name "$input"
if [[ ! "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then
error "Port must be between 1 and 65535."
exit 1
fi
mkdir -p "$DYNAMIC_DIR"
cat > "$DYNAMIC_DIR/link-${LINK_FILE_ID}.yml" <<EOF
# proxypark link: ${LINK_DOMAIN} → localhost:${port}
http:
routers:
link-${LINK_FILE_ID}:
rule: "Host(\`${LINK_DOMAIN}\`)"
entrypoints:
- websecure
service: link-${LINK_FILE_ID}
tls: {}
services:
link-${LINK_FILE_ID}:
loadBalancer:
servers:
- url: "http://host.docker.internal:${port}"
EOF
success "Linked: ${CYAN}https://${LINK_DOMAIN}${NC} → localhost:${port}"
# Regenerate certs to include new domain
regenerate_certs
}
# ─── link:docker ─────────────────────────────────────────────
cmd_link_docker() {
local input="${1:-}"
local container_arg="${2:-}"
if [ -z "$input" ] || [ -z "$container_arg" ]; then
echo ""
echo " Usage: park link:docker <name> <container>[:<port>]"
echo ""
echo " Links a Docker container without exposed ports."
echo " Use dot notation for subdomains."
echo ""
echo " Examples:"
echo " park link:docker myapp myapp-container → https://myapp.${TLD}"
echo " park link:docker myapp myapp-container:3000 → https://myapp.${TLD}"
echo " park link:docker api.myapp myapp-container → https://api.myapp.${TLD}"
# Show available services from compose in CWD
local compose_file=""
for f in docker-compose.yml docker-compose.yaml compose.yml compose.yaml; do
if [ -f "$f" ]; then compose_file="$f"; break; fi
done
if [ -n "$compose_file" ]; then
local config_yaml
config_yaml=$(docker compose config 2>/dev/null)
if [ -n "$config_yaml" ]; then
local services
services=$(docker compose config --services 2>/dev/null)
if [ -n "$services" ]; then
echo ""
echo -e " ${BOLD}Services${NC} ${DIM}(from ${compose_file})${NC}"
echo ""
printf " ${DIM}%-25s %-25s %s${NC}\n" "SERVICE" "CONTAINER" "STATE"
echo -e " ${DIM}$(printf '─%.0s' {1..60})${NC}"
while IFS= read -r svc; do
local cname cstate
# Get container_name from compose config YAML (works even when stopped)
# Match: " service:\n container_name: name" in YAML output
cname=$(echo "$config_yaml" | sed -n "/^ ${svc}:/,/^ [^ ]/{ s/^ container_name: *//p; }" | head -1)
if [ -z "$cname" ]; then
cname="${DIM}(set container_name in compose)${NC}"
fi
cstate=$(docker compose ps --format '{{.State}}' "$svc" 2>/dev/null | head -1)
cstate=${cstate:-stopped}
local state_color="${RED}"
[ "$cstate" = "running" ] && state_color="${GREEN}"
printf " %-25s %-25b ${state_color}%s${NC}\n" "$svc" "$cname" "$cstate"
done <<< "$services"
fi
fi
fi
echo ""
exit 1
fi
parse_link_name "$input"
# Parse container:port
local container port
if [[ "$container_arg" == *:* ]]; then
container="${container_arg%%:*}"
port="${container_arg##*:}"
else
container="$container_arg"
port="80"
fi
if [[ ! "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then
error "Port must be between 1 and 65535."
exit 1
fi
# Validate container is running
local running
running=$(docker inspect --format '{{.State.Running}}' "$container" 2>/dev/null || echo "")
if [ "$running" != "true" ]; then
error "Container '${container}' is not running."
exit 1
fi
# Connect container to proxy network if needed
local networks
networks=$(docker inspect --format '{{json .NetworkSettings.Networks}}' "$container" 2>/dev/null)
if ! echo "$networks" | grep -q '"proxy"'; then
info "Connecting '${container}' to proxy network..."
docker network connect proxy "$container" 2>/dev/null \
&& success "Connected to proxy network" \
|| { error "Failed to connect '${container}' to proxy network."; exit 1; }
fi
mkdir -p "$DYNAMIC_DIR"
local file_id="docker-${LINK_FILE_ID}"
# Warn if a conflicting localhost link exists
if [ -f "$DYNAMIC_DIR/link-${LINK_FILE_ID}.yml" ]; then
warn "A localhost link for '${LINK_DOMAIN}' already exists. It will take precedence."
fi
cat > "$DYNAMIC_DIR/link-${file_id}.yml" <<EOF
# proxypark docker-link: ${LINK_DOMAIN} → ${container}:${port}
http:
routers:
link-${file_id}:
rule: "Host(\`${LINK_DOMAIN}\`)"
entrypoints:
- websecure
service: link-${file_id}
tls: {}
services:
link-${file_id}:
loadBalancer:
servers:
- url: "http://${container}:${port}"
EOF
success "Linked: ${CYAN}https://${LINK_DOMAIN}${NC} → ${container}:${port}"
# Regenerate certs to include new domain
regenerate_certs
# Check compose file for persistence
check_compose_persistence "$container"
}
# ─── compose persistence check ──────────────────────────────
check_compose_persistence() {
local container="$1"
# Find compose file in CWD
local compose_file=""
for f in docker-compose.yml docker-compose.yaml compose.yml compose.yaml; do
if [ -f "$f" ]; then compose_file="$f"; break; fi
done
[ -z "$compose_file" ] && return
# Find the service that matches this container
local svc_name=""
# First try: match by container_name in config
local config_yaml
config_yaml=$(docker compose config 2>/dev/null) || return
svc_name=$(echo "$config_yaml" | sed -n "/container_name: ${container}$/{ x; p; }; h" | sed -n 's/^ \([a-zA-Z0-9_-]*\):$/\1/p' | head -1)
# Second try: match by running container name via docker compose ps
if [ -z "$svc_name" ]; then
svc_name=$(docker compose ps --format '{{.Service}}\t{{.Name}}' 2>/dev/null | awk -F'\t' -v c="$container" '$2 == c { print $1; exit }')
fi
# Third try: match by service name directly
if [ -z "$svc_name" ]; then
local services
services=$(docker compose config --services 2>/dev/null)
while IFS= read -r s; do
if [ "$s" = "$container" ]; then
svc_name="$s"
break
fi
done <<< "$services"
fi
[ -z "$svc_name" ] && return
local needs_fix=0
local missing_container_name=0
local missing_proxy_network=0
# Check container_name in the actual compose file (not rendered config)
if ! grep -q "container_name:" "$compose_file" 2>/dev/null || \
! sed -n "/^ ${svc_name}:/,/^ [^ ]/p" "$compose_file" | grep -q "container_name:"; then
missing_container_name=1
needs_fix=1
fi
# Check proxy network on service in actual compose file
local svc_block
svc_block=$(sed -n "/^ ${svc_name}:/,/^ [^ ]/p" "$compose_file")
if ! echo "$svc_block" | grep -q "proxy"; then
missing_proxy_network=1
needs_fix=1
fi
# Check top-level proxy network definition
local missing_top_network=0
if ! grep -q "external: *true" "$compose_file" 2>/dev/null || \
! sed -n '/^networks:/,$ p' "$compose_file" | grep -q "proxy"; then
missing_top_network=1
needs_fix=1
fi
[ "$needs_fix" -eq 0 ] && return
echo ""
warn "Your ${compose_file} is missing config for persistent proxy support:"
[ "$missing_container_name" -eq 1 ] && echo -e " ${DIM}• container_name: ${container} (service: ${svc_name})${NC}"
[ "$missing_proxy_network" -eq 1 ] && echo -e " ${DIM}• proxy network on service '${svc_name}'${NC}"
[ "$missing_top_network" -eq 1 ] && echo -e " ${DIM}• top-level proxy network definition${NC}"
echo ""
echo -en " Fix ${compose_file} automatically? [Y/n] "
read -r answer
answer=${answer:-Y}
if [[ ! "$answer" =~ ^[Yy]$ ]]; then
info "Skipped. Add manually to avoid 'bad gateway' after container restart."
return
fi
# Apply fixes
if [ "$missing_container_name" -eq 1 ]; then
# Add container_name after the service key
sed -i '' "/^ ${svc_name}:/a\\
\\ container_name: ${container}
" "$compose_file"
success "Added container_name: ${container}"
fi
if [ "$missing_proxy_network" -eq 1 ]; then
# Check if service already has a networks: key
if echo "$svc_block" | grep -q "^ networks:"; then
# Add proxy to existing networks list
sed -i '' "/^ ${svc_name}:/,/^ [^ ]/{
/^ networks:/a\\
\\ - proxy
}" "$compose_file"
else
# Find the last line of the service block and add networks before it
sed -i '' "/^ ${svc_name}:/,/^ [^ ]/{
/^ ${svc_name}:/a\\
\\ networks:\\
\\ - default\\
\\ - proxy
}" "$compose_file"
fi
success "Added proxy network to service '${svc_name}'"
fi
if [ "$missing_top_network" -eq 1 ]; then
# Check if top-level networks: key exists
if grep -q "^networks:" "$compose_file"; then
# Add proxy under existing networks key
sed -i '' "/^networks:/a\\
\\ proxy:\\
\\ external: true
" "$compose_file"
else
# Append networks block
printf '\nnetworks:\n proxy:\n external: true\n' >> "$compose_file"
fi
success "Added top-level proxy network definition"
fi
}
# ─── unlink ──────────────────────────────────────────────────
cmd_unlink() {
local name="${1:-}"
if [ -z "$name" ]; then
echo " Usage: park unlink <name>"
exit 1
fi
name="$(slugify "$name")"
local found=0
for f in "$DYNAMIC_DIR"/link-*"${name}".yml "$DYNAMIC_DIR"/link-*"-${name}".yml; do
if [ -f "$f" ]; then
rm "$f"
success "Removed: $(basename "$f" .yml)"
found=1
fi
done
if [ "$found" -eq 0 ]; then
error "No link found for '${name}'"
exit 1
fi
# Regenerate certs without removed domain
regenerate_certs
}
# ─── list ────────────────────────────────────────────────────
cmd_list() {
echo ""
echo -e " ${BOLD}${CYAN}proxypark routes${NC}"
echo ""
local has_links=0
# File-provider links
if compgen -G "$DYNAMIC_DIR/link-*.yml" > /dev/null 2>&1; then
printf " ${DIM}%-35s %-25s %s${NC}\n" "DOMAIN" "TARGET" "CONFIG"
echo -e " ${DIM}$(printf '─%.0s' {1..75})${NC}"
for f in "$DYNAMIC_DIR"/link-*.yml; do
local domain target filename url
domain=$(sed -n "s/.*Host(\`\([^\`]*\).*/\1/p" "$f" 2>/dev/null | head -1)
domain=${domain:-?}
url=$(sed -n 's/.*url: *"\(.*\)"/\1/p' "$f" 2>/dev/null | head -1)
if echo "$url" | grep -q 'host.docker.internal'; then
target=":$(echo "$url" | sed 's/.*://')"
else
target=$(echo "$url" | sed 's|http://||')
fi
target=${target:-?}
filename=$(basename "$f")
printf " %-35s %-25s ${DIM}%s${NC}\n" "https://${domain}" "$target" "$filename"
done
has_links=1
echo ""
fi
# Valet catchall
if [ -f "$DYNAMIC_DIR/valet.yml" ]; then
echo -e " ${DIM}Valet catchall active → http://host.docker.internal:8080${NC}"
echo ""
fi
if [ "$has_links" -eq 0 ]; then
echo " No routes registered yet."
echo ""
echo " Get started:"
echo " park link myapp 8000"
echo " park scaffold myproject"
echo ""
fi
}
# ─── status ──────────────────────────────────────────────────
cmd_status() {
echo ""
echo -e " ${BOLD}${CYAN}proxypark status${NC}"
echo ""
# Traefik
echo -n " Traefik: "
if docker compose -f "$COMPOSE_FILE" ps --status running 2>/dev/null | grep -q proxypark; then
echo -e "${GREEN}running${NC}"
else
echo -e "${RED}stopped${NC}"
fi
# dnsmasq
echo -n " dnsmasq: "
if pgrep -x dnsmasq &>/dev/null; then
echo -e "${GREEN}running${NC}"
else
echo -e "${RED}stopped${NC}"
fi
# DNS probe
echo -n " DNS: "
local dns_result
dns_result=$(dig +short "probe.${TLD}" @127.0.0.1 2>/dev/null | head -1)
if [ "$dns_result" = "127.0.0.1" ]; then
echo -e "${GREEN}OK${NC} ${DIM}(probe.${TLD} → 127.0.0.1)${NC}"
else
echo -e "${RED}FAIL${NC} ${DIM}(dnsmasq not resolving)${NC}"
fi
# Resolver
echo -n " Resolver: "
if [ -f "/etc/resolver/${TLD}" ]; then
echo -e "${GREEN}/etc/resolver/${TLD}${NC}"
else
echo -e "${RED}missing${NC}"
fi
# Certs
echo -n " Certs: "
if [ -f "$CERTS_DIR/wildcard.${TLD}.pem" ]; then
local cert_domains
cert_domains=$(openssl x509 -in "$CERTS_DIR/wildcard.${TLD}.pem" -noout -ext subjectAltName 2>/dev/null \
| sed 's/,/\n/g' | sed -n 's/.*DNS://p' | tr -s ' \n' ' ' || echo "unknown")
echo -e "${GREEN}valid${NC} ${DIM}(${cert_domains})${NC}"
else
echo -e "${RED}missing${NC}"
fi
# Docker network
echo -n " Network: "
if docker network inspect proxy &>/dev/null 2>&1; then
local containers
containers=$(docker network inspect proxy --format '{{len .Containers}}' 2>/dev/null || echo 0)
echo -e "${GREEN}proxy${NC} ${DIM}(${containers} containers)${NC}"
else
echo -e "${RED}not found${NC}"
fi
# Links
local link_count=0
if compgen -G "$DYNAMIC_DIR/link-*.yml" > /dev/null 2>&1; then
link_count=$(ls "$DYNAMIC_DIR"/link-*.yml 2>/dev/null | wc -l | tr -d ' ')
fi
echo " Links: ${link_count} registered"
echo ""
}
# ─── doctor ─────────────────────────────────────────────────
doctor_ok() { printf " ${GREEN}✔${NC} %-18s %s\n" "$1" "$2"; }
doctor_warn() { printf " ${YELLOW}⚠${NC} %-18s %s\n" "$1" "$2"; }
doctor_fail() { printf " ${RED}✖${NC} %-18s %s\n" "$1" "$2"; DOCTOR_ISSUES=$((DOCTOR_ISSUES + 1)); }
doctor_ask_fix() {
local prompt="$1"
if [ "$DOCTOR_AUTOFIX" = "1" ]; then
return 0
fi
echo -en " → ${prompt} [Y/n] "
local answer
read -r answer
answer=${answer:-Y}
[[ "$answer" =~ ^[Yy]$ ]]
}
cmd_doctor() {
local DOCTOR_ISSUES=0
local DOCTOR_AUTOFIX=0
[ "${1:-}" = "--fix" ] && DOCTOR_AUTOFIX=1
echo ""
echo -e " ${BOLD}${CYAN}proxypark doctor${NC}"
echo ""
# ── 1. Docker daemon ──
if docker info &>/dev/null; then
doctor_ok "Docker" "running"
else
doctor_fail "Docker" "not running"
echo -e " ${DIM}Start Docker Desktop or your Docker runtime manually.${NC}"
echo ""
echo -e " ${RED}Cannot continue without Docker.${NC}"
echo ""
return 1
fi
# ── 2. Port 80 ──
local port80_pid
port80_pid=$(lsof -ti :80 2>/dev/null | head -1)
if [ -z "$port80_pid" ]; then
doctor_ok "Port 80" "available"
else
local port80_name
port80_name=$(lsof -i :80 -sTCP:LISTEN 2>/dev/null | awk 'NR==2{print $1}')
if [ "$port80_name" = "com.docke" ] || [ "$port80_name" = "docker-pr" ]; then
doctor_ok "Port 80" "used by proxypark"
else
doctor_fail "Port 80" "occupied by ${port80_name:-unknown} (PID ${port80_pid})"
echo -e " ${DIM}Stop the other service or change its port.${NC}"
fi
fi
# ── 3. Port 443 ──
local port443_pid
port443_pid=$(lsof -ti :443 2>/dev/null | head -1)
if [ -z "$port443_pid" ]; then
doctor_ok "Port 443" "available"
else
local port443_name
port443_name=$(lsof -i :443 -sTCP:LISTEN 2>/dev/null | awk 'NR==2{print $1}')
if [ "$port443_name" = "com.docke" ] || [ "$port443_name" = "docker-pr" ]; then
doctor_ok "Port 443" "used by proxypark"
else
doctor_fail "Port 443" "occupied by ${port443_name:-unknown} (PID ${port443_pid})"
echo -e " ${DIM}Stop the other service or change its port.${NC}"
fi
fi
# ── 4. Docker network ──
if docker network inspect proxy &>/dev/null 2>&1; then
doctor_ok "Network" "proxy exists"
else
doctor_fail "Network" "proxy not found"
if doctor_ask_fix "Create proxy network?"; then
docker network create proxy &>/dev/null \
&& doctor_ok "Network" "proxy created (fixed)" \
|| echo -e " ${RED}Failed to create network.${NC}"
fi
fi
# ── 5. Traefik ──
if docker compose -f "$COMPOSE_FILE" ps --status running 2>/dev/null | grep -q proxypark; then
doctor_ok "Traefik" "running"
else
doctor_fail "Traefik" "not running"
if [ -f "$COMPOSE_FILE" ]; then
if doctor_ask_fix "Start Traefik?"; then
docker compose -f "$COMPOSE_FILE" up -d &>/dev/null \
&& doctor_ok "Traefik" "running (fixed)" \
|| echo -e " ${RED}Failed to start Traefik.${NC}"
fi
else
echo -e " ${DIM}Run 'park install' first.${NC}"
fi
fi
# ── 6. Homebrew ──
if command -v brew &>/dev/null; then
doctor_ok "Homebrew" "installed"
else
doctor_fail "Homebrew" "not found"
echo -e " ${DIM}Install from https://brew.sh${NC}"
fi
# ── 7. mkcert ──
if command -v mkcert &>/dev/null; then
doctor_ok "mkcert" "installed"
else
doctor_fail "mkcert" "not installed"
if command -v brew &>/dev/null && doctor_ask_fix "Install mkcert?"; then
brew install mkcert nss &>/dev/null \
&& mkcert -install &>/dev/null \
&& doctor_ok "mkcert" "installed (fixed)" \
|| echo -e " ${RED}Failed to install mkcert.${NC}"
fi
fi
# ── 8. dnsmasq installed ──
if brew list dnsmasq &>/dev/null 2>&1; then
doctor_ok "dnsmasq" "installed"
else
doctor_fail "dnsmasq" "not installed"
if doctor_ask_fix "Install dnsmasq?"; then
brew install dnsmasq &>/dev/null \
&& doctor_ok "dnsmasq" "installed (fixed)" \
|| echo -e " ${RED}Failed to install dnsmasq.${NC}"
fi
fi
# ── 9. dnsmasq running ──
if pgrep -x dnsmasq &>/dev/null; then
doctor_ok "dnsmasq" "running"
else
doctor_fail "dnsmasq" "not running"
if doctor_ask_fix "Restart dnsmasq?"; then
sudo brew services restart dnsmasq &>/dev/null \
&& doctor_ok "dnsmasq" "running (fixed)" \
|| echo -e " ${RED}Failed to restart dnsmasq.${NC}"
fi
fi
# ── 10. dnsmasq config ──
local dns_conf="$DNSMASQ_CONF_DIR/proxypark-${TLD}.conf"
if [ -f "$dns_conf" ] && grep -q "address=/.${TLD}/127.0.0.1" "$dns_conf" 2>/dev/null; then
doctor_ok "DNS config" "$dns_conf"
else
doctor_fail "DNS config" "missing or invalid"
if doctor_ask_fix "Write dnsmasq config?"; then
mkdir -p "$DNSMASQ_CONF_DIR"
cat > "$dns_conf" <<DNSEOF
# proxypark – resolve *.${TLD} to localhost
address=/.${TLD}/127.0.0.1
DNSEOF
sudo brew services restart dnsmasq &>/dev/null
doctor_ok "DNS config" "$dns_conf (fixed)"
fi
fi
# ── 11. Resolver ──
if [ -f "/etc/resolver/${TLD}" ]; then
doctor_ok "Resolver" "/etc/resolver/${TLD}"
else
doctor_fail "Resolver" "/etc/resolver/${TLD} missing"
if doctor_ask_fix "Create resolver file?"; then
sudo mkdir -p /etc/resolver
echo "nameserver 127.0.0.1" | sudo tee "/etc/resolver/${TLD}" > /dev/null \
&& doctor_ok "Resolver" "/etc/resolver/${TLD} (fixed)" \
|| echo -e " ${RED}Failed to create resolver.${NC}"
fi
fi
# ── 12. DNS resolution ──
local dns_result
dns_result=$(dig +short "probe.${TLD}" @127.0.0.1 2>/dev/null | head -1)
if [ "$dns_result" = "127.0.0.1" ]; then
doctor_ok "DNS" "probe.${TLD} → 127.0.0.1"
else
doctor_fail "DNS" "probe.${TLD} not resolving"
if doctor_ask_fix "Restart dnsmasq and flush DNS cache?"; then
sudo brew services restart dnsmasq &>/dev/null
dscacheutil -flushcache 2>/dev/null
sleep 1
dns_result=$(dig +short "probe.${TLD}" @127.0.0.1 2>/dev/null | head -1)
if [ "$dns_result" = "127.0.0.1" ]; then
doctor_ok "DNS" "probe.${TLD} → 127.0.0.1 (fixed)"
else
echo -e " ${RED}Still not resolving. Check dnsmasq logs.${NC}"
fi
fi
fi
# ── 13-15. Certificates ──
if [ -f "$CERTS_DIR/wildcard.${TLD}.pem" ]; then
# Check expiry
local cert_expiry
cert_expiry=$(openssl x509 -in "$CERTS_DIR/wildcard.${TLD}.pem" -noout -enddate 2>/dev/null \
| sed 's/notAfter=//')
local cert_expiry_epoch
cert_expiry_epoch=$(date -jf "%b %d %T %Y %Z" "$cert_expiry" "+%s" 2>/dev/null || echo "0")
local now_epoch
now_epoch=$(date "+%s")
local days_left=$(( (cert_expiry_epoch - now_epoch) / 86400 ))
if [ "$days_left" -lt 0 ]; then
doctor_fail "Certificates" "expired"
if doctor_ask_fix "Regenerate certificates?"; then
regenerate_certs
doctor_ok "Certificates" "regenerated (fixed)"
fi
elif [ "$days_left" -lt 30 ]; then
doctor_warn "Certificates" "expiring in ${days_left} days"
if doctor_ask_fix "Regenerate certificates?"; then