feat: add Minecraft proxy and refresh dependencies
This commit is contained in:
parent
a4d0880a0a
commit
cfaad47948
47 changed files with 2671 additions and 1005 deletions
230
src/engine/tunnel/gateway.rs
Normal file
230
src/engine/tunnel/gateway.rs
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
mod challenge;
|
||||
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
use super::proxy;
|
||||
use super::{Gateway, GatewayOutcome};
|
||||
use crate::config::Config;
|
||||
use crate::engine::challenge::DifficultyAdjuster;
|
||||
use crate::engine::subnet::tracker::SubnetTracker;
|
||||
use crate::filter::blacklist::Blacklist;
|
||||
use crate::filter::connection_limit::ConnectionLimiter;
|
||||
use crate::filter::rate_limit::RateLimiter;
|
||||
use crate::metrics;
|
||||
use crate::protocol::ProtocolRegistry;
|
||||
use crate::store::clickhouse::{ClickHouseEvent, ClickHouseWriter};
|
||||
use crate::traffic::reputation::IpReputation;
|
||||
use crate::xdp::XdpFilter;
|
||||
use std::collections::HashSet;
|
||||
use std::net::IpAddr;
|
||||
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::sync::Mutex as TokioMutex;
|
||||
|
||||
impl Gateway {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn new(
|
||||
config: Arc<Config>,
|
||||
rate_limiter: Arc<RateLimiter>,
|
||||
blacklist: Arc<Blacklist>,
|
||||
adjuster: Arc<Mutex<DifficultyAdjuster>>,
|
||||
whitelist: Arc<HashSet<IpAddr>>,
|
||||
reputation: Arc<IpReputation>,
|
||||
xdp: Option<Arc<Mutex<XdpFilter>>>,
|
||||
clickhouse: Option<Arc<TokioMutex<ClickHouseWriter>>>,
|
||||
allowed_1s: Arc<AtomicU64>,
|
||||
registry: Arc<ProtocolRegistry>,
|
||||
subnet_tracker: Option<Arc<SubnetTracker>>,
|
||||
) -> Self {
|
||||
let max_connections = config.limits.max_connections_per_ip as usize;
|
||||
Self {
|
||||
config,
|
||||
rate_limiter,
|
||||
connection_limiter: ConnectionLimiter::new(max_connections),
|
||||
blacklist,
|
||||
adjuster,
|
||||
whitelist,
|
||||
reputation,
|
||||
xdp,
|
||||
clickhouse,
|
||||
allowed_1s,
|
||||
registry,
|
||||
subnet_tracker,
|
||||
upstream_cursor: AtomicUsize::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
/// Полный конвейер обработки входящего соединения.
|
||||
///
|
||||
/// # Errors
|
||||
/// Возвращает ошибку только при сбое ввода-вывода апстрима; блокировки
|
||||
/// клиента ошибкой не считаются.
|
||||
#[allow(unused_mut)]
|
||||
pub async fn handle(&self, mut client: TcpStream, peer_addr: std::net::SocketAddr) -> GatewayOutcome {
|
||||
let client_addr = if self.config.proxy_protocol.ingress_enabled {
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
{
|
||||
if !self
|
||||
.config
|
||||
.proxy_protocol
|
||||
.trusted_cidrs
|
||||
.iter()
|
||||
.any(|cidr| cidr.contains(peer_addr.ip()))
|
||||
{
|
||||
return GatewayOutcome {
|
||||
client_ip: None,
|
||||
result: Err(anyhow::anyhow!("untrusted peer attempted inbound PROXY protocol")),
|
||||
};
|
||||
}
|
||||
match proxy::read_v2_header(
|
||||
&mut client,
|
||||
Duration::from_millis(self.config.proxy_protocol.ingress_timeout_ms),
|
||||
self.config.proxy_protocol.max_header_bytes,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(client_addr) => client_addr,
|
||||
Err(error) => {
|
||||
return GatewayOutcome {
|
||||
client_ip: None,
|
||||
result: Err(error),
|
||||
};
|
||||
},
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "protocol-minecraft"))]
|
||||
{
|
||||
return GatewayOutcome {
|
||||
client_ip: None,
|
||||
result: Err(anyhow::anyhow!("inbound PROXY protocol requires protocol-minecraft")),
|
||||
};
|
||||
}
|
||||
} else {
|
||||
peer_addr
|
||||
};
|
||||
let client_ip = client_addr.ip();
|
||||
let result = self.handle_client(client, peer_addr, client_addr).await;
|
||||
GatewayOutcome {
|
||||
client_ip: Some(client_ip),
|
||||
result,
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_client(
|
||||
&self,
|
||||
mut client: TcpStream,
|
||||
peer_addr: std::net::SocketAddr,
|
||||
client_addr: std::net::SocketAddr,
|
||||
) -> anyhow::Result<()> {
|
||||
let peer_ip = client_addr.ip();
|
||||
|
||||
if let Some(tracker) = &self.subnet_tracker {
|
||||
tracker.record_connection(peer_ip);
|
||||
}
|
||||
|
||||
if self.blacklist.is_blocked(peer_ip) {
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["blocked"]).inc();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let Some(_connection_permit) = self.connection_limiter.try_acquire(peer_ip) else {
|
||||
self.block_rate_limit(peer_ip).await;
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
if !self.pass_pow_gate(&mut client, peer_ip).await? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !self.rate_limiter.check(peer_ip) {
|
||||
self.block_rate_limit(peer_ip).await;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let handler = self.registry.primary()?;
|
||||
let handshake = match handler.handshake(&mut client).await {
|
||||
Ok(handshake) => handshake,
|
||||
Err(e) => {
|
||||
tracing::debug!("handshake rejected from {peer_addr}: {e}");
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["blocked"]).inc();
|
||||
return Ok(());
|
||||
},
|
||||
};
|
||||
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["allowed"]).inc();
|
||||
self.allowed_1s.fetch_add(1, Ordering::Relaxed);
|
||||
self.reputation.record_good(peer_ip);
|
||||
|
||||
let mut backend = TcpStream::connect(self.next_upstream(&handshake.upstream.addr)).await?;
|
||||
#[cfg(feature = "protocol-minecraft")]
|
||||
if self.config.protocol.minecraft.enabled && self.config.protocol.minecraft.send_proxy_protocol {
|
||||
let source = client_addr;
|
||||
let destination = client.local_addr()?;
|
||||
backend.write_all(&proxy::protocol_v2(source, destination)?).await?;
|
||||
}
|
||||
if !handshake.initial_backend_data.is_empty() {
|
||||
backend.write_all(&handshake.initial_backend_data).await?;
|
||||
}
|
||||
tokio::io::copy_bidirectional(&mut client, &mut backend).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn next_upstream<'a>(&'a self, negotiated: &'a str) -> &'a str {
|
||||
let upstreams = &self.config.backend.upstreams;
|
||||
if upstreams.len() > 1 {
|
||||
let idx = self.upstream_cursor.fetch_add(1, Ordering::Relaxed) % upstreams.len();
|
||||
return upstreams[idx].as_str();
|
||||
}
|
||||
if let Some(first) = upstreams.first() {
|
||||
return first.as_str();
|
||||
}
|
||||
negotiated
|
||||
}
|
||||
|
||||
async fn block_rate_limit(&self, ip: IpAddr) {
|
||||
metrics::RATE_LIMIT_HITS.with_label_values(&["hit"]).inc();
|
||||
metrics::CONNECTIONS_TOTAL.with_label_values(&["blocked"]).inc();
|
||||
self.reputation.record_bad(ip);
|
||||
if self.reputation.score(ip) < -40 {
|
||||
let duration_secs = Duration::from_secs(self.config.ban.ban_duration_secs);
|
||||
self.blacklist.add(ip, duration_secs, "low_reputation");
|
||||
if !self.config.proxy_protocol.ingress_enabled {
|
||||
self.xdp_ban(ip, duration_secs.as_secs());
|
||||
}
|
||||
self.push_event("block", ip, "low_reputation").await;
|
||||
tracing::info!("low reputation ban {ip}: rate-limit abuse");
|
||||
}
|
||||
}
|
||||
|
||||
fn xdp_ban(&self, ip: IpAddr, duration_secs: u64) {
|
||||
let Some(xdp) = &self.xdp else {
|
||||
return;
|
||||
};
|
||||
let IpAddr::V4(ip_v4) = ip else {
|
||||
return;
|
||||
};
|
||||
match xdp.lock().expect("xdp lock poisoned").ban_ip(ip_v4, duration_secs) {
|
||||
Ok(()) => tracing::debug!("xdp ban {ip_v4} for {duration_secs}s"),
|
||||
Err(e) => tracing::warn!("xdp ban failed for {ip_v4}: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
async fn push_event(&self, event_type: &str, ip: IpAddr, data_string: &str) {
|
||||
let Some(writer) = &self.clickhouse else {
|
||||
return;
|
||||
};
|
||||
let event = ClickHouseEvent {
|
||||
timestamp: chrono::Utc::now(),
|
||||
event_type: event_type.to_string(),
|
||||
ip: ip.to_string(),
|
||||
data_float: 0.0,
|
||||
data_int: 0,
|
||||
data_string: data_string.to_string(),
|
||||
};
|
||||
if let Err(e) = writer.lock().await.push(event).await {
|
||||
tracing::debug!("clickhouse push error: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/engine/tunnel/gateway/challenge.rs
Normal file
34
src/engine/tunnel/gateway/challenge.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use super::Gateway;
|
||||
use crate::engine::challenge::enforce as enforce_pow;
|
||||
use crate::metrics;
|
||||
use std::net::IpAddr;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
impl Gateway {
|
||||
pub(super) async fn pass_pow_gate(&self, client: &mut TcpStream, peer_ip: IpAddr) -> anyhow::Result<bool> {
|
||||
let pow_config = &self.config.pow;
|
||||
if !(pow_config.enabled && pow_config.difficulty > 0) {
|
||||
return Ok(true);
|
||||
}
|
||||
if self.whitelist.contains(&peer_ip) {
|
||||
metrics::POW_CHALLENGES_TOTAL.with_label_values(&["skipped"]).inc();
|
||||
metrics::POW_CURRENT_DIFFICULTY.set(pow_config.difficulty as i64);
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
let diff = {
|
||||
let mut adjuster = self.adjuster.lock().expect("adjuster lock poisoned");
|
||||
adjuster.record_connection();
|
||||
adjuster.current_difficulty()
|
||||
};
|
||||
let result = enforce_pow(client, peer_ip, diff).await?;
|
||||
if !result {
|
||||
metrics::POW_CHALLENGES_TOTAL.with_label_values(&["failed"]).inc();
|
||||
tracing::debug!("pow: failed for {peer_ip}, dropping connection");
|
||||
return Ok(false);
|
||||
}
|
||||
metrics::POW_CHALLENGES_TOTAL.with_label_values(&["passed"]).inc();
|
||||
metrics::POW_CURRENT_DIFFICULTY.set(diff as i64);
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
40
src/engine/tunnel/proxy.rs
Normal file
40
src/engine/tunnel/proxy.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use std::net::SocketAddr;
|
||||
|
||||
mod inbound;
|
||||
pub(super) use inbound::read_v2_header;
|
||||
|
||||
/// Encodes a PROXY protocol v2 TCP header for the backend.
|
||||
/// The backend must accept connections only from trusted Rampart nodes.
|
||||
pub(super) fn protocol_v2(source: SocketAddr, destination: SocketAddr) -> anyhow::Result<Vec<u8>> {
|
||||
const SIGNATURE: [u8; 12] = *b"\r\n\r\n\0\r\nQUIT\n";
|
||||
if source.is_ipv4() != destination.is_ipv4() {
|
||||
anyhow::bail!("PROXY protocol source/destination address families differ");
|
||||
}
|
||||
|
||||
let (family, address_len) = if source.is_ipv4() {
|
||||
(0x11, 12usize)
|
||||
} else {
|
||||
(0x21, 36usize)
|
||||
};
|
||||
let mut header = Vec::with_capacity(16 + address_len);
|
||||
header.extend_from_slice(&SIGNATURE);
|
||||
header.push(0x21);
|
||||
header.push(family);
|
||||
header.extend_from_slice(&(address_len as u16).to_be_bytes());
|
||||
match (source, destination) {
|
||||
(SocketAddr::V4(src), SocketAddr::V4(dst)) => {
|
||||
header.extend_from_slice(&src.ip().octets());
|
||||
header.extend_from_slice(&dst.ip().octets());
|
||||
header.extend_from_slice(&src.port().to_be_bytes());
|
||||
header.extend_from_slice(&dst.port().to_be_bytes());
|
||||
},
|
||||
(SocketAddr::V6(src), SocketAddr::V6(dst)) => {
|
||||
header.extend_from_slice(&src.ip().octets());
|
||||
header.extend_from_slice(&dst.ip().octets());
|
||||
header.extend_from_slice(&src.port().to_be_bytes());
|
||||
header.extend_from_slice(&dst.port().to_be_bytes());
|
||||
},
|
||||
_ => anyhow::bail!("PROXY protocol source/destination address families differ"),
|
||||
}
|
||||
Ok(header)
|
||||
}
|
||||
70
src/engine/tunnel/proxy/inbound.rs
Normal file
70
src/engine/tunnel/proxy/inbound.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
//! Strict, bounded parser for trusted inbound PROXY protocol v2 headers.
|
||||
|
||||
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
|
||||
use std::time::Duration;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
const SIGNATURE: [u8; 12] = *b"\r\n\r\n\0\r\nQUIT\n";
|
||||
const FIXED_HEADER_BYTES: usize = 16;
|
||||
|
||||
pub(crate) async fn read_v2_header(
|
||||
stream: &mut TcpStream,
|
||||
timeout: Duration,
|
||||
max_header_bytes: usize,
|
||||
) -> anyhow::Result<SocketAddr> {
|
||||
tokio::time::timeout(timeout, read_v2_header_inner(stream, max_header_bytes))
|
||||
.await
|
||||
.map_err(|_| anyhow::anyhow!("timed out reading inbound PROXY v2 header"))?
|
||||
}
|
||||
|
||||
async fn read_v2_header_inner(stream: &mut TcpStream, max_header_bytes: usize) -> anyhow::Result<SocketAddr> {
|
||||
let mut fixed = [0u8; FIXED_HEADER_BYTES];
|
||||
stream.read_exact(&mut fixed).await?;
|
||||
|
||||
if fixed[..12] != SIGNATURE {
|
||||
anyhow::bail!("invalid inbound PROXY protocol signature");
|
||||
}
|
||||
if fixed[12] != 0x21 {
|
||||
anyhow::bail!("inbound PROXY protocol requires version 2 PROXY command");
|
||||
}
|
||||
|
||||
let payload_len = usize::from(u16::from_be_bytes([fixed[14], fixed[15]]));
|
||||
let total_len = FIXED_HEADER_BYTES
|
||||
.checked_add(payload_len)
|
||||
.ok_or_else(|| anyhow::anyhow!("inbound PROXY header length overflow"))?;
|
||||
if total_len > max_header_bytes {
|
||||
anyhow::bail!("inbound PROXY header exceeds configured limit");
|
||||
}
|
||||
|
||||
let address_len = match fixed[13] {
|
||||
0x11 => 12,
|
||||
0x21 => 36,
|
||||
family => anyhow::bail!("unsupported inbound PROXY address family/protocol 0x{family:02x}"),
|
||||
};
|
||||
if payload_len < address_len {
|
||||
anyhow::bail!("inbound PROXY header is shorter than its address block");
|
||||
}
|
||||
if payload_len != address_len {
|
||||
anyhow::bail!("inbound PROXY v2 TLVs are not supported");
|
||||
}
|
||||
|
||||
let mut payload = vec![0u8; payload_len];
|
||||
stream.read_exact(&mut payload).await?;
|
||||
match fixed[13] {
|
||||
0x11 => {
|
||||
let source = Ipv4Addr::new(payload[0], payload[1], payload[2], payload[3]);
|
||||
let port = u16::from_be_bytes([payload[8], payload[9]]);
|
||||
Ok(SocketAddr::V4(SocketAddrV4::new(source, port)))
|
||||
},
|
||||
0x21 => {
|
||||
let source_bytes: [u8; 16] = payload[..16]
|
||||
.try_into()
|
||||
.map_err(|_| anyhow::anyhow!("invalid inbound PROXY IPv6 source address"))?;
|
||||
let source = Ipv6Addr::from(source_bytes);
|
||||
let port = u16::from_be_bytes([payload[32], payload[33]]);
|
||||
Ok(SocketAddr::V6(SocketAddrV6::new(source, port, 0, 0)))
|
||||
},
|
||||
family => anyhow::bail!("unsupported inbound PROXY address family/protocol 0x{family:02x}"),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue