V1.0
This commit is contained in:
parent
0b74fceaf5
commit
186ffb822f
4 changed files with 166 additions and 1 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -14,3 +14,7 @@ Cargo.lock
|
||||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||||
*.pdb
|
*.pdb
|
||||||
|
|
||||||
|
# Config files
|
||||||
|
/config.toml
|
||||||
|
/old_ip.txt
|
||||||
|
/log.txt
|
||||||
|
|
16
Cargo.toml
Normal file
16
Cargo.toml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
[package]
|
||||||
|
name = "check_ip"
|
||||||
|
version = "1.0.0"
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow = "1.0"
|
||||||
|
my_internet_ip = "0.1.1"
|
||||||
|
lettre = "0.9"
|
||||||
|
lettre_email = "0.9"
|
||||||
|
native-tls = "0.2"
|
||||||
|
toml = "0.5"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
log = "0.4"
|
||||||
|
simplelog = "^0.10.0"
|
||||||
|
clokwerk = "0.3"
|
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) <year> <copyright holders>
|
Copyright (c) 2021 Rémi BERTHO <remi.bertho@dalan.fr>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
|
145
src/main.rs
Normal file
145
src/main.rs
Normal file
|
@ -0,0 +1,145 @@
|
||||||
|
extern crate anyhow;
|
||||||
|
extern crate clokwerk;
|
||||||
|
extern crate lettre;
|
||||||
|
extern crate lettre_email;
|
||||||
|
extern crate log;
|
||||||
|
extern crate my_internet_ip;
|
||||||
|
extern crate native_tls;
|
||||||
|
extern crate serde;
|
||||||
|
extern crate simplelog;
|
||||||
|
extern crate toml;
|
||||||
|
|
||||||
|
use self::native_tls::{Protocol, TlsConnector};
|
||||||
|
use anyhow::{bail, Result};
|
||||||
|
use clokwerk::{Scheduler, TimeUnits};
|
||||||
|
use lettre::smtp::authentication::Credentials;
|
||||||
|
use lettre::{ClientSecurity, ClientTlsParameters, SmtpClient, Transport};
|
||||||
|
use lettre_email::EmailBuilder;
|
||||||
|
use log::{error, info};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use simplelog::{ConfigBuilder, LevelFilter, WriteLogger};
|
||||||
|
use std::fs::{self, OpenOptions};
|
||||||
|
use std::net::IpAddr;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::{thread, time::Duration};
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
// Init log
|
||||||
|
let logfile = OpenOptions::new()
|
||||||
|
.append(true)
|
||||||
|
.create(true)
|
||||||
|
.open("log.txt")
|
||||||
|
.unwrap();
|
||||||
|
WriteLogger::init(
|
||||||
|
LevelFilter::Info,
|
||||||
|
ConfigBuilder::new()
|
||||||
|
.set_time_format_str("%d/%m/%Y %H:%M:%S")
|
||||||
|
.set_time_to_local(true)
|
||||||
|
.build(),
|
||||||
|
logfile,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Init scheduler
|
||||||
|
let mut scheduler = Scheduler::new();
|
||||||
|
info!("Init");
|
||||||
|
scheduler.every(1.day()).at("06:00").run(|| {
|
||||||
|
if let Err(e) = check() {
|
||||||
|
error!("Error {}", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
check()?;
|
||||||
|
|
||||||
|
// Run
|
||||||
|
loop {
|
||||||
|
scheduler.run_pending();
|
||||||
|
thread::sleep(Duration::from_secs(60));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check() -> Result<()> {
|
||||||
|
info!("Check");
|
||||||
|
match get() {
|
||||||
|
Ok((old_ip, new_ip)) => {
|
||||||
|
if old_ip != new_ip {
|
||||||
|
let msg = format!("IP changed from {} to {}", old_ip, new_ip);
|
||||||
|
info!("{}", msg);
|
||||||
|
send_mail("New IP".to_owned(), msg)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
let msg = format!("Cannot get IP ({})", err);
|
||||||
|
error!("{}", msg);
|
||||||
|
send_mail("Error on getting IP".to_owned(), msg)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get() -> Result<(IpAddr, IpAddr)> {
|
||||||
|
// Get new IP
|
||||||
|
let new_ip = match my_internet_ip::get() {
|
||||||
|
Ok(ip) => ip,
|
||||||
|
Err(e) => bail!("Could not get IP: {:?}", e),
|
||||||
|
};
|
||||||
|
info!("Current IP is {}", new_ip);
|
||||||
|
|
||||||
|
// Get old IP
|
||||||
|
let path = Path::new("./old_ip.txt");
|
||||||
|
let old_ip = if path.exists() {
|
||||||
|
fs::read_to_string(path)?.parse()?
|
||||||
|
} else {
|
||||||
|
new_ip.clone()
|
||||||
|
};
|
||||||
|
info!("Old IP is {}", old_ip);
|
||||||
|
|
||||||
|
// Write new IP
|
||||||
|
fs::write(path, format!("{}", new_ip))?;
|
||||||
|
|
||||||
|
Ok((old_ip, new_ip))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send_mail(subject: String, body: String) -> Result<()> {
|
||||||
|
info!("Send mail {}", subject);
|
||||||
|
|
||||||
|
let config: Config = toml::from_str(&fs::read_to_string("config.toml")?)?;
|
||||||
|
|
||||||
|
let email = EmailBuilder::new()
|
||||||
|
.to(config.mail.from)
|
||||||
|
.from(config.mail.to)
|
||||||
|
.subject(subject)
|
||||||
|
.text(body)
|
||||||
|
.build()?;
|
||||||
|
|
||||||
|
// Create transport
|
||||||
|
let creds = Credentials::new(config.mail.username, config.mail.password);
|
||||||
|
let mut tls_builder = TlsConnector::builder();
|
||||||
|
tls_builder.min_protocol_version(Some(Protocol::Sslv3));
|
||||||
|
let tls_parameters =
|
||||||
|
ClientTlsParameters::new(config.mail.server.clone(), tls_builder.build().unwrap());
|
||||||
|
let mut mailer = SmtpClient::new(
|
||||||
|
(config.mail.server, config.mail.port),
|
||||||
|
ClientSecurity::Required(tls_parameters),
|
||||||
|
)?
|
||||||
|
.credentials(creds)
|
||||||
|
.transport();
|
||||||
|
|
||||||
|
mailer.send(email.into())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct Config {
|
||||||
|
mail: MailConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct MailConfig {
|
||||||
|
server: String,
|
||||||
|
port: u16,
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
from: (String, String),
|
||||||
|
to: String,
|
||||||
|
}
|
Loading…
Reference in a new issue