proxy.rs
1 mod ignore; 2 mod misc; 3 mod rule; 4 5 use anyhow::Result; 6 use gtk::gio::{ProxyResolver, SimpleProxyResolver}; 7 use ignore::Ignore; 8 use misc::Misc; 9 use r2d2::Pool; 10 use r2d2_sqlite::SqliteConnectionManager; 11 use rule::Rule; 12 13 pub struct Proxy { 14 pub ignore: Ignore, 15 pub rule: Rule, 16 pub misc: Misc, 17 } 18 19 impl Proxy { 20 // Constructors 21 22 pub fn init(database_pool: &Pool<SqliteConnectionManager>, profile_id: i64) -> Result<Self> { 23 Ok(Self { 24 ignore: Ignore::init(database_pool, profile_id)?, 25 misc: Misc::init(database_pool, profile_id)?, 26 rule: Rule::init(database_pool, profile_id)?, 27 }) 28 } 29 30 // Actions 31 32 pub fn save(&self) -> Result<()> { 33 self.ignore.save()?; 34 self.misc.save()?; 35 self.rule.save()?; 36 Ok(()) 37 } 38 39 // Getters 40 41 pub fn matches(&self, request: &str) -> Option<ProxyResolver> { 42 for rule in self.rule.enabled() { 43 if gtk::glib::Regex::match_simple( 44 &rule.request, 45 request, 46 gtk::glib::RegexCompileFlags::DEFAULT, 47 gtk::glib::RegexMatchFlags::DEFAULT, 48 ) { 49 return Some(SimpleProxyResolver::new( 50 Some(&rule.url), 51 self.ignore 52 .enabled() 53 .into_iter() 54 .map(|i| i.host) 55 .collect::<Vec<String>>(), 56 )); 57 } 58 } 59 None 60 } 61 } 62 63 // Tools 64 65 pub fn migrate(tx: &sqlite::Transaction) -> Result<()> { 66 // Migrate self components 67 // nothing yet.. 68 69 // Delegate migration to childs 70 ignore::migrate(tx)?; 71 misc::migrate(tx)?; 72 rule::migrate(tx)?; 73 74 Ok(()) 75 }