Dummy data instead of HTTP
This commit is contained in:
parent
66d8aeb4a5
commit
dec39cf2e3
10 changed files with 1543 additions and 1413 deletions
225
src/main.rs
225
src/main.rs
|
|
@ -1,7 +1,7 @@
|
|||
#![feature(ascii_char)]
|
||||
|
||||
mod client;
|
||||
mod http;
|
||||
mod codec;
|
||||
mod record;
|
||||
mod server;
|
||||
mod util;
|
||||
|
|
@ -10,8 +10,6 @@ use record::Records;
|
|||
|
||||
use argp::FromArgs;
|
||||
use static_cell::StaticCell;
|
||||
use tokio::sync::oneshot;
|
||||
use tokio_rustls::rustls::crypto::CryptoProvider;
|
||||
|
||||
/// Play recorded requests and responses
|
||||
#[derive(FromArgs)]
|
||||
|
|
@ -26,11 +24,14 @@ struct Opt {
|
|||
#[derive(FromArgs)]
|
||||
#[argp(subcommand)]
|
||||
enum Subcommand {
|
||||
/// Replay from records
|
||||
Play(OptPlay),
|
||||
/// Replay from records (client)
|
||||
Client(OptClient),
|
||||
/// Replay from records (server)
|
||||
Server(OptServer),
|
||||
/// Print records
|
||||
Print(OptPrint),
|
||||
/// Record traffic
|
||||
#[cfg(feature = "record")]
|
||||
Record(OptRecord),
|
||||
/// Remove record
|
||||
Remove(OptRemove),
|
||||
|
|
@ -40,32 +41,44 @@ enum Subcommand {
|
|||
|
||||
/// Replay from records
|
||||
#[derive(FromArgs)]
|
||||
#[argp(subcommand, name = "play")]
|
||||
struct OptPlay {
|
||||
#[argp(subcommand, name = "client")]
|
||||
struct OptClient {
|
||||
/// Connect to address
|
||||
#[argp(positional)]
|
||||
forward_addr: String,
|
||||
connect_addr: String,
|
||||
/// Connect to port
|
||||
#[argp(positional)]
|
||||
forward_port: u16,
|
||||
connect_port: u16,
|
||||
/// Whether to use TLS
|
||||
#[argp(switch, long = "tls")]
|
||||
tls: bool,
|
||||
/// Repeat N times
|
||||
#[argp(option, short = 'r', default = "1")]
|
||||
repeat: u32,
|
||||
/// UDP end notification will be sent to this address:port
|
||||
#[argp(option, short = 'n')]
|
||||
notify_addr: Option<String>,
|
||||
/// Only play this record
|
||||
#[argp(option)]
|
||||
record: Option<u64>,
|
||||
/// Print debug info
|
||||
#[argp(switch, short = 'd')]
|
||||
debug: bool,
|
||||
}
|
||||
|
||||
/// Replay from records
|
||||
#[derive(FromArgs)]
|
||||
#[argp(subcommand, name = "server")]
|
||||
struct OptServer {
|
||||
/// Listen to port
|
||||
#[argp(positional)]
|
||||
listen_port: u16,
|
||||
/// Path to PEM certificates and keys
|
||||
#[argp(positional)]
|
||||
certs: String,
|
||||
/// Where to use TLS
|
||||
#[argp(positional)]
|
||||
tls: String,
|
||||
/// Repeat N times
|
||||
#[argp(option, short = 'r', default = "1")]
|
||||
repeat: u32,
|
||||
/// Only play this record
|
||||
#[argp(option)]
|
||||
record: Option<u64>,
|
||||
/// Only run these parts
|
||||
#[argp(option, default = "String::from(\"both\")")]
|
||||
run: String,
|
||||
/// Whether to use TLS
|
||||
#[argp(switch, long = "tls")]
|
||||
tls: bool,
|
||||
/// Print debug info
|
||||
#[argp(switch, short = 'd')]
|
||||
debug: bool,
|
||||
|
|
@ -75,15 +88,13 @@ struct OptPlay {
|
|||
#[derive(FromArgs)]
|
||||
#[argp(subcommand, name = "print")]
|
||||
struct OptPrint {
|
||||
/// Print packets
|
||||
#[argp(switch, short = 'p')]
|
||||
packets: bool,
|
||||
/// Record number
|
||||
#[argp(option, short = 'n')]
|
||||
number: Option<u64>,
|
||||
}
|
||||
|
||||
/// Record traffic
|
||||
#[cfg(feature = "record")]
|
||||
#[derive(FromArgs)]
|
||||
#[argp(subcommand, name = "record")]
|
||||
struct OptRecord {}
|
||||
|
|
@ -108,21 +119,6 @@ struct OptRemove {
|
|||
packet_number: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
enum RunMode {
|
||||
Client,
|
||||
Server,
|
||||
Both,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
enum TlsMode {
|
||||
None,
|
||||
Client,
|
||||
Server,
|
||||
Both,
|
||||
}
|
||||
|
||||
static RECORDS: StaticCell<Records> = StaticCell::new();
|
||||
|
||||
#[tokio::main]
|
||||
|
|
@ -131,140 +127,49 @@ async fn main() {
|
|||
let opt: Opt = argp::parse_args_or_exit(argp::DEFAULT);
|
||||
|
||||
match opt.subcommand {
|
||||
Subcommand::Play(subopt) => {
|
||||
let tls_mode = match subopt.tls.as_str() {
|
||||
"none" => TlsMode::None,
|
||||
"client" => TlsMode::Client,
|
||||
"server" => TlsMode::Server,
|
||||
"both" => TlsMode::Both,
|
||||
_ => panic!("TLS mode must be one of none,client,server,both."),
|
||||
};
|
||||
let run_mode = match subopt.run.as_str() {
|
||||
"client" => RunMode::Client,
|
||||
"server" => RunMode::Server,
|
||||
"both" => RunMode::Both,
|
||||
_ => panic!("run mode must be one of client,server,both."),
|
||||
};
|
||||
Subcommand::Client(subopt) => {
|
||||
let records = RECORDS.init(record::read_record_file(&opt.record_file));
|
||||
|
||||
if let Some(only_record) = subopt.record {
|
||||
records.retain(|id, _| *id == only_record);
|
||||
}
|
||||
|
||||
let mut ciphers: Option<Vec<String>> = None;
|
||||
let mut kexes: Option<Vec<String>> = None;
|
||||
for (var, val) in std::env::vars() {
|
||||
match var.as_str() {
|
||||
"CIPHERS" => ciphers = Some(val.split(',').map(str::to_string).collect()),
|
||||
"KEXES" => kexes = Some(val.split(',').map(str::to_string).collect()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let mut prov = tokio_rustls::rustls::crypto::aws_lc_rs::default_provider();
|
||||
if let Some(ciphers) = ciphers {
|
||||
prov.cipher_suites.clear();
|
||||
for cipher in ciphers {
|
||||
match cipher.as_str() {
|
||||
"AES_256_GCM_SHA384" => prov
|
||||
.cipher_suites
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::cipher_suite::TLS13_AES_256_GCM_SHA384),
|
||||
"AES_128_GCM_SHA256" => prov
|
||||
.cipher_suites
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::cipher_suite::TLS13_AES_128_GCM_SHA256),
|
||||
"CHACHA20_POLY1305_SHA256" => prov
|
||||
.cipher_suites
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::cipher_suite::TLS13_CHACHA20_POLY1305_SHA256),
|
||||
"ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" => prov
|
||||
.cipher_suites
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::cipher_suite::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384),
|
||||
"ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" => prov
|
||||
.cipher_suites
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::cipher_suite::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
|
||||
"ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" => prov
|
||||
.cipher_suites
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::cipher_suite::TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256),
|
||||
"ECDHE_RSA_WITH_AES_256_GCM_SHA384" => prov
|
||||
.cipher_suites
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::cipher_suite::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384),
|
||||
"ECDHE_RSA_WITH_AES_128_GCM_SHA256" => prov
|
||||
.cipher_suites
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::cipher_suite::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256),
|
||||
"ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" => prov
|
||||
.cipher_suites
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::cipher_suite::TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256),
|
||||
other => {
|
||||
println!("Unknown cipher `{other}`")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(kexes) = kexes {
|
||||
prov.kx_groups.clear();
|
||||
for kex in kexes {
|
||||
match kex.as_str() {
|
||||
"X25519" => prov
|
||||
.kx_groups
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::kx_group::X25519),
|
||||
"SECP256R1" => prov
|
||||
.kx_groups
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::kx_group::SECP256R1),
|
||||
"SECP384R1" => prov
|
||||
.kx_groups
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::kx_group::SECP384R1),
|
||||
"X25519MLKEM768" => prov.kx_groups.push(
|
||||
tokio_rustls::rustls::crypto::aws_lc_rs::kx_group::X25519MLKEM768,
|
||||
),
|
||||
"SECP256R1MLKEM768" => prov.kx_groups.push(
|
||||
tokio_rustls::rustls::crypto::aws_lc_rs::kx_group::SECP256R1MLKEM768,
|
||||
),
|
||||
"MLKEM768" => prov
|
||||
.kx_groups
|
||||
.push(tokio_rustls::rustls::crypto::aws_lc_rs::kx_group::MLKEM768),
|
||||
other => {
|
||||
println!("Unknown kex `{other}`")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
CryptoProvider::install_default(prov).unwrap();
|
||||
util::init_provider();
|
||||
|
||||
let (sync_sender, sync_receiver) = oneshot::channel();
|
||||
//console_subscriber::init();
|
||||
let client = tokio::spawn({
|
||||
let records = &*records;
|
||||
async move {
|
||||
if run_mode == RunMode::Both || run_mode == RunMode::Client {
|
||||
client::play(
|
||||
records,
|
||||
tls_mode,
|
||||
(subopt.forward_addr, subopt.forward_port),
|
||||
sync_receiver,
|
||||
subopt.repeat,
|
||||
subopt.debug,
|
||||
)
|
||||
.await;
|
||||
} else {
|
||||
std::future::pending().await
|
||||
}
|
||||
}
|
||||
});
|
||||
if run_mode == RunMode::Both || run_mode == RunMode::Server {
|
||||
server::play(
|
||||
records,
|
||||
tls_mode,
|
||||
&subopt.certs,
|
||||
("0.0.0.0", subopt.listen_port),
|
||||
sync_sender,
|
||||
subopt.debug,
|
||||
)
|
||||
.await;
|
||||
client::play(
|
||||
records,
|
||||
subopt.tls,
|
||||
(subopt.connect_addr, subopt.connect_port),
|
||||
subopt.repeat,
|
||||
subopt.debug,
|
||||
)
|
||||
.await;
|
||||
if let Some(notify_addr) = subopt.notify_addr {
|
||||
let socket = std::net::UdpSocket::bind("0.0.0.0:48567").unwrap();
|
||||
socket.send_to(b"done", ¬ify_addr).unwrap();
|
||||
}
|
||||
client.await.unwrap();
|
||||
}
|
||||
Subcommand::Server(subopt) => {
|
||||
let records = RECORDS.init(record::read_record_file(&opt.record_file));
|
||||
|
||||
util::init_provider();
|
||||
|
||||
//console_subscriber::init();
|
||||
server::play(
|
||||
records,
|
||||
subopt.tls,
|
||||
&subopt.certs,
|
||||
("0.0.0.0", subopt.listen_port),
|
||||
subopt.debug,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
Subcommand::Print(subopt) => {
|
||||
let records = record::read_record_file(&opt.record_file);
|
||||
record::print_records(&records, subopt.packets, subopt.number);
|
||||
record::print_records(&records, subopt.number);
|
||||
}
|
||||
#[cfg(feature = "record")]
|
||||
Subcommand::Record(_subopt) => {
|
||||
record::make_record(&opt.record_file);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue