2021-02-27 21:58:55 +00:00
|
|
|
use super::serv_handler::to_server_sender;
|
|
|
|
use crate::chat::types::{PackedMessage, RP, SP};
|
2021-01-06 09:41:33 +00:00
|
|
|
use async_std::task;
|
2021-04-20 19:23:04 +00:00
|
|
|
use fcpv2::types::traits::FcpParser;
|
2021-04-22 02:02:20 +00:00
|
|
|
use regex::Regex;
|
2021-01-06 04:18:46 +00:00
|
|
|
use serde_derive::Deserialize;
|
|
|
|
use std::env;
|
2021-01-06 09:41:33 +00:00
|
|
|
use tokio::{
|
2021-02-27 21:58:55 +00:00
|
|
|
io::{self, AsyncReadExt},
|
|
|
|
net::{tcp::OwnedReadHalf, TcpStream},
|
2021-01-06 09:41:33 +00:00
|
|
|
};
|
2021-01-06 04:18:46 +00:00
|
|
|
|
2021-01-06 09:41:33 +00:00
|
|
|
#[tokio::main]
|
2021-01-09 14:03:00 +00:00
|
|
|
pub async fn listen_server(client_sender: SP, server_receiver: RP) -> io::Result<()> {
|
|
|
|
task::block_on(connect_to_server(client_sender, server_receiver))
|
2021-01-06 04:18:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-09 14:03:00 +00:00
|
|
|
async fn connect_to_server(client_sender: SP, server_receiver: RP) -> io::Result<()> {
|
2021-01-06 04:18:46 +00:00
|
|
|
let addr = env::args()
|
|
|
|
.nth(1)
|
|
|
|
.unwrap_or_else(|| "127.0.0.1:9481".to_string());
|
|
|
|
|
2021-03-27 06:09:59 +00:00
|
|
|
let sr = client_sender.clone();
|
2021-04-18 15:35:14 +00:00
|
|
|
let stream = TcpStream::connect(&addr)
|
|
|
|
.await
|
|
|
|
.expect("Unable to connect to FCP");
|
|
|
|
let (receiver, sender) = stream.into_split();
|
2021-04-05 16:39:36 +00:00
|
|
|
log::info!("Connected to FCP");
|
2021-01-09 14:03:00 +00:00
|
|
|
let t = task::spawn(server_responce_getter(receiver, client_sender));
|
2021-03-26 20:25:30 +00:00
|
|
|
to_server_sender(sender, server_receiver, sr).await?;
|
2021-01-09 14:03:00 +00:00
|
|
|
match t.await {
|
|
|
|
Ok(_) => Ok(()),
|
2021-01-22 16:19:40 +00:00
|
|
|
Err(e) => Err(e),
|
2021-01-09 14:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-22 16:19:40 +00:00
|
|
|
async fn server_responce_getter(mut receiver: OwnedReadHalf, client_sender: SP) -> io::Result<()> {
|
2021-04-18 15:35:14 +00:00
|
|
|
// let mut prev = [0; 1024];
|
2021-01-22 16:19:40 +00:00
|
|
|
loop {
|
2021-03-31 04:12:26 +00:00
|
|
|
// each freenet responce have an identifier and program will define what to do with request by this identifier
|
|
|
|
//TODO create handle_fcp_response function
|
2021-03-27 06:09:59 +00:00
|
|
|
let mut buffer = [0; 1024];
|
2021-01-06 09:41:33 +00:00
|
|
|
match receiver.read(&mut buffer).await {
|
2021-01-09 14:03:00 +00:00
|
|
|
Ok(_) => {
|
2021-04-18 15:35:14 +00:00
|
|
|
// if prev != buffer {
|
2021-04-20 19:23:04 +00:00
|
|
|
|
|
|
|
let received = String::from_utf8_lossy(&buffer[..]);
|
|
|
|
|
|
|
|
let req = received.lines().next().unwrap();
|
|
|
|
|
|
|
|
match req {
|
|
|
|
"SSKKeypair" => {
|
|
|
|
log::debug!("parsing keypair: {:?}", &req);
|
|
|
|
let keypair = fcpv2::types::SSKKeypair::parse(&received).unwrap();
|
2021-04-22 02:02:20 +00:00
|
|
|
/*
|
|
|
|
let id_regex = Regex::new(r"^(\w*-\w*)").unwrap();
|
|
|
|
let identifier_type: String = id_regex
|
|
|
|
.captures(&keypair.identifier[..])
|
|
|
|
.expect("wrong type identifier")[0].to_string();
|
|
|
|
*/
|
2021-04-20 19:23:04 +00:00
|
|
|
match &keypair.identifier[..] {
|
|
|
|
"config-SSK" => {
|
|
|
|
log::debug!("got SSKKeypair: {:?}", &keypair);
|
|
|
|
// TODO generating UUID and inserting it into .hole.toml
|
|
|
|
let id = uuid::Uuid::new_v4();
|
|
|
|
let conf = crate::chat::Config {
|
|
|
|
id: crate::db::types::Id(id),
|
|
|
|
public_key: keypair.insert_uri.clone(),
|
|
|
|
private_key: keypair.request_uri.clone(),
|
|
|
|
};
|
|
|
|
let config_str = toml::to_string(&conf).unwrap();
|
|
|
|
|
|
|
|
log::debug!("create toml config");
|
|
|
|
let config_json =
|
|
|
|
crate::api::response::ResponseType::InitialConfig {
|
|
|
|
id: crate::db::types::Id(id),
|
|
|
|
public_key: keypair.insert_uri.clone(),
|
|
|
|
private_key: keypair.request_uri.clone(),
|
|
|
|
};
|
|
|
|
log::debug!("create json config");
|
|
|
|
let config_path = std::path::Path::new(".hole.toml");
|
|
|
|
// writing new data to .hole.toml
|
|
|
|
crate::chat::update_config(&config_path, &config_str).unwrap();
|
|
|
|
log::debug!("rewrite config");
|
|
|
|
client_sender
|
|
|
|
.send(PackedMessage::ToClient(
|
|
|
|
serde_json::to_string(&config_json).unwrap(),
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
log::debug!("sended config to client");
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2021-04-22 02:02:20 +00:00
|
|
|
},
|
2021-04-27 21:03:20 +00:00
|
|
|
"aDataFound" => {
|
|
|
|
log::debug!("Receive a new message!!! {:?}", &received);
|
|
|
|
let message = fcpv2::node::fcp_response::AllData::parse(&received[..]).unwrap();
|
|
|
|
log::debug!("Parse new message!!!! {:?}", &message);
|
|
|
|
},
|
|
|
|
"AllData" => {
|
|
|
|
log::debug!("Receive a new message!!! {:?}", &received);
|
|
|
|
let message = fcpv2::node::fcp_response::AllData::parse(&received[..]).unwrap();
|
|
|
|
log::debug!("Parse new message!!!! {:?}", &message);
|
2021-04-22 02:02:20 +00:00
|
|
|
},
|
2021-04-20 19:23:04 +00:00
|
|
|
_ => {
|
|
|
|
log::debug!("unhandled: {}", &req);
|
|
|
|
client_sender
|
|
|
|
.send(PackedMessage::FromFreenet(received.to_string()))
|
|
|
|
.expect("Falied to send message to client thread");
|
|
|
|
log::info!("Sended to client! {}", received.chars().count());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// prev = buffer;
|
2021-04-18 15:35:14 +00:00
|
|
|
// }
|
2021-01-06 09:41:33 +00:00
|
|
|
}
|
2021-03-24 07:32:31 +00:00
|
|
|
Err(e) => log::error!("Error: {} ", e),
|
2021-01-06 09:41:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-06 04:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
struct FrontMsg {
|
2021-01-09 14:03:00 +00:00
|
|
|
user_id: u32,
|
|
|
|
receiver_id: u32,
|
2021-01-06 04:18:46 +00:00
|
|
|
message: String,
|
|
|
|
time: String,
|
|
|
|
}
|