mirror of
https://github.com/zhboner/realm.git
synced 2026-11-17 16:17:22 +00:00
realm_core: add ws/tls/wss
This commit is contained in:
Generated
+9
-7
@@ -489,9 +489,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "kaminari"
|
||||
version = "0.7.0"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e61016796e12b5c49fae513e18b92b6cd722f6ad5b0f2c65efbf6fb7b98897a7"
|
||||
checksum = "d80ca5fee4c52a5876838ead1b9d0c42266c48d78915a498ea0226105c00a891"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"lightws",
|
||||
@@ -900,12 +900,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "realm_core"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"kaminari",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"pin-project",
|
||||
"realm_io 0.2.5",
|
||||
"realm_io 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"realm_syscall 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio",
|
||||
"trust-dns-resolver 0.21.2",
|
||||
@@ -913,9 +915,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "realm_io"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "214919adf97309686e1f07abb4cf5245fd6a165e0b9ee9a50c98c949cf44eab2"
|
||||
version = "0.2.7"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"tokio",
|
||||
@@ -924,6 +924,8 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "realm_io"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a29814fb040b111cab9c2023aef866b20b10a193529c2f56c810f3b8568daad8"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"tokio",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "realm_core"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
authors = ["Realm Contributors"]
|
||||
description = "Realm's core facilities."
|
||||
repository = "https://github.com/zhboner/realm/realm_core"
|
||||
@@ -14,10 +14,17 @@ license = "MIT"
|
||||
# realm
|
||||
realm_io = "0.2.7"
|
||||
realm_syscall = "0.1.3"
|
||||
kaminari = { version = "0.7.2", optional = true }
|
||||
|
||||
# other
|
||||
futures = "0.3"
|
||||
log = "0.4"
|
||||
lazy_static = "1"
|
||||
pin-project = "1"
|
||||
trust-dns-resolver = "0.21"
|
||||
tokio = { version = "1", features = ["rt", "net", "time"] }
|
||||
|
||||
[features]
|
||||
default = ["transport", "transport-boost"]
|
||||
transport = ["kaminari"]
|
||||
transport-boost = []
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
#[cfg(feature = "transport")]
|
||||
use kaminari::mix::{MixAccept, MixConnect};
|
||||
|
||||
/// Remote address.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum RemoteAddr {
|
||||
@@ -27,6 +30,9 @@ pub struct ConnectOpts {
|
||||
pub associate_timeout: usize,
|
||||
pub bind_address: Option<SocketAddr>,
|
||||
pub bind_interface: Option<String>,
|
||||
|
||||
#[cfg(feature = "transport")]
|
||||
pub transport: Option<(MixAccept, MixConnect)>,
|
||||
}
|
||||
|
||||
/// Relay endpoint.
|
||||
|
||||
@@ -4,11 +4,20 @@ use tokio::net::TcpStream;
|
||||
|
||||
use super::socket;
|
||||
use super::plain;
|
||||
|
||||
#[cfg(feature = "transport")]
|
||||
use super::transport;
|
||||
|
||||
use crate::trick::Ref;
|
||||
use crate::endpoint::{RemoteAddr, ConnectOpts};
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub async fn connect_and_relay(local: TcpStream, raddr: Ref<RemoteAddr>, conn_opts: Ref<ConnectOpts>) -> Result<()> {
|
||||
let ConnectOpts {
|
||||
#[cfg(feature = "transport")]
|
||||
transport,
|
||||
..
|
||||
} = conn_opts.as_ref();
|
||||
|
||||
// before connect
|
||||
// ..
|
||||
|
||||
@@ -22,7 +31,23 @@ pub async fn connect_and_relay(local: TcpStream, raddr: Ref<RemoteAddr>, conn_op
|
||||
);
|
||||
|
||||
// after connected
|
||||
let res = plain::run_relay(local, remote).await;
|
||||
// ..
|
||||
|
||||
// relay
|
||||
let res = {
|
||||
#[cfg(feature = "transport")]
|
||||
{
|
||||
if let Some((ac, cc)) = transport {
|
||||
transport::run_relay(local, remote, ac, cc).await
|
||||
} else {
|
||||
plain::run_relay(local, remote).await
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "transport"))]
|
||||
{
|
||||
plain::run_relay(local, remote).await
|
||||
}
|
||||
};
|
||||
|
||||
// ignore relay error
|
||||
if let Err(e) = res {
|
||||
|
||||
@@ -4,6 +4,9 @@ mod socket;
|
||||
mod middle;
|
||||
mod plain;
|
||||
|
||||
#[cfg(feature = "transport")]
|
||||
mod transport;
|
||||
|
||||
use std::io::Result;
|
||||
|
||||
use crate::trick::Ref;
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
use std::io::Result;
|
||||
use futures::try_join;
|
||||
|
||||
use kaminari::{AsyncAccept, AsyncConnect, IOStream};
|
||||
use kaminari::mix::{MixAccept, MixConnect};
|
||||
|
||||
use realm_io::{CopyBuffer, bidi_copy_buf, buf_size};
|
||||
|
||||
pub async fn run_relay<S: IOStream>(src: S, dst: S, ac: &MixAccept, cc: &MixConnect) -> Result<()> {
|
||||
macro_rules! hs_relay {
|
||||
($ac: expr, $cc: expr) => {
|
||||
handshake_and_relay(src, dst, $ac, $cc).await
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(feature = "transport-boost")]
|
||||
{
|
||||
use MixConnect::*;
|
||||
if let Some(ac) = ac.as_plain() {
|
||||
return match cc {
|
||||
Plain(cc) => hs_relay!(ac, cc),
|
||||
Ws(cc) => hs_relay!(ac, cc),
|
||||
Tls(cc) => hs_relay!(ac, cc),
|
||||
Wss(cc) => hs_relay!(ac, cc),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "transport-boost")]
|
||||
{
|
||||
use MixAccept::*;
|
||||
if let Some(cc) = cc.as_plain() {
|
||||
return match ac {
|
||||
Plain(ac) => hs_relay!(ac, cc),
|
||||
Ws(ac) => hs_relay!(ac, cc),
|
||||
Tls(ac) => hs_relay!(ac, cc),
|
||||
Wss(ac) => hs_relay!(ac, cc),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
hs_relay!(ac, cc)
|
||||
}
|
||||
|
||||
async fn handshake_and_relay<S, AC, CC>(src: S, dst: S, ac: &AC, cc: &CC) -> Result<()>
|
||||
where
|
||||
S: IOStream,
|
||||
AC: AsyncAccept<S>,
|
||||
CC: AsyncConnect<S>,
|
||||
{
|
||||
let mut buf1 = vec![0; buf_size()];
|
||||
let mut buf2 = vec![0; buf_size()];
|
||||
|
||||
let (mut src, mut dst) = try_join!(ac.accept(src, &mut buf1), cc.connect(dst, &mut buf2))?;
|
||||
|
||||
let buf1 = CopyBuffer::new(buf1);
|
||||
let buf2 = CopyBuffer::new(buf2);
|
||||
|
||||
let _ = bidi_copy_buf(&mut src, &mut dst, buf1, buf2).await;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user