alighieri 0.4.0

Alighieri — a lightweight, secure, asynchronous SOCKS5 proxy server with Dante-inspired configuration
Documentation
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
//! TLS listener support.
//!
//! This module stays at the transport boundary: it builds a `TlsAcceptor` for
//! the server accept loop, while SOCKS5 negotiation and relay logic remain
//! unaware of whether the client stream is plaintext TCP or TLS-wrapped TCP.

use std::fs::File;
use std::future::Future;
use std::io::BufReader;
use std::path::Path;
use std::pin::Pin;
use std::sync::Arc;

use tokio::net::TcpStream;
use tokio_rustls::rustls::pki_types::pem::{self, PemObject};
use tokio_rustls::rustls::pki_types::{CertificateDer, PrivateKeyDer};
use tokio_rustls::rustls::server::Acceptor;
use tokio_rustls::rustls::ServerConfig;
use tokio_rustls::server::TlsStream;
use tokio_rustls::{LazyConfigAcceptor, TlsAcceptor};
use tokio_stream::StreamExt;

use crate::config::{AcmeConfig, Config, TlsConfig};
use crate::errors::{Error, Result};

/// A future that drives ACME certificate issuance and renewal. The server spawns
/// it once; issued certificates are then served through the listener's rustls
/// configs (which share the renewing resolver) and renewed in the background
/// without a restart.
pub type AcmeDriver = Pin<Box<dyn Future<Output = ()> + Send>>;

/// The TLS listener for the server, plus — for ACME — the renewal driver.
pub struct TlsSetup {
    pub listener: TlsListener,
    pub acme_driver: Option<AcmeDriver>,
}

/// How accepted connections are wrapped in TLS.
///
/// ACME needs two rustls configs: TLS-ALPN-01 challenge handshakes (ALPN
/// `acme-tls/1`, sent by Let's Encrypt to prove domain control) must be answered
/// with a special challenge certificate, while everything else is served the
/// issued certificate. A plain acceptor cannot do this — it never negotiates
/// `acme-tls/1`, so the challenge fails and no certificate is ever issued — so we
/// peek each ClientHello and route it to the right config.
#[derive(Clone)]
pub enum TlsListener {
    /// A fixed certificate/key acceptor (`tls.certfile`/`tls.keyfile`).
    Manual(TlsAcceptor),
    /// ACME: `acme-tls/1` challenge handshakes go to `challenge`; all other
    /// connections to `default`, which serves the issued certificate.
    Acme {
        default: Arc<ServerConfig>,
        challenge: Arc<ServerConfig>,
    },
}

impl TlsListener {
    /// Completes the TLS handshake for an accepted connection.
    ///
    /// Returns `Ok(None)` when the connection was an ACME TLS-ALPN-01 challenge
    /// answered internally: Let's Encrypt only needs the handshake to present the
    /// challenge certificate, so the caller drops it. `Ok(Some(stream))` is a
    /// normal client connection to drive through SOCKS.
    pub async fn accept(&self, stream: TcpStream) -> std::io::Result<Option<TlsStream<TcpStream>>> {
        match self {
            TlsListener::Manual(acceptor) => acceptor.accept(stream).await.map(Some),
            TlsListener::Acme { default, challenge } => {
                let handshake = LazyConfigAcceptor::new(Acceptor::default(), stream).await?;
                if rustls_acme::is_tls_alpn_challenge(&handshake.client_hello()) {
                    // `into_stream(..).await` drives the handshake to completion,
                    // sending the challenge certificate Let's Encrypt validates.
                    // Then close cleanly with a close_notify — the connection
                    // carries no application data — rather than dropping it abruptly.
                    use tokio::io::AsyncWriteExt;
                    let mut challenge_tls = handshake.into_stream(challenge.clone()).await?;
                    let _ = challenge_tls.shutdown().await;
                    Ok(None)
                } else {
                    handshake.into_stream(default.clone()).await.map(Some)
                }
            }
        }
    }
}

pub fn validate_config(config: &Config) -> Result<()> {
    // Pure validation for `--check` / service-install: build the acceptor (and,
    // for ACME, the renewal state) with no network I/O and no filesystem side
    // effects — in particular, do not create or write the ACME cache directory.
    let _ = build_acceptor(config.tls.as_ref(), false)?;
    Ok(())
}

