-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4433_domain.sh
More file actions
1044 lines (956 loc) · 32.3 KB
/
4433_domain.sh
File metadata and controls
1044 lines (956 loc) · 32.3 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
# 4433.sh — Standard Notes all-in-one installer/manager (HTTPS @ https://HOST:PORT)
# Requires: Docker + (docker compose OR docker-compose)
# Stack: monolith server (API+Files), MySQL, Redis, LocalStack, optional Web, Nginx TLS
#
# What’s new in this build
# - **Interactive domain prompt (20s timeout):** asks for a domain at install; if blank/timeout, falls back to IP mode.
# - **COOKIE_DOMAIN when a domain is provided:** fixes the login refresh-cookie loop on modern SN server builds.
# - **IP-first default** preserved: if you skip domain input, it behaves like the old IP-only installer (may show auth loop in some browsers).
# - Reuse of existing features: origin reconfigure, cert SANs (IP + optional domain), one-click reissue, etc.
#
set -euo pipefail
# -----------------------------
# Pretty printing
# -----------------------------
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
print_success() { echo -e "${GREEN}✓ $1${NC}"; }
print_error() { echo -e "${RED}✗ $1${NC}"; }
print_info() { echo -e "${YELLOW}➜ $1${NC}"; }
# -----------------------------
# Defaults
# -----------------------------
PROJECT_DIR="/root/standard-notes"
NETWORK_NAME="standard-notes"
# ============ PROJECT_DIR Configuration ============
echo -n "Enter PROJECT_DIR (default: /root/standard-notes) [7s]: "
if read -t 7 PROJECT_DIR && [ -n "$PROJECT_DIR" ]; then
echo "Using: $PROJECT_DIR"
else
PROJECT_DIR="/root/standard-notes"
echo -e "\nUsing default: $PROJECT_DIR"
fi
# Check if directory is not empty
if [ -d "$PROJECT_DIR" ] && [ "$(ls -A "$PROJECT_DIR" 2>/dev/null)" ]; then
echo "⚠ Directory exists and is NOT empty!"
echo "1) Continue 2) Remove & recreate 3) New path 4) Abort"
read -p "Choice [1-4]: " choice
case $choice in
2) rm -rf "$PROJECT_DIR" ;;
3) read -p "New PROJECT_DIR: " PROJECT_DIR ;;
4) exit 1 ;;
esac
fi
## ============ NETWORK_NAME Configuration ============
#echo -n "Enter NETWORK_NAME (default: standard-notes) [7s]: "
#if read -t 7 NETWORK_NAME && [ -n "$NETWORK_NAME" ]; then
# echo "Using: $NETWORK_NAME"
#else
# NETWORK_NAME="standard-notes"
# echo -e "\nUsing default: $NETWORK_NAME"
#fi
## Check if network exists
#if docker network ls --format '{{.Name}}' 2>/dev/null | grep -q "^${NETWORK_NAME}$"; then
# echo "⚠ Network '$NETWORK_NAME' already exists!"
# echo "1) Use existing 2) Remove & recreate 3) New name 4) Abort"
# read -p "Choice [1-4]: " choice
# case $choice in
# 2) docker network rm "$NETWORK_NAME" 2>/dev/null || {
# echo "Failed to remove"
# exit 1
# } ;;
# 3) read -p "New NETWORK_NAME: " NETWORK_NAME ;;
# 4) exit 1 ;;
# esac
#fi
API_PORT_IN=3000
FILES_PORT_IN=3104 # Internal files port (externally usually 3125)
WEB_PORT_IN=80
HTTPS_PORT_OUT=4433
DB_PORT_OUT=3306
REDIS_PORT_OUT=6379
LOCALSTACK_PORT_OUT=4566
DOMAIN_HOST="" # set by prompt_domain() if user enters a domain
# -----------------------------
# Compose wrapper
# -----------------------------
compose_cmd() {
if docker compose version >/dev/null 2>&1; then
echo "docker compose"
elif command -v docker-compose >/dev/null 2>&1; then
echo "docker-compose"
else
print_error "Docker Compose not found. Install Docker Compose."
exit 1
fi
}
# -----------------------------
# Pre-flight checks
# -----------------------------
require_root() {
if [ "${EUID:-0}" -ne 0 ]; then
print_error "Run as root (sudo)."
exit 1
fi
}
require_docker() {
if ! command -v docker >/dev/null 2>&1; then
print_error "Docker not found. Install Docker."
exit 1
fi
}
# -----------------------------
# IP / Host detection (PUBLIC-IP first)
# -----------------------------
detect_internal_ip() {
local ips first
ips="$(hostname -I 2>/dev/null || true)" || true
set -- $ips
first="${1:-}"
if [ -z "$first" ]; then first="127.0.0.1"; fi
echo "$first"
}
is_ip() { [[ "$1" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; }
_detect_from_cloud() {
# Try common cloud metadata endpoints (fast timeouts)
# GCP
curl -fsS --max-time 1 -H "Metadata-Flavor: Google" \
"http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip" 2>/dev/null || true
# AWS
curl -fsS --max-time 1 "http://169.254.169.254/latest/meta-data/public-ipv4" 2>/dev/null || true
# Azure
curl -fsS --max-time 1 -H "Metadata:true" \
"http://169.254.169.254/metadata/instance/network/interface/0/ipv4/ipAddress/0/publicIpAddress?api-version=2021-02-01&format=text" 2>/dev/null || true
}
_detect_from_dns() {
# OpenDNS
if command -v dig >/dev/null 2>&1; then
dig +short myip.opendns.com @resolver1.opendns.com 2>/dev/null || true
fi
}
_detect_from_http() {
# ipify / checkip (HTTP to avoid TLS issues on minimal hosts; fallback to HTTPS if needed)
curl -fsS --max-time 2 http://api.ipify.org 2>/dev/null ||
curl -fsS --max-time 2 http://checkip.amazonaws.com 2>/dev/null ||
curl -fsS --max-time 2 https://api.ipify.org 2>/dev/null ||
curl -fsS --max-time 2 https://checkip.amazonaws.com 2>/dev/null || true
}
pick_first_ip() {
awk 'BEGIN{FS="[[:space:]]+"} {for(i=1;i<=NF;i++){if($i ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/){print $i; exit}}}'
}
_detect_public_from_ifaces() {
# Best-effort: look at non-RFC1918 addresses on interfaces
if command -v ip >/dev/null 2>&1; then
ip -br -4 addr show 2>/dev/null | awk '{for(i=1;i<=NF;i++){if($i~"[0-9]+/[0-9]+"){gsub("/.*","",$i); print $i}}}' |
awk '!($0 ~ /^(10\.|192\.168\.|172\.(1[6-9]|2[0-9]|3[0-1])\.|127\.|169\.254\.)/) {print; exit}' || true
fi
}
detect_public_ip() {
local ip
# 1) Cloud metadata
ip=$(_detect_from_cloud | pick_first_ip)
if [ -n "${ip:-}" ] && is_ip "$ip"; then
echo "$ip"
return 0
fi
# 2) DNS
ip=$(_detect_from_dns | pick_first_ip)
if [ -n "${ip:-}" ] && is_ip "$ip"; then
echo "$ip"
return 0
fi
# 3) HTTP echo
ip=$(_detect_from_http | pick_first_ip)
if [ -n "${ip:-}" ] && is_ip "$ip"; then
echo "$ip"
return 0
fi
# 4) Non-RFC1918 iface address
ip=$(_detect_public_from_ifaces)
if [ -n "${ip:-}" ] && is_ip "$ip"; then
echo "$ip"
return 0
fi
# 5) Fall back to internal
detect_internal_ip
}
# -----------------------------
# Existing compose introspection
# -----------------------------
get_current_https_port() {
local compose="${PROJECT_DIR}/docker-compose.yml"
local port=""
if [ -f "$compose" ]; then
port="$(grep -E '^\s*-\s*"?[0-9]+:3001"?' "$compose" 2>/dev/null | sed -E 's/.*"?([0-9]+):3001"?.*/\1/' | head -n1 || true)"
fi
if [ -n "$port" ]; then echo "$port"; else echo "$HTTPS_PORT_OUT"; fi
}
get_current_origin_host() {
# Extract host used in PUBLIC_ORIGIN=https://HOST:PORT
local compose="${PROJECT_DIR}/docker-compose.yml"
[ -f "$compose" ] || {
echo ""
return 0
}
local line host
line="$(grep -E 'PUBLIC_ORIGIN=https?://[^ "]+' "$compose" | head -n1 || true)"
host="${line#*=https://}"
host="${host%%:*}"
echo "$host"
}
# -----------------------------
# Prompts
# -----------------------------
prompt_https_port() {
local input=""
echo -ne "${YELLOW}➜ External HTTPS port [default 4433] (5s to respond): ${NC}"
if read -t 5 -r input; then :; else input=""; fi
if [[ "$input" =~ ^[0-9]+$ ]] && [ "$input" -ge 1 ] && [ "$input" -le 65535 ]; then
HTTPS_PORT_OUT="$input"
else
HTTPS_PORT_OUT=4433
fi
print_info "Using external HTTPS port: ${HTTPS_PORT_OUT}"
}
is_domain_like() {
# simple domain check: letters/digits/dots/dashes and at least one dot
[[ "$1" =~ ^[A-Za-z0-9.-]+$ ]] && [[ "$1" == *.* ]]
}
prompt_domain() {
local input=""
echo -ne "${YELLOW}➜ Optional domain (e.g., notes.example.com) [20s to respond, blank = use IP]: ${NC}"
if read -t 20 -r input; then :; else input=""; fi
if [ -n "$input" ]; then
if is_domain_like "$input"; then
DOMAIN_HOST="$input"
print_info "Will use domain: ${DOMAIN_HOST} (COOKIE_DOMAIN will be set)"
else
print_error "Invalid domain syntax. Falling back to IP mode."
DOMAIN_HOST=""
fi
else
print_info "No domain provided; using IP mode (may cause auth loop in some browsers)."
fi
}
# -----------------------------
# Core write helpers
# -----------------------------
write_env() {
local host="$1"
shift
cat >.env <<EOF
###### # DB # ######
DB_HOST=db
DB_PORT=3306
DB_USERNAME=std_notes_user
DB_PASSWORD=${DB_PASSWORD}
DB_DATABASE=standard_notes_db
DB_TYPE=mysql
######### # CACHE # #########
REDIS_PORT=6379
REDIS_HOST=cache
CACHE_TYPE=redis
######## # KEYS # ########
AUTH_JWT_SECRET=${AUTH_JWT_SECRET}
AUTH_SERVER_ENCRYPTION_SERVER_KEY=${AUTH_SERVER_ENCRYPTION_SERVER_KEY}
VALET_TOKEN_SECRET=${VALET_TOKEN_SECRET}
# Public files URL via Nginx on https://${host}:${HTTPS_PORT_OUT}
PUBLIC_FILES_SERVER_URL=https://${host}:${HTTPS_PORT_OUT}/files
# Explicitly allow registrations
AUTH_SERVER_DISABLE_USER_REGISTRATION=false
EOF
}
write_nginx_conf() {
mkdir -p nginx
cat >nginx/nginx.conf <<'EOF'
events {}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
server_tokens off;
gzip on;
gzip_types text/plain text/css application/json application/javascript application/xml+rss application/xml text/javascript image/svg+xml;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 3001 ssl;
http2 on;
server_name _;
ssl_certificate /etc/nginx/certs/server.crt;
ssl_certificate_key /etc/nginx/certs/server.key;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options SAMEORIGIN;
add_header Referrer-Policy no-referrer;
client_max_body_size 50m;
# Web app on /
location / {
proxy_pass http://web:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 300s;
}
# Sync API on /api -> server:3000
location ^~ /api/ {
rewrite ^/api/?(.*)$ /$1 break;
proxy_pass http://server:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 300s;
}
# Files API on /files -> server:3104 (exposed externally as /files)
location ^~ /files/ {
rewrite ^/files/?(.*)$ /$1 break;
proxy_pass http://server:3104;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache off;
proxy_read_timeout 300s;
}
}
}
EOF
}
write_localstack_bootstrap() {
cat >localstack_bootstrap.sh <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
LOCALSTACK_HOST=localhost
AWS_REGION=us-east-1
LOCALSTACK_DUMMY_ID=000000000000
get_all_queues(){ awslocal --endpoint-url=http://${LOCALSTACK_HOST}:4566 sqs list-queues; }
create_queue(){ local n=$1; awslocal --endpoint-url=http://${LOCALSTACK_HOST}:4566 sqs create-queue --queue-name ${n}; }
get_all_topics(){ awslocal --endpoint-url=http://${LOCALSTACK_HOST}:4566 sns list-topics; }
create_topic(){ local n=$1; awslocal --endpoint-url=http://${LOCALSTACK_HOST}:4566 sns create-topic --name ${n}; }
link_queue_and_topic(){ local t=$1 q=$2; awslocal --endpoint-url=http://${LOCALSTACK_HOST}:4566 sns subscribe --topic-arn ${t} --protocol sqs --notification-endpoint ${q}; }
get_queue_arn_from_name(){ local n=$1; echo "arn:aws:sns:${AWS_REGION}:${LOCALSTACK_DUMMY_ID}:$n"; }
get_topic_arn_from_name(){ local n=$1; echo "arn:aws:sns:${AWS_REGION}:${LOCALSTACK_DUMMY_ID}:$n"; }
PAYMENTS_TOPIC_NAME="payments-local-topic"; create_topic ${PAYMENTS_TOPIC_NAME}; PAYMENTS_TOPIC_ARN=$(get_topic_arn_from_name $PAYMENTS_TOPIC_NAME)
SYNCING_SERVER_TOPIC_NAME="syncing-server-local-topic"; create_topic ${SYNCING_SERVER_TOPIC_NAME}; SYNCING_SERVER_TOPIC_ARN=$(get_topic_arn_from_name $SYNCING_SERVER_TOPIC_NAME)
AUTH_TOPIC_NAME="auth-local-topic"; create_topic ${AUTH_TOPIC_NAME}; AUTH_TOPIC_ARN=$(get_topic_arn_from_name $AUTH_TOPIC_NAME)
FILES_TOPIC_NAME="files-local-topic"; create_topic ${FILES_TOPIC_NAME}; FILES_TOPIC_ARN=$(get_topic_arn_from_name $FILES_TOPIC_NAME)
ANALYTICS_TOPIC_NAME="analytics-local-topic"; create_topic ${ANALYTICS_TOPIC_NAME}; ANALYTICS_TOPIC_ARN=$(get_topic_arn_from_name $ANALYTICS_TOPIC_NAME)
REVISIONS_TOPIC_NAME="revisions-server-local-topic"; create_topic ${REVISIONS_TOPIC_NAME}; REVISIONS_TOPIC_ARN=$(get_topic_arn_from_name $REVISIONS_TOPIC_NAME)
SCHEDULER_TOPIC_NAME="scheduler-local-topic"; create_topic ${SCHEDULER_TOPIC_NAME}; SCHEDULER_TOPIC_ARN=$(get_topic_arn_from_name $SCHEDULER_TOPIC_NAME)
QUEUE_NAME="analytics-local-queue"; create_queue ${QUEUE_NAME}; ANALYTICS_QUEUE_ARN=$(get_queue_arn_from_name $QUEUE_NAME); link_queue_and_topic $PAYMENTS_TOPIC_ARN $ANALYTICS_QUEUE_ARN
QUEUE_NAME="auth-local-queue"; create_queue ${QUEUE_NAME}; AUTH_QUEUE_ARN=$(get_queue_arn_from_name $QUEUE_NAME); link_queue_and_topic $PAYMENTS_TOPIC_ARN $AUTH_QUEUE_ARN; link_queue_and_topic $AUTH_TOPIC_ARN $AUTH_QUEUE_ARN; link_queue_and_topic $FILES_TOPIC_ARN $AUTH_QUEUE_ARN; link_queue_and_topic $REVISIONS_TOPIC_ARN $AUTH_QUEUE_ARN
QUEUE_NAME="files-local-queue"; create_queue ${QUEUE_NAME}; FILES_QUEUE_ARN=$(get_queue_arn_from_name $QUEUE_NAME); link_queue_and_topic $AUTH_TOPIC_ARN $FILES_QUEUE_ARN; link_queue_and_topic $SYNCING_SERVER_TOPIC_ARN $FILES_QUEUE_ARN
QUEUE_NAME="syncing-server-local-queue"; create_queue ${QUEUE_NAME}; SYNCING_SERVER_QUEUE_ARN=$(get_queue_arn_from_name $QUEUE_NAME); link_queue_and_topic $SYNCING_SERVER_TOPIC_ARN $SYNCING_SERVER_QUEUE_ARN; link_queue_and_topic $FILES_TOPIC_ARN $SYNCING_SERVER_QUEUE_ARN; link_queue_and_topic $SYNCING_SERVER_TOPIC_ARN $AUTH_QUEUE_ARN; link_queue_and_topic $AUTH_TOPIC_ARN $SYNCING_SERVER_QUEUE_ARN
QUEUE_NAME="revisions-server-local-queue"; create_queue ${QUEUE_NAME}; REVISIONS_QUEUE_ARN=$(get_queue_arn_from_name $QUEUE_NAME); link_queue_and_topic $SYNCING_SERVER_TOPIC_ARN $REVISIONS_QUEUE_ARN; link_queue_and_topic $REVISIONS_TOPIC_ARN $REVISIONS_QUEUE_ARN
QUEUE_NAME="scheduler-local-queue"; create_queue ${QUEUE_NAME}; SCHEDULER_QUEUE_ARN=$(get_queue_arn_from_name $QUEUE_NAME)
echo "all topics are:"; get_all_topics
echo "all queues are:"; get_all_queues
EOF
chmod +x localstack_bootstrap.sh
}
write_web_entry() {
mkdir -p web-entry
cat >web-entry/50-set-sync-server.sh <<'EOF'
#!/usr/bin/env sh
set -eu
HTML_DIR="/usr/share/nginx/html"
TARGET="${DEFAULT_SYNC_SERVER:-${NEXT_PUBLIC_DEFAULT_SYNC_SERVER:-${SF_DEFAULT_SERVER:-${SF_NEXT_VERSION_SERVER:-}}}}"
if [ -z "${TARGET:-}" ] && [ -n "${PUBLIC_ORIGIN:-}" ]; then
TARGET="${PUBLIC_ORIGIN%/}/api"
fi
if [ -z "${TARGET:-}" ]; then
echo "[web-init] No DEFAULT_SYNC_SERVER provided; leaving app defaults."
exit 0
fi
echo "[web-init] Forcing DEFAULT_SYNC_SERVER to ${TARGET}"
for f in "${HTML_DIR}"/*.js; do
[ -f "$f" ] || continue
sed -i "s#https://api\\.standardnotes\\.com#${TARGET}#g" "$f" || true
sed -i "s#https://sync\\.standardnotes\\.org#${TARGET}#g" "$f" || true
sed -i "s#https://sync\\.standardnotes\\.com#${TARGET}#g" "$f" || true
# also patch files host if present (optional)
if [ -n "${PUBLIC_ORIGIN:-}" ]; then
FILES_HOST="${PUBLIC_ORIGIN%/}/files"
sed -i "s#https://files\\.standardnotes\\.com#${FILES_HOST}#g" "$f" || true
fi
done
EOF
chmod +x web-entry/50-set-sync-server.sh
}
write_compose() {
local host="$1" # domain or IP for PUBLIC_ORIGIN
local domain="${2:-}"
mkdir -p data/mysql data/redis uploads logs nginx certs
local cookie_line=""
if [ -n "$domain" ]; then
cookie_line=" - COOKIE_DOMAIN=${domain}"
fi
cat >docker-compose.yml <<EOF
services:
db:
image: mysql:8.0
deploy:
resources:
limits:
memory: 2G # MySQL 8.0 needs decent RAM
# container_name: standard-notes-db
restart: unless-stopped
command: --default-authentication-plugin=mysql_native_password --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
environment:
MYSQL_DATABASE: \${DB_DATABASE}
MYSQL_USER: \${DB_USERNAME}
MYSQL_PASSWORD: \${DB_PASSWORD}
MYSQL_ROOT_PASSWORD: \${DB_PASSWORD}
# ports:
# - "${DB_PORT_OUT}:3306"
volumes:
- ./data/mysql:/var/lib/mysql
networks: [ ${NETWORK_NAME} ]
cache:
image: redis:7-alpine
deploy:
resources:
limits:
memory: 512M # Redis is efficient
# container_name: standard-notes-cache
restart: unless-stopped
command: redis-server --appendonly yes
# ports:
# - "${REDIS_PORT_OUT}:6379"
volumes:
- ./data/redis:/data
networks: [ ${NETWORK_NAME} ]
localstack:
image: localstack/localstack:3.0
deploy:
resources:
limits:
memory: 1G # AWS emulator can be heavy
# container_name: standard-notes-localstack
restart: unless-stopped
environment:
- SERVICES=sns,sqs
- HOSTNAME_EXTERNAL=localstack
- LS_LOG=warn
# ports:
# - "${LOCALSTACK_PORT_OUT}:${LOCALSTACK_PORT_OUT}"
volumes:
- ./localstack_bootstrap.sh:/etc/localstack/init/ready.d/localstack_bootstrap.sh
networks: [ ${NETWORK_NAME} ]
server:
image: standardnotes/server
deploy:
resources:
limits:
memory: 2G # The main backend app
# container_name: standard-notes-server
restart: unless-stopped
env_file: .env
environment:
- AUTH_SERVER_DISABLE_USER_REGISTRATION=\${AUTH_SERVER_DISABLE_USER_REGISTRATION}
${cookie_line}
volumes:
- ./logs:/var/lib/server/logs
- ./uploads:/opt/server/packages/files/dist/uploads
networks: [ ${NETWORK_NAME} ]
depends_on:
- db
- cache
- localstack
web:
image: standardnotes/web
deploy:
resources:
limits:
memory: 1G # Frontend
# container_name: standard-notes-web
restart: unless-stopped
environment:
- PUBLIC_ORIGIN=https://${host}:${HTTPS_PORT_OUT}
- DEFAULT_SYNC_SERVER=https://${host}:${HTTPS_PORT_OUT}/api
- NEXT_PUBLIC_DEFAULT_SYNC_SERVER=https://${host}:${HTTPS_PORT_OUT}/api
- SF_DEFAULT_SERVER=https://${host}:${HTTPS_PORT_OUT}/api
- SF_NEXT_VERSION_SERVER=https://${host}:${HTTPS_PORT_OUT}/api
volumes:
- ./web-entry:/docker-entrypoint.d
networks: [ ${NETWORK_NAME} ]
depends_on:
- server
nginx:
image: nginx:1.25-alpine
deploy:
resources:
limits:
memory: 256M
# container_name: standard-notes-proxy
depends_on:
- web
- server
ports:
- "${HTTPS_PORT_OUT}:3001"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./certs:/etc/nginx/certs:ro
networks: [ ${NETWORK_NAME} ]
networks:
${NETWORK_NAME}:
driver: bridge
EOF
}
# -----------------------------
# TLS cert (SANs: localhost + public IP + optional domain)
# -----------------------------
make_cert() {
local ip="$1"
shift
local domain="${1:-}"
shift || true
cat >nginx/openssl.cnf <<EOF
[ req ]
default_bits = 2048
prompt = no
default_md = sha256
req_extensions = v3_req
distinguished_name = dn
[ dn ]
CN = ${domain:-${ip}}
O = Self-Hosted Standard Notes
[ v3_req ]
subjectAltName = @alt_names
[ alt_names ]
IP.1 = ${ip}
DNS.1 = localhost
EOF
if [ -n "${domain}" ]; then
# Insert DNS.2 only if domain provided
sed -i '/DNS.1 = localhost/a DNS.2 = '"${domain}" nginx/openssl.cnf
fi
openssl req -x509 -nodes -days 825 -newkey rsa:2048 \
-keyout certs/server.key \
-out certs/server.crt \
-config nginx/openssl.cnf >/dev/null 2>&1
}
# -----------------------------
# Install flow
# -----------------------------
install_stack() {
require_root
require_docker
local DCMD
DCMD=$(compose_cmd)
# Prefer PUBLIC IP
local PUB_IP
PUB_IP=$(detect_public_ip)
prompt_https_port
prompt_domain
local HOST_CHOICE
if [ -n "${DOMAIN_HOST}" ]; then
HOST_CHOICE="${DOMAIN_HOST}"
else
HOST_CHOICE="${PUB_IP}"
fi
print_info "Setting up Standard Notes at ${PROJECT_DIR} (host: ${HOST_CHOICE})"
if [ -d "$PROJECT_DIR" ]; then
print_info "Existing install found. Stopping & removing..."
(cd "$PROJECT_DIR" && $DCMD down -v || true)
rm -rf "$PROJECT_DIR"
fi
mkdir -p "$PROJECT_DIR"/{data/mysql,data/redis,uploads,logs,nginx,certs,web-entry}
cd "$PROJECT_DIR"
print_success "Directory structure created"
print_info "Generating secrets..."
AUTH_JWT_SECRET=$(openssl rand -hex 32)
AUTH_SERVER_ENCRYPTION_SERVER_KEY=$(openssl rand -hex 32)
VALET_TOKEN_SECRET=$(openssl rand -hex 32)
DB_PASSWORD=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-25)
print_success "Secrets generated"
print_info "Writing .env..."
write_env "$HOST_CHOICE"
print_success ".env written"
print_info "Generating self-signed TLS cert (SANs: ${PUB_IP}, localhost${DOMAIN_HOST:+, ${DOMAIN_HOST}})..."
write_nginx_conf
make_cert "$PUB_IP" "${DOMAIN_HOST:-}"
print_success "Cert created at certs/server.crt (import it into your OS trust store)"
print_info "Adding web runtime entry to force local sync server..."
write_web_entry
print_success "Web runtime entry added"
print_info "Writing docker-compose.yml..."
write_compose "$HOST_CHOICE" "${DOMAIN_HOST:-}"
print_success "docker-compose.yml written"
print_info "Writing LocalStack bootstrap..."
write_localstack_bootstrap
print_success "LocalStack bootstrap created"
print_info "Pulling images..."
$DCMD pull
print_success "Images pulled"
print_info "Starting services..."
$DCMD up -d
print_info "Waiting for API to respond through Nginx (treating 2xx–4xx as ready; ignoring 5xx/502)..."
wait_for_api_ready "https://localhost:$(get_current_https_port)/api" 180 || true
$DCMD ps
echo
print_success "Installed! Visit: https://${HOST_CHOICE}:${HTTPS_PORT_OUT}"
echo "API: https://${HOST_CHOICE}:${HTTPS_PORT_OUT}/api"
echo "Files: https://${HOST_CHOICE}:${HTTPS_PORT_OUT}/files"
echo
print_info "If the browser complains, import ${PROJECT_DIR}/certs/server.crt into your OS trust store."
test_api
}
# -----------------------------
# Helper: API readiness (accept 2xx–4xx; reject 5xx/000)
# -----------------------------
wait_for_api_ready() {
local url="${1:?}"
local timeout="${2:-120}"
local start now code
start=$(date +%s)
while true; do
code=$(curl -sk -o /dev/null -w "%{http_code}" "$url" || true)
if [ -n "$code" ] && [ "$code" != "000" ] && [ "$code" -ge 200 ] && [ "$code" -lt 500 ]; then
print_success "API responded at $url (HTTP ${code})."
return 0
fi
now=$(date +%s)
if [ $((now - start)) -ge "$timeout" ]; then
print_info "API not ready after ${timeout}s; continuing (you can check logs)."
return 1
fi
sleep 3
done
}
# -----------------------------
# Manager actions
# -----------------------------
start_stack() {
local DCMD
DCMD=$(compose_cmd)
cd "$PROJECT_DIR"
$DCMD up -d
print_success "Started"
}
stop_stack() {
local DCMD
DCMD=$(compose_cmd)
cd "$PROJECT_DIR"
$DCMD down
print_success "Stopped"
}
restart_stack() {
local DCMD
DCMD=$(compose_cmd)
cd "$PROJECT_DIR"
$DCMD restart
print_success "Restarted"
}
status_stack() {
local DCMD
DCMD=$(compose_cmd)
cd "$PROJECT_DIR"
$DCMD ps
}
logs_stack() {
local DCMD
DCMD=$(compose_cmd)
cd "$PROJECT_DIR"
if [ "${1:-}" != "" ]; then $DCMD logs -f "$1"; else $DCMD logs -f; fi
}
backup_stack() {
local DCMD
DCMD=$(compose_cmd)
cd "$PROJECT_DIR"
mkdir -p backups
local ts="backups/$(date +%Y%m%d_%H%M%S)"
mkdir -p "$ts"
# shellcheck disable=SC1091
. ./.env
print_info "Dumping database..."
$DCMD exec -T db mysqldump -u${DB_USERNAME} -p${DB_PASSWORD} ${DB_DATABASE} >"$ts/database.sql"
cp -r ./uploads "$ts/uploads"
cp .env "$ts/.env"
print_success "Backup complete: $ts"
}
clean_stack() {
local DCMD
DCMD=$(compose_cmd)
cd "$PROJECT_DIR"
echo "WARNING: Deletes ALL data under $PROJECT_DIR/data and uploads/logs!"
read -p "Type 'yes' to continue: " y
if [ "$y" != "yes" ]; then
echo "Cancelled."
return 0
fi
$DCMD down -v
rm -rf data/mysql/* data/redis/* uploads/* logs/*
print_success "Data cleaned."
}
reissue_cert() {
require_root
cd "$PROJECT_DIR"
local domain="${1:-}"
shift || true
local PUB_IP
PUB_IP=$(detect_public_ip)
print_info "Reissuing self-signed cert for IP ${PUB_IP}${domain:+ and domain ${domain}}..."
make_cert "$PUB_IP" "${domain:-}"
print_success "New cert generated at certs/server.crt"
restart_stack
echo "Trust the new certificate on your devices."
}
# Update origin to PUBLIC IP (default) or keep domain if configured
update_ip() {
require_root
local DCMD
DCMD=$(compose_cmd)
cd "$PROJECT_DIR"
local NEW_IP
NEW_IP=$(detect_public_ip)
local CUR_PORT
CUR_PORT=$(get_current_https_port)
local CUR_HOST
CUR_HOST=$(get_current_origin_host)
print_info "Detected public IP: ${NEW_IP}"
# If CUR_HOST is a domain (contains letters), keep domain for origin but include NEW_IP in cert SANs
if echo "$CUR_HOST" | grep -Eq '[A-Za-z]'; then
print_info "Compose currently uses domain '${CUR_HOST}'. Keeping it and updating cert SANs with IP ${NEW_IP}."
reissue_cert "${CUR_HOST}"
# Ensure .env PUBLIC_FILES_SERVER_URL continues to use the domain
sed -i "s|^PUBLIC_FILES_SERVER_URL=.*|PUBLIC_FILES_SERVER_URL=https://${CUR_HOST}:${CUR_PORT}/files|" .env
$DCMD down
$DCMD up -d
sleep 8
echo "Access (domain): https://${CUR_HOST}:${CUR_PORT}"
echo "Access (IP): https://${NEW_IP}:${CUR_PORT}"
return 0
fi
print_info "Updating configs to use IP ${NEW_IP}..."
# Update .env
sed -i "s|^PUBLIC_FILES_SERVER_URL=.*|PUBLIC_FILES_SERVER_URL=https://${NEW_IP}:${CUR_PORT}/files|" .env
# Update docker-compose.yml web envs
sed -i -E "s#(PUBLIC_ORIGIN=)https://[^:]+:[0-9]+#\1https://${NEW_IP}:${CUR_PORT}#g" docker-compose.yml
sed -i -E "s#(DEFAULT_SYNC_SERVER=)https://[^:]+:[0-9]+/api#\1https://${NEW_IP}:${CUR_PORT}/api#g" docker-compose.yml
sed -i -E "s#(NEXT_PUBLIC_DEFAULT_SYNC_SERVER=)https://[^:]+:[0-9]+/api#\1https://${NEW_IP}:${CUR_PORT}/api#g" docker-compose.yml
sed -i -E "s#(SF_DEFAULT_SERVER=)https://[^:]+:[0-9]+/api#\1https://${NEW_IP}:${CUR_PORT}/api#g" docker-compose.yml
sed -i -E "s#(SF_NEXT_VERSION_SERVER=)https://[^:]+:[0-9]+/api#\1https://${NEW_IP}:${CUR_PORT}/api#g" docker-compose.yml
# Remove COOKIE_DOMAIN in IP mode (if present)
sed -i '/COOKIE_DOMAIN=/d' docker-compose.yml || true
# Reissue cert for new IP
make_cert "$NEW_IP" ""
print_success "IP updated to ${NEW_IP}"
$DCMD down
$DCMD up -d
sleep 8
echo "Access: https://${NEW_IP}:${CUR_PORT}"
}
# Absorb fix.sh: set origin/port to a DOMAIN (optionally specify IP to include in cert)
configure_origin() {
require_root
local host="" ip="" port="$(get_current_https_port)"
while [ $# -gt 0 ]; do
case "$1" in
--host)
host="${2:-}"
shift 2
;;
--ip)
ip="${2:-}"
shift 2
;;
--port)
port="${2:-}"
shift 2
;;
*)
print_error "Unknown option: $1"
exit 1
;;
esac
done
if [ -z "$host" ]; then
print_error "--host is required (e.g., notes.example.com)"
exit 1
fi
if [ -z "$ip" ]; then ip=$(detect_public_ip); fi
if ! is_ip "$ip"; then
print_error "Invalid IP to embed in cert SANs: '$ip'"
exit 1
fi
local DCMD
DCMD=$(compose_cmd)
cd "$PROJECT_DIR"
print_info "Stopping stack..."
$DCMD down || true
print_info "Updating .env/.yml to host=${host}, port=${port}"
sed -i "s|^PUBLIC_FILES_SERVER_URL=.*|PUBLIC_FILES_SERVER_URL=https://${host}:${port}/files|" .env
sed -i -E "s#(PUBLIC_ORIGIN=)https://[^\"]+#\1https://${host}:${port}#" docker-compose.yml
sed -i -E "s#(DEFAULT_SYNC_SERVER=)https://[^\"]+/api#\1https://${host}:${port}/api#" docker-compose.yml
sed -i -E "s#(NEXT_PUBLIC_DEFAULT_SYNC_SERVER=)https://[^\"]+/api#\1https://${host}:${port}/api#" docker-compose.yml
sed -i -E "s#(SF_DEFAULT_SERVER=)https://[^\"]+/api#\1https://${host}:${port}/api#" docker-compose.yml
sed -i -E "s#(SF_NEXT_VERSION_SERVER=)https://[^\"]+/api#\1https://${host}:${port}/api#" docker-compose.yml
# Ensure COOKIE_DOMAIN is set for domain mode
sed -i '/COOKIE_DOMAIN=/d' docker-compose.yml || true
sed -i '/AUTH_SERVER_DISABLE_USER_REGISTRATION=/a\ - COOKIE_DOMAIN='"${host}" docker-compose.yml
print_info "Regenerating self-signed cert with SANs: IP=${ip}, DNS=${host}"
make_cert "$ip" "$host"
print_info "Starting stack..."
$DCMD up -d
echo "Access: https://${host}:${port} (DNS)"
echo "Access: https://${ip}:${port} (IP)"
}
# -----------------------------
# Uninstall
# -----------------------------
uninstall_stack() {
local DCMD
DCMD=$(compose_cmd)
echo "WARNING: This will STOP and REMOVE ${PROJECT_DIR} entirely."
read -p "Type 'delete' to proceed: " conf
if [ "$conf" != "delete" ]; then
echo "Cancelled."
return 0
fi
if [ -d "$PROJECT_DIR" ]; then
(cd "$PROJECT_DIR" && $DCMD down -v || true)
(cd /root && rm -rf "$PROJECT_DIR")
fi
print_success "Uninstalled."
}
# -----------------------------
# API probe (lightweight built-in)
# -----------------------------
test_api() {
echo
print_info "Testing via Nginx proxy (HTTPS, self-signed; -k used)..."
local port base code
port=$(get_current_https_port)
base="https://localhost:${port}"
for p in "/api" "/files" "/"; do
code=$(curl -sk -o /dev/null -w "%{http_code}" "${base}${p}" || true)
if [ -n "$code" ] && [ "$code" != "000" ]; then
print_success "Proxy responded at ${p} (HTTP ${code})."
else
print_error "No response at ${p}."
fi
done
}
# -----------------------------
# Menu / CLI
# -----------------------------
show_menu() {
cat <<MENU
Standard Notes Manager (IP-first with optional domain)
=====================================================
1) Install (prompts for domain; defaults to IP)
2) Start
3) Stop
4) Restart
5) Status
6) Logs (all)
7) Logs (choose service)
8) Backup
9) Clean data (DANGEROUS)
10) Update IP + reissue cert (keep domain if configured)
11) Reissue cert only (auto IP; add domain if provided)
12) Uninstall (DANGEROUS)
13) Configure origin (domain) — optional advanced tool
0) Exit
MENU
read -p "Select an option: " choice
case "$choice" in
1) install_stack ;;
2) start_stack ;;
3) stop_stack ;;
4) restart_stack ;;
5) status_stack ;;
6) logs_stack ;;
7)
read -p "Service name (db|cache|localstack|server|web|nginx): " svc
logs_stack "$svc"
;;
8) backup_stack ;;
9) clean_stack ;;
10) update_ip ;;
11)
read -p "Optional domain for SAN (blank to skip): " d
reissue_cert "${d:-}"
;;
12) uninstall_stack ;;
13)
read -p "Domain (e.g., notes.example.com): " dom
read -p "Port [default $(get_current_https_port)]: " prt
prt="${prt:-$(get_current_https_port)}"
configure_origin --host "$dom" --port "$prt"
;;
0) exit 0 ;;
*) echo "Unknown choice" ;;
esac
}
usage() {
cat <<USAGE
Usage: $0 <command> [options]
Commands:
install Install the whole stack (prompts for domain; defaults to IP; prompts for HTTPS port)
start Start containers
stop Stop containers
restart Restart containers
status Show container status
logs [service] Tail logs (optionally one service)
backup Dump DB + uploads + .env under ./backups/
clean Stop & delete MySQL/Redis/uploads/logs data (DANGEROUS)
update-ip Detect public IP, update configs & cert; keep domain if configured