mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 10:15:31 +00:00
Use CanonicalSocketAddr in ws and http; remove old option from common
This commit is contained in:
parent
380ca222de
commit
8889ab586c
9 changed files with 36 additions and 62 deletions
|
|
@ -16,7 +16,7 @@ pub fn handle_announce_request(
|
|||
request_sender_meta: ConnectionMeta,
|
||||
request: AnnounceRequest,
|
||||
) {
|
||||
let torrent_data: &mut TorrentData = if request_sender_meta.converted_peer_ip.is_ipv4() {
|
||||
let torrent_data: &mut TorrentData = if request_sender_meta.peer_addr.is_ipv4() {
|
||||
torrent_maps.ipv4.entry(request.info_hash).or_default()
|
||||
} else {
|
||||
torrent_maps.ipv6.entry(request.info_hash).or_default()
|
||||
|
|
@ -25,11 +25,9 @@ pub fn handle_announce_request(
|
|||
// If there is already a peer with this peer_id, check that socket
|
||||
// addr is same as that of request sender. Otherwise, ignore request.
|
||||
// Since peers have access to each others peer_id's, they could send
|
||||
// requests using them, causing all sorts of issues. Checking naive
|
||||
// (non-converted) socket addresses is enough, since state is split
|
||||
// on converted peer ip.
|
||||
// requests using them, causing all sorts of issues.
|
||||
if let Some(previous_peer) = torrent_data.peers.get(&request.peer_id) {
|
||||
if request_sender_meta.naive_peer_addr != previous_peer.connection_meta.naive_peer_addr {
|
||||
if request_sender_meta.peer_addr != previous_peer.connection_meta.peer_addr {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
@ -167,7 +165,7 @@ pub fn handle_scrape_request(
|
|||
files: HashMap::with_capacity(num_to_take),
|
||||
};
|
||||
|
||||
let torrent_map: &mut TorrentMap = if meta.converted_peer_ip.is_ipv4() {
|
||||
let torrent_map: &mut TorrentMap = if meta.peer_addr.is_ipv4() {
|
||||
&mut torrent_maps.ipv4
|
||||
} else {
|
||||
&mut torrent_maps.ipv6
|
||||
|
|
|
|||
|
|
@ -2,12 +2,11 @@ pub mod handlers;
|
|||
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
|
||||
use aquatic_common::access_list::{create_access_list_cache, AccessListArcSwap, AccessListCache};
|
||||
use aquatic_common::AHashIndexMap;
|
||||
use aquatic_common::{AHashIndexMap, CanonicalSocketAddr};
|
||||
|
||||
pub use aquatic_common::ValidUntil;
|
||||
|
||||
|
|
@ -30,10 +29,7 @@ pub struct ConnectionMeta {
|
|||
/// sending back response through correct channel to correct worker.
|
||||
pub out_message_consumer_id: ConsumerId,
|
||||
pub connection_id: ConnectionId,
|
||||
/// Peer address as received from socket, meaning it wasn't converted to
|
||||
/// an IPv4 address if it was a IPv4-mapped IPv6 address
|
||||
pub naive_peer_addr: SocketAddr,
|
||||
pub converted_peer_ip: IpAddr,
|
||||
pub peer_addr: CanonicalSocketAddr,
|
||||
pub pending_scrape_id: Option<PendingScrapeId>,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,13 @@
|
|||
use std::borrow::Cow;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::BTreeMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::rc::Rc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use aquatic_common::access_list::{create_access_list_cache, AccessListArcSwap, AccessListCache};
|
||||
use aquatic_common::convert_ipv4_mapped_ipv6;
|
||||
use aquatic_common::CanonicalSocketAddr;
|
||||
use aquatic_ws_protocol::*;
|
||||
use async_tungstenite::WebSocketStream;
|
||||
use futures::stream::{SplitSink, SplitStream};
|
||||
|
|
@ -221,6 +220,7 @@ async fn run_connection(
|
|||
let peer_addr = stream
|
||||
.peer_addr()
|
||||
.map_err(|err| anyhow::anyhow!("Couldn't get peer addr: {:?}", err))?;
|
||||
let peer_addr = CanonicalSocketAddr::new(peer_addr);
|
||||
|
||||
let tls_acceptor: TlsAcceptor = tls_config.into();
|
||||
let stream = tls_acceptor.accept(stream).await?;
|
||||
|
|
@ -293,7 +293,7 @@ struct ConnectionReader {
|
|||
pending_scrape_slab: Rc<RefCell<Slab<PendingScrapeResponse>>>,
|
||||
out_message_consumer_id: ConsumerId,
|
||||
ws_in: SplitStream<WebSocketStream<TlsStream<TcpStream>>>,
|
||||
peer_addr: SocketAddr,
|
||||
peer_addr: CanonicalSocketAddr,
|
||||
connection_id: ConnectionId,
|
||||
}
|
||||
|
||||
|
|
@ -432,8 +432,7 @@ impl ConnectionReader {
|
|||
ConnectionMeta {
|
||||
connection_id: self.connection_id,
|
||||
out_message_consumer_id: self.out_message_consumer_id,
|
||||
naive_peer_addr: self.peer_addr,
|
||||
converted_peer_ip: convert_ipv4_mapped_ipv6(self.peer_addr.ip()),
|
||||
peer_addr: self.peer_addr,
|
||||
pending_scrape_id,
|
||||
}
|
||||
}
|
||||
|
|
@ -445,7 +444,7 @@ struct ConnectionWriter {
|
|||
connection_slab: Rc<RefCell<Slab<ConnectionReference>>>,
|
||||
ws_out: SplitSink<WebSocketStream<TlsStream<TcpStream>>, tungstenite::Message>,
|
||||
pending_scrape_slab: Rc<RefCell<Slab<PendingScrapeResponse>>>,
|
||||
peer_addr: SocketAddr,
|
||||
peer_addr: CanonicalSocketAddr,
|
||||
connection_id: ConnectionId,
|
||||
}
|
||||
|
||||
|
|
@ -456,7 +455,7 @@ impl ConnectionWriter {
|
|||
anyhow::anyhow!("ConnectionWriter couldn't receive message, sender is closed")
|
||||
})?;
|
||||
|
||||
if meta.naive_peer_addr != self.peer_addr {
|
||||
if meta.peer_addr != self.peer_addr {
|
||||
return Err(anyhow::anyhow!("peer addresses didn't match"));
|
||||
}
|
||||
|
||||
|
|
@ -530,7 +529,7 @@ impl ConnectionWriter {
|
|||
Err(err) => {
|
||||
::log::info!(
|
||||
"send_out_message: send to {} took to long: {}",
|
||||
self.peer_addr,
|
||||
self.peer_addr.get(),
|
||||
err
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -153,7 +153,7 @@ impl Connection<NotRegistered> {
|
|||
}
|
||||
|
||||
pub fn close(self) {
|
||||
::log::debug!("will close connection to {}", self.meta.naive_peer_addr);
|
||||
::log::debug!("will close connection to {}", self.meta.peer_addr.get());
|
||||
|
||||
match self.state {
|
||||
ConnectionState::TlsHandshaking(inner) => inner.close(),
|
||||
|
|
|
|||
|
|
@ -4,13 +4,13 @@ use std::time::{Duration, Instant};
|
|||
|
||||
use anyhow::Context;
|
||||
use aquatic_common::access_list::AccessListQuery;
|
||||
use aquatic_common::CanonicalSocketAddr;
|
||||
use hashbrown::HashMap;
|
||||
use mio::net::TcpListener;
|
||||
use mio::{Events, Interest, Poll, Token};
|
||||
use socket2::{Domain, Protocol, Socket, Type};
|
||||
use tungstenite::protocol::WebSocketConfig;
|
||||
|
||||
use aquatic_common::convert_ipv4_mapped_ipv6;
|
||||
use aquatic_ws_protocol::*;
|
||||
|
||||
use crate::common::*;
|
||||
|
|
@ -263,20 +263,17 @@ fn accept_new_streams(
|
|||
loop {
|
||||
match listener.accept() {
|
||||
Ok((stream, _)) => {
|
||||
let naive_peer_addr = if let Ok(peer_addr) = stream.peer_addr() {
|
||||
peer_addr
|
||||
let peer_addr = if let Ok(peer_addr) = stream.peer_addr() {
|
||||
CanonicalSocketAddr::new(peer_addr)
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
connections.insert_and_register_new(poll, move |token| {
|
||||
let converted_peer_ip = convert_ipv4_mapped_ipv6(naive_peer_addr.ip());
|
||||
|
||||
let meta = ConnectionMeta {
|
||||
out_message_consumer_id: ConsumerId(socket_worker_index),
|
||||
connection_id: ConnectionId(token.0),
|
||||
naive_peer_addr,
|
||||
converted_peer_ip,
|
||||
peer_addr,
|
||||
pending_scrape_id: None, // FIXME
|
||||
};
|
||||
|
||||
|
|
@ -348,11 +345,11 @@ where
|
|||
let mut remove_connection = false;
|
||||
|
||||
if let Some(connection) = connections.get_mut(&token) {
|
||||
if connection.get_meta().naive_peer_addr != meta.naive_peer_addr {
|
||||
if connection.get_meta().peer_addr != meta.peer_addr {
|
||||
::log::warn!(
|
||||
"socket worker error: connection socket addr {} didn't match channel {}. Token: {}.",
|
||||
connection.get_meta().naive_peer_addr,
|
||||
meta.naive_peer_addr,
|
||||
connection.get_meta().peer_addr.get(),
|
||||
meta.peer_addr.get(),
|
||||
token.0
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue