Skip to content

Commit

Permalink
Fix issues with private key server
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 authored Sep 25, 2024
1 parent 2efc549 commit cb2b4da
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/web3_account_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ edition = "2021"
name = "web3_account_server"
license = "MIT"
repository = "https://github.com/scx1332/"
version = "0.4.8"
version = "0.4.11"

[dependencies]
actix-cors = { workspace = true }
actix-web = { workspace = true }
actix-web-httpauth = { workspace = true }
env_logger = { workspace = true }
Expand Down
70 changes: 35 additions & 35 deletions crates/web3_account_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,48 @@ use std::io::{BufReader, BufWriter};
use std::sync::{Arc, Mutex};
use structopt::StructOpt;

fn add(item: String, file_name: &str) -> bool {
let mut results: Vec<String> = {
if let Ok(file) = OpenOptions::new().read(true).open(file_name) {
let reader = BufReader::new(file);
serde_json::from_reader(reader).unwrap_or_else(|_| Vec::new())
} else {
Vec::new()
}
};
fn read_results(file_name: &str) -> Vec<String> {
if let Ok(file) = OpenOptions::new().read(true).open(file_name) {
let reader = BufReader::new(file);
serde_json::from_reader(reader).unwrap_or_else(|_| Vec::new())
} else {
Vec::new()
}
}

fn add(item: String, file_name: &str) -> std::io::Result<bool> {
let mut results = read_results(file_name);
if results.contains(&item) {
return false;
return Ok(false);
}
results.push(item);
let file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(file_name)
.unwrap();
.inspect_err(|e| log::error!("Error opening file: {}", e))?;
let writer = BufWriter::new(file);
serde_json::to_writer(writer, &results).unwrap();
true
Ok(true)
}

fn get(file_name: &str) -> Option<String> {
let mut results: Vec<String> = {
let file = OpenOptions::new().read(true).open(file_name).unwrap();
let reader = BufReader::new(file);
serde_json::from_reader(reader).unwrap_or_else(|_| Vec::new())
};
fn get(file_name: &str) -> std::io::Result<Option<String>> {
let mut results = read_results(file_name);
// get first item
if results.is_empty() {
return None;
return Ok(None);
}
let item = results.remove(0);

// remove first item
let file = OpenOptions::new()
.write(true)
.truncate(true)
.open(file_name)
.unwrap();
.open(file_name)?;
let writer = BufWriter::new(file);
serde_json::to_writer(writer, &results).unwrap();
Some(item)
Ok(Some(item))
}

#[derive(Clone)]
Expand Down Expand Up @@ -93,30 +90,32 @@ async fn add_to_queue(data: web::Data<AppState>, item: String) -> impl Responder
if private_key.len() != 32 {
return HttpResponse::BadRequest().body("Invalid item length");
}
if add(hex::encode(private_key), &data.file_name) {
HttpResponse::Ok().body("Added to the queue")
} else {
HttpResponse::Ok().body("Item already in the queue")
match add(hex::encode(private_key), &data.file_name) {
Ok(true) => HttpResponse::Ok().body("Added to the queue"),
Ok(false) => HttpResponse::Ok().body("Item already in the queue"),
Err(e) => {
log::error!("Error adding item: {}", e);
HttpResponse::InternalServerError().finish()
}
}
}

async fn count(data: web::Data<AppState>) -> impl Responder {
let _lock = data.lock.lock().unwrap();
let file_name = &data.file_name;
let results: Vec<String> = {
let file = OpenOptions::new().read(true).open(file_name).unwrap();
let reader = BufReader::new(file);
serde_json::from_reader(reader).unwrap_or_else(|_| Vec::new())
};
let results = read_results(file_name);
HttpResponse::Ok().body(results.len().to_string())
}

async fn get_from_queue(data: web::Data<AppState>) -> impl Responder {
let _lock = data.lock.lock().unwrap();
if let Some(item) = get(&data.file_name) {
HttpResponse::Ok().body(item)
} else {
HttpResponse::BadRequest().body("Queue is empty")
match get(&data.file_name) {
Ok(Some(item)) => HttpResponse::Ok().body(item),
Ok(None) => HttpResponse::BadRequest().body("Queue is empty"),
Err(e) => {
log::error!("Error getting item: {}", e);
HttpResponse::InternalServerError().finish()
}
}
}

Expand Down Expand Up @@ -164,6 +163,7 @@ async fn main() -> std::io::Result<()> {
.app_data(web::Data::new(app_state.clone()))
.wrap(actix_web::middleware::Logger::default())
.wrap(auth)
.wrap(actix_cors::Cors::permissive())
.route("/count", web::get().to(count))
.route("/add", web::post().to(add_to_queue))
.route("/get", web::get().to(get_from_queue))
Expand Down

0 comments on commit cb2b4da

Please sign in to comment.