add json receiving in client side

This commit is contained in:
horhik 2020-11-01 18:46:47 +02:00
parent f541cc348f
commit 605319e158
11 changed files with 1542 additions and 77 deletions

2
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,2 @@
{
}

1387
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
[package] [package]
name = "hole" name = "hole"
version = "0.1.0" version = "0.0.1"
authors = ["horhik <horhik@tuta.io>"] authors = ["horhik <horhik@tuta.io>"]
edition = "2018" edition = "2018"
@ -10,3 +10,17 @@ edition = "2018"
ntru = "0.5.6" ntru = "0.5.6"
rusqlite = "0.24.0" rusqlite = "0.24.0"
toml = "0.5.6" toml = "0.5.6"
tungstenite = "0.11.1"
tokio = { version = "0.2", features = ["full"] }
log = "0.4.11"
futures = "0.3.6"
async-std = "1.6.5"
async-tls = "0.10.0"
futures-util = "*"
serde_json = "1.0.59"
serde = "1.0.116"
serde_derive = "1.0.116"
[dependencies.async-tungstenite]
version = "0.8.0"
features = ["tokio-runtime", "tokio-native-tls", "async-tls", "async-std"]

View file

@ -0,0 +1,7 @@
{
"userID": 123456789,
"receiverID": 123456789,
"message": "hey dude",
"time": "Tue Oct 13 2020 18:31:22 GMT+0300 (Eastern European Summer Time)",
}

View file

