add json receiving in client side
This commit is contained in:
parent
f541cc348f
commit
605319e158
2
.vscode/settings.json
vendored
Normal file
2
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
{
|
||||
}
|
1387
Cargo.lock
generated
1387
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
16
Cargo.toml
16
Cargo.toml
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "hole"
|
||||
version = "0.1.0"
|
||||
version = "0.0.1"
|
||||
authors = ["horhik <horhik@tuta.io>"]
|
||||
edition = "2018"
|
||||
|
||||
|
@ -10,3 +10,17 @@ edition = "2018"
|
|||
ntru = "0.5.6"
|
||||
rusqlite = "0.24.0"
|
||||
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"]
|
||||
|
|
7
examples/front_client_request.json
Normal file
7
examples/front_client_request.json
Normal 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)",
|
||||
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
use super::front_conn;
|
||||
|
||||
pub struct Message {
|
||||
sender_id: i32,
|
||||
message: String,
|
||||
|
|
|
@ -1,36 +1,13 @@
|
|||
type Decoded = String;
|
||||
type Encoded = String;
|
||||
|
||||
trait Handler<State> {
|
||||
fn process(code: State) -> Message;
|
||||
fn send(socket: &WebSocketStream<TcpStream>, msg: Message);
|
||||
}
|
||||
struct MessageServer {
|
||||
new_message: bool,
|
||||
text: String,
|
||||
}
|
||||
|
||||
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![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use crate::encrypting;
|
||||
use async_std::{
|
||||
io,
|
||||
net::{TcpListener, TcpStream},
|
||||
task,
|
||||
};
|
||||
use async_tungstenite::{accept_async, tungstenite::Message, WebSocketStream};
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use serde_derive::Deserialize;
|
||||
use std::env;
|
||||
pub fn listen_client() -> io::Result<()> {
|
||||
task::block_on(connect_to_client())
|
||||
}
|
||||
|
@ -45,10 +22,19 @@ async fn connect_to_client() -> io::Result<()> {
|
|||
while let Ok((stream, _)) = listener.accept().await {
|
||||
task::spawn(accept_client(stream));
|
||||
}
|
||||
println!("HEY");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct FrontMsg {
|
||||
userID: u32,
|
||||
receiverID: u32,
|
||||
message: String,
|
||||
time: String,
|
||||
}
|
||||
|
||||
async fn accept_client(stream: TcpStream) -> io::Result<()> {
|
||||
let addr = stream
|
||||
.peer_addr()
|
||||
|
@ -66,14 +52,36 @@ async fn accept_client(stream: TcpStream) -> io::Result<()> {
|
|||
loop {
|
||||
match new_msg.await {
|
||||
Some(msg) => {
|
||||
println!("{:?}", msg.unwrap().into_text().unwrap());
|
||||
sender
|
||||
.send(Message::Text("msg".to_owned()))
|
||||
.await
|
||||
.expect("ooops");
|
||||
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
|
||||
.send(Message::Text(String::from("msg").to_owned()))
|
||||
.await
|
||||
.expect("ooops");
|
||||
} else {
|
||||
println!("seems, that messsage formatted wrong");
|
||||
println!("{:?}", res);
|
||||
}
|
||||
|
||||
new_msg = receiver.next();
|
||||
}
|
||||
None => break,
|
||||
None => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
pub mod chat_api_base;
|
||||
pub mod front_conn;
|
||||
|
|
|
@ -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![],
|
||||
}
|
||||
}
|
||||
}
|
|
@ -67,6 +67,8 @@ pub fn encrypt_message (msg:String, key: &PublicKey ) -> String {
|
|||
return message
|
||||
}
|
||||
|
||||
type CallBack = fn ();
|
||||
|
||||
pub fn decrypt_message(msg: String, kp: &KeyPair, final_msg: &mut String) {
|
||||
let encrypted_message = msg.into_bytes();
|
||||
let decrypted = ntru::decrypt(&encrypted_message, &kp, &DEFAULT_PARAMS_256_BITS);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
pub mod encrypting;
|
||||
pub use encrypting::*;
|
||||
|
||||
pub mod chat;
|
||||
mod chat;
|
||||
pub use chat::front_conn;
|
||||
|
|
78
src/main.rs
78
src/main.rs
|
@ -1,32 +1,52 @@
|
|||
mod cli;
|
||||
mod chat;
|
||||
mod encrypting;
|
||||
use encrypting::{KeyPair, generate_kp, encrypt_message, decrypt_message};
|
||||
use async_std::io;
|
||||
use chat::front_conn::listen_client;
|
||||
|
||||
|
||||
fn main() {
|
||||
/*
|
||||
let (one, two) = keys;
|
||||
|
||||
let value =String::from_utf8_lossy(&*one);
|
||||
let strVal = String::from(value);
|
||||
let newbytes = strVal.into_bytes();
|
||||
print!("{:?}", newbytes);
|
||||
|
||||
let newkey = PrivateKey::import(newbytes);
|
||||
|
||||
let conn = Connection::open("myfile.db").unwrap();
|
||||
|
||||
conn.execute("CREATE TABLE person (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT NOT NULL
|
||||
)", NO_PARAMS).unwrap();
|
||||
let name: String = "Steve Example".to_string();
|
||||
let email: String = "steve@example.org".to_string();
|
||||
conn.execute("INSERT INTO person (name, email) VALUES (?1, ?2)",
|
||||
&[&name, &email]).unwrap();
|
||||
|
||||
*/
|
||||
//let mut std = cli::cli_base::get_stdin();
|
||||
//print!("{}",std)
|
||||
fn main() -> io::Result<()> {
|
||||
listen_client()
|
||||
}
|
||||
/*
|
||||
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 value =String::from_utf8_lossy(&*one);
|
||||
let strVal = String::from(value);
|
||||
let newbytes = strVal.into_bytes();
|
||||
print!("{:?}", newbytes);
|
||||
|
||||
let newkey = PrivateKey::import(newbytes);
|
||||
|
||||
Let conn = Connection::open("myfile.db").unwrap();
|
||||
|
||||
conn.execute("CREATE TABLE person (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT NOT NULL
|
||||
)", NO_PARAMS).unwrap();
|
||||
let name: String = "Steve Example".to_string();
|
||||
let email: String = "steve@example.org".to_string();
|
||||
conn.execute("INSERT INTO person (name, email) VALUES (?1, ?2)",
|
||||
&[&name, &email]).unwrap();
|
||||
|
||||
}
|
||||
*/
|
||||
//let mut std = cli::cli_base::get_stdin();
|
||||
|
|
Loading…
Reference in a new issue