|
| 1 | +use crate::*; |
| 2 | + |
| 3 | +// |
| 4 | +// Networking protocol types |
| 5 | +// |
| 6 | + |
| 7 | +#[derive(Clone, Debug, Serialize, Deserialize)] |
| 8 | +pub struct Identity { |
| 9 | + pub name: NodeId, |
| 10 | + pub networking_key: String, |
| 11 | + pub ws_routing: Option<(String, u16)>, |
| 12 | + pub allowed_routers: Vec<NodeId>, |
| 13 | +} |
| 14 | + |
| 15 | +/// Must be parsed from message pack vector. |
| 16 | +/// all Get actions must be sent from local process. used for debugging |
| 17 | +#[derive(Clone, Debug, Serialize, Deserialize)] |
| 18 | +pub enum NetAction { |
| 19 | + /// Received from a router of ours when they have a new pending passthrough for us. |
| 20 | + /// We should respond (if we desire) by using them to initialize a routed connection |
| 21 | + /// with the NodeId given. |
| 22 | + ConnectionRequest(NodeId), |
| 23 | + /// can only receive from trusted source, for now just ourselves locally, |
| 24 | + /// in the future could get from remote provider |
| 25 | + KnsUpdate(KnsUpdate), |
| 26 | + KnsBatchUpdate(Vec<KnsUpdate>), |
| 27 | + /// get a list of peers we are connected to |
| 28 | + GetPeers, |
| 29 | + /// get the [`Identity`] struct for a single peer |
| 30 | + GetPeer(String), |
| 31 | + /// get the [`NodeId`] associated with a given namehash, if any |
| 32 | + GetName(String), |
| 33 | + /// get a user-readable diagnostics string containing networking inforamtion |
| 34 | + GetDiagnostics, |
| 35 | + /// sign the attached blob payload, sign with our node's networking key. |
| 36 | + /// **only accepted from our own node** |
| 37 | + /// **the source [`Address`] will always be prepended to the payload** |
| 38 | + Sign, |
| 39 | + /// given a message in blob payload, verify the message is signed by |
| 40 | + /// the given source. if the signer is not in our representation of |
| 41 | + /// the PKI, will not verify. |
| 42 | + /// **the `from` [`Address`] will always be prepended to the payload** |
| 43 | + Verify { |
| 44 | + from: Address, |
| 45 | + signature: Vec<u8>, |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +/// For now, only sent in response to a ConnectionRequest. |
| 50 | +/// Must be parsed from message pack vector |
| 51 | +#[derive(Clone, Debug, Serialize, Deserialize)] |
| 52 | +pub enum NetResponse { |
| 53 | + Accepted(NodeId), |
| 54 | + Rejected(NodeId), |
| 55 | + /// response to [`NetAction::GetPeers`] |
| 56 | + Peers(Vec<Identity>), |
| 57 | + /// response to [`NetAction::GetPeer`] |
| 58 | + Peer(Option<Identity>), |
| 59 | + /// response to [`NetAction::GetName`] |
| 60 | + Name(Option<String>), |
| 61 | + /// response to [`NetAction::GetDiagnostics`]. a user-readable string. |
| 62 | + Diagnostics(String), |
| 63 | + /// response to [`NetAction::Sign`]. contains the signature in blob |
| 64 | + Signed, |
| 65 | + /// response to [`NetAction::Verify`]. boolean indicates whether |
| 66 | + /// the signature was valid or not. note that if the signer node |
| 67 | + /// cannot be found in our representation of PKI, this will return false, |
| 68 | + /// because we cannot find the networking public key to verify with. |
| 69 | + Verified(bool), |
| 70 | +} |
| 71 | + |
| 72 | +#[derive(Clone, Debug, Serialize, Deserialize)] |
| 73 | +pub struct KnsUpdate { |
| 74 | + pub name: String, // actual username / domain name |
| 75 | + pub owner: String, |
| 76 | + pub node: String, // hex namehash of node |
| 77 | + pub public_key: String, |
| 78 | + pub ip: String, |
| 79 | + pub port: u16, |
| 80 | + pub routers: Vec<String>, |
| 81 | +} |
| 82 | + |
| 83 | +// |
| 84 | +// Helpers |
| 85 | +// |
| 86 | + |
| 87 | +pub fn sign<T>(message: T) -> Result<Vec<u8>, SendError> |
| 88 | +where |
| 89 | + T: Into<Vec<u8>>, |
| 90 | +{ |
| 91 | + Request::to(("our", "net", "distro", "sys")) |
| 92 | + .body(rmp_serde::to_vec(&NetAction::Sign).unwrap()) |
| 93 | + .blob_bytes(message.into()) |
| 94 | + .send_and_await_response(30) |
| 95 | + .unwrap() |
| 96 | + .map(|_resp| get_blob().unwrap().bytes) |
| 97 | +} |
| 98 | + |
| 99 | +pub fn verify<T, U, V>(from: T, message: U, signature: V) -> Result<bool, SendError> |
| 100 | +where |
| 101 | + T: Into<Address>, |
| 102 | + U: Into<Vec<u8>>, |
| 103 | + V: Into<Vec<u8>>, |
| 104 | +{ |
| 105 | + Request::to(("our", "net", "distro", "sys")) |
| 106 | + .body( |
| 107 | + rmp_serde::to_vec(&NetAction::Verify { |
| 108 | + from: from.into(), |
| 109 | + signature: signature.into(), |
| 110 | + }) |
| 111 | + .unwrap(), |
| 112 | + ) |
| 113 | + .blob_bytes(message.into()) |
| 114 | + .send_and_await_response(30) |
| 115 | + .unwrap() |
| 116 | + .map(|resp| { |
| 117 | + let Ok(NetResponse::Verified(valid)) = |
| 118 | + rmp_serde::from_slice::<NetResponse>(resp.body()) |
| 119 | + else { |
| 120 | + return false; |
| 121 | + }; |
| 122 | + valid |
| 123 | + }) |
| 124 | +} |
0 commit comments