pub fn load_acceptor(config: Option<&TlsConfig>) -> Result<Option<TlsSetup>> {
    // Runtime setup: also prepares (creates and write-probes) the ACME cache dir.
    build_acceptor(config, true)
}

fn build_acceptor(config: Option<&TlsConfig>, prepare_cache: bool) -> Result<Option<TlsSetup>> {
    let Some(config) = config else {
        return Ok(None);
    };
    let setup = match config {
        TlsConfig::Files {
            cert_file,
            key_file,
        } => file_setup(cert_file, key_file)?,
        TlsConfig::Acme(acme) => acme_setup(acme, prepare_cache)?,
    };
    Ok(Some(setup))
}

fn file_setup(cert_file: &Path, key_file: &Path) -> Result<TlsSetup> {
    let certs = load_certs(cert_file)?;
    if certs.is_empty() {
        return Err(Error::Config(format!(
            "TLS certificate file {} did not contain any certificates",
            cert_file.display()
        )));
    }
    let key = load_private_key(key_file)?;
    let server_config = ServerConfig::builder()
        .with_no_client_auth()
        .with_single_cert(certs, key)
        .map_err(|e| Error::Config(format!("invalid TLS certificate/key pair: {e}")))?;
    Ok(TlsSetup {
        listener: TlsListener::Manual(TlsAcceptor::from(Arc::new(server_config))),
        acme_driver: None,
    })
}

fn acme_setup(acme: &AcmeConfig, prepare_cache: bool) -> Result<TlsSetup> {
    // At runtime, create and write-probe the cache directory so an unwritable
    // path fails at startup rather than silently in the background renewal task.
    // Skipped for `--check`, which must not touch the filesystem.
    if prepare_cache {
        ensure_writable_dir(&acme.cache_dir)?;
    }
    // The state owns the resolver shared with both rustls configs below,
    // persists the account/certs to the cache dir, and runs the order + renewal
    // loop when polled. `default` serves the issued certificate to clients;
    // `challenge` carries the `acme-tls/1` ALPN and challenge certificate that
    // answer Let's Encrypt's TLS-ALPN-01 validation. The accept loop routes each
    // connection to the right one (see `TlsListener::accept`).
    let mut state = rustls_acme::AcmeConfig::new(acme.domains.clone())
        .contact(acme.email.iter().map(|email| format!("mailto:{email}")))
        .cache(rustls_acme::caches::DirCache::new(acme.cache_dir.clone()))
        .directory_lets_encrypt(!acme.staging)
        .state();
    let default = state.default_rustls_config();
    let challenge = state.challenge_rustls_config();
    let driver: AcmeDriver = Box::pin(async move {
        loop {
            match state.next().await {
                Some(Ok(ok)) => tracing::info!("acme: {ok:?}"),
                Some(Err(err)) => tracing::error!("acme error: {err:?}"),
                None => {
                    tracing::warn!(
                        "acme renewal task ended; TLS certificates will no longer be renewed"
                    );
                    break;
                }
            }
        }
    });
    Ok(TlsSetup {
        listener: TlsListener::Acme { default, challenge },
        acme_driver: Some(driver),
    })
}

