1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
/* * Created on Sat Nov 28 2020 * * Copyright (c) storycraft. Licensed under the MIT Licence. */ //! # Loco protocol implemention //! //! An opensource loco protocol implemention written in Rust. //! Provides command, secure layer and crypto used in networking. //! //! ## Specification //! ### Command //! | name | size | //! |-----------|-------------------| //! | header | (Header) 22 bytes | //! | data | header.data_size | //! //! #### Header //! | name | size | //! |-----------|----------| //! | id | 4 bytes | //! | status | 2 bytes | //! | name | 11 bytes | //! | data_type | 1 bytes | //! | data_size | 4 bytes | //! //! ### Secure data //! | name | size | //! |----------------|--------------------| //! | header | (Header) 20 bytes | //! | encrypted data | header.size - 16 | //! //! #### Header //! | name | size | //! |-----------|----------| //! | iv | 16 bytes | //! | size | 4 bytes | //! //! #### Handshake //! | name | size | //! |------------------|------------| //! | key size | 4 bytes | //! | key_encrypt_type | 4 bytes | //! | encrypt_type | 4 bytes | //! | key | 256 bytes | //! //! Note: current implemention only supports RSA-AES //! //! ## Networking //! ### Command //! Stream: `Command #0 | Response command #0 | Broadcast command #1` //! //! Client -> Command #0 //! Server <- Command #0 //! Server -> Response command #0 //! Client <- Response command #0 //! Server -> Broadcast command #1 //! Client <- Broadcast command #1 //! //! Command is likely unordered. To match response and request, use header id. //! //! ### Secure data //! Stream: `Handshake | Secure data #0 | Secure data #1` //! //! Client -> Handshake //! Client -> Secure data #0 //! Server <- Secure data #0 //! Server -> Secure data #1 //! Client <- Secure data #1 //! //! //! ## Examples //! ### Echo server //! ``` //! use std::{net::{TcpListener, TcpStream}, thread, io}; //! use openssl::rsa::Rsa; //! use loco_protocol::{command::{Command, Error, Header, processor::CommandProcessor}, io::SecureClientStream, io::SecureServerStream, secure::CryptoStore}; //! //! let key = Rsa::generate(2048).expect("Cannot generate rsa key pairs."); //! //! let socket = TcpListener::bind("127.0.0.1:5022").expect("Cannot bind tcp server"); //! socket.set_nonblocking(true).expect("Cannot set socket to unblocked mode"); //! //! let tcp = TcpStream::connect("127.0.0.1:5022").expect("Cannot connect tcp stream"); //! tcp.set_nonblocking(true).expect("Cannot set client stream to unblocked mode"); //! //! let mut client = CommandProcessor::new( //! SecureClientStream::new( //! CryptoStore::new().unwrap(), //! key.clone(), //! tcp //! ) //! ); //! //! let command = Command { //! header: Header { //! id: 0, //! status: 0, //! name: [72_u8, 101, 108, 108, 111, 0, 0, 0, 0, 0, 0], //! data_type: 0, //! data_size: 5 //! }, //! //! data: "Hello".as_bytes().into(), //! }; //! //! client.write_command(command.clone()).expect("Failed to send command"); //! //! loop { //! match socket.accept() { //! //! Ok(connection) => { //! let key = key.clone(); //! let command = command.clone(); //! //! thread::spawn(move || { //! let mut server = CommandProcessor::new( //! SecureServerStream::new( //! CryptoStore::new().unwrap(), //! key, //! connection.0 //! ) //! ); //! //! println!("Connected from {}", connection.1); //! loop { //! match server.read_command() { //! //! Ok(readed) => { //! if readed.is_some() { //! let received = readed.unwrap(); //! assert_eq!(received, command); //! //! server.write_command(received.clone()).expect("Command failed to write"); //! break; //! } //! } //! //! Err(err) => panic!(format!("{}", err)) //! } //! } //! }); //! } //! //! Err(err) if err.kind() == io::ErrorKind::WouldBlock => { } //! //! Err(err) => panic!(format!("{}", err)) //! } //! //! match client.read_command() { //! //! Ok(readed) => { //! match readed { //! //! Some(received) => { //! assert_eq!(received, command.clone()); //! return; //! } //! //! None => {} //! } //! } //! //! Err(err) => { //! match err { //! Error::Io(io_err) if io_err.kind() == io::ErrorKind::WouldBlock => {} //! //! _ => panic!(format!("{}", err)) //! } //! } //! } //! } //! ``` pub mod command; pub mod io; pub mod secure; pub mod network;