@ -1,3 +1,5 @@
use super::front_conn;
pub struct Message { pub struct Message {
sender_id: i32, sender_id: i32,
message: String, message: String,

View file

@ -1,36 +1,13 @@
type Decoded = String; use crate::encrypting;
type Encoded = String; use async_std::{
io,
trait Handler<State> { net::{TcpListener, TcpStream},
fn process(code: State) -> Message; task,
fn send(socket: &WebSocketStream<TcpStream>, msg: Message); };
} use async_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
struct MessageServer { use futures::{SinkExt, StreamExt};
new_message: bool, use serde_derive::Deserialize;
text: String, use std::env;
}
struct MessageClient {
message_queue: Vec<Message>,
}
impl MessageServer {
fn new() -> MessageServer {
MessageServer {
new_message: false,
text: String::from(""),
}
}
}
impl MessageClient {
fn new() -> MessageClient {
MessageClient {
message_queue: vec![],
}
}
}
pub fn listen_client() -> io::Result<()> { pub fn listen_client() -> io::Result<()> {
task::block_on(connect_to_client()) task::block_on(connect_to_client())
} }
@ -45,10 +22,19 @@ async fn connect_to_client() -> io::Result<()> {
while let Ok((stream, _)) = listener.accept().await { while let Ok((stream, _)) = listener.accept().await {
task::spawn(accept_client(stream)); task::spawn(accept_client(stream));
} }
println!("HEY");
Ok(()) Ok(())
} }
#[derive(Deserialize, Debug)]
struct FrontMsg {
userID: u32,
receiverID: u32,
message: String,
time: String,
}
async fn accept_client(stream: TcpStream) -> io::Result<()> { async fn accept_client(stream: TcpStream) -> io::Result<()> {
let addr = stream let addr = stream
.peer_addr() .peer_addr()
@ -66,14 +52,36 @@ async fn accept_client(stream: TcpStream) -> io::Result<()> {
loop { loop {
match new_msg.await { match new_msg.await {
Some(msg) => { Some(msg) => {
println!("{:?}", msg.unwrap().into_text().unwrap()); let jsoned = msg.unwrap();
let res: serde_json::Result<FrontMsg> =
serde_json::from_str(jsoned.to_text().unwrap());
if let Ok(received_msg) = res {
let id = received_msg.receiverID;
println!("{:?}", id);
/* message example
{
"userID": 123456789,
"receiverID": 123456789,
"message": "hey dude",
"time": "Tue Oct 13 2020 18:31:22 GMT+0300 (Eastern European Summer Time)",
}
*/
sender sender
.send(Message::Text("msg".to_owned())) .send(Message::Text(String::from("msg").to_owned()))
.await .await
.expect("ooops"); .expect("ooops");
} else {
println!("seems, that messsage formatted wrong");
println!("{:?}", res);
}
new_msg = receiver.next(); new_msg = receiver.next();
} }
None => break, None => {
break;
}
} }
} }

View file

@ -1 +1,2 @@
pub mod chat_api_base; pub mod chat_api_base;
pub mod front_conn;

View file

@ -0,0 +1,32 @@
type Decoded = String;
type Encoded = String;
trait Handler<State> {
fn process(code: State) -> Message;
fn send(socket: &WebSocketStream<TcpStream>, msg: Message);
}
pub struct MessageServer {
new_message: bool,
text: String,
}
pub struct MessageClient {
message_queue: Vec<Message>,
}
impl MessageServer {
fn new() -> MessageServer {
MessageServer {
new_message: false,
text: String::from(""),
}
}
}
impl MessageClient {
fn new() -> MessageClient {
MessageClient {
message_queue: vec![],
}
}
}

View file

@ -67,6 +67,8 @@ pub fn encrypt_message (msg:String, key: &PublicKey ) -> String {
return message return message
} }
type CallBack = fn ();
pub fn decrypt_message(msg: String, kp: &KeyPair, final_msg: &mut String) { pub fn decrypt_message(msg: String, kp: &KeyPair, final_msg: &mut String) {
let encrypted_message = msg.into_bytes(); let encrypted_message = msg.into_bytes();
let decrypted = ntru::decrypt(&encrypted_message, &kp, &DEFAULT_PARAMS_256_BITS); let decrypted = ntru::decrypt(&encrypted_message, &kp, &DEFAULT_PARAMS_256_BITS);

View file

@ -1,4 +1,4 @@
pub mod encrypting; pub mod encrypting;
pub use encrypting::*;
pub mod chat; mod chat;
pub use chat::front_conn;

View file

@ -1,10 +1,31 @@
mod cli; mod chat;
mod encrypting; mod encrypting;
use encrypting::{KeyPair, generate_kp, encrypt_message, decrypt_message}; use async_std::io;
use chat::front_conn::listen_client;
fn main() -> io::Result<()> {
listen_client()
}
/*
fn main() { fn main() {
/* let server = TcpListener::bind("127.0.0.1:9001").unwrap();
println!("{:?}", &server);
for stream in server.incoming() {
spawn(move || {
println!("{:?}", &stream);
let mut websocket: WebSocket<std::net::TcpStream> = accept(stream.unwrap()).unwrap();
loop {
let msg = websocket.read_message().unwrap();
// We do not want to send back ping/pong messages.
if msg.is_binary() || msg.is_text() {
websocket.write_message(msg).unwrap();
}
}
});
}
}*/
/*
let (one, two) = keys; let (one, two) = keys;
let value =String::from_utf8_lossy(&*one); let value =String::from_utf8_lossy(&*one);
@ -14,7 +35,7 @@ fn main() {
let newkey = PrivateKey::import(newbytes); let newkey = PrivateKey::import(newbytes);
let conn = Connection::open("myfile.db").unwrap(); Let conn = Connection::open("myfile.db").unwrap();
conn.execute("CREATE TABLE person ( conn.execute("CREATE TABLE person (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
@ -26,7 +47,6 @@ fn main() {
conn.execute("INSERT INTO person (name, email) VALUES (?1, ?2)", conn.execute("INSERT INTO person (name, email) VALUES (?1, ?2)",
&[&name, &email]).unwrap(); &[&name, &email]).unwrap();
*/
//let mut std = cli::cli_base::get_stdin();
//print!("{}",std)
} }
*/
//let mut std = cli::cli_base::get_stdin();