Fixed bug
This commit is contained in:
parent
e91093b8f5
commit
aa2ce3e0f4
3 changed files with 53 additions and 34 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -18,3 +18,6 @@ Cargo.lock
|
||||||
/config.toml
|
/config.toml
|
||||||
/old_ip.txt
|
/old_ip.txt
|
||||||
/log.txt
|
/log.txt
|
||||||
|
|
||||||
|
# IDE
|
||||||
|
.idea
|
||||||
|
|
2
TODO.md
2
TODO.md
|
@ -1,4 +1,4 @@
|
||||||
# TODO
|
# TODO
|
||||||
* [ ] Examples de fichiers de config (config + systemd)
|
* [ ] Examples de fichiers de config (config + systemd)
|
||||||
* [ ] N’envoyer qu’un seul mail par test =>_test_domains renvoie Option<String>
|
|
||||||
* [ ] Rustfmt
|
* [ ] Rustfmt
|
||||||
|
* [ ] Fonction check asychrone
|
80
src/main.rs
80
src/main.rs
|
@ -6,9 +6,9 @@ extern crate log;
|
||||||
extern crate native_tls;
|
extern crate native_tls;
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
extern crate simplelog;
|
extern crate simplelog;
|
||||||
|
extern crate tokio;
|
||||||
extern crate toml;
|
extern crate toml;
|
||||||
extern crate trust_dns_resolver;
|
extern crate trust_dns_resolver;
|
||||||
extern crate tokio;
|
|
||||||
|
|
||||||
use self::native_tls::{Protocol, TlsConnector};
|
use self::native_tls::{Protocol, TlsConnector};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
|
@ -17,14 +17,15 @@ use lettre::smtp::authentication::Credentials;
|
||||||
use lettre::{ClientSecurity, ClientTlsParameters, SmtpClient, Transport};
|
use lettre::{ClientSecurity, ClientTlsParameters, SmtpClient, Transport};
|
||||||
use lettre_email::EmailBuilder;
|
use lettre_email::EmailBuilder;
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
|
use query_external_ip::Consensus;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use simplelog::{ConfigBuilder, LevelFilter, WriteLogger};
|
use simplelog::{ConfigBuilder, LevelFilter, WriteLogger};
|
||||||
use std::fs::{self, OpenOptions};
|
use std::fs::{self, OpenOptions};
|
||||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||||
|
use std::string::String;
|
||||||
use std::{thread, time::Duration};
|
use std::{thread, time::Duration};
|
||||||
use trust_dns_resolver::{config::*, Name, Resolver};
|
|
||||||
use query_external_ip::Consensus;
|
|
||||||
use tokio::runtime::{Builder, Runtime};
|
use tokio::runtime::{Builder, Runtime};
|
||||||
|
use trust_dns_resolver::{config::*, Name, Resolver};
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
// Init log
|
// Init log
|
||||||
|
@ -82,7 +83,7 @@ fn main() -> Result<()> {
|
||||||
struct Tester {
|
struct Tester {
|
||||||
config: Config,
|
config: Config,
|
||||||
dns_resolver: Resolver,
|
dns_resolver: Resolver,
|
||||||
async_runtime: Runtime
|
async_runtime: Runtime,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tester {
|
impl Tester {
|
||||||
|
@ -91,11 +92,8 @@ impl Tester {
|
||||||
resolver_opt.ip_strategy = LookupIpStrategy::Ipv4AndIpv6;
|
resolver_opt.ip_strategy = LookupIpStrategy::Ipv4AndIpv6;
|
||||||
Ok(Tester {
|
Ok(Tester {
|
||||||
config: toml::from_str(&fs::read_to_string("config.toml")?)?,
|
config: toml::from_str(&fs::read_to_string("config.toml")?)?,
|
||||||
dns_resolver: Resolver::new(ResolverConfig::default(), resolver_opt)?,
|
dns_resolver: Resolver::from_system_conf()?,
|
||||||
async_runtime: Builder::new_multi_thread()
|
async_runtime: Builder::new_multi_thread().enable_all().build().unwrap(),
|
||||||
.enable_all()
|
|
||||||
.build()
|
|
||||||
.unwrap()
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,7 +101,10 @@ impl Tester {
|
||||||
info!("Check");
|
info!("Check");
|
||||||
match self.get_ip() {
|
match self.get_ip() {
|
||||||
Ok((ipv4, ipv6)) => {
|
Ok((ipv4, ipv6)) => {
|
||||||
self.test_domains(ipv4, ipv6)?;
|
let msg = self.test_domains(ipv4, ipv6)?;
|
||||||
|
if !msg.is_empty() {
|
||||||
|
self.send_mail("IP check error".to_owned(), msg)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let msg = format!("Cannot get IP ({})", err);
|
let msg = format!("Cannot get IP ({})", err);
|
||||||
|
@ -123,33 +124,48 @@ impl Tester {
|
||||||
Ok((ipv4, ipv6))
|
Ok((ipv4, ipv6))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_domains(&self, ipv4: Ipv4Addr, ipv6: Ipv6Addr) -> Result<()> {
|
fn test_domains(&self, ipv4: Ipv4Addr, ipv6: Ipv6Addr) -> Result<String> {
|
||||||
|
macro_rules! msg {
|
||||||
|
($full_msg:expr, $($arg:tt)*) => {{
|
||||||
|
let msg = format!($($arg)*);
|
||||||
|
info!("{}", msg);
|
||||||
|
$full_msg.push_str(&msg);
|
||||||
|
$full_msg.push('\n');
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut full_msg = String::new();
|
||||||
for domain in self.config.server.domains.iter() {
|
for domain in self.config.server.domains.iter() {
|
||||||
let address = self.dns_resolver.lookup_ip(domain.clone())?;
|
if let Ok(dns_ipv4_lookup) = self.dns_resolver.ipv4_lookup(domain.clone()) {
|
||||||
for dns_ip in address.iter() {
|
let dns_ipv4 = dns_ipv4_lookup.iter().next().unwrap();
|
||||||
if dns_ip.is_ipv4() {
|
if *dns_ipv4 != ipv4 {
|
||||||
if dns_ip != ipv4 {
|
msg!(
|
||||||
let msg = format!(
|
full_msg,
|
||||||
"Wrong IPV4 for {} (DNS: {}, current: {})",
|
"Wrong IPV4 for {} (DNS: {}, current: {})",
|
||||||
domain, dns_ip, ipv4
|
domain,
|
||||||
);
|
dns_ipv4,
|
||||||
info!("{}", msg);
|
ipv4
|
||||||
self.send_mail("Wrong IP".to_owned(), msg)?;
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if dns_ip.is_ipv6() {
|
} else {
|
||||||
if dns_ip != ipv6 {
|
msg!(full_msg, "Cannot get IPv4 address for {}", domain);
|
||||||
let msg = format!(
|
}
|
||||||
"Wrong IPV6 for {} (DNS: {}, current: {})",
|
if let Ok(dns_ipv6_lookup) = self.dns_resolver.ipv6_lookup(domain.clone()) {
|
||||||
domain, dns_ip, ipv6
|
let dns_ipv6 = dns_ipv6_lookup.iter().next().unwrap();
|
||||||
);
|
if *dns_ipv6 != ipv6 {
|
||||||
info!("{}", msg);
|
msg!(
|
||||||
self.send_mail("Wrong IP".to_owned(), msg)?;
|
full_msg,
|
||||||
}
|
"Wrong IPV6 for {} (DNS: {}, current: {})",
|
||||||
|
domain,
|
||||||
|
dns_ipv6,
|
||||||
|
ipv6
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
msg!(full_msg, "Cannot get IPv6 address for {}", domain);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(full_msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_mail(&self, subject: String, body: String) -> Result<()> {
|
fn send_mail(&self, subject: String, body: String) -> Result<()> {
|
||||||
|
|
Loading…
Reference in a new issue