mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 09:05:27 +00:00
implement proxy settings dialog (rules tab)
This commit is contained in:
parent
b548fb16d3
commit
e548efc93f
12 changed files with 660 additions and 12 deletions
|
|
@ -4,7 +4,10 @@ mod rule;
|
|||
|
||||
use anyhow::Result;
|
||||
use database::Database;
|
||||
use gtk::gio::{ProxyResolver, SimpleProxyResolver};
|
||||
use gtk::{
|
||||
gio::{ProxyResolver, SimpleProxyResolver},
|
||||
glib::DateTime,
|
||||
};
|
||||
use ignore::Ignore;
|
||||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
|
|
@ -12,6 +15,7 @@ use rule::Rule;
|
|||
use std::cell::RefCell;
|
||||
|
||||
pub struct Proxy {
|
||||
database: Database,
|
||||
ignore: RefCell<Vec<Ignore>>,
|
||||
rule: RefCell<Vec<Rule>>,
|
||||
}
|
||||
|
|
@ -44,18 +48,73 @@ impl Proxy {
|
|||
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 { ignore, rule })
|
||||
Ok(Self {
|
||||
database,
|
||||
ignore,
|
||||
rule,
|
||||
})
|
||||
}
|
||||
|
||||
// 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)?;
|
||||
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) {
|
||||
if gtk::glib::Regex::match_simple(
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ mod ignore;
|
|||
mod rule;
|
||||
|
||||
use anyhow::Result;
|
||||
use gtk::glib::DateTime;
|
||||
use ignore::Ignore;
|
||||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
|
|
@ -34,6 +35,44 @@ impl Database {
|
|||
}
|
||||
|
||||
// Setters
|
||||
|
||||
pub fn clean_rules(&self, keep_id: Vec<i64>) -> Result<()> {
|
||||
let mut c = self.pool.get()?;
|
||||
let tx = c.transaction()?;
|
||||
clean_rules(&tx, keep_id)?;
|
||||
tx.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn persist_rule(
|
||||
&self,
|
||||
id: Option<i64>,
|
||||
time: DateTime,
|
||||
is_enabled: bool,
|
||||
priority: i32,
|
||||
request: String,
|
||||
url: String,
|
||||
) -> Result<i64> {
|
||||
let mut c = self.pool.get()?;
|
||||
let tx = c.transaction()?;
|
||||
let id = match id {
|
||||
Some(id) => {
|
||||
update_rule(&tx, id, time, is_enabled, priority, request, url)?;
|
||||
id
|
||||
}
|
||||
None => insert_rule(
|
||||
&tx,
|
||||
self.profile_id,
|
||||
time,
|
||||
is_enabled,
|
||||
priority,
|
||||
request,
|
||||
url,
|
||||
)?,
|
||||
};
|
||||
tx.commit()?;
|
||||
Ok(id)
|
||||
}
|
||||
}
|
||||
|
||||
// Low-level DB API
|
||||
|
|
@ -50,8 +89,7 @@ pub fn init(tx: &Transaction) -> Result<usize> {
|
|||
`is_enabled` INTEGER NOT NULL,
|
||||
`host` VARCHAR(255) NOT NULL,
|
||||
|
||||
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`),
|
||||
UNIQUE (`host`)
|
||||
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
|
|
@ -67,8 +105,7 @@ pub fn init(tx: &Transaction) -> Result<usize> {
|
|||
`request` VARCHAR(1024) NOT NULL,
|
||||
`url` VARCHAR(255) NOT NULL,
|
||||
|
||||
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`),
|
||||
UNIQUE (`request`)
|
||||
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
|
|
@ -76,6 +113,75 @@ pub fn init(tx: &Transaction) -> Result<usize> {
|
|||
Ok(s)
|
||||
}
|
||||
|
||||
pub fn clean_rules(tx: &Transaction, keep_id: Vec<i64>) -> Result<usize> {
|
||||
if keep_id.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
Ok(tx.execute(
|
||||
&format!(
|
||||
"DELETE FROM `profile_proxy_rule` WHERE `id` NOT IN ({})",
|
||||
keep_id
|
||||
.into_iter()
|
||||
.map(|id| id.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(",")
|
||||
),
|
||||
[],
|
||||
)?)
|
||||
}
|
||||
|
||||
pub fn insert_rule(
|
||||
tx: &Transaction,
|
||||
profile_id: i64,
|
||||
time: DateTime,
|
||||
is_enabled: bool,
|
||||
priority: i32,
|
||||
request: String,
|
||||
url: String,
|
||||
) -> Result<i64> {
|
||||
tx.execute(
|
||||
"INSERT INTO `profile_proxy_rule` (
|
||||
`profile_id`,
|
||||
`time`,
|
||||
`is_enabled`,
|
||||
`priority`,
|
||||
`request`,
|
||||
`url`
|
||||
) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
profile_id,
|
||||
time.to_unix(),
|
||||
is_enabled,
|
||||
priority,
|
||||
request,
|
||||
url,
|
||||
),
|
||||
)?;
|
||||
Ok(tx.last_insert_rowid())
|
||||
}
|
||||
|
||||
pub fn update_rule(
|
||||
tx: &Transaction,
|
||||
id: i64,
|
||||
time: DateTime,
|
||||
is_enabled: bool,
|
||||
priority: i32,
|
||||
request: String,
|
||||
url: String,
|
||||
) -> Result<usize> {
|
||||
Ok(tx.execute(
|
||||
"UPDATE `profile_proxy_rule`
|
||||
SET `time` = ?,
|
||||
`is_enabled` = ?,
|
||||
`priority` = ?,
|
||||
`request` = ?,
|
||||
`url` = ?
|
||||
|
||||
WHERE `id` = ?",
|
||||
(time.to_unix(), is_enabled, priority, request, url, id),
|
||||
)?)
|
||||
}
|
||||
|
||||
pub fn ignores(tx: &Transaction, profile_id: i64) -> Result<Vec<Ignore>> {
|
||||
let mut stmt = tx.prepare(
|
||||
"SELECT `id`,
|
||||
|
|
@ -125,11 +231,11 @@ pub fn rules(tx: &Transaction, profile_id: i64) -> Result<Vec<Rule>> {
|
|||
|
||||
let result = stmt.query_map([profile_id], |row| {
|
||||
Ok(Rule {
|
||||
//id: row.get(0)?,
|
||||
id: row.get(0)?,
|
||||
//profile_id: row.get(1)?,
|
||||
//time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
||||
time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
||||
is_enabled: row.get(3)?,
|
||||
//priority: row.get(4)?,
|
||||
priority: row.get(4)?,
|
||||
request: row.get(5)?,
|
||||
url: row.get(6)?,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
pub struct Rule {
|
||||
pub id: i64,
|
||||
pub is_enabled: bool,
|
||||
pub priority: i32,
|
||||
pub request: String,
|
||||
pub time: gtk::glib::DateTime,
|
||||
pub url: String,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
#[derive(Clone)]
|
||||
pub struct Rule {
|
||||
pub id: Option<i64>,
|
||||
pub is_enabled: bool,
|
||||
pub priority: i32,
|
||||
pub request: String,
|
||||
pub time: gtk::glib::DateTime,
|
||||
pub url: String,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue