Fix issue with public key request

This commit is contained in:
onyinyang 2023-02-06 13:57:23 -05:00
parent e55eda2ca6
commit 8529083285
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
1 changed files with 14 additions and 37 deletions

View File

@ -1,4 +1,3 @@
use core::slice;
use std::{ use std::{
convert::Infallible, convert::Infallible,
net::SocketAddr, net::SocketAddr,
@ -20,10 +19,6 @@ use rand::RngCore;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json; use serde_json;
use serde_with::serde_as; use serde_with::serde_as;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;
#[serde_as] #[serde_as]
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
@ -75,7 +70,6 @@ pub fn random() -> BridgeLine {
#[derive(Clone)] #[derive(Clone)]
struct LoxServerContext { struct LoxServerContext {
pubkey_filename: String,
db: Arc<Mutex<BridgeDb>>, db: Arc<Mutex<BridgeDb>>,
ba: Arc<Mutex<BridgeAuth>>, ba: Arc<Mutex<BridgeAuth>>,
} }
@ -85,9 +79,7 @@ async fn handle(
// addr: SocketAddr, // addr: SocketAddr,
req: Request<Body>, req: Request<Body>,
) -> Result<Response<Body>, Infallible> { ) -> Result<Response<Body>, Infallible> {
println!("Reqs on {}, {}", req.method(), req.uri().path()); println!("Request: {:?}", req);
println!("Whole req: {:?}", req);
match req.method() { match req.method() {
&Method::OPTIONS => Ok(Response::builder() &Method::OPTIONS => Ok(Response::builder()
.header("Access-Control-Allow-Origin", HeaderValue::from_static("*")) .header("Access-Control-Allow-Origin", HeaderValue::from_static("*"))
@ -98,7 +90,7 @@ async fn handle(
.unwrap()), .unwrap()),
_ => match (req.method(), req.uri().path()) { _ => match (req.method(), req.uri().path()) {
(&Method::GET, "/invite") => Ok::<_, Infallible>(generate_invite(context.db)), (&Method::GET, "/invite") => Ok::<_, Infallible>(generate_invite(context.db)),
(&Method::GET, "/pubkeys") => Ok::<_, Infallible>(send_keys(&context.pubkey_filename)), (&Method::GET, "/pubkeys") => Ok::<_, Infallible>(send_keys(context.ba)),
//TODO: figure out the format of the request and parse it? //TODO: figure out the format of the request and parse it?
(&Method::POST, "/openreq") => Ok::<_, Infallible>({ (&Method::POST, "/openreq") => Ok::<_, Infallible>({
let bytes = body::to_bytes(req.into_body()).await.unwrap(); let bytes = body::to_bytes(req.into_body()).await.unwrap();
@ -129,9 +121,18 @@ fn generate_invite(db: Arc<Mutex<lox::BridgeDb>>) -> Response<Body> {
resp resp
} }
fn send_keys(pubkeys_filename: &str) -> Response<Body> { fn send_keys(ba: Arc<Mutex<BridgeAuth>>) -> Response<Body> {
let data = fs::read_to_string(pubkeys_filename).expect("Unable to read file"); let ba_obj = ba.lock().unwrap();
let mut resp = Response::new(Body::from(serde_json::to_string(&data).unwrap())); // vector of public keys (to serialize)
let ba_obj_pubkeys = vec![
&ba_obj.lox_pub,
&ba_obj.migration_pub,
&ba_obj.migrationkey_pub,
&ba_obj.reachability_pub,
&ba_obj.invitation_pub,
];
let mut resp = Response::new(Body::from(serde_json::to_string(&ba_obj_pubkeys).unwrap()));
resp.headers_mut() resp.headers_mut()
.insert("Access-Control-Allow-Origin", HeaderValue::from_static("*")); .insert("Access-Control-Allow-Origin", HeaderValue::from_static("*"));
resp resp
@ -156,7 +157,6 @@ async fn shutdown_signal() {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let lox_auth_pubkeys_filename = "lox_auth_pubkeys.json";
let num_buckets = 5; let num_buckets = 5;
// Create and initialize a new db and lox_auth // Create and initialize a new db and lox_auth
let mut bridgedb = BridgeDb::new(); let mut bridgedb = BridgeDb::new();
@ -170,30 +170,7 @@ async fn main() {
// Create the encrypted bridge table // Create the encrypted bridge table
lox_auth.enc_bridge_table(); lox_auth.enc_bridge_table();
// output lox_auth pubkeys if the file doesn't exist
if !Path::new(lox_auth_pubkeys_filename).exists() {
// vector of public keys (to serialize)
let lox_auth_pubkeys = vec![
&lox_auth.lox_pub,
&lox_auth.migration_pub,
&lox_auth.migrationkey_pub,
&lox_auth.reachability_pub,
&lox_auth.invitation_pub,
];
// output lox_auth public keys
let mut lox_auth_pubkeys_outfile = File::create(lox_auth_pubkeys_filename)
.expect("Failed to create lox_auth pubkeys file");
write!(
lox_auth_pubkeys_outfile,
"{}",
serde_json::to_string(&lox_auth_pubkeys).unwrap()
)
.expect("Failed to write to lox_auth pubkeys file");
}
let context = LoxServerContext { let context = LoxServerContext {
pubkey_filename: lox_auth_pubkeys_filename.into(),
db: Arc::new(Mutex::new(bridgedb)), db: Arc::new(Mutex::new(bridgedb)),
ba: Arc::new(Mutex::new(lox_auth)), ba: Arc::new(Mutex::new(lox_auth)),
}; };