modify end server to connect to a local TCP server

Tested with netcat on each end and it works great.
main
_ 2021-07-17 03:11:01 +00:00
parent c4cd8cf1aa
commit 73d7551215
5 changed files with 72 additions and 14 deletions

View File

@ -0,0 +1,14 @@
# Testing
There are 5 processes, so you'll need 5 terminal windows or screen / tmux
sessions:
1. TCP end server: `nc -l -p 30382`
2. QUIC relay server: `RUST_LOG=quic_demo_relay_server=debug cargo run --bin quic_demo_relay_server`
3. Server-side adapter: `RUST_LOG=quic_demo_end_server=debug cargo run --bin quic_demo_end_server`
4. Client-side adapter: `RUST_LOG=quic_demo_client cargo run --bin quic_demo_client`
5. TCP end client: `nc 127.0.0.1 30381`
The netcat processes from steps 1 and 5 should now be connected to each other.

View File

@ -29,6 +29,8 @@ async fn main () -> anyhow::Result <()> {
let (mut relay_send, mut relay_recv) = connection.open_bi ().await?; let (mut relay_send, mut relay_recv) = connection.open_bi ().await?;
debug! ("Relaying bytes...");
let uplink_task = tokio::spawn (async move { let uplink_task = tokio::spawn (async move {
// Uplink - local client to relay server // Uplink - local client to relay server

View File

@ -1,9 +1,15 @@
use tokio::net::TcpStream;
use quic_demo::prelude::*; use quic_demo::prelude::*;
#[tokio::main] #[tokio::main]
async fn main () -> anyhow::Result <()> { async fn main () -> anyhow::Result <()> {
tracing_subscriber::fmt::init (); tracing_subscriber::fmt::init ();
let stream = TcpStream::connect ("127.0.0.1:30382").await?;
let (mut local_recv, mut local_send) = stream.into_split ();
let server_cert = tokio::fs::read ("quic_server.crt").await?; let server_cert = tokio::fs::read ("quic_server.crt").await?;
let server_addr = "127.0.0.1:30380".parse ()?; let server_addr = "127.0.0.1:30380".parse ()?;
let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&server_cert])?; let endpoint = make_client_endpoint ("0.0.0.0:0".parse ()?, &[&server_cert])?;
@ -15,15 +21,43 @@ async fn main () -> anyhow::Result <()> {
.. ..
} = endpoint.connect (&server_addr, "localhost")?.await?; } = endpoint.connect (&server_addr, "localhost")?.await?;
debug! ("Waiting for incoming bi stream"); debug! ("Waiting for relay server to forward a bi stream");
let (mut send, mut recv) = bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("Relay server didn't open a bi stream"))??; let (mut relay_send, mut relay_recv) = bi_streams.next ().await.ok_or_else (|| anyhow::anyhow! ("Relay server didn't open a bi stream"))??;
debug! ("Relaying bytes...");
let uplink_task = tokio::spawn (async move {
// Uplink - local client to relay server
let mut buf = vec! [0u8; 65_536]; let mut buf = vec! [0u8; 65_536];
while let Some (bytes_read) = recv.read (&mut buf).await? { loop {
let s = format! ("bytes_read: {}", bytes_read); let bytes_read = local_recv.read (&mut buf).await?;
send.write_all (s.as_bytes ()).await?; let buf_slice = &buf [0..bytes_read];
relay_send.write_all (buf_slice).await?;
} }
debug! ("Uplink closed");
Ok::<_, anyhow::Error> (())
});
let downlink_task = tokio::spawn (async move {
// Downlink - Relay server to local client
let mut buf = vec! [0u8; 65_536];
while let Some (bytes_read) = relay_recv.read (&mut buf).await? {
let buf_slice = &buf [0..bytes_read];
local_send.write_all (buf_slice).await?;
}
debug! ("Downlink closed");
Ok::<_, anyhow::Error> (())
});
uplink_task.await??;
downlink_task.await??;
Ok (()) Ok (())
} }

View File

@ -2,6 +2,7 @@ pub use std::{
time::Duration, time::Duration,
}; };
pub use anyhow::bail;
pub use futures_util::StreamExt; pub use futures_util::StreamExt;
pub use tokio::{ pub use tokio::{
io::{ io::{

View File

@ -1,14 +1,16 @@
// I'm not sure where I got this module from, but it's probably from the // I'm not sure where I got this module from, but it's probably from the
// quinn examples, so the license should be okay. // quinn examples, so the license should be okay.
use std::{
net::SocketAddr,
sync::Arc,
time::Duration,
};
use quinn::{ use quinn::{
Certificate, CertificateChain, ClientConfig, ClientConfigBuilder, Endpoint, Incoming, Certificate, CertificateChain, ClientConfig, ClientConfigBuilder, Endpoint, Incoming,
PrivateKey, ServerConfig, ServerConfigBuilder, TransportConfig, PrivateKey, ServerConfig, ServerConfigBuilder, TransportConfig,
}; };
use std::{
net::SocketAddr,
sync::Arc,
};
/// Constructs a QUIC endpoint configured for use a client only. /// Constructs a QUIC endpoint configured for use a client only.
/// ///
@ -20,9 +22,14 @@ pub fn make_client_endpoint(
bind_addr: SocketAddr, bind_addr: SocketAddr,
server_certs: &[&[u8]], server_certs: &[&[u8]],
) -> anyhow::Result<Endpoint> { ) -> anyhow::Result<Endpoint> {
let client_cfg = configure_client(server_certs)?; let mut client_cfg = configure_client (server_certs)?;
let mut endpoint_builder = Endpoint::builder(); let mut transport = quinn::TransportConfig::default ();
endpoint_builder.default_client_config(client_cfg); transport.keep_alive_interval (Some (Duration::from_millis (5_000)));
client_cfg.transport = Arc::new (transport);
let mut endpoint_builder = Endpoint::builder ();
endpoint_builder.default_client_config (client_cfg);
let (endpoint, _incoming) = endpoint_builder.bind(&bind_addr)?; let (endpoint, _incoming) = endpoint_builder.bind(&bind_addr)?;
Ok(endpoint) Ok(endpoint)
} }