adding log to unauthorized, and error log when err accepting conn

This commit is contained in:
midefos 2025-01-18 15:10:55 +01:00
parent 3499f430b1
commit 7722a5eff5
3 changed files with 44 additions and 16 deletions

View File

@ -12,17 +12,21 @@ impl ServerBuilder {
self self
} }
pub fn private_ips(mut self) -> Self { pub fn enable_private_ips(self) -> Self {
self.config.ips_filter = true; self.set_private_ips(true)
}
pub fn set_private_ips(mut self, enabled: bool) -> Self {
self.config.private_ips = enabled;
self.update_ip_filter_state();
self.config.private_ips = true;
self self
} }
pub fn ips(mut self, ips: Vec<String>) -> Self { pub fn ips(mut self, ips: Vec<String>) -> Self {
self.config.ips_filter = true;
self.config.ips = ips; self.config.ips = ips;
self.update_ip_filter_state();
self self
} }
@ -36,4 +40,12 @@ impl ServerBuilder {
config: Arc::new(self.config), config: Arc::new(self.config),
} }
} }
fn update_ip_filter_state(&mut self) {
if self.config.private_ips || !self.config.ips.is_empty() {
self.config.ip_filter = true;
} else {
self.config.ip_filter = false;
}
}
} }

View File

@ -6,16 +6,20 @@ pub struct ServerConfig {
pub ip: String, pub ip: String,
pub port: u16, pub port: u16,
pub ips_filter: bool, // IP filtering
pub ip_filter: bool,
pub private_ips: bool, pub private_ips: bool,
pub ips: Vec<String>, pub ips: Vec<String>,
// Request filtering
pub api_key: Option<String>, pub api_key: Option<String>,
pub log_unauthorized: bool,
} }
impl ServerConfig { impl ServerConfig {
pub fn is_ip_authorized(&self, ip: &IpAddr) -> bool { pub fn is_ip_authorized(&self, ip: &IpAddr) -> bool {
if !self.ips_filter { if !self.ip_filter {
return true; return true;
} }
@ -57,11 +61,13 @@ impl Default for ServerConfig {
ip: "127.0.0.1".to_string(), ip: "127.0.0.1".to_string(),
port: 8080, port: 8080,
ips_filter: false, ip_filter: false,
private_ips: false, private_ips: false,
ips: Vec::new(), ips: Vec::new(),
api_key: None, api_key: None,
log_unauthorized: true,
} }
} }
} }

View File

@ -31,13 +31,16 @@ impl Server {
let handler = Arc::new(handler); let handler = Arc::new(handler);
loop { loop {
let listener_res = listener.accept().await; let (tcp, client_addr) = match listener.accept().await {
if listener_res.is_err() { Ok(conn) => conn,
continue; Err(error) => {
} error!(
error = error.to_string().as_str();
let (tcp, client_addr) = listener_res.unwrap(); "Failed to accept connection"
let client_ip = client_addr.ip(); );
continue;
}
};
let io = TokioIo::new(tcp); let io = TokioIo::new(tcp);
let config = Arc::clone(&self.config); let config = Arc::clone(&self.config);
@ -52,9 +55,16 @@ impl Server {
let handler = Arc::clone(&handler); let handler = Arc::clone(&handler);
async move { async move {
if !config.is_ip_authorized(&client_ip) if !config.is_ip_authorized(&client_addr.ip())
|| !config.is_req_authorized(&req) || !config.is_req_authorized(&req)
{ {
if config.log_unauthorized {
error!(tag = "ban",
ip = client_addr.ip().to_string().as_str();
"Unauthorized"
);
}
Responder::unathorized() Responder::unathorized()
} else { } else {
handler(req).await handler(req).await