diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-06-20 08:43:51 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-06-21 08:28:05 -0300 |
| commit | 9b2d8b924c2d2ff641fde0f640fc30be49fa0fa8 (patch) | |
| tree | 69b3769c2399c69f10d2cfdda061b761c955099c | |
| parent | 4beeb63a3f763a36b9e770bf0e7c50b64b9aa487 (diff) | |
| download | mullvadvpn-9b2d8b924c2d2ff641fde0f640fc30be49fa0fa8.tar.xz mullvadvpn-9b2d8b924c2d2ff641fde0f640fc30be49fa0fa8.zip | |
Restore persisted DNS backup on startup
| -rw-r--r-- | talpid-core/src/firewall/linux/dns.rs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/talpid-core/src/firewall/linux/dns.rs b/talpid-core/src/firewall/linux/dns.rs index a16f70e1ef..9353476d56 100644 --- a/talpid-core/src/firewall/linux/dns.rs +++ b/talpid-core/src/firewall/linux/dns.rs @@ -1,11 +1,11 @@ extern crate notify; extern crate resolv_conf; -use std::fs; use std::net::IpAddr; use std::ops::DerefMut; +use std::path::Path; use std::sync::{mpsc, Arc, Mutex, MutexGuard}; -use std::thread; +use std::{fs, io, thread}; use error_chain::ChainedError; @@ -54,6 +54,8 @@ pub struct DnsSettings { impl DnsSettings { pub fn new() -> Result<Self> { + Self::restore_persisted_state()?; + let state = Arc::new(Mutex::new(None)); let watcher = DnsWatcher::start(state.clone())?; @@ -97,6 +99,24 @@ impl DnsSettings { .lock() .expect("a thread panicked while using the DNS configuration state") } + + fn restore_persisted_state() -> Result<()> { + let backup_file = Path::new(RESOLV_CONF_BACKUP_PATH); + + match fs::read(&backup_file) { + Ok(backup) => { + info!("Restoring DNS state from backup"); + fs::write(RESOLV_CONF_PATH, &backup).chain_err(|| ErrorKind::RestoreResolvConf)?; + fs::remove_file(&backup_file).chain_err(|| ErrorKind::RemoveBackup)?; + } + Err(ref error) if error.kind() == io::ErrorKind::NotFound => { + trace!("No DNS state backup to restore") + } + Err(error) => return Err(Error::with_chain(error, ErrorKind::RestoreResolvConf)), + } + + Ok(()) + } } struct State { |
