Skip to content

Commit c65a34a

Browse files
loreste32claude
andcommitted
Make server setup intuitive and client startup resilient
Server (pale-server): - Validate all required secrets together at startup and exit with an actionable message (what is missing, how to generate it) instead of panicking on the first one - SIP TLS now auto-enables when PALE_SIP_TLS_CERT/KEY are provided and stays off otherwise (previously TLS defaulted to required, failing startup with no certs); explicit PALE_SIP_TLS still wins - Warn when PALE_SIP_EXTERNAL_ADDR is unset and the registrar would be advertised as 127.0.0.1 (remote clients cannot register) - Log a one-line resolved-config summary at startup (transports, TLS, SRTP, storage backend, TURN, rate limit) - docker-compose: POSTGRES_PASSWORD and TURN_SECRET no longer default to "changeme" — compose fails loudly if they are unset - Add .env.example (full annotated settings list) and scripts/generate-secrets.sh (writes .env with strong secrets); README quick start updated to use them Client (Tauri): - PJSIP engine init failure no longer crashes the app: SIP commands return the init error to the UI, while chat/history/settings work - Replace setup() panics (app data dir, call history DB) with contextful errors - pjsip-sys: pin auto-detected video deps off (--disable-ffmpeg, -openh264, -vpx, -sdl) so builds do not depend on whatever is installed on the build machine Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 00e4ff1 commit c65a34a

7 files changed

Lines changed: 274 additions & 67 deletions

File tree

‎.env.example‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Pale Server configuration
2+
#
3+
# Copy to .env (docker compose reads it automatically), or run
4+
# ./scripts/generate-secrets.sh to create a .env with strong random secrets.
5+
6+
# ── Required secrets (min 24 characters each) ───────────────────────────────
7+
# Generate with: openssl rand -base64 32
8+
PALE_SERVER_TOKEN=
9+
PALE_ADMIN_PASSWORD=
10+
PALE_STORAGE_KEY=
11+
# PostgreSQL password (use openssl rand -hex 32 — hex avoids characters that
12+
# need quoting in connection strings)
13+
POSTGRES_PASSWORD=
14+
# Shared secret between pale-server and coturn
15+
TURN_SECRET=
16+
17+
# ── Common settings ─────────────────────────────────────────────────────────
18+
# Admin account name (default: admin)
19+
#PALE_ADMIN_USERNAME=admin
20+
21+
# Public hostname/IP clients use to reach SIP. REQUIRED for remote clients —
22+
# without it the registrar is advertised as 127.0.0.1.
23+
#PALE_SIP_EXTERNAL_ADDR=sip.example.com:5060
24+
25+
# HTTP API bind address (default 127.0.0.1:8080; docker-compose overrides to 0.0.0.0:8080)
26+
#PALE_HTTP_ADDR=0.0.0.0:8080
27+
# HTTPS for the API (strongly recommended when bound to a non-loopback address)
28+
#PALE_HTTP_TLS_CERT=/path/to/fullchain.pem
29+
#PALE_HTTP_TLS_KEY=/path/to/privkey.pem
30+
31+
# ── SIP transports ──────────────────────────────────────────────────────────
32+
# SIP bind address (default 0.0.0.0:5060)
33+
#PALE_SIP_ADDR=0.0.0.0:5060
34+
# SIP TLS turns on automatically when both cert and key are set.
35+
# Set PALE_SIP_TLS=true/false to force it on/off.
36+
#PALE_SIP_TLS_CERT=/path/to/fullchain.pem
37+
#PALE_SIP_TLS_KEY=/path/to/privkey.pem
38+
#PALE_SIP_TLS_PORT=5061
39+
# Require encrypted media (default true)
40+
#PALE_SIP_SRTP=true
41+
# Plain UDP transport (default: on unless TLS is enabled)
42+
#PALE_SIP_UDP=true
43+
#PALE_SIP_TCP=false
44+
45+
# ── NAT traversal ───────────────────────────────────────────────────────────
46+
# Comma-separated STUN servers. Default uses Google's public STUN — point this
47+
# at your own server if that is a privacy concern.
48+
#PALE_STUN_SERVERS=stun:stun.l.google.com:19302
49+
#PALE_TURN_SERVER=turn:your.server.ip:3478
50+
#PALE_TURN_TRANSPORT=udp
51+
#PALE_TURN_REALM=pale.local
52+
53+
# ── Persistence ─────────────────────────────────────────────────────────────
54+
# PostgreSQL connection string. Without it the server uses local SQLite in
55+
# PALE_DATA_DIR (fine for a single node; use PostgreSQL in production).
56+
#PALE_DATABASE_URL=host=localhost user=pale password=... dbname=pale
57+
#PALE_PG_MAX_CONNECTIONS=10
58+
#PALE_DATA_DIR=./pale-data
59+
60+
# ── Tuning / logging ────────────────────────────────────────────────────────
61+
#PALE_RATE_LIMIT_RPS=100
62+
#PALE_MAX_UPLOAD_BYTES=104857600
63+
#PALE_LOG_JSON=false
64+
#RUST_LOG=info

‎README.md‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,8 @@ sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libasound2-dev \
183183
### Deploy Pale Server
184184

185185
```bash
186-
# Generate secrets
187-
export PALE_SERVER_TOKEN=$(openssl rand -base64 32)
188-
export PALE_ADMIN_PASSWORD=$(openssl rand -base64 32)
189-
export PALE_STORAGE_KEY=$(openssl rand -base64 32)
190-
export TURN_SECRET=$(openssl rand -base64 32)
191-
export POSTGRES_PASSWORD=$(openssl rand -base64 32)
186+
# Generate secrets (writes .env, which docker compose reads automatically)
187+
./scripts/generate-secrets.sh
192188

193189
# Start server stack (PostgreSQL + pale-server + TURN relay)
194190
docker compose up -d
@@ -204,6 +200,15 @@ The server exposes:
204200
- **SIP TLS** on port 5061
205201
- **TURN relay** on port 3478
206202

203+
All settings are environment variables — see [`.env.example`](.env.example)
204+
for the full annotated list. Two you will almost certainly want in production:
205+
206+
- `PALE_SIP_EXTERNAL_ADDR` — the public hostname/IP clients use to reach SIP.
207+
Without it the registrar is advertised as `127.0.0.1` and only local
208+
clients can register.
209+
- `PALE_SIP_TLS_CERT` / `PALE_SIP_TLS_KEY` — providing both enables SIP over
210+
TLS automatically (and disables plain UDP by default).
211+
207212
### Build & Run Desktop Client
208213

209214
```bash

‎docker-compose.yml‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# Pale Server — Docker Compose with PostgreSQL + TURN
22
#
33
# Quick start:
4-
# export PALE_SERVER_TOKEN=$(openssl rand -base64 32)
5-
# export PALE_ADMIN_PASSWORD=$(openssl rand -base64 32)
6-
# export PALE_STORAGE_KEY=$(openssl rand -base64 32)
7-
# export TURN_SECRET=$(openssl rand -base64 32)
4+
# ./scripts/generate-secrets.sh # writes .env with all required secrets
85
# docker compose up -d
6+
# curl http://localhost:8090/health
7+
#
8+
# All required variables fail loudly if unset — see .env.example for the
9+
# full list and optional settings.
910

1011
services:
1112
postgres:
@@ -15,7 +16,7 @@ services:
1516
environment:
1617
POSTGRES_DB: pale
1718
POSTGRES_USER: pale
18-
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD:-changeme}"
19+
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD (try: openssl rand -base64 32)}"
1920
volumes:
2021
- pale-pgdata:/var/lib/postgresql/data
2122
ports:
@@ -33,7 +34,7 @@ services:
3334
network_mode: host
3435
volumes:
3536
- ./turnserver.conf:/etc/turnserver.conf:ro
36-
command: ["-c", "/etc/turnserver.conf", "--static-auth-secret=${TURN_SECRET:-changeme-turn-secret}"]
37+
command: ["-c", "/etc/turnserver.conf", "--static-auth-secret=${TURN_SECRET:?Set TURN_SECRET (try: openssl rand -base64 32)}"]
3738
healthcheck:
3839
test: ["CMD", "turnadmin", "-l"]
3940
interval: 30s
@@ -57,7 +58,7 @@ services:
5758
PALE_HTTP_ADDR: "0.0.0.0:8080"
5859
PALE_SIP_ADDR: "0.0.0.0:5060"
5960
PALE_DATA_DIR: "/data"
60-
PALE_DATABASE_URL: "host=postgres user=pale password=${POSTGRES_PASSWORD:-changeme} dbname=pale"
61+
PALE_DATABASE_URL: "host=postgres user=pale password=${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD (try: openssl rand -base64 32)} dbname=pale"
6162
PALE_PG_MAX_CONNECTIONS: "20"
6263
PALE_SERVER_TOKEN: "${PALE_SERVER_TOKEN:?Set PALE_SERVER_TOKEN (min 24 chars)}"
6364
PALE_ADMIN_PASSWORD: "${PALE_ADMIN_PASSWORD:?Set PALE_ADMIN_PASSWORD (min 24 chars)}"
@@ -70,7 +71,7 @@ services:
7071
PALE_TURN_SERVER: "${PALE_TURN_SERVER:-turn:127.0.0.1:3478}"
7172
PALE_TURN_TRANSPORT: "udp"
7273
PALE_TURN_USERNAME: "pale"
73-
PALE_TURN_PASSWORD: "${TURN_SECRET:-changeme-turn-secret}"
74+
PALE_TURN_PASSWORD: "${TURN_SECRET:?Set TURN_SECRET (try: openssl rand -base64 32)}"
7475
PALE_TURN_REALM: "${PALE_TURN_REALM:-pale.local}"
7576
PALE_SIP_TLS: "${PALE_SIP_TLS:-false}"
7677
PALE_SIP_SRTP: "${PALE_SIP_SRTP:-false}"

‎scripts/generate-secrets.sh‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
# Generate a .env file with the secrets pale-server's docker-compose requires.
3+
# Safe to run once per deployment; refuses to overwrite an existing .env.
4+
set -eu
5+
6+
cd "$(dirname "$0")/.."
7+
8+
if [ -f .env ]; then
9+
echo "error: .env already exists — remove it first if you really want new secrets." >&2
10+
echo " (Rotating secrets invalidates existing sessions and the storage key" >&2
11+
echo " makes previously stored files unreadable.)" >&2
12+
exit 1
13+
fi
14+
15+
umask 077
16+
cat > .env <<EOF
17+
# Generated by scripts/generate-secrets.sh on $(date -u +%Y-%m-%dT%H:%M:%SZ)
18+
# See .env.example for optional settings.
19+
PALE_SERVER_TOKEN=$(openssl rand -base64 32)
20+
PALE_ADMIN_PASSWORD=$(openssl rand -base64 32)
21+
PALE_STORAGE_KEY=$(openssl rand -base64 32)
22+
POSTGRES_PASSWORD=$(openssl rand -hex 32)
23+
TURN_SECRET=$(openssl rand -hex 32)
24+
EOF
25+
26+
echo "Wrote .env with PALE_SERVER_TOKEN, PALE_ADMIN_PASSWORD, PALE_STORAGE_KEY, POSTGRES_PASSWORD, TURN_SECRET."
27+
echo "Admin login: user 'admin' (override with PALE_ADMIN_USERNAME), password is PALE_ADMIN_PASSWORD in .env."
28+
echo "Next: docker compose up -d && curl http://localhost:8090/health"

‎src-tauri/crates/pale-server/src/main.rs‎

Lines changed: 95 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
2727
// Install Prometheus metrics recorder
2828
let prom_handle = Arc::new(metrics::install_recorder());
2929

