diff options
| -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 { |
