feat: Basic HTTP listener
This commit is contained in:
parent
c77223048c
commit
452da73413
7 changed files with 350 additions and 7 deletions
265
Cargo.lock
generated
265
Cargo.lock
generated
|
|
@ -33,6 +33,64 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atomic-waker"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
|
||||
|
||||
[[package]]
|
||||
name = "axum"
|
||||
version = "0.8.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90"
|
||||
dependencies = [
|
||||
"axum-core",
|
||||
"bytes",
|
||||
"form_urlencoded",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"http-body-util",
|
||||
"hyper",
|
||||
"hyper-util",
|
||||
"itoa",
|
||||
"matchit",
|
||||
"memchr",
|
||||
"mime",
|
||||
"percent-encoding",
|
||||
"pin-project-lite",
|
||||
"serde_core",
|
||||
"serde_json",
|
||||
"serde_path_to_error",
|
||||
"serde_urlencoded",
|
||||
"sync_wrapper",
|
||||
"tokio",
|
||||
"tower",
|
||||
"tower-layer",
|
||||
"tower-service",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "axum-core"
|
||||
version = "0.5.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"http",
|
||||
"http-body",
|
||||
"http-body-util",
|
||||
"mime",
|
||||
"pin-project-lite",
|
||||
"sync_wrapper",
|
||||
"tower-layer",
|
||||
"tower-service",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.22.1"
|
||||
|
|
@ -67,6 +125,24 @@ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
|||
name = "dn_escape"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "form_urlencoded"
|
||||
version = "1.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf"
|
||||
dependencies = [
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-core"
|
||||
version = "0.3.34"
|
||||
|
|
@ -121,6 +197,92 @@ dependencies = [
|
|||
"r-efi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "http"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"itoa",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "http-body"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"http",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "http-body-util"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"http",
|
||||
"http-body",
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "httparse"
|
||||
version = "1.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
|
||||
|
||||
[[package]]
|
||||
name = "httpdate"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
|
||||
|
||||
[[package]]
|
||||
name = "hyper"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "27b501faa50e7a26c3d3560ca625132f4078a17771f4810baf70475ae48cbe43"
|
||||
dependencies = [
|
||||
"atomic-waker",
|
||||
"bytes",
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"http",
|
||||
"http-body",
|
||||
"httparse",
|
||||
"httpdate",
|
||||
"itoa",
|
||||
"pin-project-lite",
|
||||
"smallvec",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyper-util"
|
||||
version = "0.1.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"http",
|
||||
"http-body",
|
||||
"hyper",
|
||||
"pin-project-lite",
|
||||
"tokio",
|
||||
"tower-service",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.104"
|
||||
|
|
@ -176,6 +338,7 @@ name = "llldap"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"argh",
|
||||
"axum",
|
||||
"camino",
|
||||
"dn_escape",
|
||||
"futures-util",
|
||||
|
|
@ -183,6 +346,7 @@ dependencies = [
|
|||
"serde",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"tower",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"uuid",
|
||||
|
|
@ -194,12 +358,24 @@ version = "0.4.34"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6"
|
||||
|
||||
[[package]]
|
||||
name = "matchit"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||
|
||||
[[package]]
|
||||
name = "mime"
|
||||
version = "0.3.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
|
|
@ -269,6 +445,12 @@ version = "0.8.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7011d97b484a5ebdc4b1fdb3b12d5e4bbbea56e9d22b688f2e79e04b65a7d8a6"
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.17"
|
||||
|
|
@ -305,6 +487,12 @@ version = "1.0.23"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.229"
|
||||
|
|
@ -335,6 +523,42 @@ dependencies = [
|
|||
"syn 3.0.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.151"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_path_to_error"
|
||||
version = "0.1.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"serde",
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_urlencoded"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd"
|
||||
dependencies = [
|
||||
"form_urlencoded",
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.7"
|
||||
|
|
@ -388,6 +612,12 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sync_wrapper"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "2.0.20"
|
||||
|
|
@ -456,12 +686,41 @@ dependencies = [
|
|||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tower"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"pin-project-lite",
|
||||
"sync_wrapper",
|
||||
"tokio",
|
||||
"tower-layer",
|
||||
"tower-service",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tower-layer"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"
|
||||
|
||||
[[package]]
|
||||
name = "tower-service"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
|
||||
dependencies = [
|
||||
"log",
|
||||
"pin-project-lite",
|
||||
"tracing-attributes",
|
||||
"tracing-core",
|
||||
|
|
@ -601,3 +860,9 @@ checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
|||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ edition = "2024"
|
|||
|
||||
[dependencies]
|
||||
argh = "0.1.19"
|
||||
axum = { version = "0.8.9", optional = true }
|
||||
camino = "1.2.5"
|
||||
dn_escape = { path = "vendor/dn_escape" }
|
||||
futures-util = { version = "0.3.34", features = ["sink"] }
|
||||
|
|
@ -12,6 +13,7 @@ ldap3_proto = "0.8.1"
|
|||
serde = { version = "1.0.229", features = ["derive"] }
|
||||
tokio = { version = "1.53.1", features = ["macros", "net", "rt", "time", "sync"] }
|
||||
tokio-util = { version = "0.7.19", features = ["codec"] }
|
||||
tower = "0.5.3"
|
||||
tracing = "0.1.44"
|
||||
tracing-subscriber = "0.3.23"
|
||||
uuid = { version = "1.26.0", features = ["v4"] }
|
||||
|
|
@ -22,3 +24,7 @@ complexity = "deny"
|
|||
pedantic = "deny"
|
||||
perf = "deny"
|
||||
style = "deny"
|
||||
|
||||
[features]
|
||||
default = [ "http" ]
|
||||
http = ["dep:axum"]
|
||||
|
|
|
|||
10
src/cli.rs
10
src/cli.rs
|
|
@ -3,7 +3,11 @@ use argh::FromArgs;
|
|||
/// Run the llldap server
|
||||
#[derive(FromArgs)]
|
||||
pub struct CliArgs {
|
||||
/// address or socket to listen on
|
||||
#[argh(positional, default = "String::from(\"127.0.0.1:3389\")")]
|
||||
pub listen: String,
|
||||
/// address or socket to listen on for LDAP connections
|
||||
#[argh(option, default = "String::from(\"127.0.0.1:3389\")")]
|
||||
pub listen_ldap: String,
|
||||
|
||||
/// address or socket to listen on for HTTP connections
|
||||
#[argh(option, default = "String::from(\"127.0.0.1:3390\")")]
|
||||
pub listen_http: String,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ impl<D: DatabaseInterface> DatabaseInterface for Database<D> {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait DatabaseInterface {
|
||||
pub trait DatabaseInterface: Clone + Send + Sync + 'static {
|
||||
async fn get_user(&self, user: &UserRef) -> Result<Option<User>, BoxedError>;
|
||||
async fn create_user(
|
||||
&mut self,
|
||||
|
|
|
|||
57
src/http/mod.rs
Normal file
57
src/http/mod.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use axum::Router;
|
||||
use axum::routing::get;
|
||||
use axum::serve::Listener as AxumListener;
|
||||
|
||||
use crate::db::{Database, DatabaseInterface};
|
||||
use crate::listener::{Listener, ListenerKind};
|
||||
use crate::stream::{AbstractSocketAddr, AbstractStreamKind};
|
||||
|
||||
impl AxumListener for Listener {
|
||||
type Io = AbstractStreamKind;
|
||||
type Addr = AbstractSocketAddr;
|
||||
|
||||
async fn accept(&mut self) -> (Self::Io, Self::Addr) {
|
||||
loop {
|
||||
let res = match &self.kind {
|
||||
ListenerKind::Tcp(l) => l
|
||||
.accept()
|
||||
.await
|
||||
.map(|(stream, remote_addr)| (stream.into(), remote_addr.into())),
|
||||
ListenerKind::Uds(l) => l
|
||||
.accept()
|
||||
.await
|
||||
.map(|(stream, remote_addr)| (stream.into(), remote_addr.into())),
|
||||
};
|
||||
|
||||
match res {
|
||||
Ok((stream, remote_addr)) => return (stream, remote_addr),
|
||||
Err(e) => {
|
||||
// Here the error could be fatal, or could simply be that a client aborted the connected,
|
||||
// in which case we don't want to crash the server, simply skip this client connection.
|
||||
// https://doc.rust-lang.org/stable/std/net/struct.TcpListener.html#errors
|
||||
match e.kind() {
|
||||
std::io::ErrorKind::ConnectionAborted => {}
|
||||
_ => panic!("Unrecoverable HTTP client connection error: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn local_addr(&self) -> std::io::Result<Self::Addr> {
|
||||
match &self.kind {
|
||||
ListenerKind::Tcp(l) => l.local_addr().map(Into::into),
|
||||
ListenerKind::Uds(l) => l.local_addr().map(Into::into),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn http_listen<D: DatabaseInterface>(listener: Listener, db: Database<D>) {
|
||||
let app = Router::new().route("/", get(handler)).with_state(db);
|
||||
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
pub async fn handler() -> &'static str {
|
||||
"hello world"
|
||||
}
|
||||
|
|
@ -153,7 +153,7 @@ fn search_entry_from_user(user: &User, req_attrs: &[String]) -> LdapSearchResult
|
|||
for attr in req_attrs {
|
||||
if let Some(attr_value) = match attr.as_str() {
|
||||
"uid" => Some(user.username.clone()),
|
||||
"cn"|"mail" => Some(user.mail.clone()),
|
||||
"cn" | "mail" => Some(user.mail.clone()),
|
||||
_ => {
|
||||
tracing::warn!("Ignoring unknown attr in search query: {attr}");
|
||||
None
|
||||
|
|
|
|||
15
src/main.rs
15
src/main.rs
|
|
@ -3,10 +3,14 @@
|
|||
mod cli;
|
||||
mod db;
|
||||
mod error;
|
||||
#[cfg(feature = "http")]
|
||||
mod http;
|
||||
mod ldap;
|
||||
mod listener;
|
||||
mod stream;
|
||||
|
||||
#[cfg(feature = "http")]
|
||||
use crate::http::http_listen;
|
||||
use cli::CliArgs;
|
||||
use db::{Database, DatabaseInterface, MemoryDatabase, User};
|
||||
use error::GlobalError;
|
||||
|
|
@ -39,14 +43,21 @@ async fn main() -> Result<(), GlobalError> {
|
|||
// .init();
|
||||
// tracing::subscriber::set_global_default(subscriber)?;
|
||||
|
||||
let listener = ListenerPath::new(&cli.listen)?.listener().await?;
|
||||
let ldap_listener = ListenerPath::new(&cli.listen_ldap)?.listener().await?;
|
||||
|
||||
let mut db = MemoryDatabase::new();
|
||||
create_dummy_users(&mut db).await;
|
||||
|
||||
#[cfg(feature = "http")]
|
||||
{
|
||||
let http_listener = ListenerPath::new(&cli.listen_http)?.listener().await?;
|
||||
let http_db = db.clone();
|
||||
tokio::spawn(http_listen(http_listener, http_db));
|
||||
}
|
||||
|
||||
// If the connection is None, it's because the client aborted early
|
||||
// so there's nothing to do about it.
|
||||
while let Some(stream) = listener.accept_ldap().await? {
|
||||
while let Some(stream) = ldap_listener.accept_ldap().await? {
|
||||
let db = db.clone();
|
||||
tokio::spawn(ldap_handler(stream, db));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue