From 94c912f54fada05d2d53b30d9a1b9cdce1d8862c Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Fri, 8 Aug 2025 09:51:26 -0700 Subject: [PATCH] can't seem to start the server --- Cargo.lock | 308 ++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 6 + src/app/widgets.rs | 2 +- src/config.rs | 21 +++- src/http_server.rs | 120 ++++++++++++++++-- src/models.rs | 2 +- 6 files changed, 427 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0255fb1..3dfc08e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -97,6 +97,12 @@ dependencies = [ "windows-sys 0.60.2", ] +[[package]] +name = "arc-swap" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" + [[package]] name = "async-trait" version = "0.1.88" @@ -129,6 +135,29 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +[[package]] +name = "aws-lc-rs" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08b5d4e069cbc868041a64bd68dc8cb39a0d79585cd6c5a24caa8c2d622121be" +dependencies = [ + "aws-lc-sys", + "zeroize", +] + +[[package]] +name = "aws-lc-sys" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbfd150b5dbdb988bcc8fb1fe787eb6b7ee6180ca24da683b61ea5405f3d43ff" +dependencies = [ + "bindgen", + "cc", + "cmake", + "dunce", + "fs_extra", +] + [[package]] name = "axum" version = "0.8.4" @@ -137,7 +166,7 @@ checksum = "021e862c184ae977658b36c4500f7feac3221ca5da43e3f25bd04ab6c79a29b5" dependencies = [ "axum-core", "axum-macros", - "bytes", + "bytes 1.10.1", "form_urlencoded", "futures-util", "http", @@ -170,7 +199,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68464cd0412f486726fb3373129ef5d2993f90c34bc2bc1c1e9943b2f4fc7ca6" dependencies = [ - "bytes", + "bytes 1.10.1", "futures-core", "http", "http-body", @@ -195,6 +224,28 @@ dependencies = [ "syn 2.0.104", ] +[[package]] +name = "axum-server" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "495c05f60d6df0093e8fb6e74aa5846a0ad06abaf96d76166283720bf740f8ab" +dependencies = [ + "arc-swap", + "bytes 1.10.1", + "fs-err", + "http", + "http-body", + "hyper", + "hyper-util", + "pin-project-lite", + "rustls", + "rustls-pemfile", + "rustls-pki-types", + "tokio", + "tokio-rustls", + "tower-service", +] + [[package]] name = "backtrace" version = "0.3.75" @@ -216,6 +267,29 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "bindgen" +version = "0.69.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "itertools 0.12.1", + "lazy_static", + "lazycell", + "log", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn 2.0.104", + "which", +] + [[package]] name = "bitflags" version = "2.9.1" @@ -249,6 +323,16 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "bytes" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" +dependencies = [ + "byteorder", + "iovec", +] + [[package]] name = "bytes" version = "1.10.1" @@ -276,9 +360,20 @@ version = "1.2.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3a42d84bb6b69d3a8b3eaacf0d88f179e1929695e1ad012b6cf64d9caaa5fd2" dependencies = [ + "jobserver", + "libc", "shlex", ] +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + [[package]] name = "cfg-if" version = "1.0.1" @@ -299,6 +394,26 @@ dependencies = [ "windows-link", ] +[[package]] +name = "clang-sys" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4" +dependencies = [ + "glob", + "libc", + "libloading", +] + +[[package]] +name = "cmake" +version = "0.1.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7caa3f9de89ddbe2c607f4101924c5abec803763ae9534e4f4d7d8f84aa81f0" +dependencies = [ + "cc", +] + [[package]] name = "colorchoice" version = "1.0.4" @@ -466,6 +581,12 @@ dependencies = [ "syn 2.0.104", ] +[[package]] +name = "dunce" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" + [[package]] name = "educe" version = "0.6.0" @@ -610,6 +731,28 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fs-err" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88d7be93788013f265201256d58f04936a8079ad5dc898743aa20525f503b683" +dependencies = [ + "autocfg", + "tokio", +] + +[[package]] +name = "fs_extra" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" + +[[package]] +name = "futures" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678" + [[package]] name = "futures" version = "0.3.31" @@ -747,6 +890,12 @@ version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +[[package]] +name = "glob" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" + [[package]] name = "h2" version = "0.4.12" @@ -754,7 +903,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3c0b69cfcb4e1b9f1bf2f53f95f766e4661169728ec61cd3fe5a0166f2d1386" dependencies = [ "atomic-waker", - "bytes", + "bytes 1.10.1", "fnv", "futures-core", "futures-sink", @@ -789,13 +938,22 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "home" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589533453244b0995c858700322199b2becb13b627df2851f64a2775d024abcf" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "http" version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4a85d31aea989eead29a3aaf9e1115a180df8282431156e533de47660892565" dependencies = [ - "bytes", + "bytes 1.10.1", "fnv", "itoa", ] @@ -806,7 +964,7 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" dependencies = [ - "bytes", + "bytes 1.10.1", "http", ] @@ -816,7 +974,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" dependencies = [ - "bytes", + "bytes 1.10.1", "futures-core", "http", "http-body", @@ -841,7 +999,7 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc2b571658e38e0c01b1fdca3bbbe93c00d3d71693ff2770043f8c29bc7d6f80" dependencies = [ - "bytes", + "bytes 1.10.1", "futures-channel", "futures-util", "h2", @@ -878,7 +1036,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" dependencies = [ - "bytes", + "bytes 1.10.1", "http-body-util", "hyper", "hyper-util", @@ -895,7 +1053,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d9b05277c7e8da2c93a568989bb6207bef0112e8d17df7a6eda4a3cf143bc5e" dependencies = [ "base64", - "bytes", + "bytes 1.10.1", "futures-channel", "futures-core", "futures-util", @@ -1097,6 +1255,15 @@ dependencies = [ "libc", ] +[[package]] +name = "iovec" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" +dependencies = [ + "libc", +] + [[package]] name = "ipnet" version = "2.11.0" @@ -1119,6 +1286,15 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.13.0" @@ -1158,17 +1334,30 @@ dependencies = [ "syn 2.0.104", ] +[[package]] +name = "jobserver" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38f262f097c174adebe41eb73d66ae9c06b2844fb0da69969647bbddd9b0538a" +dependencies = [ + "getrandom 0.3.3", + "libc", +] + [[package]] name = "jocalsend" version = "0.1.0" dependencies = [ "axum", + "axum-server", "chrono", "crossterm", "directories", "env_logger", "figment", - "futures", + "futures 0.3.31", + "hyper", + "hyper-util", "julid-rs", "local-ip-address", "log", @@ -1185,9 +1374,12 @@ dependencies = [ "sha256", "thiserror", "tokio", + "tokio-io", "tokio-rustls", "toml 0.9.5", + "tower", "tower-http", + "tower-service", "tui-input", "tui-logger", "unicode-segmentation", @@ -1219,12 +1411,28 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" version = "0.2.174" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" +[[package]] +name = "libloading" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07033963ba89ebaf1584d767badaa2e8fcec21aedea6b8c0346d487d49c28667" +dependencies = [ + "cfg-if", + "windows-targets 0.52.6", +] + [[package]] name = "libredox" version = "0.1.9" @@ -1318,6 +1526,12 @@ dependencies = [ "unicase", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.8.9" @@ -1394,6 +1608,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -1605,6 +1829,16 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "prettyplease" +version = "0.2.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "061c1221631e079b26479d25bbf2275bfe5917ae8419cd7e34f13bfc2aa7539a" +dependencies = [ + "proc-macro2", + "syn 2.0.104", +] + [[package]] name = "proc-macro2" version = "1.0.95" @@ -1684,7 +1918,7 @@ dependencies = [ "crossterm", "indoc", "instability", - "itertools", + "itertools 0.13.0", "lru", "paste", "strum", @@ -1772,7 +2006,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cbc931937e6ca3a06e3b6c0aa7841849b160a90351d6ab467a8b9b9959767531" dependencies = [ "base64", - "bytes", + "bytes 1.10.1", "encoding_rs", "futures-core", "h2", @@ -1825,6 +2059,12 @@ version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace" +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustix" version = "0.38.44" @@ -1857,6 +2097,7 @@ version = "0.23.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0ebcbd2f03de0fc1122ad9bb24b127a5a6cd51d72604a3f3c50ac459762b6cc" dependencies = [ + "aws-lc-rs", "log", "once_cell", "rustls-pki-types", @@ -1865,6 +2106,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rustls-pemfile" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "rustls-pki-types" version = "1.12.0" @@ -1880,6 +2130,7 @@ version = "0.103.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0a17884ae0c1b773f1ccd2bd4a8c72f16da897310a98b0e84bf349ad5ead92fc" dependencies = [ + "aws-lc-rs", "ring", "rustls-pki-types", "untrusted", @@ -2025,7 +2276,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f880fc8562bdeb709793f00eb42a2ad0e672c4f883bbe59122b926eca935c8f6" dependencies = [ "async-trait", - "bytes", + "bytes 1.10.1", "hex", "sha2", "tokio", @@ -2267,7 +2518,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89e49afdadebb872d3145a5638b59eb0691ea23e46ca484037cfab3b76b95038" dependencies = [ "backtrace", - "bytes", + "bytes 1.10.1", "io-uring", "libc", "mio", @@ -2278,6 +2529,17 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "tokio-io" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57fc868aae093479e3131e3d165c93b1c7474109d13c90ec0dda2a1bbfff0674" +dependencies = [ + "bytes 0.4.12", + "futures 0.1.31", + "log", +] + [[package]] name = "tokio-macros" version = "2.5.0" @@ -2315,7 +2577,7 @@ version = "0.7.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14307c986784f72ef81c89db7d9e28d6ac26d16213b109ea501696195e6e3ce5" dependencies = [ - "bytes", + "bytes 1.10.1", "futures-core", "futures-sink", "pin-project-lite", @@ -2425,7 +2687,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adc82fd73de2a9722ac5da747f12383d2bfdb93591ee6c58486e0097890f05f2" dependencies = [ "bitflags", - "bytes", + "bytes 1.10.1", "futures-util", "http", "http-body", @@ -2540,7 +2802,7 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3644627a5af5fa321c95b9b235a72fd24cd29c648c2c379431e6628655627bf" dependencies = [ - "itertools", + "itertools 0.13.0", "unicode-segmentation", "unicode-width 0.1.14", ] @@ -2703,6 +2965,18 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "which" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" +dependencies = [ + "either", + "home", + "once_cell", + "rustix 0.38.44", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 63531ae..976c763 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,12 +5,15 @@ edition = "2024" [dependencies] axum = { version = "0.8", features = ["macros"] } +axum-server = { version = "0.7.2", features = ["tls-rustls", "tokio-rustls"] } chrono = "0.4" crossterm = { version = "0.28", features = ["event-stream"] } directories = "6" env_logger = "0.11" figment = { version = "0.10", features = ["toml", "test", "env"] } futures = "0.3" +hyper = { version = "1.6.0", features = ["http1"] } +hyper-util = { version = "0.1.16", features = ["http1", "tokio", "service", "server"] } julid-rs = { version = "1", default-features = false, features = ["serde"] } local-ip-address = "0.6" log = "0.4" @@ -27,9 +30,12 @@ serde_json = "1" sha256 = "1.6" thiserror = "2" tokio = { version = "1", default-features = false, features = ["time", "macros", "rt-multi-thread"] } +tokio-io = "0.1.13" tokio-rustls = { version = "0.26.2", default-features = false, features = ["tls12", "logging"] } toml = "0.9.5" +tower = { version = "0.5.2", features = ["make"] } tower-http = { version = "0.6", features = ["limit"] } +tower-service = "0.3.3" tui-input = "0.14" tui-logger = { version = "0.17", features = ["crossterm"] } unicode-segmentation = "1" diff --git a/src/app/widgets.rs b/src/app/widgets.rs index 512ea6d..46e9f57 100644 --- a/src/app/widgets.rs +++ b/src/app/widgets.rs @@ -147,7 +147,7 @@ impl Widget for &mut App { buf, ); network_info( - &self.service.config.local_ip_addr, + &self.service.config.local_ip_addr.unwrap_or([0u8; 4].into()), footer_left.inner(footer_margin), buf, ); diff --git a/src/config.rs b/src/config.rs index 7b1a70b..072c709 100644 --- a/src/config.rs +++ b/src/config.rs @@ -23,10 +23,22 @@ pub struct Config { pub download_dir: PathBuf, pub data_dir: PathBuf, #[serde(skip_serializing)] - pub local_ip_addr: Ipv4Addr, + pub local_ip_addr: Option, pub device: Device, } +impl Default for Config { + fn default() -> Self { + Self { + multicast_addr: SocketAddrV4::new(MULTICAST_IP, DEFAULT_PORT), + download_dir: Default::default(), + data_dir: Default::default(), + local_ip_addr: None, + device: Default::default(), + } + } +} + impl Config { pub fn new() -> Result { let dirs = directories::BaseDirs::new().ok_or(LocalSendError::NoHomeDir)?; @@ -56,7 +68,7 @@ impl Config { let config = Self { multicast_addr: SocketAddrV4::new(MULTICAST_IP, DEFAULT_PORT), download_dir, - local_ip_addr, + local_ip_addr: Some(local_ip_addr), data_dir, device: Device { alias: rustix::system::uname() @@ -83,9 +95,10 @@ impl Config { Ok(config) } + /// Returns (key, cert) paths pub fn ssl(&self) -> (PathBuf, PathBuf) { - let key = self.data_dir.join("jocalsend").join("key.pem"); - let cert = self.data_dir.join("jocalsend").join("cert.pem"); + let key = self.data_dir.join("key.pem"); + let cert = self.data_dir.join("cert.pem"); (key, cert) } } diff --git a/src/http_server.rs b/src/http_server.rs index f92ce63..2468e7e 100644 --- a/src/http_server.rs +++ b/src/http_server.rs @@ -1,12 +1,27 @@ -use std::net::SocketAddr; +use std::{net::SocketAddr, path::Path, sync::Arc}; use axum::{ Json, Router, - extract::DefaultBodyLimit, + extract::{ConnectInfo, DefaultBodyLimit, Request}, routing::{get, post}, }; -use tokio::{net::TcpListener, sync::mpsc}; +use axum_server::tls_rustls::RustlsConfig; +use hyper::body::Incoming; +use hyper_util::rt::{TokioExecutor, TokioIo}; +use tokio::{ + net::TcpListener, + sync::{Mutex, mpsc}, +}; +use tokio_rustls::{ + TlsAcceptor, + rustls::{ + ServerConfig, + pki_types::{CertificateDer, PrivateKeyDer, pem::PemObject}, + }, +}; +use tower::MakeService; use tower_http::limit::RequestBodyLimitLayer; +use tower_service::Service; use crate::{ JocalService, @@ -19,14 +34,76 @@ impl JocalService { let app = self.create_router(); // TODO: make addr config let addr = SocketAddr::from(([0, 0, 0, 0], self.config.device.port)); + let (key, cert) = self.config.ssl(); let listener = TcpListener::bind(&addr).await?; - axum::serve( - listener, - app.into_make_service_with_connect_info::(), - ) - .with_graceful_shutdown(shutdown(stop_rx)) - .await?; + let ssl_config = rustls_server_config(key, cert); + + // let acceptor = TlsAcceptor::from(ssl_config); + + // let stop = Arc::new(Mutex::new(stop_rx)); + + /* + loop { + let tower_service = app.clone(); + let tls_acceptor = acceptor.clone(); + let stop = stop.clone(); + let mut stop = stop.lock().await; + + tokio::select! { + // Wait for new tcp connection + Ok((cnx, addr)) = listener.accept() => { + tokio::spawn(async move { + // Wait for tls handshake to happen + let Ok(stream) = tls_acceptor.accept(cnx).await else { + log::error!("error during tls handshake connection from {}", addr); + return; + }; + + // Hyper has its own `AsyncRead` and `AsyncWrite` traits and doesn't use tokio. + // `TokioIo` converts between them. + let stream = TokioIo::new(stream); + + // Hyper also has its own `Service` trait and doesn't use tower. We can use + // `hyper::service::service_fn` to create a hyper `Service` that calls our app + // through `tower::Service::call`. + let hyper_service = + hyper::service::service_fn(move |request: Request| { + // We have to clone `tower_service` because hyper's `Service` uses `&self` + // whereas tower's `Service` requires `&mut self`. + // + // We don't need to call `poll_ready` since `Router` is always ready. + tower_service.clone().call(request) + }); + + let ret = hyper_util::server::conn::auto::Builder::new(TokioExecutor::new()) + .serve_connection_with_upgrades(stream, hyper_service) + .await; + + if let Err(err) = ret { + log::error!("error serving connection from {}: {}", addr, err); + } + }); + } + _ = stop.recv() => { + log::info!("stopping http server"); + break; + } + } + } + */ + + axum_server::bind_rustls(addr, ssl_config) + .serve(app.into_make_service_with_connect_info::()) + .await?; + + // axum::serve( + // listener, + // app.into_make_service_with_connect_info::(), + // ) + // .with_graceful_shutdown(shutdown(stop_rx)) + // .await?; + Ok(()) } @@ -49,3 +126,28 @@ impl JocalService { async fn shutdown(mut rx: mpsc::Receiver<()>) { rx.recv().await.unwrap_or_default() } + +fn rustls_server_config(key: impl AsRef, cert: impl AsRef) -> RustlsConfig { + let key = match PrivateKeyDer::from_pem_file(&key) { + Ok(k) => k, + Err(e) => { + let path = key.as_ref().display(); + log::error!("could not open {path} for reading; got {e:?}"); + panic!() + } + }; + + let certs = CertificateDer::pem_file_iter(cert) + .unwrap() + .map(|cert| cert.unwrap()) + .collect(); + + let mut config = ServerConfig::builder() + .with_no_client_auth() + .with_single_cert(certs, key) + .expect("bad certificate/key"); + + config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()]; + + RustlsConfig::from_config(config.into()) +} diff --git a/src/models.rs b/src/models.rs index 5626e9f..03a0b70 100644 --- a/src/models.rs +++ b/src/models.rs @@ -126,7 +126,7 @@ impl Default for Device { device_type: Some(DeviceType::Headless), fingerprint: Julid::new().to_string(), port: crate::DEFAULT_PORT, - protocol: "http".to_string(), + protocol: "https".to_string(), download: false, announce: Some(true), }