Skip to content

Commit

Permalink
dev: implment 3 connection cookie formats
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Sep 8, 2022
1 parent 84ae58c commit bd5e9c0
Show file tree
Hide file tree
Showing 27 changed files with 836 additions and 855 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
/.idea/
/config.toml
/data.db
/.vscode/launch.json

17 changes: 17 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"cSpell.words": [
"byteorder",
"hasher",
"leechers",
"nanos",
"rngs",
"Seedable",
"thiserror",
"torrust",
"typenum"
],
"[rust]": {
"editor.defaultFormatter": "matklad.rust-analyzer", // Makes the magic
"editor.formatOnSave": false // Optional
},
}
132 changes: 44 additions & 88 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ hex = "0.4.3"
percent-encoding = "2.1.0"
binascii = "0.1"
blake3 = "1.3.1"
rust-crypto = "^0.2"
lazy_static = "1.4.0"
blowfish = "0.9.1"
cipher = "0.4.3"
byteorder = "1.4.3"
crypto-common = {version = "0.1.6", features = ["rand_core"] }

openssl = { version = "0.10.41", features = ["vendored"] }

Expand Down
64 changes: 50 additions & 14 deletions benches/generate_connection_id.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,74 @@
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
use aquatic_udp_protocol::ConnectionId;
use criterion::{Criterion, criterion_group, criterion_main};
use torrust_tracker::{udp::connection_id::get_connection_id, protocol::clock::current_timestamp};
use criterion::{criterion_group, criterion_main, Criterion};

use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4};
use std::time::Duration;

use torrust_tracker::protocol::clock::{DefaultClock, Clock};
use torrust_tracker::udp::connection::client_image::{Create, KeyedImage, PlainImage, PlainHash};
use torrust_tracker::udp::connection::connection_cookie::{
ConnectionCookie, EncryptedCookie, HashedCookie, WitnessCookie,
};

fn get_connection_id_old(current_time: u64, port: u16) -> ConnectionId {
let time_i64 = (current_time / 3600) as i64;

ConnectionId((time_i64 | port as i64) << 36)
}

pub fn benchmark_generate_id_with_time_and_port(bench: &mut Criterion) {
pub fn benchmark_make_old_unencoded_id(bench: &mut Criterion) {
let remote_address = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 117);
let current_time = current_timestamp();
let current_time = DefaultClock::now();

bench.bench_function("benchmark_make_old_unencoded_id", |b| {
b.iter(|| {
// Inner closure, the actual test
let _ = get_connection_id_old(current_time.into(), remote_address.port());
})
});
}

pub fn benchmark_make_hashed_encoded_id(bench: &mut Criterion) {
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

bench.bench_function("benchmark_make_hashed_encoded_id", |b| {
b.iter(|| {
// Inner closure, the actual test
let client_image = KeyedImage::new(&socket);
let _ = HashedCookie::new(client_image, Duration::new(1, 0));
})
});
}

pub fn benchmark_make_witness_encoded_id(bench: &mut Criterion) {
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

bench.bench_function("generate_id_with_time_and_port", |b| {
bench.bench_function("benchmark_make_witness_encoded_id", |b| {
b.iter(|| {
// Inner closure, the actual test
let _ = get_connection_id_old(current_time, remote_address.port());
let client_image = KeyedImage::new(&socket);
let _ = WitnessCookie::new(client_image, Duration::new(1, 0));
})
});
}

pub fn benchmark_generate_id_with_hashed_time_and_ip_and_port_and_salt(bench: &mut Criterion) {
let remote_address = SocketAddr::from(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 117));
let current_time = current_timestamp();
let server_secret = [0;32];
pub fn benchmark_make_encrypted_encoded_id(bench: &mut Criterion) {
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

bench.bench_function("generate_id_with_hashed_time_and_ip_and_port_and_salt", |b| {
bench.bench_function("benchmark_make_encrypted_encoded_id", |b| {
b.iter(|| {
// Inner closure, the actual test
let _ = get_connection_id(&server_secret, &remote_address, current_time);
let client_image = <PlainImage as Create<PlainHash>>::new(&socket);
let _ = EncryptedCookie::new(client_image, Duration::new(1, 0));
})
});
}

criterion_group!(benches, benchmark_generate_id_with_time_and_port, benchmark_generate_id_with_hashed_time_and_ip_and_port_and_salt);
criterion_group!(
benches,
benchmark_make_old_unencoded_id,
benchmark_make_hashed_encoded_id,
benchmark_make_witness_encoded_id,
benchmark_make_encrypted_encoded_id,
);
criterion_main!(benches);
Loading

0 comments on commit bd5e9c0

Please sign in to comment.