Test IPV6

This commit is contained in:
Rémi BERTHO 2021-10-06 21:37:00 +02:00
parent 1d49fab7de
commit e91093b8f5
Signed by: dalan
GPG key ID: EE3B917931C07B64
3 changed files with 30 additions and 15 deletions

View file

@ -5,7 +5,6 @@ edition = "2018"
[dependencies] [dependencies]
anyhow = "1.0" anyhow = "1.0"
my_internet_ip = "0.1.1"
lettre = "0.9" lettre = "0.9"
lettre_email = "0.9" lettre_email = "0.9"
native-tls = "0.2" native-tls = "0.2"
@ -15,3 +14,5 @@ log = "0.4"
simplelog = "^0.10.0" simplelog = "^0.10.0"
clokwerk = "0.3" clokwerk = "0.3"
trust-dns-resolver = {version = "0.20", features = ["serde-config"] } trust-dns-resolver = {version = "0.20", features = ["serde-config"] }
query_external_ip = "0.1"
tokio = "1"

View file

@ -1,5 +1,4 @@
# TODO # TODO
* [ ] Examples de fichiers de config (config + systemd) * [ ] Examples de fichiers de config (config + systemd)
* [ ] Nenvoyer quun seul mail par test =>_test_domains renvoie Option<String> * [ ] Nenvoyer quun seul mail par test =>_test_domains renvoie Option<String>
* [ ] Test de lIPv6 (https://crates.io/crates/query_external_ip)
* [ ] Rustfmt * [ ] Rustfmt

View file

@ -3,12 +3,12 @@ extern crate clokwerk;
extern crate lettre; extern crate lettre;
extern crate lettre_email; extern crate lettre_email;
extern crate log; extern crate log;
extern crate my_internet_ip;
extern crate native_tls; extern crate native_tls;
extern crate serde; extern crate serde;
extern crate simplelog; extern crate simplelog;
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};
@ -20,9 +20,11 @@ use log::{error, info};
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::IpAddr; use std::net::{Ipv4Addr, Ipv6Addr};
use std::{thread, time::Duration}; use std::{thread, time::Duration};
use trust_dns_resolver::{config::*, Name, Resolver}; use trust_dns_resolver::{config::*, Name, Resolver};
use query_external_ip::Consensus;
use tokio::runtime::{Builder, Runtime};
fn main() -> Result<()> { fn main() -> Result<()> {
// Init log // Init log
@ -80,6 +82,7 @@ fn main() -> Result<()> {
struct Tester { struct Tester {
config: Config, config: Config,
dns_resolver: Resolver, dns_resolver: Resolver,
async_runtime: Runtime
} }
impl Tester { impl Tester {
@ -89,14 +92,18 @@ impl Tester {
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::new(ResolverConfig::default(), resolver_opt)?,
async_runtime: Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
}) })
} }
fn check(&self) -> Result<()> { fn check(&self) -> Result<()> {
info!("Check"); info!("Check");
match self.get_ip() { match self.get_ip() {
Ok(ipv4) => { Ok((ipv4, ipv6)) => {
self.test_domains(ipv4)?; self.test_domains(ipv4, ipv6)?;
} }
Err(err) => { Err(err) => {
let msg = format!("Cannot get IP ({})", err); let msg = format!("Cannot get IP ({})", err);
@ -107,18 +114,16 @@ impl Tester {
Ok(()) Ok(())
} }
fn get_ip(&self) -> Result<IpAddr> { fn get_ip(&self) -> Result<(Ipv4Addr, Ipv6Addr)> {
// Get new IP let cons = self.async_runtime.block_on(Consensus::get())?;
let new_ip = match my_internet_ip::get() { let ipv4 = cons.v4().unwrap();
Ok(ip) => ip, let ipv6 = cons.v6().unwrap();
Err(e) => bail!("Could not get IP: {:?}", e), info!("Current IPv4 is {} and IPv6 {}", ipv4, ipv6);
};
info!("Current IP is {}", new_ip);
Ok(new_ip) Ok((ipv4, ipv6))
} }
fn test_domains(&self, ipv4: IpAddr) -> Result<()> { fn test_domains(&self, ipv4: Ipv4Addr, ipv6: Ipv6Addr) -> Result<()> {
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())?; let address = self.dns_resolver.lookup_ip(domain.clone())?;
for dns_ip in address.iter() { for dns_ip in address.iter() {
@ -132,6 +137,16 @@ impl Tester {
self.send_mail("Wrong IP".to_owned(), msg)?; self.send_mail("Wrong IP".to_owned(), msg)?;
} }
} }
if dns_ip.is_ipv6() {
if dns_ip != ipv6 {
let msg = format!(
"Wrong IPV6 for {} (DNS: {}, current: {})",
domain, dns_ip, ipv6
);
info!("{}", msg);
self.send_mail("Wrong IP".to_owned(), msg)?;
}
}
} }
} }
Ok(()) Ok(())