Skip to content

Commit 77ee4aa

Browse files
author
yggverse
committed
separate host/port columns, increase host len to 1024 bytes
1 parent a7230fd commit 77ee4aa

3 files changed

Lines changed: 51 additions & 39 deletions

File tree

src/profile/tofu.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ mod database;
44
use anyhow::Result;
55
use certificate::Certificate;
66
use database::Database;
7-
use gtk::{gio::TlsCertificate, glib::Uri};
7+
use gtk::{
8+
gio::TlsCertificate,
9+
glib::{GString, Uri},
10+
};
811
use r2d2::Pool;
912
use r2d2_sqlite::SqliteConnectionManager;
1013
use sqlite::Transaction;
@@ -15,7 +18,7 @@ use std::{cell::RefCell, collections::HashMap};
1518
/// https://geminiprotocol.net/docs/protocol-specification.gmi#tls-server-certificate-validation
1619
pub struct Tofu {
1720
database: Database,
18-
memory: RefCell<HashMap<String, Certificate>>,
21+
memory: RefCell<HashMap<(GString, i32), Certificate>>,
1922
}
2023

2124
impl Tofu {
@@ -31,8 +34,11 @@ impl Tofu {
3134
// build in-memory index...
3235
let mut m = memory.borrow_mut();
3336
for r in records {
34-
if m.insert(r.address, Certificate::from_db(Some(r.id), &r.pem, r.time)?)
35-
.is_some()
37+
if m.insert(
38+
(r.host.into(), r.port),
39+
Certificate::from_db(Some(r.id), &r.pem, r.time)?,
40+
)
41+
.is_some()
3642
{
3743
panic!() // expect unique address
3844
}
@@ -50,31 +56,34 @@ impl Tofu {
5056
default_port: i32,
5157
tls_certificate: TlsCertificate,
5258
) -> Result<bool> {
53-
match address(uri, default_port) {
54-
Some(k) => Ok(self
59+
match uri.host() {
60+
Some(host) => Ok(self
5561
.memory
5662
.borrow_mut()
57-
.insert(k, Certificate::from_tls_certificate(tls_certificate)?)
63+
.insert(
64+
(host, port(uri.port(), default_port)),
65+
Certificate::from_tls_certificate(tls_certificate)?,
66+
)
5867
.is_none()),
5968
None => Ok(false),
6069
}
6170
}
6271

6372
pub fn server_certificate(&self, uri: &Uri, default_port: i32) -> Option<TlsCertificate> {
64-
address(uri, default_port).and_then(|k| {
73+
uri.host().and_then(|host| {
6574
self.memory
6675
.borrow()
67-
.get(&k)
76+
.get(&(host, port(uri.port(), default_port)))
6877
.map(|c| c.tls_certificate().clone())
6978
})
7079
}
7180

7281
/// Save in-memory index to the permanent database (on app close)
7382
pub fn save(&self) -> Result<()> {
74-
for (address, certificate) in self.memory.borrow_mut().drain() {
83+
for ((host, port), certificate) in self.memory.borrow_mut().drain() {
7584
if certificate.id().is_none() {
7685
self.database
77-
.add(address, certificate.time(), &certificate.pem())?;
86+
.add(host.into(), port, certificate.time(), &certificate.pem())?;
7887
}
7988
}
8089
Ok(())
@@ -94,17 +103,10 @@ pub fn migrate(tx: &Transaction) -> Result<()> {
94103
Ok(())
95104
}
96105

97-
fn address(uri: &Uri, default_port: i32) -> Option<String> {
98-
uri.host().map(|host| {
99-
let port = uri.port();
100-
format!(
101-
"{}:{}",
102-
host,
103-
if port.is_positive() {
104-
port
105-
} else {
106-
default_port
107-
}
108-
)
109-
})
106+
fn port(port: i32, default_port: i32) -> i32 {
107+
if port.is_positive() {
108+
port
109+
} else {
110+
default_port
111+
}
110112
}

src/profile/tofu/database.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ impl Database {
3434

3535
/// Create new record in database
3636
/// * return last insert ID on success
37-
pub fn add(&self, address: String, time: &DateTime, pem: &str) -> Result<i64> {
37+
pub fn add(&self, host: String, port: i32, time: &DateTime, pem: &str) -> Result<i64> {
3838
let mut connection = self.pool.get()?;
3939
let tx = connection.transaction()?;
40-
let id = insert(&tx, self.profile_id, address, time, pem)?;
40+
let id = insert(&tx, self.profile_id, host, port, time, pem)?;
4141
tx.commit()?;
4242
Ok(id)
4343
}
@@ -52,11 +52,12 @@ pub fn init(tx: &Transaction) -> Result<usize> {
5252
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
5353
`profile_id` INTEGER NOT NULL,
5454
`time` INTEGER NOT NULL,
55-
`address` VARCHAR(255) NOT NULL,
55+
`port` INTEGER NOT NULL,
56+
`host` VARCHAR(1024) NOT NULL,
5657
`pem` TEXT NOT NULL,
5758
5859
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`),
59-
UNIQUE (`address`)
60+
UNIQUE (`host`, `port`)
6061
)",
6162
[],
6263
)?)
@@ -65,36 +66,44 @@ pub fn init(tx: &Transaction) -> Result<usize> {
6566
pub fn insert(
6667
tx: &Transaction,
6768
profile_id: i64,
68-
address: String,
69+
host: String,
70+
port: i32,
6971
time: &DateTime,
7072
pem: &str,
7173
) -> Result<i64> {
7274
tx.execute(
7375
"INSERT INTO `profile_tofu` (
7476
`profile_id`,
7577
`time`,
76-
`address`,
78+
`host`,
79+
`port`,
7780
`pem`
78-
) VALUES (?, ?, ?, ?) ON CONFLICT (`address`)
79-
DO UPDATE SET `time` = `excluded`.`time`,
80-
`pem` = `excluded`.`pem`",
81-
(profile_id, time.to_unix(), address, pem),
81+
) VALUES (?, ?, ?, ?, ?) ON CONFLICT (`host`, `port`)
82+
DO UPDATE SET `time` = `excluded`.`time`,
83+
`pem` = `excluded`.`pem`",
84+
(profile_id, time.to_unix(), host, port, pem),
8285
)?;
8386
Ok(tx.last_insert_rowid())
8487
}
8588

8689
pub fn select(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>> {
8790
let mut stmt = tx.prepare(
88-
"SELECT `id`, `profile_id`, `address`, `time`, `pem` FROM `profile_tofu` WHERE `profile_id` = ?",
91+
"SELECT `id`,
92+
`profile_id`,
93+
`host`,
94+
`port`,
95+
`time`,
96+
`pem` FROM `profile_tofu` WHERE `profile_id` = ?",
8997
)?;
9098

9199
let result = stmt.query_map([profile_id], |row| {
92100
Ok(Row {
93101
id: row.get(0)?,
94102
//profile_id: row.get(1)?,
95-
address: row.get(2)?,
96-
time: DateTime::from_unix_local(row.get(3)?).unwrap(),
97-
pem: row.get(4)?,
103+
host: row.get(2)?,
104+
port: row.get(3)?,
105+
time: DateTime::from_unix_local(row.get(4)?).unwrap(),
106+
pem: row.get(5)?,
98107
})
99108
})?;
100109

src/profile/tofu/database/row.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub struct Row {
2-
pub address: String,
2+
pub host: String,
33
pub id: i64,
44
pub pem: String,
5+
pub port: i32,
56
pub time: gtk::glib::DateTime,
67
}

0 commit comments

Comments
 (0)