This commit is contained in:
Bianca Fürstenau 2025-02-11 14:57:40 +01:00
parent bd40e5c8d8
commit 51828ef351
3 changed files with 48 additions and 27 deletions

View file

@ -1,7 +1,7 @@
use rand::prelude::*; use rand::prelude::*;
use tokio::sync::Mutex;
use rusqlite::Connection;
use ring_compat::signature::ed25519::SigningKey; use ring_compat::signature::ed25519::SigningKey;
use rusqlite::Connection;
use tokio::sync::Mutex;
pub struct AppState { pub struct AppState {
pub db: Connection, pub db: Connection,
@ -30,6 +30,11 @@ impl AppState {
let last_sync = i64::MIN; let last_sync = i64::MIN;
let id = rng.gen(); let id = rng.gen();
let key = SigningKey::generate(&mut rng); let key = SigningKey::generate(&mut rng);
AppState { db, last_sync, id, key } AppState {
db,
last_sync,
id,
key,
}
} }
} }

View file

@ -19,55 +19,60 @@ impl easy::Handler for Collector {
fn read(&mut self, data: &mut [u8]) -> Result<usize, easy::ReadError> { fn read(&mut self, data: &mut [u8]) -> Result<usize, easy::ReadError> {
let p = self.2; let p = self.2;
let src: &[u8] = self.1.as_ref(); let src: &[u8] = self.1.as_ref();
let n = usize::min(src.len()-p, data.len()); let n = usize::min(src.len() - p, data.len());
data[..n].copy_from_slice(&src[p..(p+n)]); data[..n].copy_from_slice(&src[p..(p + n)]);
self.2 = n+p; self.2 = n + p;
Ok(n) Ok(n)
} }
fn seek(&mut self, whence: std::io::SeekFrom) -> easy::SeekResult { fn seek(&mut self, whence: std::io::SeekFrom) -> easy::SeekResult {
use std::io::SeekFrom::{Start, End, Current}; use std::io::SeekFrom::{Current, End, Start};
match whence { match whence {
Start(p) => { Start(p) => {
self.2 = p as usize; self.2 = p as usize;
easy::SeekResult::Ok easy::SeekResult::Ok
}, }
End(d) => { End(d) => {
let p = self.1.len() as i64; let p = self.1.len() as i64;
if p+d < 0 { if p + d < 0 {
easy::SeekResult::Fail easy::SeekResult::Fail
} else { } else {
self.2 = (p+d) as usize; self.2 = (p + d) as usize;
easy::SeekResult::Ok easy::SeekResult::Ok
} }
}, }
Current(d) => { Current(d) => {
let p = self.2 as i64; let p = self.2 as i64;
if p+d < 0 { if p + d < 0 {
easy::SeekResult::Fail easy::SeekResult::Fail
} else { } else {
self.2 = (p+d) as usize; self.2 = (p + d) as usize;
easy::SeekResult::Ok easy::SeekResult::Ok
} }
}, }
} }
} }
} }
pub fn data_client(file: &str) -> Result<Easy2<Collector>, ()> { pub fn data_client(file: &str) -> Result<Easy2<Collector>, ()> {
let mut client = Easy2::new(Collector(Vec::new(), Vec::new(), 0)); let mut client = Easy2::new(Collector(Vec::new(), Vec::new(), 0));
let url = format!("https://cloud.seebruecke.org/public.php/webdav/data/{}", file); let url = format!(
client.url(&url) "https://cloud.seebruecke.org/public.php/webdav/data/{}",
.map_err(|_| ())?; file
);
client.url(&url).map_err(|_| ())?;
client.username(include_str!("cloud_user.txt")).map_err(|_| ())?; client.username(include_str!("cloud_user.txt")).map_err(|_| ())?;
client.http_auth(easy::Auth::new().auto(true)).map_err(|_| ())?; client.http_auth(easy::Auth::new().auto(true))
.map_err(|_| ())?;
client.ssl_cainfo_blob(include_bytes!("isrg-root-x1.pem")) client.ssl_cainfo_blob(include_bytes!("isrg-root-x1.pem"))
.map_err(|_| ())?; .map_err(|_| ())?;
Ok(client) Ok(client)
} }
#[tauri::command] #[tauri::command]
pub async fn pull_data(state: State<'_, Mutex<AppState>>) -> Result<String, ()> { pub async fn pull_data(
state: State<'_, Mutex<AppState>>,
) -> Result<String, ()> {
let mut client = data_client("")?; let mut client = data_client("")?;
client.custom_request("PROPFIND").map_err(|_| ())?; client.custom_request("PROPFIND").map_err(|_| ())?;
client.perform().map_err(|_| ())?; client.perform().map_err(|_| ())?;
@ -76,7 +81,9 @@ pub async fn pull_data(state: State<'_, Mutex<AppState>>) -> Result<String, ()>
} }
#[tauri::command] #[tauri::command]
pub async fn push_data(state: State<'_, Mutex<AppState>>) -> Result<String, ()> { pub async fn push_data(
state: State<'_, Mutex<AppState>>,
) -> Result<String, ()> {
let state = state.lock().await; let state = state.lock().await;
let key_file = format!("{:016X}.key", state.id); let key_file = format!("{:016X}.key", state.id);
let mut client = data_client(&key_file)?; let mut client = data_client(&key_file)?;

View file

@ -4,8 +4,8 @@ use rusqlite::{types::ToSqlOutput, Connection, ToSql};
use tauri::{Manager, State}; use tauri::{Manager, State};
use tokio::sync::Mutex; use tokio::sync::Mutex;
mod data_door;
mod app_state; mod app_state;
mod data_door;
use app_state::AppState; use app_state::AppState;
@ -63,22 +63,31 @@ async fn swap(
rng.gen::<i64>(), rng.gen::<i64>(),
store, store,
acc, acc,
state.id, i64::from_ne_bytes(state.id.to_ne_bytes()),
Utc::now().timestamp(), Utc::now().timestamp(),
false, false,
), ),
) )
.map_err(|_| ())?; .map_err(|e| println!("{:?}", e))?;
Ok(()) Ok(())
} }
#[tauri::command] #[tauri::command]
async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> { async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
let state = state.lock().await; let state = state.lock().await;
let mut stmt = state.db.prepare("SELECT COUNT(*) FROM swap").unwrap(); let mut stmt =
let mut rows = stmt.query([]).unwrap(); state.db.prepare("SELECT COUNT(*) FROM swap")
let row = rows.next().unwrap().unwrap(); .map_err(|e| println!("{:?}", e))?;
let cnt: u64 = row.get_unwrap(0); let mut rows = stmt.query([]).map_err(|e| println!("{:?}", e))?;
let row = rows.next().map_err(|e| println!("{:?}", e))?;
let row = match row {
Some(r) => Ok(r),
None => {
println!("No rows");
Err(())
}
}?;
let cnt: u64 = row.get(0).map_err(|e| println!("{:?}", e))?;
Ok(cnt.to_string()) Ok(cnt.to_string())
} }