Add disabled-by-default insecure-tcp feature to tokio-xmpp for use by component connections
This commit is contained in:
parent
38bfba4a18
commit
019450ff4b
9 changed files with 112 additions and 12 deletions
10
tokio-xmpp/src/tcp/component.rs
Normal file
10
tokio-xmpp/src/tcp/component.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
use crate::{Component, Error};
|
||||
|
||||
use super::TcpServerConnector;
|
||||
|
||||
impl Component<TcpServerConnector> {
|
||||
/// Start a new XMPP component
|
||||
pub async fn new(jid: &str, password: &str, server: String) -> Result<Self, Error> {
|
||||
Self::new_with_connector(jid, password, TcpServerConnector::new(server)).await
|
||||
}
|
||||
}
|
||||
26
tokio-xmpp/src/tcp/error.rs
Normal file
26
tokio-xmpp/src/tcp/error.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
//! TCP ServerConnector Error
|
||||
|
||||
use core::fmt;
|
||||
|
||||
/// TCP ServerConnector Error
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// tokio-xmpp error
|
||||
TokioXMPP(crate::error::Error),
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Error::TokioXMPP(e) => write!(fmt, "TokioXMPP error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::error::Error> for Error {
|
||||
fn from(e: crate::error::Error) -> Self {
|
||||
Error::TokioXMPP(e)
|
||||
}
|
||||
}
|
||||
49
tokio-xmpp/src/tcp/mod.rs
Normal file
49
tokio-xmpp/src/tcp/mod.rs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
//! `starttls::ServerConfig` provides a `ServerConnector` for starttls connections
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
use crate::{
|
||||
connect::{ServerConnector, ServerConnectorError},
|
||||
xmpp_stream::XMPPStream,
|
||||
Component,
|
||||
};
|
||||
|
||||
use self::error::Error;
|
||||
|
||||
mod component;
|
||||
pub mod error;
|
||||
|
||||
/// Component that connects over TCP
|
||||
pub type TcpComponent = Component<TcpServerConnector>;
|
||||
|
||||
/// Connect via insecure plaintext TCP to an XMPP server
|
||||
/// This should only be used over localhost or otherwise when you know what you are doing
|
||||
/// Probably mostly useful for Components
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TcpServerConnector(Arc<String>);
|
||||
|
||||
impl TcpServerConnector {
|
||||
/// Create a new connector with the given address
|
||||
pub fn new(addr: String) -> Self {
|
||||
Self(addr.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl ServerConnectorError for Error {}
|
||||
|
||||
impl ServerConnector for TcpServerConnector {
|
||||
type Stream = TcpStream;
|
||||
type Error = Error;
|
||||
async fn connect(
|
||||
&self,
|
||||
jid: &xmpp_parsers::Jid,
|
||||
ns: &str,
|
||||
) -> Result<XMPPStream<Self::Stream>, Self::Error> {
|
||||
let stream = TcpStream::connect(&*self.0)
|
||||
.await
|
||||
.map_err(|e| crate::Error::Io(e))?;
|
||||
Ok(XMPPStream::start(stream, jid.clone(), ns.to_owned()).await?)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue