implement proxy backend features

This commit is contained in:
yggverse 2025-07-24 06:25:01 +03:00
parent 77ee4aa78c
commit 4c305f967f
10 changed files with 281 additions and 1 deletions

View file

@ -39,7 +39,10 @@ GTK 4 / Libadwaita client written in Rust
* [x] Page navigation * [x] Page navigation
* [x] Recently visited * [x] Recently visited
* [x] Recently closed * [x] Recently closed
* [ ] Proxy * [ ] Proxy (by [SimpleProxyResolver](https://docs.gtk.org/gio/class.SimpleProxyResolver.html))
* [x] Multiple regex rules by the priority
* [x] Custom ignored hosts
* [ ] UI controls (frontend)
* [ ] Session * [ ] Session
* [ ] Window * [ ] Window
* [x] Size * [x] Size

View file

@ -98,6 +98,11 @@ impl Gemini {
is_snap_history: bool, is_snap_history: bool,
) { ) {
use ggemini::client::connection::request::{Mode, Request}; use ggemini::client::connection::request::{Mode, Request};
self.client
.socket
.set_proxy_resolver(self.page.profile.proxy.matches(&uri).as_ref());
match uri.scheme().as_str() { match uri.scheme().as_str() {
"gemini" => handle( "gemini" => handle(
self, self,

View file

@ -67,6 +67,7 @@ impl Nex {
} }
let socket = SocketClient::new(); let socket = SocketClient::new();
socket.set_proxy_resolver(self.page.profile.proxy.matches(&uri).as_ref());
socket.set_protocol(SocketProtocol::Tcp); socket.set_protocol(SocketProtocol::Tcp);
socket.set_timeout(30); // @TODO optional socket.set_timeout(30); // @TODO optional

View file

@ -2,6 +2,7 @@ mod bookmark;
mod database; mod database;
mod history; mod history;
mod identity; mod identity;
mod proxy;
mod search; mod search;
mod tofu; mod tofu;
@ -11,6 +12,7 @@ use database::Database;
use gtk::glib::{DateTime, user_config_dir}; use gtk::glib::{DateTime, user_config_dir};
use history::History; use history::History;
use identity::Identity; use identity::Identity;
use proxy::Proxy;
use r2d2::Pool; use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager; use r2d2_sqlite::SqliteConnectionManager;
use search::Search; use search::Search;
@ -30,6 +32,7 @@ pub struct Profile {
pub database: Database, pub database: Database,
pub history: History, pub history: History,
pub identity: Identity, pub identity: Identity,
pub proxy: Proxy,
pub search: Search, pub search: Search,
pub tofu: Tofu, pub tofu: Tofu,
} }
@ -86,6 +89,7 @@ impl Profile {
let bookmark = Bookmark::build(&database_pool, profile_id)?; let bookmark = Bookmark::build(&database_pool, profile_id)?;
let history = History::build(&database_pool, profile_id)?; let history = History::build(&database_pool, profile_id)?;
let identity = Identity::build(&database_pool, profile_id)?; let identity = Identity::build(&database_pool, profile_id)?;
let proxy = Proxy::init(&database_pool, profile_id)?;
let search = Search::build(&database_pool, profile_id)?; let search = Search::build(&database_pool, profile_id)?;
let tofu = Tofu::init(&database_pool, profile_id)?; let tofu = Tofu::init(&database_pool, profile_id)?;
@ -96,6 +100,7 @@ impl Profile {
database, database,
history, history,
identity, identity,
proxy,
search, search,
tofu, tofu,
}) })
@ -118,6 +123,7 @@ pub fn migrate(tx: &Transaction) -> Result<()> {
bookmark::migrate(tx)?; bookmark::migrate(tx)?;
history::migrate(tx)?; history::migrate(tx)?;
identity::migrate(tx)?; identity::migrate(tx)?;
proxy::migrate(tx)?;
search::migrate(tx)?; search::migrate(tx)?;
tofu::migrate(tx)?; tofu::migrate(tx)?;

101
src/profile/proxy.rs Normal file
View file

@ -0,0 +1,101 @@
mod database;
mod ignore;
mod rule;
use anyhow::Result;
use database::Database;
use gtk::{
gio::{ProxyResolver, SimpleProxyResolver},
glib::Uri,
};
use ignore::Ignore;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rule::Rule;
use std::cell::RefCell;
pub struct Proxy {
ignore: RefCell<Vec<Ignore>>,
rule: RefCell<Vec<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 {
is_enabled: r.is_enabled,
regex: r.regex,
url: r.url,
});
}
}
Ok(Self { ignore, rule })
}
// Actions
pub fn matches(&self, request: &Uri) -> Option<ProxyResolver> {
for rule in self.rule.borrow().iter().filter(|r| r.is_enabled) {
if gtk::glib::Regex::match_simple(
&rule.regex,
request.to_str(),
gtk::glib::RegexCompileFlags::DEFAULT,
gtk::glib::RegexMatchFlags::DEFAULT,
) {
return Some(SimpleProxyResolver::new(
Some(&rule.url),
self.ignore
.borrow()
.iter()
.filter_map(|i| {
if i.is_enabled {
Some(i.host.clone())
} else {
None
}
})
.collect::<Vec<String>>(),
));
}
}
None
}
}
// Tools
pub fn migrate(tx: &sqlite::Transaction) -> Result<()> {
// Migrate self components
database::init(tx)?;
// Delegate migration to childs
// nothing yet...
// Success
Ok(())
}

View file

@ -0,0 +1,146 @@
mod ignore;
mod rule;
use anyhow::Result;
use ignore::Ignore;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use rule::Rule;
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 rules(&self) -> Result<Vec<Rule>> {
rules(&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
}
// Low-level DB API
pub fn init(tx: &Transaction) -> Result<usize> {
let mut s = 0;
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`),
UNIQUE (`host`)
)",
[],
)?;
s += tx.execute(
"CREATE TABLE IF NOT EXISTS `profile_proxy_rule`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`profile_id` INTEGER NOT NULL,
`time` INTEGER NOT NULL,
`is_enabled` INTEGER NOT NULL,
`priority` INTEGER NOT NULL,
`regex` VARCHAR(255) NOT NULL,
`url` VARCHAR(255) NOT NULL,
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`),
UNIQUE (`regex`)
)",
[],
)?;
Ok(s)
}
pub fn ignores(tx: &Transaction, profile_id: i64) -> Result<Vec<Ignore>> {
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(
"SELECT `id`,
`profile_id`,
`time`,
`is_enabled`,
`priority`,
`regex`,
`url`
FROM `profile_proxy_rule`
WHERE `profile_id` = ?
ORDER BY `priority` ASC",
)?;
let result = stmt.query_map([profile_id], |row| {
Ok(Rule {
//id: row.get(0)?,
//profile_id: row.get(1)?,
//time: DateTime::from_unix_local(row.get(2)?).unwrap(),
is_enabled: row.get(3)?,
//priority: row.get(4)?,
regex: row.get(5)?,
url: row.get(6)?,
})
})?;
let mut records = Vec::new();
for record in result {
let table = record?;
records.push(table);
}
Ok(records)
}

View file

@ -0,0 +1,4 @@
pub struct Ignore {
pub host: String,
pub is_enabled: bool,
}

View file

@ -0,0 +1,5 @@
pub struct Rule {
pub is_enabled: bool,
pub regex: String,
pub url: String,
}

View file

@ -0,0 +1,4 @@
pub struct Ignore {
pub is_enabled: bool,
pub host: String,
}

View file

@ -0,0 +1,5 @@
pub struct Rule {
pub is_enabled: bool,
pub regex: String,
pub url: String,
}