ws: avoid X-Forwarded-For parsing since we only need to know IPv4/IPv6

This commit is contained in:
Joakim Frostegård 2022-07-19 16:12:00 +02:00
parent 3b94b8e588
commit a62b2033a5
6 changed files with 65 additions and 122 deletions

View file

@ -1,11 +1,28 @@
use std::sync::Arc;
use std::{net::IpAddr, sync::Arc};
use aquatic_common::access_list::AccessListArcSwap;
use aquatic_common::CanonicalSocketAddr;
pub use aquatic_common::ValidUntil;
use aquatic_ws_protocol::{InfoHash, PeerId};
#[derive(Copy, Clone, Debug)]
pub enum IpVersion {
V4,
V6,
}
impl IpVersion {
pub fn canonical_from_ip(ip: IpAddr) -> IpVersion {
match ip {
IpAddr::V4(_) => Self::V4,
IpAddr::V6(addr) => match addr.octets() {
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, _, _, _, _] => Self::V4,
_ => Self::V6,
},
}
}
}
#[derive(Default, Clone)]
pub struct State {
pub access_list: Arc<AccessListArcSwap>,
@ -17,7 +34,7 @@ pub struct PendingScrapeId(pub usize);
#[derive(Copy, Clone, Debug)]
pub struct ConsumerId(pub usize);
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ConnectionId(pub usize);
#[derive(Clone, Copy, Debug)]
@ -26,7 +43,7 @@ pub struct ConnectionMeta {
/// sending back response through correct channel to correct worker.
pub out_message_consumer_id: ConsumerId,
pub connection_id: ConnectionId,
pub peer_addr: CanonicalSocketAddr,
pub ip_version: IpVersion,
pub pending_scrape_id: Option<PendingScrapeId>,
}
@ -35,6 +52,6 @@ pub enum SwarmControlMessage {
ConnectionClosed {
info_hash: InfoHash,
peer_id: PeerId,
peer_addr: CanonicalSocketAddr,
ip_version: IpVersion,
},
}