/// Ensures `dir` exists and is writable, failing fast at startup otherwise.
/// `create_dir_all` alone is not enough: it succeeds on an existing directory
/// even when it is not writable, so probe by writing and removing a temp file.
fn ensure_writable_dir(dir: &Path) -> Result<()> {
    // Reject a pre-placed symlink (or non-directory) at the cache path: an
    // attacker who can write the parent could otherwise redirect the ACME account
    // key and issued certificates elsewhere, since `create_dir_all` silently
    // follows a symlink to a directory.
    match std::fs::symlink_metadata(dir) {
        Ok(meta) if meta.file_type().is_symlink() => {
            return Err(Error::Config(format!(
                "ACME cache directory {} is a symlink; refusing to use it",
                dir.display()
            )));
        }
        Ok(meta) if !meta.is_dir() => {
            return Err(Error::Config(format!(
                "ACME cache path {} exists but is not a directory",
                dir.display()
            )));
        }
        Ok(_) => {}                                              // a real directory: fine
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} // create it below
        // Surface an unexpected inspection failure (e.g. permission denied)
        // directly, rather than masking it as a later "create failed".
        Err(e) => {
            return Err(Error::Config(format!(
                "cannot inspect ACME cache directory {}: {e}",
                dir.display()
            )));
        }
    }
    create_private_dir(dir).map_err(|e| {
        Error::Config(format!(
            "failed to create ACME cache directory {}: {e}",
            dir.display()
        ))
    })?;
    // Probe with a per-run-unique name (pid + a high-resolution nonce) created
    // exclusively (create_new), so the probe never follows a symlink or clobbers
    // an existing file — the cache dir may be writable by a less-trusted user
    // than this process — and a stale probe left by a crashed run with a reused
    // pid cannot cause a false "not writable".
    let nonce = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_nanos())
        .unwrap_or(0);
    let probe = dir.join(format!(
        ".alighieri-acme-write-test.{}.{nonce}",
        std::process::id()
    ));
    match std::fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&probe)
    {
        Ok(file) => {
            drop(file); // close before removing (Windows cannot remove an open file)
                        // The write already proved the directory writable, so a failed cleanup
                        // does not block startup, but warn so the leftover probe is visible.
            if let Err(e) = std::fs::remove_file(&probe) {
                tracing::warn!(
                    path = %probe.display(),
                    error = %e,
                    "could not remove ACME cache write-probe file"
                );
            }
            Ok(())
        }
        Err(e) => Err(Error::Config(format!(
            "ACME cache directory {} is not writable: {e}",
            dir.display()
        ))),
    }
}

/// Creates `dir` (and any missing parents) owner-only (mode `0700`) on Unix, so
/// the ACME account key and certificates it will hold are not group/other
/// readable. An already-existing directory is tightened to `0700` best-effort:
/// `DirBuilder`'s mode applies only to directories it creates, and a cache owned
/// by another user cannot be chmod'd by us, which only warns since it may be
/// deliberately managed elsewhere.
#[cfg(unix)]
fn create_private_dir(dir: &Path) -> std::io::Result<()> {
    use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt};
    // mkdir with a mode is atomic, so a freshly created directory is private from
    // the start (no window where it exists with looser bits).
    std::fs::DirBuilder::new()
        .recursive(true)
        .mode(0o700)
        .create(dir)?;
    // Tighten an already-existing directory through a descriptor, not the path:
    // open it with O_NOFOLLOW|O_DIRECTORY so a symlink swapped in after the
    // earlier check/create fails the open (closing the TOCTOU window the
    // `symlink_metadata` check leaves), then fstat/fchmod the descriptor, which
    // refers to the real directory regardless of any later path swap.
    let dir_file = std::fs::OpenOptions::new()
        .read(true)
        .custom_flags(libc::O_NOFOLLOW | libc::O_DIRECTORY)
        .open(dir)?;
    let mode = dir_file.metadata()?.permissions().mode() & 0o777;
    if mode & 0o077 != 0 {
        if let Err(e) = dir_file.set_permissions(std::fs::Permissions::from_mode(0o700)) {
            tracing::warn!(
                dir = %dir.display(),
                error = %e,
                "ACME cache directory is group/other-accessible (mode {mode:o}) and could not be tightened to 0700; it holds the ACME account key and issued certificates"
            );
        }
    }
    Ok(())
}

#[cfg(not(unix))]
fn create_private_dir(dir: &Path) -> std::io::Result<()> {
    std::fs::create_dir_all(dir)
}

fn load_certs(path: &Path) -> Result<Vec<CertificateDer<'static>>> {
    let file = File::open(path).map_err(|e| {
        Error::Config(format!(
            "failed to open TLS certificate file {}: {e}",
            path.display()
        ))
    })?;
    let mut reader = BufReader::new(file);
    CertificateDer::pem_reader_iter(&mut reader)
        .collect::<std::result::Result<Vec<_>, _>>()
        .map_err(|e| {
            Error::Config(format!(
                "failed to read TLS certificate file {}: {e}",
                path.display()
            ))
        })
}

