Merge branch 'main' into download-manager

This commit is contained in:
DecDuck
2024-12-09 21:20:44 +11:00
9 changed files with 1104 additions and 537 deletions

View File

@ -4,6 +4,7 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};
use chrono::Utc;
use log::{info, warn};
use openssl::{ec::EcKey, hash::MessageDigest, pkey::PKey, sign::Signer};
use serde::{Deserialize, Serialize};
@ -57,11 +58,7 @@ pub fn generate_authorization_header() -> String {
db.auth.clone().unwrap()
};
let start = SystemTime::now();
let timestamp = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
let nonce = timestamp.as_millis().to_string();
let nonce = Utc::now().timestamp_millis().to_string();
let signature = sign_nonce(certs.private, nonce.clone()).unwrap();
@ -139,8 +136,9 @@ pub fn recieve_handshake(app: AppHandle, path: String) {
app.emit("auth/processing", ()).unwrap();
let handshake_result = recieve_handshake_logic(&app, path);
if handshake_result.is_err() {
app.emit("auth/failed", ()).unwrap();
if let Err(e) = handshake_result {
warn!("error with authentication: {}", e);
app.emit("auth/failed", e.to_string()).unwrap();
return;
}
@ -200,7 +198,6 @@ pub fn retry_connect(state: tauri::State<'_, Mutex<AppState>>) -> Result<(), ()>
pub fn setup() -> Result<(AppStatus, Option<User>), ()> {
let data = DB.borrow_data().unwrap();
// If we have certs, exit for now
if data.auth.is_some() {
let user_result = fetch_user();
if user_result.is_err() {

View File

@ -205,15 +205,15 @@ impl GameDownloadAgent {
let file = File::create(path.clone()).unwrap();
let mut running_offset = 0;
for (i, length) in chunk.lengths.iter().enumerate() {
for (index, length) in chunk.lengths.iter().enumerate() {
contexts.push(DropDownloadContext {
file_name: raw_path.to_string(),
version: chunk.version_name.to_string(),
offset: running_offset,
index: i,
index,
game_id: game_id.to_string(),
path: path.clone(),
checksum: chunk.checksums[i].clone(),
checksum: chunk.checksums[index].clone(),
length: *length,
});
running_offset += *length as u64;

View File

@ -8,14 +8,18 @@ pub enum DownloadThreadControlFlag {
Stop,
Go,
}
/// Go => true
/// Stop => false
impl From<DownloadThreadControlFlag> for bool {
fn from(value: DownloadThreadControlFlag) -> Self {
match value {
DownloadThreadControlFlag::Stop => false,
DownloadThreadControlFlag::Go => true,
DownloadThreadControlFlag::Stop => false,
}
}
}
/// true => Go
/// false => Stop
impl From<bool> for DownloadThreadControlFlag {
fn from(value: bool) -> Self {
match value {

View File

@ -17,10 +17,14 @@ use db::{
use downloads::download_commands::*;
use downloads::download_manager::DownloadManager;
use downloads::download_manager_builder::DownloadManagerBuilder;
use env_logger::Env;
use http::{header::*, response::Builder as ResponseBuilder};
use library::{fetch_game, fetch_game_status, fetch_game_verion_options, fetch_library, Game};
use log::{debug, info};
use log::{debug, info, LevelFilter};
use log4rs::append::console::ConsoleAppender;
use log4rs::append::file::FileAppender;
use log4rs::config::{Appender, Root};
use log4rs::encode::pattern::PatternEncoder;
use log4rs::Config;
use remote::{gen_drop_url, use_remote};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
@ -71,8 +75,28 @@ fn fetch_state(state: tauri::State<'_, Mutex<AppState>>) -> Result<AppState, Str
}
fn setup(handle: AppHandle) -> AppState {
debug!("Starting env");
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let logfile = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new("{t}|{l}|{f} - {m}{n}")))
.build(DATA_ROOT_DIR.lock().unwrap().join("./drop.log"))
.unwrap();
let console = ConsoleAppender::builder()
.encoder(Box::new(PatternEncoder::new("{t}|{l}|{f} - {m}{n}\n")))
.build();
let config = Config::builder()
.appenders(vec![
Appender::builder().build("logfile", Box::new(logfile)),
Appender::builder().build("console", Box::new(console)),
])
.build(
Root::builder()
.appenders(vec!["logfile", "console"])
.build(LevelFilter::Info),
)
.unwrap();
log4rs::init_config(config).unwrap();
let games = HashMap::new();
let download_manager = Arc::new(DownloadManagerBuilder::build(handle));