mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
separate children proxy components
This commit is contained in:
parent
02f6419b92
commit
b15e4a5fc1
11 changed files with 310 additions and 193 deletions
|
|
@ -1,122 +1,40 @@
|
|||
mod database;
|
||||
mod ignore;
|
||||
mod rule;
|
||||
|
||||
use anyhow::Result;
|
||||
use database::Database;
|
||||
use gtk::{
|
||||
gio::{ProxyResolver, SimpleProxyResolver},
|
||||
glib::DateTime,
|
||||
};
|
||||
use gtk::gio::{ProxyResolver, SimpleProxyResolver};
|
||||
use ignore::Ignore;
|
||||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use rule::Rule;
|
||||
use std::cell::RefCell;
|
||||
|
||||
pub struct Proxy {
|
||||
database: Database,
|
||||
ignore: RefCell<Vec<Ignore>>,
|
||||
rule: RefCell<Vec<Rule>>,
|
||||
pub ignore: Ignore,
|
||||
pub rule: Rule,
|
||||
}
|
||||
|
||||
impl Proxy {
|
||||
// Constructors
|
||||
|
||||
pub fn init(database_pool: &Pool<SqliteConnectionManager>, profile_id: i64) -> Result<Self> {
|
||||
let database = Database::init(database_pool, profile_id);
|
||||
|
||||
let ignores = database.ignores()?;
|
||||
let ignore = RefCell::new(Vec::with_capacity(ignores.len()));
|
||||
|
||||
{
|
||||
// build in-memory index...
|
||||
let mut b = ignore.borrow_mut();
|
||||
for i in ignores {
|
||||
b.push(Ignore {
|
||||
is_enabled: i.is_enabled,
|
||||
host: i.host,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let rules = database.rules()?;
|
||||
let rule = RefCell::new(Vec::with_capacity(rules.len()));
|
||||
|
||||
{
|
||||
// build in-memory index...
|
||||
let mut b = rule.borrow_mut();
|
||||
for r in rules {
|
||||
b.push(Rule {
|
||||
id: Some(r.id),
|
||||
time: r.time,
|
||||
is_enabled: r.is_enabled,
|
||||
priority: r.priority,
|
||||
request: r.request,
|
||||
url: r.url,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
database,
|
||||
ignore,
|
||||
rule,
|
||||
ignore: Ignore::init(database_pool, profile_id)?,
|
||||
rule: Rule::init(database_pool, profile_id)?,
|
||||
})
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
pub fn add_rule(
|
||||
&self,
|
||||
id: Option<i64>,
|
||||
is_enabled: bool,
|
||||
priority: i32,
|
||||
request: String,
|
||||
url: String,
|
||||
time: DateTime,
|
||||
) {
|
||||
let mut rules = self.rule.borrow_mut();
|
||||
rules.push(Rule {
|
||||
id,
|
||||
time,
|
||||
is_enabled,
|
||||
priority,
|
||||
request,
|
||||
url,
|
||||
}) // @TODO validate?
|
||||
}
|
||||
|
||||
pub fn clear(&self) {
|
||||
self.ignore.borrow_mut().clear();
|
||||
self.rule.borrow_mut().clear();
|
||||
}
|
||||
|
||||
pub fn save(&self) -> Result<()> {
|
||||
let rules = self.rule.take();
|
||||
let mut keep_id = Vec::with_capacity(rules.len());
|
||||
for rule in rules {
|
||||
keep_id.push(self.database.persist_rule(
|
||||
rule.id,
|
||||
rule.time,
|
||||
rule.is_enabled,
|
||||
rule.priority,
|
||||
rule.request,
|
||||
rule.url,
|
||||
)?);
|
||||
}
|
||||
self.database.clean_rules(keep_id)?;
|
||||
self.rule.save()?;
|
||||
//self.ignore.save()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
pub fn rules(&self) -> Vec<Rule> {
|
||||
self.rule.borrow().iter().cloned().collect()
|
||||
}
|
||||
|
||||
pub fn matches(&self, request: &str) -> Option<ProxyResolver> {
|
||||
for rule in self.rule.borrow().iter().filter(|r| r.is_enabled) {
|
||||
for rule in self.rule.enabled() {
|
||||
if gtk::glib::Regex::match_simple(
|
||||
&rule.request,
|
||||
request,
|
||||
|
|
@ -126,15 +44,9 @@ impl Proxy {
|
|||
return Some(SimpleProxyResolver::new(
|
||||
Some(&rule.url),
|
||||
self.ignore
|
||||
.borrow()
|
||||
.iter()
|
||||
.filter_map(|i| {
|
||||
if i.is_enabled {
|
||||
Some(i.host.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.enabled()
|
||||
.into_iter()
|
||||
.map(|i| i.host)
|
||||
.collect::<Vec<String>>(),
|
||||
));
|
||||
}
|
||||
|
|
@ -147,11 +59,11 @@ impl Proxy {
|
|||
|
||||
pub fn migrate(tx: &sqlite::Transaction) -> Result<()> {
|
||||
// Migrate self components
|
||||
database::init(tx)?;
|
||||
// nothing yet..
|
||||
|
||||
// Delegate migration to childs
|
||||
// nothing yet...
|
||||
ignore::migrate(tx)?;
|
||||
rule::migrate(tx)?;
|
||||
|
||||
// Success
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue