address filter
This commit is contained in:
@@ -370,6 +370,14 @@ impl Network {
|
||||
c.network.connection_initial_timeout_ms
|
||||
};
|
||||
|
||||
if self
|
||||
.network_manager()
|
||||
.address_filter()
|
||||
.is_punished(dial_info.address().to_ip_addr())
|
||||
{
|
||||
return Ok(NetworkResult::no_connection_other("punished"));
|
||||
}
|
||||
|
||||
match dial_info.protocol_type() {
|
||||
ProtocolType::UDP => {
|
||||
let peer_socket_addr = dial_info.to_socket_addr();
|
||||
@@ -429,6 +437,14 @@ impl Network {
|
||||
c.network.connection_initial_timeout_ms
|
||||
};
|
||||
|
||||
if self
|
||||
.network_manager()
|
||||
.address_filter()
|
||||
.is_punished(dial_info.address().to_ip_addr())
|
||||
{
|
||||
return Ok(NetworkResult::no_connection_other("punished"));
|
||||
}
|
||||
|
||||
match dial_info.protocol_type() {
|
||||
ProtocolType::UDP => {
|
||||
let peer_socket_addr = dial_info.to_socket_addr();
|
||||
|
@@ -108,12 +108,29 @@ impl Network {
|
||||
}
|
||||
};
|
||||
|
||||
// XXX
|
||||
// warn!(
|
||||
// "DEBUGACCEPT: local={} remote={}",
|
||||
// tcp_stream.local_addr().unwrap(),
|
||||
// tcp_stream.peer_addr().unwrap(),
|
||||
// );
|
||||
// Limit the number of connections from the same IP address
|
||||
// and the number of total connections
|
||||
// XXX limiting here instead for connection table? may be faster and avoids tls negotiation
|
||||
let peer_addr = match tcp_stream.peer_addr() {
|
||||
Ok(addr) => addr,
|
||||
Err(e) => {
|
||||
log_net!(debug "failed to get peer address: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let address_filter = self.network_manager().address_filter();
|
||||
// Check to see if it is punished
|
||||
if address_filter.is_punished(peer_addr.ip()) {
|
||||
return;
|
||||
}
|
||||
|
||||
let local_addr = match tcp_stream.local_addr() {
|
||||
Ok(addr) => addr,
|
||||
Err(e) => {
|
||||
log_net!(debug "failed to get local address: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(e) = tcp_stream.set_linger(Some(core::time::Duration::from_secs(0))) {
|
||||
log_net!(debug "Couldn't set TCP linger: {}", e);
|
||||
@@ -127,24 +144,6 @@ impl Network {
|
||||
let listener_state = listener_state.clone();
|
||||
let connection_manager = connection_manager.clone();
|
||||
|
||||
// Limit the number of connections from the same IP address
|
||||
// and the number of total connections
|
||||
let peer_addr = match tcp_stream.peer_addr() {
|
||||
Ok(addr) => addr,
|
||||
Err(e) => {
|
||||
log_net!(debug "failed to get peer address: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let local_addr = match tcp_stream.local_addr() {
|
||||
Ok(addr) => addr,
|
||||
Err(e) => {
|
||||
log_net!(debug "failed to get local address: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
// XXX limiting here instead for connection table? may be faster and avoids tls negotiation
|
||||
|
||||
log_net!("TCP connection from: {}", peer_addr);
|
||||
|
||||
// Create a stream we can peek on
|
||||
|
@@ -66,8 +66,6 @@ impl Network {
|
||||
.await
|
||||
{
|
||||
Ok(Ok((size, descriptor))) => {
|
||||
// XXX: Limit the number of packets from the same IP address?
|
||||
|
||||
// Network accounting
|
||||
network_manager.stats_packet_rcvd(
|
||||
descriptor.remote_address().to_ip_addr(),
|
||||
@@ -143,7 +141,10 @@ impl Network {
|
||||
let socket_arc = Arc::new(udp_socket);
|
||||
|
||||
// Create protocol handler
|
||||
let udpv4_handler = RawUdpProtocolHandler::new(socket_arc);
|
||||
let udpv4_handler = RawUdpProtocolHandler::new(
|
||||
socket_arc,
|
||||
Some(self.network_manager().address_filter()),
|
||||
);
|
||||
|
||||
inner.outbound_udpv4_protocol_handler = Some(udpv4_handler);
|
||||
}
|
||||
@@ -164,7 +165,10 @@ impl Network {
|
||||
let socket_arc = Arc::new(udp_socket);
|
||||
|
||||
// Create protocol handler
|
||||
let udpv6_handler = RawUdpProtocolHandler::new(socket_arc);
|
||||
let udpv6_handler = RawUdpProtocolHandler::new(
|
||||
socket_arc,
|
||||
Some(self.network_manager().address_filter()),
|
||||
);
|
||||
|
||||
inner.outbound_udpv6_protocol_handler = Some(udpv6_handler);
|
||||
}
|
||||
@@ -191,7 +195,8 @@ impl Network {
|
||||
let socket_arc = Arc::new(udp_socket);
|
||||
|
||||
// Create protocol handler
|
||||
let protocol_handler = RawUdpProtocolHandler::new(socket_arc);
|
||||
let protocol_handler =
|
||||
RawUdpProtocolHandler::new(socket_arc, Some(self.network_manager().address_filter()));
|
||||
|
||||
// Create message_handler records
|
||||
self.inner
|
||||
|
@@ -22,7 +22,11 @@ impl ProtocolNetworkConnection {
|
||||
local_address: Option<SocketAddr>,
|
||||
dial_info: &DialInfo,
|
||||
timeout_ms: u32,
|
||||
address_filter: AddressFilter,
|
||||
) -> io::Result<NetworkResult<ProtocolNetworkConnection>> {
|
||||
if address_filter.is_punished(dial_info.address().to_ip_addr()) {
|
||||
return Ok(NetworkResult::no_connection_other("punished"));
|
||||
}
|
||||
match dial_info.protocol_type() {
|
||||
ProtocolType::UDP => {
|
||||
panic!("Should not connect to UDP dialinfo");
|
||||
|
@@ -5,13 +5,15 @@ use sockets::*;
|
||||
pub struct RawUdpProtocolHandler {
|
||||
socket: Arc<UdpSocket>,
|
||||
assembly_buffer: AssemblyBuffer,
|
||||
address_filter: Option<AddressFilter>,
|
||||
}
|
||||
|
||||
impl RawUdpProtocolHandler {
|
||||
pub fn new(socket: Arc<UdpSocket>) -> Self {
|
||||
pub fn new(socket: Arc<UdpSocket>, address_filter: Option<AddressFilter>) -> Self {
|
||||
Self {
|
||||
socket,
|
||||
assembly_buffer: AssemblyBuffer::new(),
|
||||
address_filter,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +23,13 @@ impl RawUdpProtocolHandler {
|
||||
// Get a packet
|
||||
let (size, remote_addr) = network_result_value_or_log!(self.socket.recv_from(data).await.into_network_result()? => continue);
|
||||
|
||||
// Check to see if it is punished
|
||||
if let Some(af) = self.address_filter.as_ref() {
|
||||
if af.is_punished(remote_addr.ip()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Insert into assembly buffer
|
||||
let Some(message) = self.assembly_buffer.insert_frame(&data[0..size], remote_addr) else {
|
||||
continue;
|
||||
@@ -66,6 +75,13 @@ impl RawUdpProtocolHandler {
|
||||
bail_io_error_other!("sending too large UDP message");
|
||||
}
|
||||
|
||||
// Check to see if it is punished
|
||||
if let Some(af) = self.address_filter.as_ref() {
|
||||
if af.is_punished(remote_addr.ip()) {
|
||||
return Ok(NetworkResult::no_connection_other("punished"));
|
||||
}
|
||||
}
|
||||
|
||||
// Fragment and send
|
||||
let sender = |framed_chunk: Vec<u8>, remote_addr: SocketAddr| async move {
|
||||
let len = network_result_try!(self
|
||||
@@ -111,6 +127,6 @@ impl RawUdpProtocolHandler {
|
||||
// get local wildcard address for bind
|
||||
let local_socket_addr = compatible_unspecified_socket_addr(&socket_addr);
|
||||
let socket = UdpSocket::bind(local_socket_addr).await?;
|
||||
Ok(RawUdpProtocolHandler::new(Arc::new(socket)))
|
||||
Ok(RawUdpProtocolHandler::new(Arc::new(socket), None))
|
||||
}
|
||||
}
|
||||
|
@@ -609,7 +609,7 @@ impl Network {
|
||||
ip_addrs,
|
||||
tcp_port,
|
||||
false,
|
||||
Box::new(move |c, _| Box::new(RawTcpProtocolHandler::new(c))),
|
||||
Box::new(|c, _| Box::new(RawTcpProtocolHandler::new(c))),
|
||||
)
|
||||
.await?;
|
||||
trace!("TCP: listener started on {:#?}", socket_addresses);
|
||||
|
Reference in New Issue
Block a user