use cargo workspace

This commit is contained in:
cel 🌸 2024-12-04 18:18:37 +00:00
parent 03764f8ced
commit 1b91ff6904
26 changed files with 2056 additions and 127 deletions

View File

@ -1,27 +1,7 @@
[package]
name = "luz"
authors = ["cel <cel@blos.sm>"]
version = "0.0.1"
edition = "2021"
[workspace]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-recursion = "1.0.4"
async-trait = "0.1.68"
lazy_static = "1.4.0"
nanoid = "0.4.0"
# TODO: remove unneeded features
rsasl = { version = "2.0.1", path = "../rsasl", default_features = false, features = ["provider_base64", "plain", "config_builder", "scram-sha-1"] }
tokio = { version = "1.28", features = ["full"] }
tokio-native-tls = "0.3.1"
tracing = "0.1.40"
trust-dns-resolver = "0.22.0"
try_map = "0.3.1"
peanuts = { version = "0.1.0", path = "../peanuts" }
futures = "0.3.31"
[dev-dependencies]
test-log = { version = "0.2", features = ["trace"] }
env_logger = "*"
tracing-subscriber = {version = "0.3", default-features = false, features = ["env-filter", "fmt"]}
members = [
"luz",
"jabber",
"stanza", "jid",
]

View File

@ -4,7 +4,7 @@
- [ ] error states for all negotiation parts
- [ ] better errors
- [ ] rename structs
- [ ] remove commented code
- [x] rename structs
- [x] remove commented code
- [ ] asynchronous connect (with take_mut?)
- [ ] split into separate crates: stanza, jabber, and luz

1935
jabber/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

29
jabber/Cargo.toml Normal file
View File

@ -0,0 +1,29 @@
[package]
name = "jabber"
authors = ["cel <cel@bunny.garden>"]
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-recursion = "1.0.4"
async-trait = "0.1.68"
lazy_static = "1.4.0"
nanoid = "0.4.0"
# TODO: remove unneeded features
rsasl = { version = "2.0.1", path = "../../rsasl", default_features = false, features = ["provider_base64", "plain", "config_builder", "scram-sha-1"] }
tokio = { version = "1.28", features = ["full"] }
tokio-native-tls = "0.3.1"
tracing = "0.1.40"
trust-dns-resolver = "0.22.0"
try_map = "0.3.1"
stanza = { version = "0.1.0", path = "../stanza" }
peanuts = { version = "0.1.0", path = "../../peanuts" }
jid = { version = "0.1.0", path = "../jid" }
futures = "0.3.31"
[dev-dependencies]
test-log = { version = "0.2", features = ["trace"] }
env_logger = "*"
tracing-subscriber = {version = "0.3", default-features = false, features = ["env-filter", "fmt"]}

View File

@ -1,16 +1,16 @@
use std::{pin::pin, sync::Arc, task::Poll};
use futures::{Sink, Stream, StreamExt};
use jid::ParseError;
use rsasl::config::SASLConfig;
use crate::{
connection::{Tls, Unencrypted},
jid::ParseError,
stanza::{
use stanza::{
client::Stanza,
sasl::Mechanisms,
stream::{Feature, Features},
},
};
use crate::{
connection::{Tls, Unencrypted},
Connection, Error, JabberStream, Result, JID,
};
@ -113,7 +113,7 @@ impl ConnectionState {
))
}
Connecting::InsecureGotFeatures((features, jabber_stream)) => {
match features.negotiate()? {
match features.negotiate().ok_or(Error::Negotiation)? {
Feature::StartTls(_start_tls) => {
self =
ConnectionState::Connecting(Connecting::StartTls(jabber_stream))
@ -138,7 +138,7 @@ impl ConnectionState {
))
}
Connecting::GotFeatures((features, jabber_stream)) => {
match features.negotiate()? {
match features.negotiate().ok_or(Error::Negotiation)? {
Feature::StartTls(_start_tls) => return Err(Error::AlreadyTls),
Feature::Sasl(mechanisms) => {
self = ConnectionState::Connecting(Connecting::Sasl(
@ -190,35 +190,6 @@ impl Connecting {
}
}
impl Features {
pub fn negotiate(self) -> Result<Feature> {
if let Some(Feature::StartTls(s)) = self
.features
.iter()
.find(|feature| matches!(feature, Feature::StartTls(_s)))
{
// TODO: avoid clone
return Ok(Feature::StartTls(s.clone()));
} else if let Some(Feature::Sasl(mechanisms)) = self
.features
.iter()
.find(|feature| matches!(feature, Feature::Sasl(_)))
{
// TODO: avoid clone
return Ok(Feature::Sasl(mechanisms.clone()));
} else if let Some(Feature::Bind) = self
.features
.into_iter()
.find(|feature| matches!(feature, Feature::Bind))
{
Ok(Feature::Bind)
} else {
// TODO: better error
return Err(Error::Negotiation);
}
}
}
pub enum InsecureConnecting {
Disconnected,
ConnectionEstablished(Connection),

View File

@ -1,10 +1,10 @@
use std::str::Utf8Error;
use jid::ParseError;
use rsasl::mechname::MechanismNameError;
use crate::stanza::client::error::Error as ClientError;
use crate::stanza::stream::Error as StreamError;
use crate::{jid::ParseError, stanza::sasl::Failure};
use stanza::client::error::Error as ClientError;
use stanza::sasl::Failure;
use stanza::stream::Error as StreamError;
#[derive(Debug)]
pub enum Error {

View File

@ -2,26 +2,25 @@ use std::pin::pin;
use std::str::{self, FromStr};
use std::sync::Arc;
use async_recursion::async_recursion;
use futures::StreamExt;
use jid::JID;
use peanuts::element::{FromContent, IntoElement};
use peanuts::{Reader, Writer};
use rsasl::prelude::{Mechname, SASLClient, SASLConfig};
use stanza::bind::{Bind, BindType, FullJidType, ResourceType};
use stanza::client::iq::{Iq, IqType, Query};
use stanza::client::Stanza;
use stanza::sasl::{Auth, Challenge, Mechanisms, Response, ServerResponse};
use stanza::starttls::{Proceed, StartTls};
use stanza::stream::{Features, Stream};
use stanza::XML_VERSION;
use tokio::io::{AsyncRead, AsyncWrite, ReadHalf, WriteHalf};
use tokio_native_tls::native_tls::TlsConnector;
use tracing::{debug, instrument};
use crate::connection::{Tls, Unencrypted};
use crate::error::Error;
use crate::stanza::bind::{Bind, BindType, FullJidType, ResourceType};
use crate::stanza::client::iq::{Iq, IqType, Query};
use crate::stanza::client::Stanza;
use crate::stanza::sasl::{Auth, Challenge, Mechanisms, Response, ServerResponse};
use crate::stanza::starttls::{Proceed, StartTls};
use crate::stanza::stream::{Feature, Features, Stream};
use crate::stanza::XML_VERSION;
use crate::JID;
use crate::{Connection, Result};
use crate::Result;
// open stream (streams started)
pub struct JabberStream<S> {

View File

@ -6,8 +6,6 @@ pub mod client;
pub mod connection;
pub mod error;
pub mod jabber_stream;
pub mod jid;
pub mod stanza;
pub use connection::Connection;
use connection::Tls;

6
jid/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "jid"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -19,15 +19,6 @@ pub enum ParseError {
Malformed(String),
}
impl From<ParseError> for peanuts::Error {
fn from(e: ParseError) -> Self {
match e {
ParseError::Empty => peanuts::Error::DeserializeError("".to_string()),
ParseError::Malformed(e) => peanuts::Error::DeserializeError(e),
}
}
}
impl JID {
pub fn new(
localpart: Option<String>,

6
luz/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "luz"
version = "0.1.0"
edition = "2021"
[dependencies]

3
luz/src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

8
stanza/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "stanza"
version = "0.1.0"
edition = "2021"
[dependencies]
peanuts = { version = "0.1.0", path = "../../peanuts" }
jid = { version = "0.1.0", path = "../jid" }

View File

@ -1,10 +1,9 @@
use jid::JID;
use peanuts::{
element::{FromElement, IntoElement},
Element,
};
use crate::JID;
pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-bind";
#[derive(Clone)]

View File

@ -3,8 +3,8 @@ use std::str::FromStr;
use peanuts::element::{FromElement, IntoElement};
use peanuts::{DeserializeError, Element};
use crate::stanza::stanza_error::Error as StanzaError;
use crate::stanza::stanza_error::Text;
use crate::stanza_error::Error as StanzaError;
use crate::stanza_error::Text;
use super::XMLNS;

View File

@ -1,16 +1,14 @@
use std::str::FromStr;
use jid::JID;
use peanuts::{
element::{FromElement, IntoElement},
DeserializeError, Element, XML_NS,
};
use crate::{
stanza::{
bind::{self, Bind},
client::error::Error,
},
JID,
};
use super::XMLNS;

View File

@ -1,12 +1,11 @@
use std::str::FromStr;
use jid::JID;
use peanuts::{
element::{FromElement, IntoElement},
DeserializeError, Element, XML_NS,
};
use crate::JID;
use super::XMLNS;
pub struct Message {

View File

@ -1,12 +1,11 @@
use std::str::FromStr;
use jid::JID;
use peanuts::{
element::{FromElement, IntoElement},
DeserializeError, Element, XML_NS,
};
use crate::JID;
use super::{error::Error, XMLNS};
pub struct Presence {

View File

@ -4,7 +4,6 @@ use peanuts::{
element::{FromElement, IntoElement},
DeserializeError, Element,
};
use tracing::debug;
pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-sasl";
@ -17,15 +16,11 @@ impl FromElement for Mechanisms {
fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> {
element.check_name("mechanisms")?;
element.check_namespace(XMLNS)?;
debug!("getting mechanisms");
let mechanisms: Vec<Mechanism> = element.pop_children()?;
debug!("gottting mechanisms");
let mechanisms = mechanisms
.into_iter()
.map(|Mechanism(mechanism)| mechanism)
.collect();
debug!("gottting mechanisms");
Ok(Mechanisms { mechanisms })
}
}
@ -135,7 +130,6 @@ pub enum ServerResponse {
impl FromElement for ServerResponse {
fn from_element(element: Element) -> peanuts::element::DeserializeResult<Self> {
debug!("identification: {:?}", element.identify());
match element.identify() {
(Some(XMLNS), "challenge") => {
Ok(ServerResponse::Challenge(Challenge::from_element(element)?))

View File

@ -1,12 +1,10 @@
use std::collections::{HashMap, HashSet};
use jid::JID;
use peanuts::element::{Content, ElementBuilder, FromElement, IntoElement, NamespaceDeclaration};
use peanuts::XML_NS;
use peanuts::{element::Name, Element};
use tracing::debug;
use crate::stanza::bind;
use crate::JID;
use crate::bind;
use super::sasl::{self, Mechanisms};
use super::starttls::{self, StartTls};
@ -99,6 +97,34 @@ pub struct Features {
pub features: Vec<Feature>,
}
impl Features {
pub fn negotiate(self) -> Option<Feature> {
if let Some(Feature::StartTls(s)) = self
.features
.iter()
.find(|feature| matches!(feature, Feature::StartTls(_s)))
{
// TODO: avoid clone
return Some(Feature::StartTls(s.clone()));
} else if let Some(Feature::Sasl(mechanisms)) = self
.features
.iter()
.find(|feature| matches!(feature, Feature::Sasl(_)))
{
// TODO: avoid clone
return Some(Feature::Sasl(mechanisms.clone()));
} else if let Some(Feature::Bind) = self
.features
.into_iter()
.find(|feature| matches!(feature, Feature::Bind))
{
Some(Feature::Bind)
} else {
return None;
}
}
}
impl IntoElement for Features {
fn builder(&self) -> ElementBuilder {
Element::builder("features", Some(XMLNS)).push_children(self.features.clone())
@ -112,9 +138,7 @@ impl FromElement for Features {
element.check_namespace(XMLNS)?;
element.check_name("features")?;
debug!("got features stanza");
let features = element.children()?;
debug!("got features period");
Ok(Features { features })
}
@ -141,25 +165,15 @@ impl IntoElement for Feature {
impl FromElement for Feature {
fn from_element(element: Element) -> peanuts::element::DeserializeResult<Self> {
let identity = element.identify();
debug!("identity: {:?}", identity);
match element.identify() {
(Some(starttls::XMLNS), "starttls") => {
debug!("identified starttls");
Ok(Feature::StartTls(StartTls::from_element(element)?))
}
(Some(sasl::XMLNS), "mechanisms") => {
debug!("identified mechanisms");
Ok(Feature::Sasl(Mechanisms::from_element(element)?))
}
(Some(bind::XMLNS), "bind") => {
debug!("identified bind");
Ok(Feature::Bind)
}
_ => {
debug!("identified unknown feature");
Ok(Feature::Unknown)
}
(Some(bind::XMLNS), "bind") => Ok(Feature::Bind),
_ => Ok(Feature::Unknown),
}
}
}