log level and suspicioius peer

This commit is contained in:
serxoz 2024-04-16 15:41:33 +02:00
parent c1c01b51d5
commit 908a6895a3
3 changed files with 28 additions and 8 deletions

View File

@ -6,6 +6,8 @@ bind_port = 5063
[log]
# log file location
log_file = "sip-tarpit.log"
# level of logging (DEBUG, INFO, WARN, ERROR)
level = "INFO"
[tarpit]
# delay in milliseconds

View File

@ -16,6 +16,7 @@ struct ConfigTomlListen {
#[derive(Serialize, Deserialize, Debug)]
struct ConfigTomlLog {
log_file: Option<String>,
level: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
@ -28,6 +29,7 @@ pub struct Config {
pub bind_addr: String,
pub bind_port: u16,
pub log_file: String,
pub log_level: String,
pub delay: u64,
}
@ -67,9 +69,12 @@ impl Config {
None => ("127.0.0.1".to_owned(), 5060)
};
let log_file = match config_toml.log {
Some(ConfigTomlLog { log_file }) => log_file.unwrap_or("sip-tarpit.log".to_owned()),
None => "sip-tarpit.log".to_owned()
let (log_file, log_level) = match config_toml.log {
Some(ConfigTomlLog { log_file, level }) => (
log_file.unwrap_or("sip-tarpit.log".to_owned()),
level.unwrap_or("INFO".to_owned())
),
None => ("sip-tarpit.log".to_owned(), "INFO".to_owned())
};
let delay = match config_toml.tarpit {
@ -81,6 +86,7 @@ impl Config {
bind_addr,
bind_port,
log_file,
log_level,
delay
}
}

View File

@ -43,8 +43,7 @@ impl Server {
// If so then we try to send it back to the original source, waiting
// until it's writable and we're able to do so.
if let Some((size, peer)) = to_send {
// let amt = socket.send_to(&buf[..size], &peer).await?;
// println!("Echoed {}/{} bytes to {}", amt, size, peer);
log::info!("Suspicious peer: {}", peer);
//
// test type of message received
let msg = String::from_utf8(buf[..size].to_vec()).unwrap();
@ -164,7 +163,20 @@ impl Server {
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let config: config::Config = config::Config::new();
// println!("{:#?}", config);
let log_level = match config.log_level.to_lowercase().as_str() {
"trace" => LevelFilter::Trace,
"debug" => LevelFilter::Debug,
"info" => LevelFilter::Info,
"warn" => LevelFilter::Warn,
"error" => LevelFilter::Error,
_ => LevelFilter::Info,
};
if log_level == LevelFilter::Debug {
log::info!("Debug mode enabled");
println!("{:#?}", config);
}
let logfile = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new("{d} - {l} - {m}\n")))
@ -173,7 +185,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let logconfig = Config::builder()
.appender(Appender::builder().build("logfile", Box::new(logfile)))
.build(Root::builder().appender("logfile").build(LevelFilter::Trace))
.build(Root::builder().appender("logfile").build(log_level))
.unwrap();
log4rs::init_config(logconfig).unwrap();
@ -185,7 +197,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
socket,
buf: vec![0; 1024],
to_send: None,
config: config,
config,
};
// This starts the server task.