feat(client): protocol v2 wire layer, screen sharing, channel webhooks and secure voice
Verified against the real gateway via tests/gateway_interop.rs.
This commit is contained in:
parent
8460fb28fd
commit
3d7dd5413b
65 changed files with 6048 additions and 1156 deletions
112
src/net/transport.rs
Normal file
112
src/net/transport.rs
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
//! TCP/TLS transport selection. TLS never falls back to plaintext after an error.
|
||||
use anyhow::{Context, Result, bail};
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
pub trait AsyncStream: AsyncRead + AsyncWrite + Unpin + Send {}
|
||||
impl<T: AsyncRead + AsyncWrite + Unpin + Send> AsyncStream for T {}
|
||||
pub type Stream = Box<dyn AsyncStream>;
|
||||
|
||||
pub struct Connected {
|
||||
pub stream: Stream,
|
||||
pub peer: std::net::SocketAddr,
|
||||
}
|
||||
pub async fn connect(address: &str) -> Result<Stream> {
|
||||
Ok(connect_with_peer(address).await?.stream)
|
||||
}
|
||||
pub async fn connect_with_peer(address: &str) -> Result<Connected> {
|
||||
let explicit_plain = address.starts_with("lnex://");
|
||||
let authority = address
|
||||
.strip_prefix("lnexs://")
|
||||
.or_else(|| address.strip_prefix("lnex://"))
|
||||
.unwrap_or(address);
|
||||
if authority.contains('/')
|
||||
|| authority.contains('@')
|
||||
|| authority.contains('?')
|
||||
|| authority.contains('#')
|
||||
{
|
||||
bail!("expected a server host and port");
|
||||
}
|
||||
let (host, port) = if let Some(value) = authority.strip_prefix('[') {
|
||||
let (host, tail) = value.split_once(']').context("invalid IPv6 address")?;
|
||||
let port = if tail.is_empty() {
|
||||
7600
|
||||
} else {
|
||||
tail.strip_prefix(':')
|
||||
.context("invalid server address")?
|
||||
.parse()?
|
||||
};
|
||||
(host, port)
|
||||
} else if let Some((host, port)) = authority.rsplit_once(':') {
|
||||
(host, port.parse()?)
|
||||
} else {
|
||||
(authority, 7600)
|
||||
};
|
||||
if host.is_empty() {
|
||||
bail!("server host is empty");
|
||||
}
|
||||
let tcp = TcpStream::connect((host, port))
|
||||
.await
|
||||
.context("connect to gateway")?;
|
||||
tcp.set_nodelay(true)?;
|
||||
let peer = tcp.peer_addr()?;
|
||||
let loopback_host = host.eq_ignore_ascii_case("localhost")
|
||||
|| host
|
||||
.parse::<std::net::IpAddr>()
|
||||
.is_ok_and(|ip| ip.is_loopback());
|
||||
let plain = explicit_plain || (!address.starts_with("lnexs://") && loopback_host);
|
||||
if plain {
|
||||
if !tcp.peer_addr()?.ip().is_loopback() {
|
||||
bail!("plaintext LNEx is restricted to loopback development; use lnexs://");
|
||||
}
|
||||
return Ok(Connected {
|
||||
stream: Box::new(tcp),
|
||||
peer,
|
||||
});
|
||||
}
|
||||
let roots = rustls::RootCertStore::from_iter(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
|
||||
let config = rustls::ClientConfig::builder_with_provider(
|
||||
rustls::crypto::aws_lc_rs::default_provider().into(),
|
||||
)
|
||||
.with_protocol_versions(&[&rustls::version::TLS13])?
|
||||
.with_root_certificates(roots)
|
||||
.with_no_client_auth();
|
||||
let server_name = rustls::pki_types::ServerName::try_from(host.to_owned())
|
||||
.context("invalid TLS server name")?;
|
||||
let tls = tokio_rustls::TlsConnector::from(std::sync::Arc::new(config))
|
||||
.connect(server_name, tcp)
|
||||
.await
|
||||
.context("TLS certificate/connection verification failed")?;
|
||||
Ok(Connected {
|
||||
stream: Box::new(tls),
|
||||
peer,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tokio::io::AsyncReadExt;
|
||||
#[tokio::test]
|
||||
async fn tls_failure_never_retries_plaintext() {
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let task = tokio::spawn(async move {
|
||||
let (mut stream, _) = listener.accept().await.unwrap();
|
||||
let first = stream.read_u8().await.unwrap();
|
||||
assert_eq!(
|
||||
first, 22,
|
||||
"first record must be a TLS handshake, never LNEx plaintext"
|
||||
);
|
||||
drop(stream);
|
||||
assert!(
|
||||
tokio::time::timeout(std::time::Duration::from_millis(30), listener.accept())
|
||||
.await
|
||||
.is_err(),
|
||||
"certificate/handshake errors must not cause a downgrade reconnect"
|
||||
);
|
||||
});
|
||||
assert!(connect(&format!("lnexs://{address}")).await.is_err());
|
||||
task.await.unwrap();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue