simplify
This commit is contained in:
parent
e5a40d5ae7
commit
bb8a83cb62
@ -13,7 +13,7 @@ netlink-packet-core = "0.7.0"
|
||||
netlink-packet-route = "0.19.0"
|
||||
netlink-sys = { version = "0.8.6", features = ["tokio"] }
|
||||
reqwest = { version = "0.12.5", features = ["json"] }
|
||||
rtnetlink = "0.14.1" # Updated to a version compatible with netlink-packet-route v0.20.1
|
||||
rtnetlink = "0.14.1"
|
||||
serde = { version = "1.0.204", features = ["rc", "derive"] }
|
||||
serde_json = "1.0.120"
|
||||
tokio = { version = "1.38.0", features = ["full"] }
|
||||
|
@ -74,7 +74,7 @@ async fn main() {
|
||||
tokio::spawn(conn);
|
||||
info!("Listening for IPv4 address changes and interface connect/disconnect events...");
|
||||
|
||||
let mut handler = MessageHandler::new(
|
||||
let mut message_handler = MessageHandler::new(
|
||||
&mut cloudflare,
|
||||
config.max_errors_in_row.unwrap_or(MAX_ERORS_IN_ROW_DEFAULT),
|
||||
);
|
||||
@ -84,7 +84,7 @@ async fn main() {
|
||||
_ = notify.notified() => break,
|
||||
message = messages.next() => {
|
||||
if let Some((message, _)) = message {
|
||||
handler.handle_message(message).await;
|
||||
message_handler.handle_message(message).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::fmt;
|
||||
|
||||
use log::{debug, error, info, log_enabled, Level};
|
||||
use netlink_packet_core::{NetlinkMessage, NetlinkPayload};
|
||||
use netlink_packet_route::RouteNetlinkMessage as RtnlMessage;
|
||||
@ -22,59 +24,57 @@ impl<'a> MessageHandler<'a> {
|
||||
pub async fn handle_message(&mut self, message: NetlinkMessage<RtnlMessage>) -> Option<()> {
|
||||
match message.payload {
|
||||
NetlinkPayload::InnerMessage(RtnlMessage::NewAddress(msg)) => {
|
||||
if log_enabled!(Level::Debug) {
|
||||
debug!("New IPv4 address message: {:?}", msg);
|
||||
} else {
|
||||
info!("New IPv4 address");
|
||||
if let Err(e) = self.cloudflare.check().await {
|
||||
self.errs_counter += 1;
|
||||
error!(
|
||||
"Failed to check cloudflare ({}/{}): {:?}",
|
||||
self.errs_counter, self.errs_max, &e
|
||||
);
|
||||
if self.errs_counter >= self.errs_max {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.log_and_check("New IPv4 address", &msg).await
|
||||
}
|
||||
NetlinkPayload::InnerMessage(RtnlMessage::DelAddress(msg)) => {
|
||||
if log_enabled!(Level::Debug) {
|
||||
debug!("Deleted IPv4 address message: {:?}", msg);
|
||||
} else {
|
||||
info!("Deleted IPv4 address");
|
||||
}
|
||||
self.log_info("Deleted IPv4 address", &msg).await
|
||||
}
|
||||
NetlinkPayload::InnerMessage(RtnlMessage::NewLink(link)) => {
|
||||
if log_enabled!(Level::Debug) {
|
||||
debug!("New link message (interface connected): {:?}", link);
|
||||
} else {
|
||||
info!("New link (interface connected)");
|
||||
if let Err(e) = self.cloudflare.check().await {
|
||||
self.errs_counter += 1;
|
||||
error!(
|
||||
"Failed to check cloudflare ({}/{}): {:?}",
|
||||
self.errs_counter, self.errs_max, &e
|
||||
);
|
||||
if self.errs_counter >= self.errs_max {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.log_and_check("New link (interface connected)", &link)
|
||||
.await
|
||||
}
|
||||
NetlinkPayload::InnerMessage(RtnlMessage::DelLink(link)) => {
|
||||
if log_enabled!(Level::Debug) {
|
||||
debug!("Deleted link message (interface disconnected): {:?}", link);
|
||||
} else {
|
||||
info!("Deleted link (interface disconnected)");
|
||||
}
|
||||
self.log_info("Deleted link (interface disconnected)", &link)
|
||||
.await
|
||||
}
|
||||
_ => {
|
||||
if log_enabled!(Level::Debug) {
|
||||
debug!("Unhandled message payload: {:?}", message.payload);
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn log_and_check<D, M>(&mut self, log_msg: &D, msg: &M) -> Option<()>
|
||||
where
|
||||
D: fmt::Display + ?Sized,
|
||||
M: fmt::Debug,
|
||||
{
|
||||
if log_enabled!(Level::Debug) {
|
||||
debug!("{}: {:?}", log_msg, msg);
|
||||
} else {
|
||||
info!("{}", log_msg);
|
||||
if let Err(e) = self.cloudflare.check().await {
|
||||
self.errs_counter += 1;
|
||||
error!(
|
||||
"Failed to check cloudflare ({}/{}): {:?}",
|
||||
self.errs_counter, self.errs_max, &e
|
||||
);
|
||||
if self.errs_counter >= self.errs_max {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
async fn log_info<M: fmt::Debug>(&self, log_msg: &str, msg: &M) -> Option<()> {
|
||||
if log_enabled!(Level::Debug) {
|
||||
debug!("{:?} message: {:?}", log_msg, msg);
|
||||
} else {
|
||||
info!("{}", log_msg);
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user