30-
let config = config_from_env();
30+
let config = match config_from_env() {
31+
Ok(config) => config,
32+
Err(message) => {
33+
eprintln!("pale-server: {message}");
34+
std::process::exit(1);
35+
}
36+
};
3137
let mut app_state = AppState::persistent(
3238
config.data_dir.clone(),
3339
config.http_token.clone(),
@@ -72,11 +78,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
7278
app_state.set_rate_limit_rps(rate_limit_rps);
7379
// Use PALE_SIP_EXTERNAL_ADDR if set (public-facing SIP address for clients),
7480
// otherwise derive from HTTP bind address, replacing 0.0.0.0 with 127.0.0.1
75-
let sip_external = std::env::var("PALE_SIP_EXTERNAL_ADDR").unwrap_or_else(|_| {
76-
let addr = config.sip_addr.to_string();
77-
addr.replace("0.0.0.0", "127.0.0.1")
78-
});
79-
app_state.set_sip_registrar(sip_external);
81+
let sip_external = match std::env::var("PALE_SIP_EXTERNAL_ADDR") {
82+
Ok(addr) => addr,
83+
Err(_) => {
84+
let addr = config.sip_addr.to_string();
85+
if addr.starts_with("0.0.0.0") {
86+
log::warn!(
87+
"PALE_SIP_EXTERNAL_ADDR is not set — advertising the SIP registrar as 127.0.0.1, \
88+
so only clients on this machine can register. Set PALE_SIP_EXTERNAL_ADDR to this \
89+
server's public hostname or IP for remote clients."
90+
);
91+
}
92+
addr.replace("0.0.0.0", "127.0.0.1")
93+
}
94+
};
95+
app_state.set_sip_registrar(sip_external.clone());
8096

8197
let state = Arc::new(app_state);
8298
tokio::fs::create_dir_all(state.files_dir()).await?;
@@ -86,13 +102,34 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
86102
SipBackend::Pjsip => {
87103
let tls = tls_config_from_env(config.sip_addr.port())?;
88104
let encrypted_by_default = tls.is_some();
105+
let enable_udp = env_bool("PALE_SIP_UDP", !encrypted_by_default);
106+
let enable_tcp = env_bool("PALE_SIP_TCP", false);
107+
let require_srtp = env_bool("PALE_SIP_SRTP", true);
108+
log::info!(
109+
"Config: HTTP {} (TLS {}) | SIP {} [UDP {} / TCP {} / TLS {}] SRTP {} | registrar advertised as {} | storage: {} | TURN: {} | rate limit {} req/s",
110+
config.http_addr,
111+
onoff(config.http_tls_cert.is_some() && config.http_tls_key.is_some()),
112+
config.sip_addr,
113+
onoff(enable_udp),
114+
onoff(enable_tcp),
115+
tls.as_ref().map(|t| format!("on (port {})", t.port)).unwrap_or_else(|| "off".to_string()),
116+
onoff(require_srtp),
117+
sip_external,
118+
if state.pg_store().is_some() {
119+
"PostgreSQL".to_string()
120+
} else {
121+
format!("SQLite at {} (set PALE_DATABASE_URL for PostgreSQL)", config.data_dir.display())
122+
},
123+
config.media.turn.as_ref().map(|t| t.server.as_str()).unwrap_or("none"),
124+
rate_limit_rps,
125+
);
89126
let runtime = PjsipRuntime::start(
90127
PjsipRuntimeConfig {
91128
sip_addr: config.sip_addr,
92-
enable_udp: env_bool("PALE_SIP_UDP", !encrypted_by_default),
93-
enable_tcp: env_bool("PALE_SIP_TCP", false),
129+
enable_udp,
130+
enable_tcp,
94131
tls,
95-
require_srtp: env_bool("PALE_SIP_SRTP", true),
132+
require_srtp,
96133
media: config.media.clone(),
97134
},
98135
state.clone(),
@@ -184,30 +221,43 @@ impl EnvAddr for ServerConfig {
184221
}
185222
}
186223

187-
fn config_from_env() -> ServerConfig {
188-
ServerConfig {
224+
fn config_from_env() -> Result<ServerConfig, String> {
225+
let mut errors = Vec::new();
226+
let http_token = checked_secret("PALE_SERVER_TOKEN", &mut errors);
227+
let admin_password = checked_secret("PALE_ADMIN_PASSWORD", &mut errors);
228+
let storage_key = checked_secret("PALE_STORAGE_KEY", &mut errors);
229+
if !errors.is_empty() {
230+
return Err(format!(
231+
"configuration errors:\n - {}\n\n\
232+
Generate a strong secret with: openssl rand -base64 32\n\
233+
See .env.example for the full list of settings.",
234+
errors.join("\n - ")
235+
));
236+
}
237+
238+
Ok(ServerConfig {
189239
http_addr: ServerConfig::read_addr("PALE_HTTP_ADDR", "127.0.0.1:8080"),
190240
sip_addr: ServerConfig::read_addr("PALE_SIP_ADDR", "0.0.0.0:5060"),
191241
data_dir: std::env::var("PALE_DATA_DIR")
192242
.map(PathBuf::from)
193243
.unwrap_or_else(|_| PathBuf::from("./pale-data")),
194-
http_token: required_secret("PALE_SERVER_TOKEN"),
244+
http_token: http_token.expect("validated above"),
195245
admin_username: std::env::var("PALE_ADMIN_USERNAME")
196246
.ok()
197247
.filter(|value| !value.trim().is_empty())
198248
.unwrap_or_else(|| "admin".to_string()),
199249
admin_password_hash: pale_server::sha256_hex(
200-
required_secret("PALE_ADMIN_PASSWORD").as_bytes(),
250+
admin_password.expect("validated above").as_bytes(),
201251
),
202-
storage_key: required_secret("PALE_STORAGE_KEY"),
252+
storage_key: storage_key.expect("validated above"),
203253
max_upload_bytes: std::env::var("PALE_MAX_UPLOAD_BYTES")
204254
.ok()
205255
.and_then(|value| value.parse().ok())
206256
.unwrap_or(100 * 1024 * 1024),
207257
http_tls_cert: optional_env("PALE_HTTP_TLS_CERT").map(PathBuf::from),
208258
http_tls_key: optional_env("PALE_HTTP_TLS_KEY").map(PathBuf::from),
209259
media: media_config_from_env(),
210-
}
260+
})
211261
}
212262

213263
fn media_config_from_env() -> MediaConfig {
@@ -250,15 +300,31 @@ fn turn_transport_from_env() -> TurnTransport {
250300
}
251301
}
252302

253-
fn required_secret(name: &str) -> String {
254-
std::env::var(name)
255-
.ok()
256-
.filter(|value| value.len() >= 24)
257-
.unwrap_or_else(|| panic!("{} must be set to at least 24 characters", name))
303+
fn checked_secret(name: &str, errors: &mut Vec<String>) -> Option<String> {
304+
match std::env::var(name) {
305+
Ok(value) if value.len() >= 24 => Some(value),
306+
Ok(_) => {
307+
errors.push(format!("{name} is set but shorter than 24 characters"));
308+
None
309+
}
310+
Err(_) => {
311+
errors.push(format!("{name} is not set"));
312+
None
313+
}
314+
}
258315
}
259316

260317
fn tls_config_from_env(sip_port: u16) -> Result<Option<TlsConfig>, Box<dyn std::error::Error + Send + Sync>> {
261-
if !env_bool("PALE_SIP_TLS", true) {
318+
// Explicit PALE_SIP_TLS=true/false wins. When unset, TLS is enabled
319+
// exactly when a cert and key are provided.
320+
let certs_present =
321+
optional_env("PALE_SIP_TLS_CERT").is_some() && optional_env("PALE_SIP_TLS_KEY").is_some();
322+
if !env_bool("PALE_SIP_TLS", certs_present) {
323+
if certs_present {
324+
log::warn!("PALE_SIP_TLS_CERT/KEY are set but PALE_SIP_TLS=false; SIP TLS stays disabled");
325+
} else {
326+
log::info!("SIP TLS disabled — set PALE_SIP_TLS_CERT and PALE_SIP_TLS_KEY to enable it");
327+
}
262328
return Ok(None);
263329
}
264330

@@ -295,6 +361,14 @@ fn optional_env(name: &str) -> Option<String> {
295361
.filter(|value| !value.trim().is_empty())
296362
}
297363

364+
fn onoff(value: bool) -> &'static str {
365+
if value {
366+
"on"
367+
} else {
368+
"off"
369+
}
370+
}
371+
298372
fn env_bool(name: &str, default: bool) -> bool {
299373
std::env::var(name)
300374
.map(|value| matches!(value.to_ascii_lowercase().as_str(), "1" | "true" | "yes" | "on"))

‎src-tauri/crates/pjsip-sys/build.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,17 @@ fn build_pjsip(pj_src_dir: &Path, target_os: &str, target_arch: &str) {
253253
// Determine OpenSSL path
254254
let openssl_prefix = find_openssl_prefix(target_os);
255255

256-
// Build configure arguments
256+
// Build configure arguments.
257+
// Pin auto-detected optional video deps off so builds don't depend on
258+
// whatever happens to be installed on the build machine (e.g. a Homebrew
259+
// ffmpeg whose API doesn't match what PJSIP 2.14 expects).
257260
let mut configure_args = vec![
258261
"./configure".to_string(),
259262
"--enable-shared=no".to_string(),
263+
"--disable-ffmpeg".to_string(),
264+
"--disable-openh264".to_string(),
265+
"--disable-vpx".to_string(),
266+
"--disable-sdl".to_string(),
260267
];
261268

262269
// Windows cross/native compilation

0 commit comments

Comments
 (0)