Rust implementation of NextGraph, a Decentralized and local-first web 3.0 ecosystem
https://nextgraph.org
byzantine-fault-tolerancecrdtsdappsdecentralizede2eeeventual-consistencyjson-ldlocal-firstmarkdownocapoffline-firstp2pp2p-networkprivacy-protectionrdfrich-text-editorself-hostedsemantic-websparqlweb3collaboration
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
7.0 KiB
253 lines
7.0 KiB
1 year ago
|
/*
|
||
8 months ago
|
* Copyright (c) 2022-2024 Niko Bonnieure, Par le Peuple, NextGraph.org developers
|
||
1 year ago
|
* All rights reserved.
|
||
|
* Licensed under the Apache License, Version 2.0
|
||
|
* <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
|
||
|
* or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
|
||
|
* at your option. All files in the project carrying such
|
||
|
* notice may not be copied, modified, or distributed except
|
||
|
* according to those terms.
|
||
|
*/
|
||
|
|
||
1 year ago
|
use crate::actors::noise::Noise;
|
||
|
use crate::connection::NoiseFSM;
|
||
7 months ago
|
use crate::types::{
|
||
|
AdminRequest, CoreBrokerConnect, CoreBrokerConnectResponse, CoreBrokerConnectResponseV0,
|
||
7 months ago
|
CoreMessage, CoreMessageV0, CoreResponse, CoreResponseContentV0, CoreResponseV0, ExtResponse,
|
||
7 months ago
|
};
|
||
1 year ago
|
use crate::{actor::*, errors::ProtocolError, types::ProtocolMessage};
|
||
|
use async_std::sync::Mutex;
|
||
6 months ago
|
use ng_repo::log::*;
|
||
1 year ago
|
use serde::{Deserialize, Serialize};
|
||
|
use std::any::{Any, TypeId};
|
||
|
use std::sync::Arc;
|
||
|
|
||
1 year ago
|
// pub struct Noise3(Noise);
|
||
1 year ago
|
|
||
|
/// Start chosen protocol
|
||
7 months ago
|
/// First message sent by the connecting peer
|
||
1 year ago
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||
|
pub enum StartProtocol {
|
||
|
Client(ClientHello),
|
||
|
Ext(ExtHello),
|
||
7 months ago
|
Core(CoreHello),
|
||
1 year ago
|
Admin(AdminRequest),
|
||
1 year ago
|
}
|
||
|
|
||
|
impl StartProtocol {
|
||
|
pub fn type_id(&self) -> TypeId {
|
||
|
match self {
|
||
|
StartProtocol::Client(a) => a.type_id(),
|
||
7 months ago
|
StartProtocol::Core(a) => a.type_id(),
|
||
1 year ago
|
StartProtocol::Ext(a) => a.type_id(),
|
||
1 year ago
|
StartProtocol::Admin(a) => a.type_id(),
|
||
1 year ago
|
}
|
||
|
}
|
||
|
pub fn get_actor(&self) -> Box<dyn EActor> {
|
||
|
match self {
|
||
|
StartProtocol::Client(a) => a.get_actor(),
|
||
7 months ago
|
StartProtocol::Core(a) => a.get_actor(),
|
||
1 year ago
|
StartProtocol::Ext(a) => a.get_actor(),
|
||
1 year ago
|
StartProtocol::Admin(a) => a.get_actor(),
|
||
1 year ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
impl From<StartProtocol> for ProtocolMessage {
|
||
|
fn from(msg: StartProtocol) -> ProtocolMessage {
|
||
|
ProtocolMessage::Start(msg)
|
||
|
}
|
||
|
}
|
||
|
|
||
7 months ago
|
/// Core Hello (finalizes the Noise handshake and sends CoreConnect)
|
||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||
|
pub struct CoreHello {
|
||
|
// contains the 3rd Noise handshake message "s,se"
|
||
|
pub noise: Noise,
|
||
|
|
||
|
/// Noise encrypted payload (a CoreMessage::CoreRequest::BrokerConnect)
|
||
|
pub payload: Vec<u8>,
|
||
|
}
|
||
|
|
||
|
impl CoreHello {
|
||
|
pub fn get_actor(&self) -> Box<dyn EActor> {
|
||
|
Actor::<CoreBrokerConnect, CoreBrokerConnectResponse>::new_responder()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl TryFrom<ProtocolMessage> for CoreBrokerConnectResponse {
|
||
|
type Error = ProtocolError;
|
||
|
fn try_from(msg: ProtocolMessage) -> Result<Self, Self::Error> {
|
||
|
if let ProtocolMessage::CoreMessage(CoreMessage::V0(CoreMessageV0::Response(
|
||
7 months ago
|
CoreResponse::V0(CoreResponseV0 {
|
||
7 months ago
|
content: CoreResponseContentV0::BrokerConnectResponse(a),
|
||
|
..
|
||
7 months ago
|
}),
|
||
7 months ago
|
))) = msg
|
||
|
{
|
||
7 months ago
|
Ok(a)
|
||
7 months ago
|
} else {
|
||
|
log_debug!("INVALID {:?}", msg);
|
||
|
Err(ProtocolError::InvalidValue)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<CoreHello> for ProtocolMessage {
|
||
|
fn from(msg: CoreHello) -> ProtocolMessage {
|
||
|
ProtocolMessage::Start(StartProtocol::Core(msg))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<CoreBrokerConnect> for ProtocolMessage {
|
||
|
fn from(msg: CoreBrokerConnect) -> ProtocolMessage {
|
||
|
unimplemented!();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Actor<'_, CoreBrokerConnect, CoreBrokerConnectResponse> {}
|
||
|
|
||
|
#[async_trait::async_trait]
|
||
|
impl EActor for Actor<'_, CoreBrokerConnect, CoreBrokerConnectResponse> {
|
||
|
async fn respond(
|
||
|
&mut self,
|
||
|
msg: ProtocolMessage,
|
||
|
fsm: Arc<Mutex<NoiseFSM>>,
|
||
|
) -> Result<(), ProtocolError> {
|
||
|
//let req = CoreBrokerConnect::try_from(msg)?;
|
||
|
// let res = CoreBrokerConnectResponse::V0(CoreBrokerConnectResponseV0 {
|
||
|
// successes: vec![],
|
||
|
// errors: vec![],
|
||
|
// });
|
||
|
// fsm.lock().await.send(res.into()).await?;
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// External Hello (finalizes the Noise handshake and sends first ExtRequest)
|
||
1 year ago
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||
|
pub struct ExtHello {
|
||
|
// contains the 3rd Noise handshake message "s,se"
|
||
|
pub noise: Noise,
|
||
|
|
||
|
/// Noise encrypted payload (an ExtRequest)
|
||
|
pub payload: Vec<u8>,
|
||
|
}
|
||
|
|
||
|
impl ExtHello {
|
||
|
pub fn get_actor(&self) -> Box<dyn EActor> {
|
||
|
Actor::<ExtHello, ExtResponse>::new_responder()
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
impl From<ExtHello> for ProtocolMessage {
|
||
|
fn from(msg: ExtHello) -> ProtocolMessage {
|
||
|
ProtocolMessage::Start(StartProtocol::Ext(msg))
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
|
/// Client Hello
|
||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||
|
pub enum ClientHello {
|
||
|
// contains the 3rd Noise handshake message "s,se"
|
||
|
Noise3(Noise),
|
||
|
Local,
|
||
|
}
|
||
|
|
||
|
impl ClientHello {
|
||
|
pub fn type_id(&self) -> TypeId {
|
||
|
match self {
|
||
|
ClientHello::Noise3(a) => a.type_id(),
|
||
|
ClientHello::Local => TypeId::of::<ClientHello>(),
|
||
|
}
|
||
|
}
|
||
|
pub fn get_actor(&self) -> Box<dyn EActor> {
|
||
|
Actor::<ClientHello, ServerHello>::new_responder()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Server hello sent upon a client connection
|
||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||
|
pub struct ServerHelloV0 {
|
||
|
/// Nonce for ClientAuth
|
||
|
#[serde(with = "serde_bytes")]
|
||
|
pub nonce: Vec<u8>,
|
||
|
}
|
||
|
|
||
|
/// Server hello sent upon a client connection
|
||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||
|
pub enum ServerHello {
|
||
|
V0(ServerHelloV0),
|
||
|
}
|
||
|
|
||
|
impl ServerHello {
|
||
|
pub fn nonce(&self) -> &Vec<u8> {
|
||
|
match self {
|
||
|
ServerHello::V0(o) => &o.nonce,
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
impl From<ClientHello> for ProtocolMessage {
|
||
|
fn from(msg: ClientHello) -> ProtocolMessage {
|
||
|
ProtocolMessage::Start(StartProtocol::Client(msg))
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
|
impl TryFrom<ProtocolMessage> for ClientHello {
|
||
|
type Error = ProtocolError;
|
||
|
fn try_from(msg: ProtocolMessage) -> Result<Self, Self::Error> {
|
||
|
if let ProtocolMessage::Start(StartProtocol::Client(a)) = msg {
|
||
|
Ok(a)
|
||
|
} else {
|
||
|
Err(ProtocolError::InvalidValue)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl TryFrom<ProtocolMessage> for ServerHello {
|
||
|
type Error = ProtocolError;
|
||
|
fn try_from(msg: ProtocolMessage) -> Result<Self, Self::Error> {
|
||
|
if let ProtocolMessage::ServerHello(server_hello) = msg {
|
||
|
Ok(server_hello)
|
||
|
} else {
|
||
|
Err(ProtocolError::InvalidValue)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<ServerHello> for ProtocolMessage {
|
||
|
fn from(msg: ServerHello) -> ProtocolMessage {
|
||
|
ProtocolMessage::ServerHello(msg)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Actor<'_, ClientHello, ServerHello> {}
|
||
|
|
||
|
#[async_trait::async_trait]
|
||
|
impl EActor for Actor<'_, ClientHello, ServerHello> {
|
||
|
async fn respond(
|
||
|
&mut self,
|
||
|
msg: ProtocolMessage,
|
||
|
fsm: Arc<Mutex<NoiseFSM>>,
|
||
|
) -> Result<(), ProtocolError> {
|
||
|
let req = ClientHello::try_from(msg)?;
|
||
|
let res = ServerHello::V0(ServerHelloV0 { nonce: vec![] });
|
||
|
fsm.lock().await.send(res.into()).await?;
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Actor<'_, ExtHello, ExtResponse> {}
|
||
|
|
||
|
#[async_trait::async_trait]
|
||
|
impl EActor for Actor<'_, ExtHello, ExtResponse> {
|
||
|
async fn respond(
|
||
|
&mut self,
|
||
|
msg: ProtocolMessage,
|
||
|
fsm: Arc<Mutex<NoiseFSM>>,
|
||
|
) -> Result<(), ProtocolError> {
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|