mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-08-26 06:21:58 +00:00
separate children proxy components
This commit is contained in:
parent
02f6419b92
commit
b15e4a5fc1
@ -60,10 +60,10 @@ impl Proxy for adw::PreferencesDialog {
|
|||||||
d.connect_closed({
|
d.connect_closed({
|
||||||
let profile = profile.clone();
|
let profile = profile.clone();
|
||||||
move |_| {
|
move |_| {
|
||||||
profile.proxy.clear();
|
profile.proxy.rule.clear();
|
||||||
for rule in rules.take() {
|
for rule in rules.take() {
|
||||||
if rule.validate() {
|
if rule.validate() {
|
||||||
profile.proxy.add_rule(
|
profile.proxy.rule.add(
|
||||||
rule.id,
|
rule.id,
|
||||||
rule.is_enabled(),
|
rule.is_enabled(),
|
||||||
rule.priority(),
|
rule.priority(),
|
||||||
|
@ -17,7 +17,7 @@ pub struct Rules {
|
|||||||
|
|
||||||
impl Rules {
|
impl Rules {
|
||||||
pub fn build(profile: &Rc<Profile>) -> Self {
|
pub fn build(profile: &Rc<Profile>) -> Self {
|
||||||
let config = profile.proxy.rules();
|
let config = profile.proxy.rule.all();
|
||||||
|
|
||||||
let rules: Rc<RefCell<HashMap<GString, Rule>>> =
|
let rules: Rc<RefCell<HashMap<GString, Rule>>> =
|
||||||
Rc::new(RefCell::new(HashMap::with_capacity(config.len())));
|
Rc::new(RefCell::new(HashMap::with_capacity(config.len())));
|
||||||
|
@ -1,122 +1,40 @@
|
|||||||
mod database;
|
|
||||||
mod ignore;
|
mod ignore;
|
||||||
mod rule;
|
mod rule;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use database::Database;
|
use gtk::gio::{ProxyResolver, SimpleProxyResolver};
|
||||||
use gtk::{
|
|
||||||
gio::{ProxyResolver, SimpleProxyResolver},
|
|
||||||
glib::DateTime,
|
|
||||||
};
|
|
||||||
use ignore::Ignore;
|
use ignore::Ignore;
|
||||||
use r2d2::Pool;
|
use r2d2::Pool;
|
||||||
use r2d2_sqlite::SqliteConnectionManager;
|
use r2d2_sqlite::SqliteConnectionManager;
|
||||||
use rule::Rule;
|
use rule::Rule;
|
||||||
use std::cell::RefCell;
|
|
||||||
|
|
||||||
pub struct Proxy {
|
pub struct Proxy {
|
||||||
database: Database,
|
pub ignore: Ignore,
|
||||||
ignore: RefCell<Vec<Ignore>>,
|
pub rule: Rule,
|
||||||
rule: RefCell<Vec<Rule>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Proxy {
|
impl Proxy {
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
pub fn init(database_pool: &Pool<SqliteConnectionManager>, profile_id: i64) -> Result<Self> {
|
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 {
|
Ok(Self {
|
||||||
database,
|
ignore: Ignore::init(database_pool, profile_id)?,
|
||||||
ignore,
|
rule: Rule::init(database_pool, profile_id)?,
|
||||||
rule,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actions
|
// 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<()> {
|
pub fn save(&self) -> Result<()> {
|
||||||
let rules = self.rule.take();
|
self.rule.save()?;
|
||||||
let mut keep_id = Vec::with_capacity(rules.len());
|
//self.ignore.save()?;
|
||||||
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
pub fn rules(&self) -> Vec<Rule> {
|
|
||||||
self.rule.borrow().iter().cloned().collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn matches(&self, request: &str) -> Option<ProxyResolver> {
|
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(
|
if gtk::glib::Regex::match_simple(
|
||||||
&rule.request,
|
&rule.request,
|
||||||
request,
|
request,
|
||||||
@ -126,15 +44,9 @@ impl Proxy {
|
|||||||
return Some(SimpleProxyResolver::new(
|
return Some(SimpleProxyResolver::new(
|
||||||
Some(&rule.url),
|
Some(&rule.url),
|
||||||
self.ignore
|
self.ignore
|
||||||
.borrow()
|
.enabled()
|
||||||
.iter()
|
.into_iter()
|
||||||
.filter_map(|i| {
|
.map(|i| i.host)
|
||||||
if i.is_enabled {
|
|
||||||
Some(i.host.clone())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect::<Vec<String>>(),
|
.collect::<Vec<String>>(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -147,11 +59,11 @@ impl Proxy {
|
|||||||
|
|
||||||
pub fn migrate(tx: &sqlite::Transaction) -> Result<()> {
|
pub fn migrate(tx: &sqlite::Transaction) -> Result<()> {
|
||||||
// Migrate self components
|
// Migrate self components
|
||||||
database::init(tx)?;
|
// nothing yet..
|
||||||
|
|
||||||
// Delegate migration to childs
|
// Delegate migration to childs
|
||||||
// nothing yet...
|
ignore::migrate(tx)?;
|
||||||
|
rule::migrate(tx)?;
|
||||||
|
|
||||||
// Success
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,68 @@
|
|||||||
|
mod database;
|
||||||
|
mod memory;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use database::Database;
|
||||||
|
use memory::Memory;
|
||||||
|
use r2d2::Pool;
|
||||||
|
use r2d2_sqlite::SqliteConnectionManager;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
pub struct Ignore {
|
pub struct Ignore {
|
||||||
pub is_enabled: bool,
|
database: Database,
|
||||||
pub host: String,
|
memory: RefCell<Vec<Memory>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ignore {
|
||||||
|
// Constructors
|
||||||
|
|
||||||
|
pub fn init(database_pool: &Pool<SqliteConnectionManager>, profile_id: i64) -> Result<Self> {
|
||||||
|
let database = Database::init(database_pool, profile_id);
|
||||||
|
|
||||||
|
let rows = database.rows()?;
|
||||||
|
let memory = RefCell::new(Vec::with_capacity(rows.len()));
|
||||||
|
|
||||||
|
{
|
||||||
|
// build in-memory index...
|
||||||
|
let mut m = memory.borrow_mut();
|
||||||
|
for i in rows {
|
||||||
|
m.push(Memory {
|
||||||
|
is_enabled: i.is_enabled,
|
||||||
|
host: i.host,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Self { database, memory })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
|
||||||
|
pub fn all(&self) -> Vec<Memory> {
|
||||||
|
self.memory.borrow().iter().cloned().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn enabled(&self) -> Vec<Memory> {
|
||||||
|
self.memory
|
||||||
|
.borrow()
|
||||||
|
.iter()
|
||||||
|
.filter(|r| r.is_enabled)
|
||||||
|
.cloned()
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tools
|
||||||
|
|
||||||
|
pub fn migrate(tx: &sqlite::Transaction) -> Result<()> {
|
||||||
|
// Migrate self components
|
||||||
|
database::init(tx)?;
|
||||||
|
|
||||||
|
// Delegate migration to childs
|
||||||
|
// nothing yet...
|
||||||
|
|
||||||
|
// Success
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
79
src/profile/proxy/ignore/database.rs
Normal file
79
src/profile/proxy/ignore/database.rs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
mod row;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use r2d2::Pool;
|
||||||
|
use r2d2_sqlite::SqliteConnectionManager;
|
||||||
|
use row::Row;
|
||||||
|
use sqlite::Transaction;
|
||||||
|
|
||||||
|
pub struct Database {
|
||||||
|
pool: Pool<SqliteConnectionManager>,
|
||||||
|
profile_id: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Database {
|
||||||
|
// Constructors
|
||||||
|
|
||||||
|
pub fn init(pool: &Pool<SqliteConnectionManager>, profile_id: i64) -> Self {
|
||||||
|
Self {
|
||||||
|
pool: pool.clone(),
|
||||||
|
profile_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
|
||||||
|
pub fn rows(&self) -> Result<Vec<Row>> {
|
||||||
|
rows(&self.pool.get()?.unchecked_transaction()?, self.profile_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Low-level DB API
|
||||||
|
|
||||||
|
pub fn init(tx: &Transaction) -> Result<usize> {
|
||||||
|
Ok(tx.execute(
|
||||||
|
"CREATE TABLE IF NOT EXISTS `profile_proxy_ignore`
|
||||||
|
(
|
||||||
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||||
|
`profile_id` INTEGER NOT NULL,
|
||||||
|
`time` INTEGER NOT NULL,
|
||||||
|
`is_enabled` INTEGER NOT NULL,
|
||||||
|
`host` VARCHAR(255) NOT NULL,
|
||||||
|
|
||||||
|
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
|
||||||
|
)",
|
||||||
|
[],
|
||||||
|
)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rows(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>> {
|
||||||
|
let mut stmt = tx.prepare(
|
||||||
|
"SELECT `id`,
|
||||||
|
`profile_id`,
|
||||||
|
`time`,
|
||||||
|
`host`,
|
||||||
|
`is_enabled`
|
||||||
|
|
||||||
|
FROM `profile_proxy_ignore`
|
||||||
|
WHERE `profile_id` = ?",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let result = stmt.query_map([profile_id], |row| {
|
||||||
|
Ok(Row {
|
||||||
|
//id: row.get(0)?,
|
||||||
|
//profile_id: row.get(1)?,
|
||||||
|
//time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
||||||
|
host: row.get(3)?,
|
||||||
|
is_enabled: row.get(4)?,
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let mut rows = Vec::new();
|
||||||
|
|
||||||
|
for r in result {
|
||||||
|
let row = r?;
|
||||||
|
rows.push(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(rows)
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
pub struct Ignore {
|
pub struct Row {
|
||||||
pub host: String,
|
pub host: String,
|
||||||
pub is_enabled: bool,
|
pub is_enabled: bool,
|
||||||
}
|
}
|
5
src/profile/proxy/ignore/memory.rs
Normal file
5
src/profile/proxy/ignore/memory.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Memory {
|
||||||
|
pub host: String,
|
||||||
|
pub is_enabled: bool,
|
||||||
|
}
|
@ -1,9 +1,114 @@
|
|||||||
#[derive(Clone)]
|
mod database;
|
||||||
|
mod memory;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use database::Database;
|
||||||
|
use gtk::glib::DateTime;
|
||||||
|
use memory::Memory;
|
||||||
|
use r2d2::Pool;
|
||||||
|
use r2d2_sqlite::SqliteConnectionManager;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
pub struct Rule {
|
pub struct Rule {
|
||||||
pub id: Option<i64>,
|
database: Database,
|
||||||
pub is_enabled: bool,
|
memory: RefCell<Vec<Memory>>,
|
||||||
pub priority: i32,
|
}
|
||||||
pub request: String,
|
|
||||||
pub time: gtk::glib::DateTime,
|
impl Rule {
|
||||||
pub url: String,
|
// Constructors
|
||||||
|
|
||||||
|
pub fn init(database_pool: &Pool<SqliteConnectionManager>, profile_id: i64) -> Result<Self> {
|
||||||
|
let database = Database::init(database_pool, profile_id);
|
||||||
|
|
||||||
|
let rows = database.rows()?;
|
||||||
|
let memory = RefCell::new(Vec::with_capacity(rows.len()));
|
||||||
|
|
||||||
|
{
|
||||||
|
// build in-memory index...
|
||||||
|
let mut m = memory.borrow_mut();
|
||||||
|
for row in rows {
|
||||||
|
m.push(Memory {
|
||||||
|
id: Some(row.id),
|
||||||
|
time: row.time,
|
||||||
|
is_enabled: row.is_enabled,
|
||||||
|
priority: row.priority,
|
||||||
|
request: row.request,
|
||||||
|
url: row.url,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Self { database, memory })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setters
|
||||||
|
|
||||||
|
pub fn add(
|
||||||
|
&self,
|
||||||
|
id: Option<i64>,
|
||||||
|
is_enabled: bool,
|
||||||
|
priority: i32,
|
||||||
|
request: String,
|
||||||
|
url: String,
|
||||||
|
time: DateTime,
|
||||||
|
) {
|
||||||
|
let mut rules = self.memory.borrow_mut();
|
||||||
|
rules.push(Memory {
|
||||||
|
id,
|
||||||
|
time,
|
||||||
|
is_enabled,
|
||||||
|
priority,
|
||||||
|
request,
|
||||||
|
url,
|
||||||
|
}) // @TODO validate?
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&self) {
|
||||||
|
self.memory.borrow_mut().clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(&self) -> Result<()> {
|
||||||
|
let rules = self.memory.take();
|
||||||
|
let mut keep_id = Vec::with_capacity(rules.len());
|
||||||
|
for rule in rules {
|
||||||
|
keep_id.push(self.database.persist(
|
||||||
|
rule.id,
|
||||||
|
rule.time,
|
||||||
|
rule.is_enabled,
|
||||||
|
rule.priority,
|
||||||
|
rule.request,
|
||||||
|
rule.url,
|
||||||
|
)?);
|
||||||
|
}
|
||||||
|
self.database.clean(keep_id)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
|
||||||
|
pub fn all(&self) -> Vec<Memory> {
|
||||||
|
self.memory.borrow().iter().cloned().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn enabled(&self) -> Vec<Memory> {
|
||||||
|
self.memory
|
||||||
|
.borrow()
|
||||||
|
.iter()
|
||||||
|
.filter(|r| r.is_enabled)
|
||||||
|
.cloned()
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tools
|
||||||
|
|
||||||
|
pub fn migrate(tx: &sqlite::Transaction) -> Result<()> {
|
||||||
|
// Migrate self components
|
||||||
|
database::init(tx)?;
|
||||||
|
|
||||||
|
// Delegate migration to childs
|
||||||
|
// nothing yet...
|
||||||
|
|
||||||
|
// Success
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
mod ignore;
|
mod row;
|
||||||
mod rule;
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use gtk::glib::DateTime;
|
use gtk::glib::DateTime;
|
||||||
use ignore::Ignore;
|
|
||||||
use r2d2::Pool;
|
use r2d2::Pool;
|
||||||
use r2d2_sqlite::SqliteConnectionManager;
|
use r2d2_sqlite::SqliteConnectionManager;
|
||||||
use rule::Rule;
|
use row::Row;
|
||||||
use sqlite::Transaction;
|
use sqlite::Transaction;
|
||||||
|
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
@ -26,25 +24,21 @@ impl Database {
|
|||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
pub fn rules(&self) -> Result<Vec<Rule>> {
|
pub fn rows(&self) -> Result<Vec<Row>> {
|
||||||
rules(&self.pool.get()?.unchecked_transaction()?, self.profile_id)
|
rows(&self.pool.get()?.unchecked_transaction()?, self.profile_id)
|
||||||
}
|
|
||||||
|
|
||||||
pub fn ignores(&self) -> Result<Vec<Ignore>> {
|
|
||||||
ignores(&self.pool.get()?.unchecked_transaction()?, self.profile_id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
pub fn clean_rules(&self, keep_id: Vec<i64>) -> Result<()> {
|
pub fn clean(&self, keep_id: Vec<i64>) -> Result<()> {
|
||||||
let mut c = self.pool.get()?;
|
let mut c = self.pool.get()?;
|
||||||
let tx = c.transaction()?;
|
let tx = c.transaction()?;
|
||||||
clean_rules(&tx, keep_id)?;
|
clean(&tx, keep_id)?;
|
||||||
tx.commit()?;
|
tx.commit()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn persist_rule(
|
pub fn persist(
|
||||||
&self,
|
&self,
|
||||||
id: Option<i64>,
|
id: Option<i64>,
|
||||||
time: DateTime,
|
time: DateTime,
|
||||||
@ -57,10 +51,10 @@ impl Database {
|
|||||||
let tx = c.transaction()?;
|
let tx = c.transaction()?;
|
||||||
let id = match id {
|
let id = match id {
|
||||||
Some(id) => {
|
Some(id) => {
|
||||||
update_rule(&tx, id, time, is_enabled, priority, request, url)?;
|
update(&tx, id, time, is_enabled, priority, request, url)?;
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
None => insert_rule(
|
None => insert(
|
||||||
&tx,
|
&tx,
|
||||||
self.profile_id,
|
self.profile_id,
|
||||||
time,
|
time,
|
||||||
@ -78,23 +72,7 @@ impl Database {
|
|||||||
// Low-level DB API
|
// Low-level DB API
|
||||||
|
|
||||||
pub fn init(tx: &Transaction) -> Result<usize> {
|
pub fn init(tx: &Transaction) -> Result<usize> {
|
||||||
let mut s = 0;
|
Ok(tx.execute(
|
||||||
|
|
||||||
s += tx.execute(
|
|
||||||
"CREATE TABLE IF NOT EXISTS `profile_proxy_ignore`
|
|
||||||
(
|
|
||||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
||||||
`profile_id` INTEGER NOT NULL,
|
|
||||||
`time` INTEGER NOT NULL,
|
|
||||||
`is_enabled` INTEGER NOT NULL,
|
|
||||||
`host` VARCHAR(255) NOT NULL,
|
|
||||||
|
|
||||||
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
|
|
||||||
)",
|
|
||||||
[],
|
|
||||||
)?;
|
|
||||||
|
|
||||||
s += tx.execute(
|
|
||||||
"CREATE TABLE IF NOT EXISTS `profile_proxy_rule`
|
"CREATE TABLE IF NOT EXISTS `profile_proxy_rule`
|
||||||
(
|
(
|
||||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||||
@ -108,12 +86,10 @@ pub fn init(tx: &Transaction) -> Result<usize> {
|
|||||||
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
|
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
|
||||||
)",
|
)",
|
||||||
[],
|
[],
|
||||||
)?;
|
)?)
|
||||||
|
|
||||||
Ok(s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clean_rules(tx: &Transaction, keep_id: Vec<i64>) -> Result<usize> {
|
fn clean(tx: &Transaction, keep_id: Vec<i64>) -> Result<usize> {
|
||||||
if keep_id.is_empty() {
|
if keep_id.is_empty() {
|
||||||
return Ok(0);
|
return Ok(0);
|
||||||
}
|
}
|
||||||
@ -130,7 +106,7 @@ pub fn clean_rules(tx: &Transaction, keep_id: Vec<i64>) -> Result<usize> {
|
|||||||
)?)
|
)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_rule(
|
fn insert(
|
||||||
tx: &Transaction,
|
tx: &Transaction,
|
||||||
profile_id: i64,
|
profile_id: i64,
|
||||||
time: DateTime,
|
time: DateTime,
|
||||||
@ -160,7 +136,7 @@ pub fn insert_rule(
|
|||||||
Ok(tx.last_insert_rowid())
|
Ok(tx.last_insert_rowid())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_rule(
|
fn update(
|
||||||
tx: &Transaction,
|
tx: &Transaction,
|
||||||
id: i64,
|
id: i64,
|
||||||
time: DateTime,
|
time: DateTime,
|
||||||
@ -182,39 +158,7 @@ pub fn update_rule(
|
|||||||
)?)
|
)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ignores(tx: &Transaction, profile_id: i64) -> Result<Vec<Ignore>> {
|
fn rows(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>> {
|
||||||
let mut stmt = tx.prepare(
|
|
||||||
"SELECT `id`,
|
|
||||||
`profile_id`,
|
|
||||||
`time`,
|
|
||||||
`host`,
|
|
||||||
`is_enabled`
|
|
||||||
|
|
||||||
FROM `profile_proxy_ignore`
|
|
||||||
WHERE `profile_id` = ?",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let result = stmt.query_map([profile_id], |row| {
|
|
||||||
Ok(Ignore {
|
|
||||||
//id: row.get(0)?,
|
|
||||||
//profile_id: row.get(1)?,
|
|
||||||
//time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
|
||||||
host: row.get(3)?,
|
|
||||||
is_enabled: row.get(4)?,
|
|
||||||
})
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let mut records = Vec::new();
|
|
||||||
|
|
||||||
for record in result {
|
|
||||||
let table = record?;
|
|
||||||
records.push(table);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(records)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn rules(tx: &Transaction, profile_id: i64) -> Result<Vec<Rule>> {
|
|
||||||
let mut stmt = tx.prepare(
|
let mut stmt = tx.prepare(
|
||||||
"SELECT `id`,
|
"SELECT `id`,
|
||||||
`profile_id`,
|
`profile_id`,
|
||||||
@ -230,7 +174,7 @@ pub fn rules(tx: &Transaction, profile_id: i64) -> Result<Vec<Rule>> {
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
let result = stmt.query_map([profile_id], |row| {
|
let result = stmt.query_map([profile_id], |row| {
|
||||||
Ok(Rule {
|
Ok(Row {
|
||||||
id: row.get(0)?,
|
id: row.get(0)?,
|
||||||
//profile_id: row.get(1)?,
|
//profile_id: row.get(1)?,
|
||||||
time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
||||||
@ -241,12 +185,11 @@ pub fn rules(tx: &Transaction, profile_id: i64) -> Result<Vec<Rule>> {
|
|||||||
})
|
})
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let mut records = Vec::new();
|
let mut rows = Vec::new();
|
||||||
|
|
||||||
for record in result {
|
for r in result {
|
||||||
let table = record?;
|
rows.push(r?);
|
||||||
records.push(table);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(records)
|
Ok(rows)
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
pub struct Rule {
|
pub struct Row {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub is_enabled: bool,
|
pub is_enabled: bool,
|
||||||
pub priority: i32,
|
pub priority: i32,
|
9
src/profile/proxy/rule/memory.rs
Normal file
9
src/profile/proxy/rule/memory.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Memory {
|
||||||
|
pub id: Option<i64>,
|
||||||
|
pub is_enabled: bool,
|
||||||
|
pub priority: i32,
|
||||||
|
pub request: String,
|
||||||
|
pub time: gtk::glib::DateTime,
|
||||||
|
pub url: String,
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user