-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplex
More file actions
executable file
·155 lines (130 loc) · 5.67 KB
/
Copy pathsimplex
File metadata and controls
executable file
·155 lines (130 loc) · 5.67 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
#!/usr/bin/env bash
set -e
# Log file for setup operations
LOG_FILE="./data/setup.log"
# Export current UID and GID for Docker Compose to use
export USER_UID=$(id -u)
export USER_GID=$(id -g)
# 1. Handle .env file initialization if it doesn't exist
if [ ! -f .env ]; then
echo "📄 .env file not found. Initializing from .env.example..."
if [ -f .env.example ]; then
cp .env.example .env
else
echo "⚠️ .env.example not found! Creating a blank .env file..."
touch .env
fi
fi
# 2. Ensure data directories exist with current user permissions
echo "⚙️ Ensuring data directories exist..."
mkdir -p ./data/tor \
./data/smp/config \
./data/smp/state \
./data/xftp/config \
./data/xftp/state \
./data/xftp/files
TARGET_SMP="./data/tor/simplex-smp"
TARGET_XFTP="./data/tor/simplex-xftp"
# 3. Generate Tor Onion v3 Keys natively via official Tor container if missing
if [ ! -d "$TARGET_SMP" ] || [ ! -d "$TARGET_XFTP" ]; then
echo "🔑 Tor keys missing. Generating official Onion v3 addresses via native Tor container..."
# Pull the official Tor image to ensure it is available for key generation
echo "⬇️ Pulling Tor image..."
if ! docker pull lncm/tor:latest > "$LOG_FILE" 2>&1; then
echo "❌ Error: Docker pull failed. Check $LOG_FILE for details."
cat "$LOG_FILE" # Display the log for user reference
exit 1
fi
# Run an ephemeral Tor container to generate genuine cryptographic v3 keypairs natively
timeout 2s docker run --rm \
--user "$USER_UID:$USER_GID" \
-v "$(pwd)/data/tor:/var/lib/tor" \
lncm/tor:latest \
tor --ignore-missing-torrc \
--HiddenServiceDir /var/lib/tor/simplex-smp \
--HiddenServicePort "5223 127.0.0.1:5223" \
--HiddenServiceDir /var/lib/tor/simplex-xftp \
--HiddenServicePort "443 127.0.0.1:443" > /dev/null 2>&1 || true
# Read the keys generated directly by Tor
ONION_SMP=$(cat "$TARGET_SMP/hostname")
ONION_XFTP=$(cat "$TARGET_XFTP/hostname")
echo "✨ Generated new Onion keys natively."
else
echo "🔑 Tor keys already exist. Reading current Onion addresses..."
# Read the existing onion addresses from the hostname files
ONION_SMP=$(cat "$TARGET_SMP/hostname")
ONION_XFTP=$(cat "$TARGET_XFTP/hostname")
fi
# 4. Safely ensure environment variables match reality
echo "📝 Checking and enforcing .env variables..."
ensure_env_var() {
local var_name=$1
local var_value=$2
# Check if the variable is already defined in the .env file
if grep -q "^${var_name}=" .env; then
# Extract the current value from the .env file
local current_value=$(grep "^${var_name}=" .env | cut -d'=' -f2-)
# If the value is different, update it in place using sed
if [ "$current_value" != "$var_value" ]; then
local escaped_value=$(echo "$var_value" | sed 's/[&/]/\\&/g')
sed -i "s|^${var_name}=.*|${var_name}=${escaped_value}|" .env
echo " 🔄 Updated ${var_name} to match system reality."
fi
else
# If it doesn't exist, check for missing trailing newline once and append
if [ -f .env ] && [ -n "$(tail -c1 .env 2>/dev/null)" ]; then
echo "" >> .env
fi
echo "${var_name}=${var_value}" >> .env
echo " ✅ Added ${var_name} to .env"
fi
}
# Enforce exact matches for the Tor addresses (reality strictly overrides the .env)
ensure_env_var "ADDR_SMP" "${ONION_SMP}"
ensure_env_var "ADDR_XFTP" "${ONION_XFTP}"
echo "✨ Environment validation complete!"
echo " 🔹 SMP Server: $ONION_SMP"
echo " 🔹 XFTP Server: $ONION_XFTP"
# 5. Handle custom commands or pass through to Docker Compose
if [ "$1" == "info" ] || [ "$1" == "urls" ]; then
echo "ℹ️ SimpleX Node Connection Information:"
# Check if the containers have already generated the fingerprints
if [ -f "./data/smp/config/fingerprint" ] && [ -f "./data/xftp/config/fingerprint" ]; then
# Read fingerprints and strip any trailing newlines/spaces
FP_SMP=$(cat ./data/smp/config/fingerprint | tr -d '[:space:]')
FP_XFTP=$(cat ./data/xftp/config/fingerprint | tr -d '[:space:]')
URI_SMP="smp://${FP_SMP}@${ONION_SMP}"
URI_XFTP="xftp://${FP_XFTP}@${ONION_XFTP}"
# Format and display the connection strings
echo "----------------------------------------"
echo "simplex-smp | ${URI_SMP}"
echo "simplex-xftp | ${URI_XFTP}"
echo "----------------------------------------"
# If qrencode is available, display QR codes side by side
if command -v qrencode &> /dev/null; then
echo ""
printf "%-45s %-45s\n" "--- SMP ---" "--- XFTP ---"
qrencode -m 2 -t UTF8 "$URI_SMP" > /tmp/qr_smp.txt
qrencode -m 2 -t UTF8 "$URI_XFTP" > /tmp/qr_xftp.txt
# Use paste to display both QR codes side by side
paste -d ' ' /tmp/qr_smp.txt /tmp/qr_xftp.txt
# Clean up temporary QR code files
rm /tmp/qr_smp.txt /tmp/qr_xftp.txt
else
echo "⚠️ Install 'qrencode' to display QR codes for the connection strings."
fi
else
echo "⚠️ Fingerprint files not found yet."
echo " Please run './simplex' first to boot the node and generate the server profiles."
fi
exit 0
fi
# Default behavior for Docker Compose
echo "🚀 Running Docker Compose..."
if [ $# -eq 0 ]; then
# Default behavior: boot the node up in the background
docker compose up -d
else
# Pass through standard compose commands (down, logs, ps, etc.)
docker compose "$@"
fi