mirror of
https://github.com/uqbar-dao/nectar.git
synced 2024-12-24 00:53:37 +03:00
merged
This commit is contained in:
commit
57a655fcac
16
src/main.rs
16
src/main.rs
@ -169,7 +169,9 @@ async fn main() {
|
|||||||
{
|
{
|
||||||
ip
|
ip
|
||||||
} else {
|
} else {
|
||||||
println!( "\x1b[38;5;196mfailed to find public IPv4 address: booting as a routed node\x1b[0m");
|
println!(
|
||||||
|
"\x1b[38;5;196mfailed to find public IPv4 address: booting as a routed node\x1b[0m"
|
||||||
|
);
|
||||||
std::net::Ipv4Addr::LOCALHOST
|
std::net::Ipv4Addr::LOCALHOST
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -203,10 +205,14 @@ async fn main() {
|
|||||||
} => (our, decoded_keyfile, encoded_keyfile),
|
} => (our, decoded_keyfile, encoded_keyfile),
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("saving encrypted networking keys to {}/.keys", home_directory_path);
|
println!(
|
||||||
|
"saving encrypted networking keys to {}/.keys",
|
||||||
|
home_directory_path
|
||||||
|
);
|
||||||
|
|
||||||
fs::write(format!("{}/.keys", home_directory_path), encoded_keyfile)
|
fs::write(format!("{}/.keys", home_directory_path), encoded_keyfile)
|
||||||
.await.unwrap();
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
println!("registration complete!");
|
println!("registration complete!");
|
||||||
|
|
||||||
@ -215,7 +221,9 @@ async fn main() {
|
|||||||
home_directory_path.clone(),
|
home_directory_path.clone(),
|
||||||
decoded_keyfile.file_key,
|
decoded_keyfile.file_key,
|
||||||
fs_config,
|
fs_config,
|
||||||
).await.expect("fs load failed!");
|
)
|
||||||
|
.await
|
||||||
|
.expect("fs load failed!");
|
||||||
|
|
||||||
let _ = kill_tx.send(true);
|
let _ = kill_tx.send(true);
|
||||||
let _ = print_sender
|
let _ = print_sender
|
||||||
|
@ -9,7 +9,10 @@ use sha2::Sha256;
|
|||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use tokio::sync::{mpsc, oneshot};
|
use tokio::sync::{mpsc, oneshot};
|
||||||
use warp::{
|
use warp::{
|
||||||
http::{ StatusCode, header::{HeaderValue, SET_COOKIE}, },
|
http::{
|
||||||
|
header::{HeaderValue, SET_COOKIE},
|
||||||
|
StatusCode,
|
||||||
|
},
|
||||||
Filter, Rejection, Reply,
|
Filter, Rejection, Reply,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -42,9 +45,8 @@ pub async fn register(
|
|||||||
kill_rx: oneshot::Receiver<bool>,
|
kill_rx: oneshot::Receiver<bool>,
|
||||||
ip: String,
|
ip: String,
|
||||||
port: u16,
|
port: u16,
|
||||||
keyfile: Vec<u8>
|
keyfile: Vec<u8>,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
let our_arc = Arc::new(Mutex::new(None));
|
let our_arc = Arc::new(Mutex::new(None));
|
||||||
let our_ws_info = our_arc.clone();
|
let our_ws_info = our_arc.clone();
|
||||||
|
|
||||||
@ -60,29 +62,34 @@ pub async fn register(
|
|||||||
.and(warp::fs::file("./src/register/build/index.html"));
|
.and(warp::fs::file("./src/register/build/index.html"));
|
||||||
|
|
||||||
let api = warp::path("has-keyfile")
|
let api = warp::path("has-keyfile")
|
||||||
.and(warp::get()
|
.and(
|
||||||
|
warp::get()
|
||||||
.and(warp::any().map(move || keyfile_has.clone()))
|
.and(warp::any().map(move || keyfile_has.clone()))
|
||||||
.and_then(handle_has_keyfile))
|
.and_then(handle_has_keyfile),
|
||||||
.or(warp::path("info")
|
)
|
||||||
.and(warp::get()
|
.or(warp::path("info").and(
|
||||||
|
warp::get()
|
||||||
.and(warp::any().map(move || ip.clone()))
|
.and(warp::any().map(move || ip.clone()))
|
||||||
.and(warp::any().map(move || our_ws_info.clone()))
|
.and(warp::any().map(move || our_ws_info.clone()))
|
||||||
.and(warp::any().map(move || net_keypair_ws_info.clone()))
|
.and(warp::any().map(move || net_keypair_ws_info.clone()))
|
||||||
.and_then(handle_info)))
|
.and_then(handle_info),
|
||||||
.or(warp::path("vet-keyfile")
|
))
|
||||||
.and(warp::post()
|
.or(warp::path("vet-keyfile").and(
|
||||||
|
warp::post()
|
||||||
.and(warp::body::content_length_limit(1024 * 16))
|
.and(warp::body::content_length_limit(1024 * 16))
|
||||||
.and(warp::body::json())
|
.and(warp::body::json())
|
||||||
.and_then(handle_keyfile_check)))
|
.and_then(handle_keyfile_check),
|
||||||
.or(warp::path("boot")
|
))
|
||||||
.and(warp::put()
|
.or(warp::path("boot").and(
|
||||||
|
warp::put()
|
||||||
.and(warp::body::content_length_limit(1024 * 16))
|
.and(warp::body::content_length_limit(1024 * 16))
|
||||||
.and(warp::body::json())
|
.and(warp::body::json())
|
||||||
.and(warp::any().map(move || tx.clone()))
|
.and(warp::any().map(move || tx.clone()))
|
||||||
.and(warp::any().map(move || our_arc.lock().unwrap().take().unwrap()))
|
.and(warp::any().map(move || our_arc.lock().unwrap().take().unwrap()))
|
||||||
.and(warp::any().map(move || net_keypair_arc.lock().unwrap().take().unwrap()))
|
.and(warp::any().map(move || net_keypair_arc.lock().unwrap().take().unwrap()))
|
||||||
.and(warp::any().map(move || keyfile_arc.lock().unwrap().take().unwrap()))
|
.and(warp::any().map(move || keyfile_arc.lock().unwrap().take().unwrap()))
|
||||||
.and_then(handle_boot)));
|
.and_then(handle_boot),
|
||||||
|
));
|
||||||
|
|
||||||
let routes = static_files.or(react_app).or(api);
|
let routes = static_files.or(react_app).or(api);
|
||||||
|
|
||||||
@ -95,25 +102,17 @@ pub async fn register(
|
|||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_has_keyfile(
|
async fn handle_has_keyfile(keyfile: Arc<Mutex<Option<Vec<u8>>>>) -> Result<impl Reply, Rejection> {
|
||||||
keyfile: Arc<Mutex<Option<Vec<u8>>>>,
|
|
||||||
) -> Result<impl Reply, Rejection> {
|
|
||||||
|
|
||||||
Ok(warp::reply::json(&keyfile.lock().unwrap().is_some()))
|
Ok(warp::reply::json(&keyfile.lock().unwrap().is_some()))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_keyfile_check(
|
async fn handle_keyfile_check(payload: KeyfileCheck) -> Result<impl Reply, Rejection> {
|
||||||
payload: KeyfileCheck
|
|
||||||
) -> Result<impl Reply, Rejection> {
|
|
||||||
|
|
||||||
let keyfile = base64::decode(payload.keyfile).unwrap();
|
let keyfile = base64::decode(payload.keyfile).unwrap();
|
||||||
|
|
||||||
match keygen::decode_keyfile(keyfile, &payload.password) {
|
match keygen::decode_keyfile(keyfile, &payload.password) {
|
||||||
Ok(_) => Ok(warp::reply::with_status(warp::reply(), StatusCode::OK)),
|
Ok(_) => Ok(warp::reply::with_status(warp::reply(), StatusCode::OK)),
|
||||||
Err(_) => Err(warp::reject()),
|
Err(_) => Err(warp::reject()),
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_keyfile_gen(
|
async fn handle_keyfile_gen(
|
||||||
@ -122,9 +121,7 @@ async fn handle_keyfile_gen(
|
|||||||
networking_keypair: Arc<Mutex<Option<Document>>>,
|
networking_keypair: Arc<Mutex<Option<Document>>>,
|
||||||
jwt_secret: Arc<Mutex<Option<Vec<u8>>>>,
|
jwt_secret: Arc<Mutex<Option<Vec<u8>>>>,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
|
|
||||||
Ok(warp::reply::with_status(warp::reply(), StatusCode::OK))
|
Ok(warp::reply::with_status(warp::reply(), StatusCode::OK))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_boot(
|
async fn handle_boot(
|
||||||
@ -160,13 +157,14 @@ async fn handle_boot(
|
|||||||
let mut jwt_secret = [0u8, 32];
|
let mut jwt_secret = [0u8, 32];
|
||||||
ring::rand::SecureRandom::fill(&seed, &mut jwt_secret).unwrap();
|
ring::rand::SecureRandom::fill(&seed, &mut jwt_secret).unwrap();
|
||||||
|
|
||||||
let networking_pair = signature::Ed25519KeyPair::from_pkcs8(networking_keypair.as_ref()).unwrap();
|
let networking_pair =
|
||||||
|
signature::Ed25519KeyPair::from_pkcs8(networking_keypair.as_ref()).unwrap();
|
||||||
|
|
||||||
Keyfile {
|
Keyfile {
|
||||||
username: our.name.clone(),
|
username: our.name.clone(),
|
||||||
routers: our.allowed_routers.clone(),
|
routers: our.allowed_routers.clone(),
|
||||||
networking_keypair: signature::Ed25519KeyPair
|
networking_keypair: signature::Ed25519KeyPair::from_pkcs8(networking_keypair.as_ref())
|
||||||
::from_pkcs8(networking_keypair.as_ref()).unwrap(),
|
.unwrap(),
|
||||||
jwt_secret_bytes: jwt_secret.to_vec(),
|
jwt_secret_bytes: jwt_secret.to_vec(),
|
||||||
file_key: keygen::generate_file_key(),
|
file_key: keygen::generate_file_key(),
|
||||||
}
|
}
|
||||||
@ -188,7 +186,10 @@ async fn handle_boot(
|
|||||||
None => return Err(warp::reject()),
|
None => return Err(warp::reject()),
|
||||||
};
|
};
|
||||||
|
|
||||||
sender.send((our.clone(), decoded_keyfile, encoded_keyfile.clone())).await.unwrap();
|
sender
|
||||||
|
.send((our.clone(), decoded_keyfile, encoded_keyfile.clone()))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let encoded_keyfile_str = match info.keyfile.clone().is_empty() {
|
let encoded_keyfile_str = match info.keyfile.clone().is_empty() {
|
||||||
true => base64::encode(encoded_keyfile),
|
true => base64::encode(encoded_keyfile),
|
||||||
@ -201,10 +202,14 @@ async fn handle_boot(
|
|||||||
).into_response();
|
).into_response();
|
||||||
|
|
||||||
let headers = response.headers_mut();
|
let headers = response.headers_mut();
|
||||||
headers.append(SET_COOKIE, HeaderValue::from_str(
|
headers.append(
|
||||||
&format!("uqbar-auth_{}={};", &our.name, &token)).unwrap());
|
SET_COOKIE,
|
||||||
headers.append(SET_COOKIE, HeaderValue::from_str(
|
HeaderValue::from_str(&format!("uqbar-auth_{}={};", &our.name, &token)).unwrap(),
|
||||||
&format!("uqbar-ws-auth_{}={};", &our.name, &token)).unwrap());
|
);
|
||||||
|
headers.append(
|
||||||
|
SET_COOKIE,
|
||||||
|
HeaderValue::from_str(&format!("uqbar-ws-auth_{}={};", &our.name, &token)).unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
Ok(response)
|
Ok(response)
|
||||||
|
|
||||||
@ -237,7 +242,6 @@ async fn handle_info(
|
|||||||
*our_arc.lock().unwrap() = Some(our.clone());
|
*our_arc.lock().unwrap() = Some(our.clone());
|
||||||
|
|
||||||
Ok(warp::reply::json(&our))
|
Ok(warp::reply::json(&our))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_post(
|
async fn handle_post(
|
||||||
|
Loading…
Reference in New Issue
Block a user