-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsetup-server.sh
More file actions
1145 lines (1008 loc) · 39.3 KB
/
Copy pathsetup-server.sh
File metadata and controls
1145 lines (1008 loc) · 39.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
#!/bin/bash
# Idempotent setup script for Ubuntu/Debian VPS
# Safe to run multiple times - only installs/configures what's missing
# Works on any VPS provider: DigitalOcean, Linode, Vultr, AWS EC2, Hetzner, etc.
#
# Run manually: bash scripts/setup-server.sh
# Or via GitHub Actions (runs automatically on deploy)
#
# Environment variables (set in .env file or export):
# VPS_USER - System user for the service (default: ubuntu)
# CERTBOT_EMAIL - Email for Let's Encrypt (default: admin@bittorrented.com)
# SSH_PORT - SSH port for firewall/fail2ban (default: 22)
# FORCE_SSL - Set to 1 to force SSL setup even if DNS check fails
set -e
# Load environment variables from .env file if it exists
# Uses a safe method that doesn't execute values as bash commands
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
if [ -f "${PROJECT_ROOT}/.env" ]; then
echo "=== Loading environment from .env ==="
# Only load simple KEY=value lines, skip comments and complex values
# This safely handles values with special characters like parentheses
while IFS='=' read -r key value; do
# Skip empty lines and comments
[[ -z "$key" || "$key" =~ ^[[:space:]]*# ]] && continue
# Remove leading/trailing whitespace from key
key=$(echo "$key" | xargs)
# Skip if key is empty after trimming
[[ -z "$key" ]] && continue
# Only export if it's a valid variable name (letters, numbers, underscore)
if [[ "$key" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
# Remove surrounding quotes from value if present
value="${value%\"}"
value="${value#\"}"
value="${value%\'}"
value="${value#\'}"
export "$key=$value"
fi
done < "${PROJECT_ROOT}/.env"
fi
# Configuration
DOMAIN="bittorrented.com"
REPO="media-streamer"
VPS_USER="${VPS_USER:-ubuntu}" # Override with env var for different providers
DEPLOY_PATH="/home/${VPS_USER}/www/${DOMAIN}/${REPO}"
SERVICE_NAME="bittorrented"
NODE_VERSION="22" # LTS version
PNPM_HOME="${HOME}/.local/share/pnpm"
CERTBOT_EMAIL="${CERTBOT_EMAIL:-admin@bittorrented.com}" # Override with env var if needed
SSH_PORT="${SSH_PORT:-22}" # Override with env var for custom SSH port (e.g., 2048)
echo "=== BitTorrented Server Setup (Idempotent) ==="
echo "Deploy path: ${DEPLOY_PATH}"
echo ""
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Configure swap if not already present (needed for memory-intensive builds)
SWAP_FILE="/swapfile"
if [ ! -f "$SWAP_FILE" ]; then
echo "=== Configuring swap (2GB) for builds ==="
sudo fallocate -l 2G "$SWAP_FILE"
sudo chmod 600 "$SWAP_FILE"
sudo mkswap "$SWAP_FILE"
sudo swapon "$SWAP_FILE"
# Make swap permanent
if ! grep -q "$SWAP_FILE" /etc/fstab; then
echo "$SWAP_FILE none swap sw 0 0" | sudo tee -a /etc/fstab
fi
echo "✓ Swap configured"
else
# Ensure swap is active
if ! swapon --show | grep -q "$SWAP_FILE"; then
sudo swapon "$SWAP_FILE" 2>/dev/null || true
fi
echo "=== Swap already configured ==="
fi
# Update system (only if not updated recently - within 1 hour)
LAST_UPDATE_FILE="/tmp/apt-last-update"
if [ ! -f "$LAST_UPDATE_FILE" ] || [ $(find "$LAST_UPDATE_FILE" -mmin +60 2>/dev/null) ]; then
echo "=== Updating system packages ==="
sudo apt-get update
touch "$LAST_UPDATE_FILE"
else
echo "=== System packages recently updated, skipping ==="
fi
# Install essential packages (only if missing)
echo "=== Checking essential packages ==="
PACKAGES_TO_INSTALL=""
for pkg in curl git build-essential ffmpeg rsync ufw fail2ban nginx certbot python3-certbot-nginx redis-server; do
if ! dpkg -l | grep -q "^ii $pkg "; then
PACKAGES_TO_INSTALL="$PACKAGES_TO_INSTALL $pkg"
fi
done
if [ -n "$PACKAGES_TO_INSTALL" ]; then
echo "Installing:$PACKAGES_TO_INSTALL"
sudo apt-get install -y $PACKAGES_TO_INSTALL
else
echo "All essential packages already installed"
fi
# Chromium runtime libraries for headless Puppeteer (used by news article
# extractor and SiriusXM device-grant minter). Puppeteer ships its own
# Chromium binary; this just makes sure the system shared libs it needs
# are present. Package names differ between Ubuntu 22.04 and 24.04, so
# probe before adding to the install list.
echo "=== Checking Puppeteer / Chromium runtime libraries ==="
PUPPETEER_CANDIDATES="ca-certificates fonts-liberation libatk-bridge2.0-0 libatk1.0-0 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxtst6 lsb-release wget xdg-utils"
# libasound2 was renamed to libasound2t64 in Ubuntu 24.04.
if apt-cache show libasound2t64 >/dev/null 2>&1; then
PUPPETEER_CANDIDATES="$PUPPETEER_CANDIDATES libasound2t64"
elif apt-cache show libasound2 >/dev/null 2>&1; then
PUPPETEER_CANDIDATES="$PUPPETEER_CANDIDATES libasound2"
fi
PUPPETEER_PACKAGES_TO_INSTALL=""
for pkg in $PUPPETEER_CANDIDATES; do
if ! dpkg -l | grep -q "^ii $pkg "; then
if apt-cache show "$pkg" >/dev/null 2>&1; then
PUPPETEER_PACKAGES_TO_INSTALL="$PUPPETEER_PACKAGES_TO_INSTALL $pkg"
fi
fi
done
if [ -n "$PUPPETEER_PACKAGES_TO_INSTALL" ]; then
echo "Installing Puppeteer runtime libraries:$PUPPETEER_PACKAGES_TO_INSTALL"
sudo apt-get install -y $PUPPETEER_PACKAGES_TO_INSTALL
else
echo "Puppeteer runtime libraries already present"
fi
# Enable and start Redis (for IPTV playlist caching)
echo "=== Configuring Redis ==="
sudo systemctl enable redis-server 2>/dev/null || true
sudo systemctl start redis-server 2>/dev/null || true
if systemctl is-active --quiet redis-server; then
echo "✓ Redis is running"
else
echo "WARNING: Redis failed to start"
fi
# Install Docker if not present (for Coturn STUN/TURN server)
if ! command_exists docker; then
echo "=== Installing Docker ==="
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker ${VPS_USER}
sudo systemctl enable docker
sudo systemctl start docker
echo "✓ Docker installed"
else
echo "=== Docker $(docker --version 2>/dev/null | head -1 || echo 'installed') already installed ==="
fi
# Install Docker Compose plugin if not present
if ! docker compose version >/dev/null 2>&1; then
echo "=== Installing Docker Compose plugin ==="
sudo apt-get install -y docker-compose-plugin
echo "✓ Docker Compose plugin installed"
else
echo "=== Docker Compose $(docker compose version 2>/dev/null | head -1 || echo 'installed') already installed ==="
fi
# Configure and start Coturn STUN/TURN server (for WebRTC NAT traversal)
echo "=== Configuring Coturn STUN/TURN server ==="
COTURN_DIR="${PROJECT_ROOT}/coturn"
COTURN_CONFIG="${COTURN_DIR}/turnserver.conf"
# Get server's public IP for TURN external-ip
SERVER_IP=$(curl -s ifconfig.me 2>/dev/null || curl -s icanhazip.com 2>/dev/null || echo "")
# Create coturn directory if it doesn't exist
if [ ! -d "${COTURN_DIR}" ]; then
mkdir -p "${COTURN_DIR}"
echo " Created ${COTURN_DIR} directory"
fi
# Create turnserver.conf if it doesn't exist
if [ ! -f "${COTURN_CONFIG}" ]; then
echo " Creating Coturn configuration file..."
# Generate a random secret if TURN_SECRET is not set
TURN_SECRET_VALUE="${TURN_SECRET:-$(openssl rand -hex 32)}"
TURN_REALM_VALUE="${TURN_REALM:-bittorrented.com}"
cat > "${COTURN_CONFIG}" << COTURN_EOF
# Coturn TURN/STUN Server Configuration
# For WebTorrent P2P streaming NAT traversal
# Auto-generated by setup-server.sh
# Network settings
listening-port=3478
tls-listening-port=5349
listening-ip=0.0.0.0
# External IP - server's public IP
external-ip=${SERVER_IP}
# Realm and server name
realm=${TURN_REALM_VALUE}
server-name=turn.${TURN_REALM_VALUE}
# Authentication using shared secret (time-limited credentials)
# This is more secure than static username/password
# The app generates time-limited credentials using HMAC-SHA1
use-auth-secret
static-auth-secret=${TURN_SECRET_VALUE}
# Disable static user accounts (use only time-limited credentials)
no-cli
# Performance and limits
# Total bandwidth quota in KB/s (0 = unlimited)
# 6TB/month = ~2.3 GB/day = ~27 MB/s average
# Set to 50000 KB/s (50 MB/s) to allow bursts while staying under limit
total-quota=50000
# Per-session bandwidth limit in bytes/sec (0 = unlimited)
# Limit each session to 5 MB/s to prevent single users from hogging bandwidth
bps-capacity=5000000
# Maximum allocations per user
user-quota=100
# Stale nonce lifetime in seconds
stale-nonce=600
# Maximum number of simultaneous TURN allocations
# Helps prevent resource exhaustion
max-allocate-lifetime=3600
# Relay settings
# Min/max ports for media relay (UDP)
min-port=49152
max-port=65535
# Enable fingerprinting for better NAT traversal
fingerprint
# Enable long-term credential mechanism
lt-cred-mech
# Logging
# Log to stdout for Docker
log-file=stdout
# Security settings
# Disable multicast peers
no-multicast-peers
# Disable TCP relay (WebRTC uses UDP)
no-tcp-relay
# Deny peers on loopback
denied-peer-ip=127.0.0.0-127.255.255.255
denied-peer-ip=::1
# Allow only UDP and TCP for client connections
# WebRTC primarily uses UDP
no-tlsv1
no-tlsv1_1
# Mobility (for mobile clients that change networks)
mobility
# Process settings
proc-user=nobody
proc-group=nogroup
COTURN_EOF
echo "✓ Created Coturn configuration at ${COTURN_CONFIG}"
echo " TURN_SECRET: ${TURN_SECRET_VALUE}"
echo " TURN_REALM: ${TURN_REALM_VALUE}"
echo " External IP: ${SERVER_IP}"
# Automatically add TURN variables to .env file
ENV_FILE="${PROJECT_ROOT}/.env"
echo ""
echo "=== Updating .env file with TURN configuration ==="
# Create .env from .env.example if it doesn't exist
if [ ! -f "${ENV_FILE}" ] && [ -f "${PROJECT_ROOT}/.env.example" ]; then
cp "${PROJECT_ROOT}/.env.example" "${ENV_FILE}"
echo " Created .env from .env.example"
fi
# Add or update TURN variables in .env
if [ -f "${ENV_FILE}" ]; then
# Function to add or update a variable in .env
update_env_var() {
local key="$1"
local value="$2"
if grep -q "^${key}=" "${ENV_FILE}" 2>/dev/null; then
# Update existing variable
sed -i "s|^${key}=.*|${key}=${value}|" "${ENV_FILE}"
echo " Updated ${key} in .env"
else
# Add new variable
echo "${key}=${value}" >> "${ENV_FILE}"
echo " Added ${key} to .env"
fi
}
update_env_var "TURN_SECRET" "${TURN_SECRET_VALUE}"
update_env_var "TURN_REALM" "${TURN_REALM_VALUE}"
update_env_var "TURN_EXTERNAL_IP" "${SERVER_IP}"
update_env_var "NEXT_PUBLIC_TURN_SERVER_URL" "turn:${TURN_REALM_VALUE}:3478"
echo "✓ TURN configuration added to .env"
else
echo " WARNING: .env file not found. Please create it and add:"
echo " TURN_SECRET=${TURN_SECRET_VALUE}"
echo " TURN_REALM=${TURN_REALM_VALUE}"
echo " TURN_EXTERNAL_IP=${SERVER_IP}"
echo " NEXT_PUBLIC_TURN_SERVER_URL=turn:${TURN_REALM_VALUE}:3478"
fi
else
echo " Coturn config already exists at ${COTURN_CONFIG}"
# Update external-ip if not already set
if [ -n "${SERVER_IP}" ]; then
if ! grep -q "^external-ip=" "${COTURN_CONFIG}"; then
echo "external-ip=${SERVER_IP}" >> "${COTURN_CONFIG}"
echo " Added external-ip=${SERVER_IP} to Coturn config"
fi
fi
fi
# Start Coturn via Docker Compose
cd "${PROJECT_ROOT}"
if docker compose ps coturn 2>/dev/null | grep -q "running"; then
echo "✓ Coturn is already running"
else
echo " Starting Coturn container..."
docker compose up -d coturn
sleep 2
if docker compose ps coturn 2>/dev/null | grep -q "running"; then
echo "✓ Coturn started successfully"
else
echo "WARNING: Coturn failed to start. Check logs with: docker compose logs coturn"
fi
fi
# TURN server health check
echo "=== Checking TURN server health ==="
if [ -n "${SERVER_IP}" ]; then
# Test STUN binding request using netcat (basic connectivity check)
if command_exists nc; then
if nc -z -u -w 2 ${SERVER_IP} 3478 2>/dev/null; then
echo "✓ TURN server is responding on UDP port 3478"
elif nc -z -w 2 ${SERVER_IP} 3478 2>/dev/null; then
echo "✓ TURN server is responding on TCP port 3478"
else
echo " TURN server port check inconclusive (may still be working)"
fi
fi
# Check if turnutils_uclient is available for proper STUN test
if command_exists turnutils_uclient; then
echo " Running STUN binding test..."
if turnutils_uclient -T -p 3478 ${SERVER_IP} 2>/dev/null | grep -q "success"; then
echo "✓ STUN binding test passed"
else
echo " STUN binding test inconclusive"
fi
fi
# Check Docker logs for any errors
COTURN_ERRORS=$(docker compose logs coturn 2>/dev/null | tail -20 | grep -i "error\|failed\|cannot" || true)
if [ -n "${COTURN_ERRORS}" ]; then
echo " WARNING: Found potential errors in Coturn logs:"
echo "${COTURN_ERRORS}" | head -5
else
echo "✓ No errors found in Coturn logs"
fi
echo ""
echo " TURN server endpoints:"
echo " STUN: stun:${SERVER_IP}:3478"
echo " TURN: turn:${SERVER_IP}:3478"
echo " TURNS: turns:${SERVER_IP}:5349"
else
echo " Skipping health check (server IP not detected)"
fi
# Install Node.js if not present or wrong version
if ! command_exists node || ! node --version | grep -q "v${NODE_VERSION}"; then
echo "=== Installing Node.js ${NODE_VERSION} ==="
curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x | sudo -E bash -
sudo apt-get install -y nodejs
else
echo "=== Node.js $(node --version) already installed ==="
fi
# Install pnpm if not present
if ! command_exists pnpm; then
echo "=== Installing pnpm ==="
curl -fsSL https://get.pnpm.io/install.sh | sh -
else
echo "=== pnpm $(pnpm --version 2>/dev/null || echo 'installed') already installed ==="
fi
# Add pnpm to PATH for this session
export PATH="$PNPM_HOME:$PATH"
# Add pnpm to bashrc if not already there
if ! grep -q "PNPM_HOME" ~/.bashrc 2>/dev/null; then
echo "=== Adding pnpm to ~/.bashrc ==="
echo '' >> ~/.bashrc
echo '# pnpm' >> ~/.bashrc
echo 'export PNPM_HOME="$HOME/.local/share/pnpm"' >> ~/.bashrc
echo 'export PATH="$PNPM_HOME:$PATH"' >> ~/.bashrc
fi
# Install reliq (HTML parser required by torge)
if ! command_exists reliq; then
echo "=== Installing reliq (HTML parser) ==="
RELIQ_TMP=$(mktemp -d)
cd "$RELIQ_TMP"
git clone https://github.com/TUVIMEN/reliq.git
cd reliq
make
sudo make install
cd /
rm -rf "$RELIQ_TMP"
echo "✓ reliq installed"
else
echo "=== reliq already installed ==="
fi
# Install torge (torrent search CLI)
if ! command_exists torge; then
echo "=== Installing torge (torrent search CLI) ==="
TORGE_TMP=$(mktemp -d)
cd "$TORGE_TMP"
git clone https://github.com/TUVIMEN/torge.git
cd torge
sudo install -m 755 torge /usr/bin/torge
cd /
rm -rf "$TORGE_TMP"
echo "✓ torge installed"
else
echo "=== torge already installed ==="
fi
# Create deploy directory if not exists
if [ ! -d "${DEPLOY_PATH}" ]; then
echo "=== Creating deploy directory ==="
mkdir -p "${DEPLOY_PATH}"
else
echo "=== Deploy directory already exists ==="
fi
# Configure firewall (idempotent - ufw handles duplicates)
echo "=== Configuring firewall ==="
sudo ufw default deny incoming 2>/dev/null || true
sudo ufw default allow outgoing 2>/dev/null || true
# Allow SSH on configured port (default 22, can be overridden with SSH_PORT env var)
sudo ufw allow ${SSH_PORT}/tcp 2>/dev/null || true
sudo ufw allow 80/tcp 2>/dev/null || true
sudo ufw allow 443/tcp 2>/dev/null || true
sudo ufw allow 3000/tcp 2>/dev/null || true
sudo ufw allow 6881/tcp 2>/dev/null || true
sudo ufw allow 6881/udp 2>/dev/null || true
sudo ufw allow 6882:6889/udp 2>/dev/null || true
# Coturn STUN/TURN server ports for WebRTC NAT traversal
sudo ufw allow 3478/tcp 2>/dev/null || true # STUN/TURN
sudo ufw allow 3478/udp 2>/dev/null || true # STUN/TURN
sudo ufw allow 5349/tcp 2>/dev/null || true # TURN over TLS
sudo ufw allow 49152:65535/udp 2>/dev/null || true # TURN media relay range
# Enable firewall if not already enabled
if ! sudo ufw status | grep -q "Status: active"; then
sudo ufw --force enable
fi
# Configure fail2ban (only if config doesn't exist or SSH port changed)
FAIL2BAN_CONFIG="/etc/fail2ban/jail.local"
if [ ! -f "$FAIL2BAN_CONFIG" ] || ! grep -q "port = ${SSH_PORT}" "$FAIL2BAN_CONFIG" 2>/dev/null; then
echo "=== Configuring fail2ban (SSH port: ${SSH_PORT}) ==="
sudo tee "$FAIL2BAN_CONFIG" > /dev/null << EOF
[DEFAULT]
bantime = 600
findtime = 600
maxretry = 10
[sshd]
enabled = true
port = ${SSH_PORT}
filter = sshd
logpath = /var/log/auth.log
maxretry = 10
bantime = 600
EOF
sudo systemctl restart fail2ban
else
echo "=== fail2ban already configured for port ${SSH_PORT} ==="
fi
# Configure nginx reverse proxy
NGINX_SITE="/etc/nginx/sites-available/${DOMAIN}"
SSL_CERT="/etc/letsencrypt/live/${DOMAIN}/fullchain.pem"
# Phase 1: Create initial HTTP-only config for certbot (if no SSL cert exists yet)
if [ ! -f "${SSL_CERT}" ]; then
echo "=== Configuring nginx (HTTP-only for initial certbot setup) ==="
sudo tee "${NGINX_SITE}" > /dev/null << 'EOF'
# Initial HTTP-only config for certbot validation
# This will be replaced with full HTTPS config after certbot runs
server {
listen 80;
listen [::]:80;
server_name bittorrented.com www.bittorrented.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_bypass $http_upgrade;
proxy_read_timeout 86400;
proxy_send_timeout 86400;
proxy_buffering off;
}
location /api/health {
proxy_pass http://127.0.0.1:3000/api/health;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
client_max_body_size 100M;
}
EOF
else
echo "=== SSL certificate exists, configuring full HTTPS nginx ==="
fi
# Enable the site by creating symlink
if [ ! -L "/etc/nginx/sites-enabled/${DOMAIN}" ]; then
sudo ln -s "${NGINX_SITE}" "/etc/nginx/sites-enabled/${DOMAIN}"
fi
# Remove default site if it exists and is enabled
if [ -L "/etc/nginx/sites-enabled/default" ]; then
sudo rm /etc/nginx/sites-enabled/default
fi
# Test and reload nginx
echo "=== Testing nginx configuration ==="
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl enable nginx
# Configure SSL with Let's Encrypt (only if certificate doesn't exist)
if [ ! -f "${SSL_CERT}" ]; then
echo "=== Obtaining SSL certificate from Let's Encrypt ==="
# Check if domain resolves to this server (basic check)
SERVER_IP=$(curl -s ifconfig.me 2>/dev/null || curl -s icanhazip.com 2>/dev/null || echo "unknown")
# Get all A records and check if any match (handles CNAME chains)
DOMAIN_IPS=$(dig +short ${DOMAIN} A 2>/dev/null | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' || echo "")
DOMAIN_IP=$(echo "$DOMAIN_IPS" | head -1)
echo " Server IP: ${SERVER_IP}"
echo " Domain IPs: ${DOMAIN_IPS:-none}"
# Check if server IP is in the list of domain IPs
IP_MATCH=false
if echo "$DOMAIN_IPS" | grep -q "^${SERVER_IP}$"; then
IP_MATCH=true
fi
if [ "$IP_MATCH" = true ] || [ -n "${FORCE_SSL:-}" ]; then
echo "Domain ${DOMAIN} resolves to this server (${SERVER_IP})"
echo "Requesting SSL certificate..."
# Run certbot in non-interactive mode
sudo certbot --nginx \
-d ${DOMAIN} \
-d www.${DOMAIN} \
--non-interactive \
--agree-tos \
--email ${CERTBOT_EMAIL} \
--redirect \
--staple-ocsp
echo "=== SSL certificate obtained! ==="
# Set up automatic renewal
if ! systemctl is-enabled certbot.timer >/dev/null 2>&1; then
sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer
fi
echo "Automatic certificate renewal is enabled"
# Phase 2: Now update nginx with full HTTPS config including www redirect
echo "=== Updating nginx with full HTTPS configuration ==="
sudo tee "${NGINX_SITE}" > /dev/null << 'EOF'
# Redirect www to non-www on HTTP (port 80)
server {
listen 80;
listen [::]:80;
server_name www.bittorrented.com;
return 301 https://bittorrented.com$request_uri;
}
# Redirect HTTP to HTTPS for main domain (port 80)
server {
listen 80;
listen [::]:80;
server_name bittorrented.com;
return 301 https://bittorrented.com$request_uri;
}
# Redirect www to non-www on HTTPS (port 443)
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.bittorrented.com;
ssl_certificate /etc/letsencrypt/live/bittorrented.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/bittorrented.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
return 301 https://bittorrented.com$request_uri;
}
# Main HTTPS server (port 443)
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name bittorrented.com;
ssl_certificate /etc/letsencrypt/live/bittorrented.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/bittorrented.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_bypass $http_upgrade;
proxy_read_timeout 86400;
proxy_send_timeout 86400;
proxy_buffering off;
}
location /api/health {
proxy_pass http://127.0.0.1:3000/api/health;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
client_max_body_size 100M;
}
EOF
# Test and reload nginx with new config
echo "=== Testing updated nginx configuration ==="
sudo nginx -t
sudo systemctl reload nginx
echo "=== Full HTTPS nginx configuration applied! ==="
else
echo "WARNING: Domain ${DOMAIN} does not resolve to this server"
echo " Server IP: ${SERVER_IP}"
echo " Domain IP: ${DOMAIN_IP}"
echo " Skipping SSL certificate request"
echo " To force SSL setup, run: FORCE_SSL=1 bash scripts/setup-server.sh"
echo ""
fi
else
echo "=== SSL certificate already exists ==="
# Check certificate expiry
EXPIRY=$(sudo openssl x509 -enddate -noout -in "${SSL_CERT}" 2>/dev/null | cut -d= -f2)
echo " Certificate expires: ${EXPIRY}"
# Ensure full HTTPS config is in place (in case script was interrupted before)
if ! grep -q "listen 443 ssl" "${NGINX_SITE}" 2>/dev/null; then
echo "=== Updating nginx with full HTTPS configuration ==="
sudo tee "${NGINX_SITE}" > /dev/null << 'EOF'
# Redirect www to non-www on HTTP (port 80)
server {
listen 80;
listen [::]:80;
server_name www.bittorrented.com;
return 301 https://bittorrented.com$request_uri;
}
# Redirect HTTP to HTTPS for main domain (port 80)
server {
listen 80;
listen [::]:80;
server_name bittorrented.com;
return 301 https://bittorrented.com$request_uri;
}
# Redirect www to non-www on HTTPS (port 443)
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.bittorrented.com;
ssl_certificate /etc/letsencrypt/live/bittorrented.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/bittorrented.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
return 301 https://bittorrented.com$request_uri;
}
# Main HTTPS server (port 443)
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name bittorrented.com;
ssl_certificate /etc/letsencrypt/live/bittorrented.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/bittorrented.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_bypass $http_upgrade;
proxy_read_timeout 86400;
proxy_send_timeout 86400;
proxy_buffering off;
}
location /api/health {
proxy_pass http://127.0.0.1:3000/api/health;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
client_max_body_size 100M;
}
EOF
sudo nginx -t
sudo systemctl reload nginx
echo "=== Full HTTPS nginx configuration applied! ==="
fi
fi
# Create log files with proper permissions
LOG_FILE="/var/log/${DOMAIN}.log"
ERROR_LOG_FILE="/var/log/${DOMAIN}.error.log"
echo "=== Setting up log files ==="
sudo touch "${LOG_FILE}" "${ERROR_LOG_FILE}"
sudo chown ${VPS_USER}:${VPS_USER} "${LOG_FILE}" "${ERROR_LOG_FILE}"
sudo chmod 644 "${LOG_FILE}" "${ERROR_LOG_FILE}"
# Create/update systemd service
echo "=== Updating systemd service ==="
sudo tee /etc/systemd/system/${SERVICE_NAME}.service > /dev/null << EOF
[Unit]
Description=BitTorrented Media Streamer
After=network.target
[Service]
Type=simple
User=${VPS_USER}
WorkingDirectory=${DEPLOY_PATH}
# Run via a login shell so pnpm AND node resolve from the user's environment
# (mise/nvm-managed). A hardcoded \${PNPM_HOME}/pnpm path breaks when pnpm/node
# move (e.g. to mise), which systemd reports as "failed because of unavailable
# resources or another system error".
ExecStart=/bin/bash -lc 'exec pnpm start'
Restart=on-failure
RestartSec=10
Environment=NODE_ENV=production
# Log to files instead of journald
StandardOutput=append:${LOG_FILE}
StandardError=append:${ERROR_LOG_FILE}
# Increase file descriptor limits for torrents
LimitNOFILE=65535
# Allow binding to privileged ports if needed
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
EOF
# Remove any stale drop-in override that shadows this unit. A leftover
# /etc/systemd/system/bittorrented.service.d/override.conf was pointing ExecStart
# and EnvironmentFile at paths that no longer exist (pnpm/node moved to mise),
# making systemd fail with "Failed to load environment files / Failed to spawn
# 'start' task" → result 'resources'. The app loads its own .env from
# WorkingDirectory, so the clean unit above is sufficient.
sudo rm -rf "/etc/systemd/system/${SERVICE_NAME}.service.d"
# Set up log rotation for the domain logs
echo "=== Setting up log rotation ==="
sudo tee /etc/logrotate.d/${DOMAIN} > /dev/null << EOF
${LOG_FILE} ${ERROR_LOG_FILE} {
daily
rotate 2
compress
delaycompress
missingok
notifempty
create 644 ${VPS_USER} ${VPS_USER}
copytruncate
}
EOF
# Create/update IPTV Cache Worker systemd service
IPTV_WORKER_SERVICE="${SERVICE_NAME}-iptv-worker"
IPTV_WORKER_LOG="/var/log/${SERVICE_NAME}-iptv-worker.log"
IPTV_WORKER_ERROR_LOG="/var/log/${SERVICE_NAME}-iptv-worker.error.log"
echo "=== Setting up IPTV Cache Worker service ==="
sudo touch "${IPTV_WORKER_LOG}" "${IPTV_WORKER_ERROR_LOG}"
sudo chown ${VPS_USER}:${VPS_USER} "${IPTV_WORKER_LOG}" "${IPTV_WORKER_ERROR_LOG}"
sudo chmod 644 "${IPTV_WORKER_LOG}" "${IPTV_WORKER_ERROR_LOG}"
sudo tee /etc/systemd/system/${IPTV_WORKER_SERVICE}.service > /dev/null << EOF
[Unit]
Description=BitTorrented IPTV Cache Worker
After=network.target redis-server.service
[Service]
Type=simple
User=${VPS_USER}
WorkingDirectory=${DEPLOY_PATH}
# Use bash to source .env file properly (handles quotes and complex values)
ExecStart=/bin/bash -c 'set -a; source ${DEPLOY_PATH}/.env; set +a; exec ${PNPM_HOME}/pnpm iptv-worker'
Restart=on-failure
RestartSec=30
Environment=NODE_ENV=production
Environment=PATH=${PNPM_HOME}:/usr/local/bin:/usr/bin:/bin
# Log to files
StandardOutput=append:${IPTV_WORKER_LOG}
StandardError=append:${IPTV_WORKER_ERROR_LOG}
# Increase file descriptor limits
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
# Add IPTV worker logs to logrotate
echo "=== Setting up IPTV worker log rotation ==="
sudo tee /etc/logrotate.d/${IPTV_WORKER_SERVICE} > /dev/null << EOF
${IPTV_WORKER_LOG} ${IPTV_WORKER_ERROR_LOG} {
daily
rotate 2
compress
delaycompress
missingok
notifempty
create 644 ${VPS_USER} ${VPS_USER}
copytruncate
}
EOF
# Create/update Podcast Notifier Worker systemd service
PODCAST_WORKER_SERVICE="${SERVICE_NAME}-podcast-worker"
PODCAST_WORKER_LOG="/var/log/${SERVICE_NAME}-podcast-worker.log"
PODCAST_WORKER_ERROR_LOG="/var/log/${SERVICE_NAME}-podcast-worker.error.log"
echo "=== Setting up Podcast Notifier Worker service ==="
sudo touch "${PODCAST_WORKER_LOG}" "${PODCAST_WORKER_ERROR_LOG}"
sudo chown ${VPS_USER}:${VPS_USER} "${PODCAST_WORKER_LOG}" "${PODCAST_WORKER_ERROR_LOG}"
sudo chmod 644 "${PODCAST_WORKER_LOG}" "${PODCAST_WORKER_ERROR_LOG}"
sudo tee /etc/systemd/system/${PODCAST_WORKER_SERVICE}.service > /dev/null << EOF
[Unit]
Description=BitTorrented Podcast Notifier Worker
After=network.target
[Service]
Type=simple
User=${VPS_USER}
WorkingDirectory=${DEPLOY_PATH}
# Use bash to source .env file properly (handles quotes and complex values)
ExecStart=/bin/bash -c 'set -a; source ${DEPLOY_PATH}/.env; set +a; exec ${PNPM_HOME}/pnpm podcast-worker'
Restart=on-failure
RestartSec=30
# Never let crash-looping wedge the worker in systemd's start limiter; it must
# keep retrying so it self-heals after a transient failure (e.g. feed/network).
StartLimitIntervalSec=0
Environment=NODE_ENV=production
Environment=PATH=${PNPM_HOME}:/usr/local/bin:/usr/bin:/bin
# Log to files
StandardOutput=append:${PODCAST_WORKER_LOG}
StandardError=append:${PODCAST_WORKER_ERROR_LOG}
# Increase file descriptor limits
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
# Add Podcast worker logs to logrotate
echo "=== Setting up Podcast worker log rotation ==="
sudo tee /etc/logrotate.d/${PODCAST_WORKER_SERVICE} > /dev/null << EOF
${PODCAST_WORKER_LOG} ${PODCAST_WORKER_ERROR_LOG} {
daily
rotate 2
compress
delaycompress
missingok
notifempty
create 644 ${VPS_USER} ${VPS_USER}
copytruncate
}
EOF
# Cap systemd journal size (idempotent - overwrites config)
echo "=== Configuring systemd journal size limit ==="
sudo mkdir -p /etc/systemd/journald.conf.d
sudo tee /etc/systemd/journald.conf.d/size-limit.conf > /dev/null << EOF
[Journal]
SystemMaxUse=200M
MaxRetentionSec=2day
EOF
sudo systemctl restart systemd-journald 2>/dev/null || true
echo "✓ Journal capped at 200M / 2 days"
# ============================================
# DHT CRAWLER SERVICES (Bitmagnet + DHT API)
# ============================================
# Skip if DHT_ENABLED is explicitly set to false
if [ "${DHT_ENABLED:-true}" != "false" ]; then
echo ""
echo "=== Setting up DHT Crawler Services ==="
# Call the dedicated DHT setup script
DHT_SETUP_SCRIPT="${PROJECT_ROOT}/services/dht-search-api/scripts/setup-dht-services.sh"
if [ -f "${DHT_SETUP_SCRIPT}" ]; then
echo " Running: ${DHT_SETUP_SCRIPT}"
sudo bash "${DHT_SETUP_SCRIPT}"
else
echo " WARNING: DHT setup script not found at ${DHT_SETUP_SCRIPT}"
echo " Skipping DHT services installation"
fi
else
echo ""
echo "=== Skipping DHT Crawler Services (DHT_ENABLED=false) ==="
fi