Most of server is finished.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
syntax = "proto3";
|
syntax = "proto3";
|
||||||
package nyash_proto;
|
package nyash_proto;
|
||||||
|
|
||||||
|
// serving key brutforcing for aes xts
|
||||||
service NyashLuks {
|
service NyashLuks {
|
||||||
// Requesting work, gettin in response work data, error, or message that there is no work currently awaylable.
|
// Requesting work, gettin in response work data, error, or message that there is no work currently awaylable.
|
||||||
rpc RequestWork (WorkRequest) returns (WorkReply);
|
rpc RequestWork (WorkRequest) returns (WorkReply);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
|
|
||||||
use std::{ops::Not, path::Path, u64, u128};
|
use std::{path::Path, u64};
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
|||||||
@@ -4,12 +4,8 @@ use redb::{
|
|||||||
Database, ReadableDatabase, ReadableTable, ReadableTableMetadata, TableDefinition
|
Database, ReadableDatabase, ReadableTable, ReadableTableMetadata, TableDefinition
|
||||||
};
|
};
|
||||||
// use std::collections::HashMap;
|
// use std::collections::HashMap;
|
||||||
use std::{ops::Not, path::Path, u64, u128};
|
use std::{path::Path, u64, u128};
|
||||||
|
|
||||||
//tables defenition
|
|
||||||
const DB_FILE: &str = "./data";
|
|
||||||
|
|
||||||
const COMPLETED_RANGES: TableDefinition<u128, u128> = TableDefinition::new("completed_ranges");
|
|
||||||
|
|
||||||
// const RANGE_STATUS: MultimapTableDefinition<bool, u128> =
|
// const RANGE_STATUS: MultimapTableDefinition<bool, u128> =
|
||||||
// MultimapTableDefinition::new("range_status");
|
// MultimapTableDefinition::new("range_status");
|
||||||
@@ -183,15 +179,15 @@ impl RangeRecord {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tweaks_left(&self) -> u128 {
|
// fn tweaks_left(&self) -> u128 {
|
||||||
let t_left = (self.tweak_end - self.tweak_current) + 1;
|
// let t_left = (self.tweak_end - self.tweak_current) + 1;
|
||||||
if t_left == 1 && self.key_committed_progress == u128::MAX {
|
// if t_left == 1 && self.key_committed_progress == u128::MAX {
|
||||||
0u128
|
// 0u128
|
||||||
}
|
// }
|
||||||
else {
|
// else {
|
||||||
t_left
|
// t_left
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
fn completed_ratio(&self) -> f64 {
|
fn completed_ratio(&self) -> f64 {
|
||||||
let t_left = self.tweak_end - self.tweak_current;
|
let t_left = self.tweak_end - self.tweak_current;
|
||||||
@@ -268,15 +264,15 @@ struct RangesTable<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> RangesTable<'a> {
|
impl<'a> RangesTable<'a> {
|
||||||
fn new(
|
// fn new(
|
||||||
ranges: redb::Table<'a, u16, (u128, u128, u128, u128)>,
|
// ranges: redb::Table<'a, u16, (u128, u128, u128, u128)>,
|
||||||
ranges_to_use: redb::Table<'a, u16, ()>,
|
// ranges_to_use: redb::Table<'a, u16, ()>,
|
||||||
) -> Self {
|
// ) -> Self {
|
||||||
RangesTable {
|
// RangesTable {
|
||||||
ranges: ranges,
|
// ranges: ranges,
|
||||||
ranges_to_use: ranges_to_use,
|
// ranges_to_use: ranges_to_use,
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
fn open_rw(trx: &'a redb::WriteTransaction) -> Result<Self, redb::Error> {
|
fn open_rw(trx: &'a redb::WriteTransaction) -> Result<Self, redb::Error> {
|
||||||
Ok(RangesTable {
|
Ok(RangesTable {
|
||||||
@@ -333,16 +329,16 @@ impl<'a> RangesTable<'a> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_progress(&self) -> Result<f64, redb::StorageError> {
|
// fn compute_progress(&self) -> Result<f64, redb::StorageError> {
|
||||||
let mut mean_prog: f64 = 0.0;
|
// let mut mean_prog: f64 = 0.0;
|
||||||
for range_res in self.ranges.iter()? {
|
// for range_res in self.ranges.iter()? {
|
||||||
let ag = range_res?;
|
// let ag = range_res?;
|
||||||
let range = RangeRecord::from_value(ag.0.value(), &ag.1.value());
|
// let range = RangeRecord::from_value(ag.0.value(), &ag.1.value());
|
||||||
mean_prog += range.completed_ratio();
|
// mean_prog += range.completed_ratio();
|
||||||
}
|
// }
|
||||||
|
|
||||||
Ok(mean_prog / 65536.0f64)
|
// Ok(mean_prog / 65536.0f64)
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -452,6 +448,7 @@ pub struct FoundKey {
|
|||||||
pub enc_key: u128,
|
pub enc_key: u128,
|
||||||
pub timestump: u64
|
pub timestump: u64
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn db_get_found_keys(db: &Database) -> Result<Vec<FoundKey>, redb::Error> {
|
pub fn db_get_found_keys(db: &Database) -> Result<Vec<FoundKey>, redb::Error> {
|
||||||
let trx: redb::ReadTransaction = db.begin_read()?;
|
let trx: redb::ReadTransaction = db.begin_read()?;
|
||||||
let mut res:Vec<FoundKey> = Vec::new();
|
let mut res:Vec<FoundKey> = Vec::new();
|
||||||
@@ -477,10 +474,10 @@ pub fn db_put_found_key(db: &Database, tw_k: u128, en_k: u128) -> Result<u64, re
|
|||||||
let trx: redb::WriteTransaction = db.begin_write()?;
|
let trx: redb::WriteTransaction = db.begin_write()?;
|
||||||
let new_keys_count = {
|
let new_keys_count = {
|
||||||
let mut found_keys_t = trx.open_table(FOUND_KEYS_TABLE)?;
|
let mut found_keys_t = trx.open_table(FOUND_KEYS_TABLE)?;
|
||||||
found_keys_t.insert((tw_k,en_k), get_timestump());
|
found_keys_t.insert((tw_k,en_k), get_timestump())?;
|
||||||
found_keys_t.len()?
|
found_keys_t.len()?
|
||||||
};
|
};
|
||||||
trx.commit();
|
trx.commit()?;
|
||||||
|
|
||||||
return Ok(new_keys_count);
|
return Ok(new_keys_count);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
// i: [u32; 2],
|
// i: [u32; 2],
|
||||||
// }
|
// }
|
||||||
|
|
||||||
use prost::bytes::BufMut;
|
|
||||||
|
|
||||||
fn u128_to_u32arr(a: u128) -> [u32;4] {
|
fn u128_to_u32arr(a: u128) -> [u32;4] {
|
||||||
let mut res = [0u32;4];
|
let mut res = [0u32;4];
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
use std::ops::DerefMut;
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::atomic::AtomicBool;
|
|
||||||
use std::{path::PathBuf};
|
use std::{path::PathBuf};
|
||||||
|
|
||||||
|
|
||||||
use redb;
|
use redb;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc};
|
||||||
use tokio::sync::RwLock as AsyncRwLock;
|
use tokio::sync::RwLock as AsyncRwLock;
|
||||||
use tokio::time::{interval, Duration};
|
|
||||||
|
|
||||||
use tonic::{Request, Response, Status, transport::Server};
|
use tonic::{Request, Response, Status, transport::Server};
|
||||||
|
|
||||||
@@ -20,7 +17,7 @@ pub mod nyash_proto {
|
|||||||
|
|
||||||
use nyash_proto::nyash_luks_server::{NyashLuks, NyashLuksServer};
|
use nyash_proto::nyash_luks_server::{NyashLuks, NyashLuksServer};
|
||||||
use nyash_proto::{
|
use nyash_proto::{
|
||||||
CommitReply, KeyData, ProgressReply, ProgressRequest, WorkCommit, WorkData, WorkReply, WorkRequest, work_commit,
|
CommitReply, ProgressReply, ProgressRequest, WorkCommit, WorkData, WorkReply, WorkRequest, work_commit,
|
||||||
work_reply,
|
work_reply,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -43,27 +40,6 @@ fn work_rec_to_work_data(work_rec: database::JobRecord) -> WorkData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shared state
|
|
||||||
// #[derive(Clone, Debug)]
|
|
||||||
// pub struct ServiceState {
|
|
||||||
// pub db: Arc<redb::Database>,
|
|
||||||
// pub key_found: bool,
|
|
||||||
// pub progress: f64,
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Background task to update state
|
|
||||||
// async fn update_service_state(state: Arc<AsyncRwLock<ServiceState>>) {
|
|
||||||
// loop {
|
|
||||||
// tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
|
|
||||||
// let mut guard = state.write().await;
|
|
||||||
// match database::db_get_progress(&guard.db) {
|
|
||||||
// Ok(p) => {
|
|
||||||
// guard.progress = p;
|
|
||||||
// },
|
|
||||||
// Err(_) => println!("Error getting progress from DB")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Background task to update progress from DB
|
// Background task to update progress from DB
|
||||||
async fn update_service_progress(progress: Arc<AsyncRwLock<f64>>, db: Arc<redb::Database>) {
|
async fn update_service_progress(progress: Arc<AsyncRwLock<f64>>, db: Arc<redb::Database>) {
|
||||||
@@ -91,6 +67,12 @@ pub struct NyashService {
|
|||||||
impl NyashLuks for NyashService {
|
impl NyashLuks for NyashService {
|
||||||
async fn request_work(&self, request: Request<WorkRequest>) -> Result<Response<WorkReply>, Status> {
|
async fn request_work(&self, request: Request<WorkRequest>) -> Result<Response<WorkReply>, Status> {
|
||||||
println!("Got a request: {:?}", request);
|
println!("Got a request: {:?}", request);
|
||||||
|
|
||||||
|
|
||||||
|
if *self.key_found.read().await == true {
|
||||||
|
return Ok(Response::new(WorkReply { result: Some(work_reply::Result::NoWork(true)) }));
|
||||||
|
}
|
||||||
|
|
||||||
let pref_work_size: u64 = request.into_inner().pref_work_size;
|
let pref_work_size: u64 = request.into_inner().pref_work_size;
|
||||||
|
|
||||||
match database::db_create_job(&self.db, pref_work_size) {
|
match database::db_create_job(&self.db, pref_work_size) {
|
||||||
@@ -196,7 +178,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
if found_keys.len() >0 {
|
if found_keys.len() >0 {
|
||||||
println!("Key already found!");
|
println!("Key already found!");
|
||||||
for key in found_keys {
|
for key in found_keys {
|
||||||
println!("Key found: {:?}", key);
|
println!("Key found! Timestump: {}, Tweak key: {}, Enc key: {}",key.timestump, key.tweak_key, key.enc_key);
|
||||||
}
|
}
|
||||||
println!("Exit!");
|
println!("Exit!");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|||||||
Reference in New Issue
Block a user