fn load_private_key(path: &Path) -> Result<PrivateKeyDer<'static>> {
    let file = File::open(path).map_err(|e| {
        Error::Config(format!(
            "failed to open TLS private key file {}: {e}",
            path.display()
        ))
    })?;
    let mut reader = BufReader::new(file);
    // Accepts PKCS#8, PKCS#1 (RSA), and SEC1 (EC) private keys.
    PrivateKeyDer::from_pem_reader(&mut reader).map_err(|e| match e {
        pem::Error::NoItemsFound => Error::Config(format!(
            "TLS private key file {} did not contain a supported private key",
            path.display()
        )),
        other => Error::Config(format!(
            "failed to read TLS private key file {}: {other}",
            path.display()
        )),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    const CERT: &str = r#"-----BEGIN CERTIFICATE-----
MIIDCTCCAfGgAwIBAgIUOCKlMVRPWTVj4douy93+KLpOLwMwDQYJKoZIhvcNAQEL
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI2MDYwMTE5MjIzNFoXDTI2MDYw
MjE5MjIzNFowFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF
AAOCAQ8AMIIBCgKCAQEAhA94gPhVMU0tDg4YJGUI1JW7F6Vf5G//49yLXw66K2qH
nV5ByAMXlUQX57m6ahmxJOmjFJoDBN08NZk60dEyFpC0nmIXSDXVpr+vJOi1EsJs
FRDdpNK0A3b5sVzHFWnEqWpCi6+4fxYWqb0Vuda5oSAUydmDiTfzfVf/nGicfzGf
Zy2ELZQSszRyVWZ3bLH6hrtvutznULGF2D6hBvjuW9s35rYWbyyUOKt635FxxS3f
uy0K3uBlZHpw8XisxeNEOTD+qVe287BePRbMR8SdzA0OqEwM9l1bxtzoNRDIAKkt
0J/XxeXvaE6/SDKwHMyzhxjrdwX/KH60+j4olHTJpQIDAQABo1MwUTAdBgNVHQ4E
FgQU2J8UlQt9vqXo4xT4ZM/SYWDYik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PA==
-----END PRIVATE KEY-----
"#;

    #[test]
    fn loads_acceptor_from_pem_files() {
        let dir = tempfile::tempdir().unwrap();
        let cert_file = dir.path().join("server.crt");
        let key_file = dir.path().join("server.key");
        fs::write(&cert_file, CERT).unwrap();
        fs::write(&key_file, KEY).unwrap();

        let config = TlsConfig::Files {
            cert_file,
            key_file,
        };

        let setup = load_acceptor(Some(&config)).unwrap().unwrap();
        assert!(setup.acme_driver.is_none());
    }

    #[test]
    fn acme_config_builds_an_acceptor_and_driver() {
        // Constructing the ACME state and acceptor does no network I/O, so this
        // exercises the wiring offline.
        let dir = tempfile::tempdir().unwrap();
        let config = TlsConfig::Acme(crate::config::AcmeConfig {
            domains: vec!["proxy.example.com".into()],
            email: Some("admin@example.com".into()),
            cache_dir: dir.path().to_path_buf(),
            staging: true,
        });
        let setup = load_acceptor(Some(&config)).unwrap().unwrap();
        assert!(setup.acme_driver.is_some());
    }

    #[test]
    fn acme_listener_offers_the_tls_alpn_challenge_protocol() {
        // Regression: the listener must answer TLS-ALPN-01. The challenge config
        // has to offer the `acme-tls/1` ALPN (or Let's Encrypt cannot negotiate
        // the challenge and no certificate is ever issued), while the default
        // config must NOT force it on ordinary clients.
        let dir = tempfile::tempdir().unwrap();
        let config = TlsConfig::Acme(crate::config::AcmeConfig {
            domains: vec!["proxy.example.com".into()],
            email: None,
            cache_dir: dir.path().to_path_buf(),
            staging: true,
        });
        let setup = load_acceptor(Some(&config)).unwrap().unwrap();
        let acme_tls = b"acme-tls/1".to_vec();
        match setup.listener {
            TlsListener::Acme { default, challenge } => {
                assert!(
                    challenge.alpn_protocols.contains(&acme_tls),
                    "challenge config must offer the acme-tls/1 ALPN"
                );
                assert!(
                    !default.alpn_protocols.contains(&acme_tls),
                    "default config must not force acme-tls/1 on normal clients"
                );
            }
            TlsListener::Manual(_) => panic!("expected an ACME listener"),
        }
    }

    #[test]
    fn validation_is_side_effect_free_but_runtime_prepares_cache() {
        let dir = tempfile::tempdir().unwrap();
        let cache = dir.path().join("not-created-yet");
        let config = TlsConfig::Acme(crate::config::AcmeConfig {
            domains: vec!["proxy.example.com".into()],
            email: None,
            cache_dir: cache.clone(),
            staging: true,
        });

        // Pure validation must not touch the filesystem.
        build_acceptor(Some(&config), false).unwrap();
        assert!(!cache.exists(), "validation must not create the cache dir");

        // Runtime setup creates (and write-probes) it.
        build_acceptor(Some(&config), true).unwrap();
        assert!(cache.exists());
    }

    #[test]
    fn rejects_missing_private_key() {
        let dir = tempfile::tempdir().unwrap();
        let cert_file = dir.path().join("server.crt");
        let key_file = dir.path().join("server.key");
        fs::write(&cert_file, CERT).unwrap();
        fs::write(&key_file, "not a private key").unwrap();

        let config = TlsConfig::Files {
            cert_file,
            key_file,
        };
        let err = match load_acceptor(Some(&config)) {
            Ok(_) => panic!("invalid private key was accepted"),
            Err(err) => err,
        };
        assert!(err.to_string().contains("did not contain"));
    }

    #[cfg(unix)]
    #[test]
    fn ensure_writable_dir_rejects_a_symlinked_cache_dir() {
        use std::os::unix::fs::symlink;
        let tmp = tempfile::tempdir().unwrap();
        let real = tmp.path().join("real");
        fs::create_dir(&real).unwrap();
        let link = tmp.path().join("cache");
        symlink(&real, &link).unwrap();

        let err = ensure_writable_dir(&link).unwrap_err();
        assert!(err.to_string().contains("symlink"), "{err}");
    }

    #[cfg(unix)]
    #[test]
    fn ensure_writable_dir_creates_a_private_cache_dir() {
        use std::os::unix::fs::PermissionsExt;
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path().join("acme");

        ensure_writable_dir(&cache).unwrap();

        let mode = fs::metadata(&cache).unwrap().permissions().mode() & 0o777;
        assert_eq!(
            mode, 0o700,
            "a fresh cache dir must be owner-only, got {mode:o}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn ensure_writable_dir_tightens_an_existing_loose_dir() {
        use std::os::unix::fs::PermissionsExt;
        let tmp = tempfile::tempdir().unwrap();
        let cache = tmp.path().join("acme");
        fs::create_dir(&cache).unwrap();
        fs::set_permissions(&cache, fs::Permissions::from_mode(0o755)).unwrap();

        ensure_writable_dir(&cache).unwrap();

        let mode = fs::metadata(&cache).unwrap().permissions().mode() & 0o777;
        assert_eq!(
            mode, 0o700,
            "an existing loose cache dir must be tightened, got {mode:o}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn create_private_dir_does_not_chmod_through_a_symlink() {
        // A symlink raced onto the cache path after the earlier check must not let
        // the chmod follow it to the target. `DirBuilder` no-ops (the link's target
        // dir already exists, so the recursive create returns Ok), then the
        // `O_NOFOLLOW` open refuses the symlink — so the target is never chmod'd.
        use std::os::unix::fs::{symlink, PermissionsExt};
        let tmp = tempfile::tempdir().unwrap();
        let target = tmp.path().join("target");
        fs::create_dir(&target).unwrap();
        fs::set_permissions(&target, fs::Permissions::from_mode(0o755)).unwrap();
        let link = tmp.path().join("cache");
        symlink(&target, &link).unwrap();

        assert!(create_private_dir(&link).is_err());
        let target_mode = fs::metadata(&target).unwrap().permissions().mode() & 0o777;
        assert_eq!(
            target_mode, 0o755,
            "the symlink target must not be chmod'd, got {target_mode:o}"
        );
    }
}