break out massive loop

This commit is contained in:
Love 2024-07-14 15:54:15 +02:00
parent e535c66b50
commit ba401ccdc5
2 changed files with 68 additions and 63 deletions

View File

@ -1,13 +1,13 @@
mod cloudflare; mod cloudflare;
mod config; mod config;
mod logging;
mod public_ip; mod public_ip;
pub mod utils; pub mod utils;
mod logging;
pub use cloudflare::CloudflareClient; pub use cloudflare::CloudflareClient;
pub use config::{get_config_path, read_config, Config}; pub use config::{get_config_path, read_config, Config};
pub use public_ip::get_current_public_ipv4;
pub use logging::init_logger; pub use logging::init_logger;
pub use public_ip::get_current_public_ipv4;
pub const PROGRAM_NAME: &'static str = "dynip-cloudflare"; pub const PROGRAM_NAME: &'static str = "dynip-cloudflare";
pub const MAX_ERORS_IN_ROW_DEFAULT: usize = 10; pub const MAX_ERORS_IN_ROW_DEFAULT: usize = 10;

View File

@ -5,7 +5,7 @@ use std::sync::{
use futures::stream::StreamExt; use futures::stream::StreamExt;
use log::{debug, error, info, log_enabled, Level}; use log::{debug, error, info, log_enabled, Level};
use netlink_packet_core::NetlinkPayload; use netlink_packet_core::{NetlinkMessage, NetlinkPayload};
use netlink_packet_route::RouteNetlinkMessage as RtnlMessage; use netlink_packet_route::RouteNetlinkMessage as RtnlMessage;
use netlink_sys::{AsyncSocket, SocketAddr}; use netlink_sys::{AsyncSocket, SocketAddr};
use rtnetlink::new_connection; use rtnetlink::new_connection;
@ -81,70 +81,75 @@ async fn main() {
while !should_exit.load(Ordering::SeqCst) { while !should_exit.load(Ordering::SeqCst) {
tokio::select! { tokio::select! {
_ = notify.notified() => { _ = notify.notified() => break,
break;
}
message = messages.next() => { message = messages.next() => {
if let Some((message, _)) = message { if let Some((message, _)) = message {
match message.payload { handle_message(message, &mut cloudflare, &mut errs_counter, errs_max).await;
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) = cloudflare.check().await {
errs_counter += 1;
error!(
"Failed to check cloudflare ({}/{}): {:?}",
errs_counter, errs_max, &e
);
if errs_counter >= errs_max {
return;
}
}
}
}
NetlinkPayload::InnerMessage(RtnlMessage::DelAddress(msg)) => {
if log_enabled!(Level::Debug) {
debug!("Deleted IPv4 address message: {:?}", msg);
} else {
info!("Deleted IPv4 address");
}
}
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) = cloudflare.check().await {
errs_counter += 1;
error!(
"Failed to check cloudflare ({}/{}): {:?}",
errs_counter, errs_max, &e
);
if errs_counter >= errs_max {
return;
}
}
}
}
NetlinkPayload::InnerMessage(RtnlMessage::DelLink(link)) => {
if log_enabled!(Level::Debug) {
debug!("Deleted link message (interface disconnected): {:?}", link);
} else {
info!("Deleted link (interface disconnected)");
}
}
_ => {
if log_enabled!(Level::Debug) {
debug!("Unhandled message payload: {:?}", message.payload);
}
}
}
} }
} }
} }
} }
} }
async fn handle_message(
message: NetlinkMessage<RtnlMessage>,
cloudflare: &mut CloudflareClient,
errs_counter: &mut usize,
errs_max: usize,
) {
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) = cloudflare.check().await {
*errs_counter += 1;
error!(
"Failed to check cloudflare ({}/{}): {:?}",
errs_counter, errs_max, &e
);
if *errs_counter >= errs_max {
return;
}
}
}
}
NetlinkPayload::InnerMessage(RtnlMessage::DelAddress(msg)) => {
if log_enabled!(Level::Debug) {
debug!("Deleted IPv4 address message: {:?}", msg);
} else {
info!("Deleted IPv4 address");
}
}
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) = cloudflare.check().await {
*errs_counter += 1;
error!(
"Failed to check cloudflare ({}/{}): {:?}",
errs_counter, errs_max, &e
);
if *errs_counter >= errs_max {
return;
}
}
}
}
NetlinkPayload::InnerMessage(RtnlMessage::DelLink(link)) => {
if log_enabled!(Level::Debug) {
debug!("Deleted link message (interface disconnected): {:?}", link);
} else {
info!("Deleted link (interface disconnected)");
}
}
_ => {
if log_enabled!(Level::Debug) {
debug!("Unhandled message payload: {:?}", message.payload);
}
}
}
}