refactor(relay): use RangeInclusive to specify available ports (#5820)

This commit is contained in:
Thomas Eizinger
2024-07-11 16:26:21 +10:00
committed by GitHub
parent f6b6055f65
commit 8ec6a809a1
5 changed files with 11 additions and 14 deletions

View File

@@ -351,7 +351,7 @@ impl Firewall {
impl TestRelay {
fn new(local: impl Into<RelaySocket>, span: Span) -> Self {
let local = local.into();
let inner = firezone_relay::Server::new(to_ip_stack(local), OsRng, 3478, 49152, 65535);
let inner = firezone_relay::Server::new(to_ip_stack(local), OsRng, 3478, 49152..=65535);
Self {
inner,

View File

@@ -91,8 +91,7 @@ impl StateMachineTest for TunnelTest {
IpStack::from((ip4, ip6)),
rand::rngs::StdRng::seed_from_u64(seed),
3478,
49152,
65535,
49152..=65535,
))
},
debug_span!("relay", rid = %id),

View File

@@ -114,8 +114,7 @@ async fn main() -> Result<()> {
public_addr,
make_rng(args.rng_seed),
args.listen_port,
args.lowest_port,
args.highest_port,
args.lowest_port..=args.highest_port,
);
let last_heartbeat_sent = Arc::new(Mutex::new(Option::<Instant>::None));

View File

@@ -20,6 +20,7 @@ use secrecy::SecretString;
use std::collections::{HashMap, VecDeque};
use std::hash::Hash;
use std::net::{IpAddr, SocketAddr};
use std::ops::RangeInclusive;
use std::time::{Duration, Instant, SystemTime};
use stun_codec::rfc5389::attributes::{
ErrorCode, MessageIntegrity, Nonce, Realm, Username, XorMappedAddress,
@@ -62,8 +63,8 @@ pub struct Server<R> {
HashMap<(AllocationPort, PeerSocket), (ClientSocket, ChannelNumber)>,
listen_port: u16,
lowest_port: u16,
highest_port: u16,
ports: RangeInclusive<u16>,
/// Channel numbers are unique by client, thus indexed by both.
channels_by_client_and_number: HashMap<(ClientSocket, ChannelNumber), Channel>,
@@ -157,8 +158,7 @@ where
public_address: impl Into<IpStack>,
mut rng: R,
listen_port: u16,
lowest_port: u16,
highest_port: u16,
ports: RangeInclusive<u16>,
) -> Self {
// TODO: Validate that local IP isn't multicast / loopback etc.
@@ -185,8 +185,7 @@ where
allocations: Default::default(),
clients_by_allocation: Default::default(),
listen_port,
lowest_port,
highest_port,
ports,
channels_by_client_and_number: Default::default(),
channel_numbers_by_client_and_peer: Default::default(),
pending_commands: Default::default(),
@@ -807,7 +806,7 @@ where
);
let port = loop {
let candidate = AllocationPort(self.rng.gen_range(self.lowest_port..self.highest_port));
let candidate = AllocationPort(self.rng.gen_range(self.ports.clone()));
if !self.clients_by_allocation.contains_key(&candidate) {
break candidate;
@@ -823,7 +822,7 @@ where
}
fn max_available_ports(&self) -> u16 {
self.highest_port - self.lowest_port
self.ports.clone().count() as u16
}
fn create_channel_binding(

View File

@@ -696,7 +696,7 @@ struct TestServer {
impl TestServer {
fn new(relay_public_addr: impl Into<IpStack>) -> Self {
Self {
server: Server::new(relay_public_addr, StepRng::new(0, 0), 3478, 49152, 65535),
server: Server::new(relay_public_addr, StepRng::new(0, 0), 3478, 49152..=65535),
}
}