Compare commits

..

No commits in common. "e91093b8f5221a0bdc65a6a0a961e829f61581a8" and "94050d4af437558a2502d7a70b3c41f28980f1f8" have entirely different histories.

3 changed files with 16 additions and 33 deletions

View file

@ -5,6 +5,7 @@ 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"
@ -14,5 +15,3 @@ 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,4 +1,5 @@
# 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,11 +20,9 @@ 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::{Ipv4Addr, Ipv6Addr}; use std::net::IpAddr;
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
@ -82,28 +80,21 @@ fn main() -> Result<()> {
struct Tester { struct Tester {
config: Config, config: Config,
dns_resolver: Resolver, dns_resolver: Resolver,
async_runtime: Runtime
} }
impl Tester { impl Tester {
fn new() -> Result<Tester> { fn new() -> Result<Tester> {
let mut resolver_opt = ResolverOpts::default();
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::new(ResolverConfig::default(), ResolverOpts::default())?,
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, ipv6)) => { Ok(ipv4) => {
self.test_domains(ipv4, ipv6)?; self.test_domains(ipv4)?;
} }
Err(err) => { Err(err) => {
let msg = format!("Cannot get IP ({})", err); let msg = format!("Cannot get IP ({})", err);
@ -114,16 +105,18 @@ impl Tester {
Ok(()) Ok(())
} }
fn get_ip(&self) -> Result<(Ipv4Addr, Ipv6Addr)> { fn get_ip(&self) -> Result<IpAddr> {
let cons = self.async_runtime.block_on(Consensus::get())?; // Get new IP
let ipv4 = cons.v4().unwrap(); let new_ip = match my_internet_ip::get() {
let ipv6 = cons.v6().unwrap(); Ok(ip) => ip,
info!("Current IPv4 is {} and IPv6 {}", ipv4, ipv6); Err(e) => bail!("Could not get IP: {:?}", e),
};
info!("Current IP is {}", new_ip);
Ok((ipv4, ipv6)) Ok(new_ip)
} }
fn test_domains(&self, ipv4: Ipv4Addr, ipv6: Ipv6Addr) -> Result<()> { fn test_domains(&self, ipv4: IpAddr) -> 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() {
@ -137,16 +130,6 @@ 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(())