Fucking funkar jag e fett cool
This commit is contained in:
parent
87a5cb8a78
commit
cc36c3dce5
|
@ -1,37 +0,0 @@
|
|||
use ircparser;
|
||||
use pgp::{composed::signed_key::*, Deserializable};
|
||||
use std::sync::mpsc;
|
||||
|
||||
fn initialize_connection(
|
||||
tx: mpsc::Sender<String>,
|
||||
rx: mpsc::Receiver<String>,
|
||||
person: &str,
|
||||
key_string: &str,
|
||||
) -> Result<SignedPublicKey, pgp::errors::Error> {
|
||||
tx.send(format!("PRIVMSG {person} INITIALIZE"))
|
||||
.expect("Closed");
|
||||
|
||||
for line in key_string.split('\n') {
|
||||
tx.send(format!("PRIVMSG {person} {line}")).unwrap();
|
||||
}
|
||||
|
||||
let mut key_lines: Vec<String> = vec![];
|
||||
let mut line: String = String::new();
|
||||
|
||||
while line != "END" {
|
||||
key_lines.push(line);
|
||||
line = match ircparser::parse(&rx.recv().expect("Channel Closed")) {
|
||||
Ok(parsed) => {
|
||||
let line = &parsed[0];
|
||||
|
||||
line.params[1].clone()
|
||||
}
|
||||
Err(e) => {
|
||||
println!("WARN: got invalid line {e}");
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(SignedPublicKey::from_string(&key_lines.join("\n"))?.0)
|
||||
}
|
|
@ -3,8 +3,8 @@ use std::net::TcpListener;
|
|||
use std::sync::mpsc;
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn listen_to_client(tx: mpsc::Sender<String>, rx: mpsc::Receiver<String>) {
|
||||
let listener = TcpListener::bind("127.0.0.1:6666").unwrap();
|
||||
pub fn listen_to_client(tx: mpsc::Sender<String>, rx: mpsc::Receiver<String>, port: &str) {
|
||||
let listener = TcpListener::bind("127.0.0.1:".to_string() + port).unwrap();
|
||||
let mut stream = listener.accept().unwrap().0;
|
||||
|
||||
stream
|
||||
|
|
119
src/main.rs
119
src/main.rs
|
@ -38,9 +38,10 @@ fn bytes_to_privmsg_base64(message: Vec<u8>, reciever: &str) -> String {
|
|||
.chunks(50)
|
||||
.map(|c| c.iter().collect::<String>())
|
||||
{
|
||||
command.push_str(&format!("PRIVMSG {reciever} {line}\n"));
|
||||
command.push_str(&format!("PRIVMSG {reciever} :{line}\r\n"));
|
||||
}
|
||||
|
||||
println!("{}", command);
|
||||
command
|
||||
}
|
||||
|
||||
|
@ -53,22 +54,36 @@ fn encrypt(key: &SignedPublicKey, message: &str) -> Result<Vec<u8>, pgp::errors:
|
|||
Ok(message.to_bytes()?)
|
||||
}
|
||||
|
||||
fn decrypt(key: &SignedSecretKey, message: Vec<u8>, password: &str) -> Result<&str, pgp::errors::Error> {
|
||||
let message = Message::from_bytes(message.as_slice())?;
|
||||
fn decrypt<'a>(
|
||||
key: &'a SignedSecretKey,
|
||||
message: Vec<u8>,
|
||||
password: &'a str,
|
||||
) -> Result<String, pgp::errors::Error> {
|
||||
let message = Message::from_bytes(message.as_slice())?; // Convert message bytes to message object
|
||||
let message = message.decrypt(|| password.to_string(), &[key])?.0; // Decrypt
|
||||
let message = message.map(|x| x.unwrap()).collect::<Vec<Message>>(); // Get all messages
|
||||
let message = &message[0]; // Get first message
|
||||
let message = message.get_content()?.unwrap_or(Vec::new()); // Get message content as Vec<u8>
|
||||
let message = String::from_utf8(message).unwrap(); // Convert to String
|
||||
|
||||
let (message, _) = message.decrypt(|| password.to_string(), &[key])?;
|
||||
Ok(message)
|
||||
}
|
||||
|
||||
Ok(String::from_utf8_lossy(
|
||||
message.get_content()?.unwrap_or(String::new()),
|
||||
))
|
||||
fn get_nick(userstring: &str) -> String {
|
||||
let userstring = userstring.chars().collect::<Vec<char>>();
|
||||
let start_pos = userstring.iter().position(|&x| x == ':').unwrap() + 1;
|
||||
let end_pos = userstring.iter().position(|&x| x == '!').unwrap();
|
||||
userstring[start_pos..end_pos].iter().collect::<String>()
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = env::args().collect::<Vec<String>>();
|
||||
let mut args = env::args().collect::<Vec<String>>();
|
||||
|
||||
let server = args[1].clone();
|
||||
let port = args[2].clone();
|
||||
|
||||
let default_password = String::new();
|
||||
let passwd = args.get(2).unwrap_or(&default_password);
|
||||
let passwd = args.pop().unwrap_or(default_password);
|
||||
|
||||
let stream = TcpStream::connect(format!("{server}:6697"))?;
|
||||
|
||||
|
@ -98,13 +113,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
let (writer_channel_tx, writer_channel_send_rx) = mpsc::channel();
|
||||
let (writer_channel_recv_tx, writer_channel_rx) = mpsc::channel();
|
||||
|
||||
thread::spawn(|| {
|
||||
listener_server::listen_to_client(listener_channel_send_tx, listener_channel_recv_rx)
|
||||
thread::spawn(move || {
|
||||
listener_server::listen_to_client(listener_channel_send_tx, listener_channel_recv_rx, &port)
|
||||
});
|
||||
let tmp_server = server.clone();
|
||||
thread::spawn(|| {
|
||||
writer_client::write_to_server(
|
||||
writer_stream,
|
||||
server,
|
||||
tmp_server,
|
||||
writer_channel_send_rx,
|
||||
writer_channel_recv_tx,
|
||||
)
|
||||
|
@ -116,7 +132,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
loop {
|
||||
let recieved = try_recv!(listener_channel_rx);
|
||||
|
||||
if recieved != "" {
|
||||
if !recieved.is_empty() {
|
||||
let command = &ircparser::parse(&recieved).expect("Got an invalid IRC instruction")[0];
|
||||
|
||||
if command.command == "PRIVMSG" && !command.params[0].starts_with("#") {
|
||||
|
@ -124,10 +140,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
if !keys.contains_key(reciever) {
|
||||
dbg!(recieved);
|
||||
|
||||
writer_channel_tx.send(format!("PRIVMSG {reciever} START_KEY\n"))?;
|
||||
writer_channel_tx.send(format!("PRIVMSG {reciever} :START_KEY\r\n"))?;
|
||||
writer_channel_tx
|
||||
.send(bytes_to_privmsg_base64(public_key.clone(), reciever))?;
|
||||
writer_channel_tx.send(format!("PRIVMSG {reciever} END_KEY\n"))?;
|
||||
writer_channel_tx.send(format!("PRIVMSG {reciever} :END_KEY\r\n"))?;
|
||||
|
||||
let mut foreign_key: Vec<String> = Vec::new();
|
||||
while !foreign_key.contains(&"END_KEY".to_string()) {
|
||||
|
@ -142,13 +158,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
.unwrap_or("".to_string())
|
||||
.starts_with(&begin_source_reciever)
|
||||
{
|
||||
writer_channel_tx.send(recieved_raw)?;
|
||||
listener_channel_tx.send(recieved_raw)?;
|
||||
continue;
|
||||
}
|
||||
|
||||
foreign_key.push(recieved.params[1].clone());
|
||||
foreign_key.push(dbg!(recieved.params[1].clone()));
|
||||
}
|
||||
foreign_key.pop();
|
||||
println!("Got a foreign key from {reciever}");
|
||||
let foreign_key = foreign_key.concat();
|
||||
|
||||
let foreign_key = general_purpose::STANDARD.decode(foreign_key)?;
|
||||
|
@ -160,41 +177,38 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
|
||||
let foreign_key = keys.get(reciever).unwrap();
|
||||
|
||||
writer_channel_tx.send("START_MESSAGE".to_string())?;
|
||||
writer_channel_tx.send(format!("PRIVMSG {reciever} START_MESSAGE\r\n"))?;
|
||||
writer_channel_tx.send(bytes_to_privmsg_base64(
|
||||
encrypt(&foreign_key, &command.params[1])?,
|
||||
reciever,
|
||||
))?;
|
||||
writer_channel_tx.send("END_MESSAGE".to_string())?;
|
||||
writer_channel_tx.send(format!("PRIVMSG {reciever} END_MESSAGE\r\n"))?;
|
||||
} else {
|
||||
dbg!(&recieved);
|
||||
writer_channel_tx.send(recieved)?;
|
||||
writer_channel_tx.send(recieved.replace("127.0.0.1", &server))?;
|
||||
}
|
||||
}
|
||||
|
||||
let recieved = try_recv!(writer_channel_rx);
|
||||
if recieved != "" {
|
||||
if !recieved.is_empty() {
|
||||
dbg!(&recieved);
|
||||
let recieved_parsed = &ircparser::parse(&recieved).unwrap()[0];
|
||||
|
||||
if recieved_parsed.command != "PRIVMSG"
|
||||
|| recieved_parsed
|
||||
.params
|
||||
.get(0)
|
||||
.unwrap_or(&"".to_string())
|
||||
.unwrap_or(&String::new())
|
||||
.starts_with("#")
|
||||
{
|
||||
listener_channel_tx.send(recieved.replace(&server, "127.0.0.1"))?;
|
||||
continue;
|
||||
}
|
||||
|
||||
let reciever = recieved_parsed.source.clone();
|
||||
let reciever = {
|
||||
let full_reciever = reciever.unwrap().chars().collect::<Vec<char>>();
|
||||
let start_pos = full_reciever.iter().position(|&x| x == ':').unwrap() + 1;
|
||||
let end_pos = full_reciever.iter().position(|&x| x == '!').unwrap();
|
||||
full_reciever[start_pos..end_pos].iter().collect::<String>()
|
||||
};
|
||||
|
||||
if recieved_parsed.params[1] == "START_MESSAGE" {
|
||||
let reciever = get_nick(&recieved_parsed.source.clone().unwrap());
|
||||
let sample_privmsg = recieved.clone();
|
||||
|
||||
let mut encrypted_message: Vec<String> = Vec::new();
|
||||
while !encrypted_message.contains(&"END_MESSAGE".to_string()) {
|
||||
let recieved_raw = writer_channel_rx.recv()?;
|
||||
|
@ -208,7 +222,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
.unwrap_or("".to_string())
|
||||
.starts_with(&begin_source_reciever)
|
||||
{
|
||||
writer_channel_tx.send(recieved_raw)?;
|
||||
listener_channel_tx.send(recieved_raw)?;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -218,13 +232,46 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
|
||||
let encrypted_message = encrypted_message.join("");
|
||||
let encrypted_message = general_purpose::STANDARD.decode(encrypted_message)?;
|
||||
let encrypted_message = Message::from_bytes(encrypted_message.as_slice())?;
|
||||
|
||||
let passwd = passwd.clone();
|
||||
let decrypted_message =
|
||||
encrypted_message.decrypt(|| passwd.to_string(), &[&secret_key]);
|
||||
let decrypted_message = decrypt(&secret_key, encrypted_message, &passwd)?;
|
||||
listener_channel_tx
|
||||
.send(sample_privmsg.replace("START_MESSAGE", &decrypted_message))?;
|
||||
} else if recieved_parsed.params[1] == "START_KEY" {
|
||||
let reciever = get_nick(&recieved_parsed.source.clone().unwrap());
|
||||
let mut foreign_key: Vec<String> = Vec::new();
|
||||
while !foreign_key.contains(&"END_KEY".to_string()) {
|
||||
let recieved_raw = writer_channel_rx.recv()?;
|
||||
|
||||
let recieved = &ircparser::parse(&recieved_raw).unwrap()[0];
|
||||
let begin_source_reciever = format!(":{reciever}!");
|
||||
if recieved.command != "PRIVMSG"
|
||||
|| !recieved
|
||||
.source
|
||||
.clone()
|
||||
.unwrap_or("".to_string())
|
||||
.starts_with(&begin_source_reciever)
|
||||
{
|
||||
listener_channel_tx.send(recieved_raw)?;
|
||||
continue;
|
||||
}
|
||||
|
||||
foreign_key.push(recieved.params[1].clone());
|
||||
}
|
||||
foreign_key.pop();
|
||||
println!("Got a foreign key from {reciever}");
|
||||
let foreign_key = foreign_key.concat();
|
||||
|
||||
let foreign_key = general_purpose::STANDARD.decode(foreign_key)?;
|
||||
|
||||
let key = SignedPublicKey::from_bytes(foreign_key.as_slice())?;
|
||||
|
||||
keys.insert(reciever.to_string(), key);
|
||||
|
||||
let reciever = get_nick(&recieved_parsed.source.clone().unwrap());
|
||||
let to_send = bytes_to_privmsg_base64(public_key.clone(), &reciever);
|
||||
writer_channel_tx.send(to_send)?;
|
||||
writer_channel_tx.send(format!("PRIVMSG {reciever} END_KEY\r\n"))?;
|
||||
}
|
||||
listener_channel_tx.send(recieved)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
use ircparser;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
struct MessageStream<'a, StreamType: Write + Read + ?Sized> {
|
||||
stream: Box<StreamType>,
|
||||
reciver: &'a str,
|
||||
sender: &'a str,
|
||||
}
|
||||
|
||||
trait Stream: Write + Read {}
|
||||
|
||||
impl Write for MessageStream<'_, dyn Stream> {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
let to_send = "PRIVMSG".to_owned() + self.sender + &String::from_utf8_lossy(buf) + "\n";
|
||||
self.stream.write(to_send.as_bytes())?;
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
self.stream.flush()
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for MessageStream<'_, dyn Stream> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
let mut readed = String::new();
|
||||
let _ = self.stream.read_to_string(&mut readed);
|
||||
let commands = ircparser::parse(&readed).unwrap();
|
||||
|
||||
let mut messages: Vec<String> = vec![];
|
||||
|
||||
for command in commands {
|
||||
let command_string = &command.command;
|
||||
let command_reciver = &command.params[0];
|
||||
let command_sender = command.source.unwrap_or("".to_string());
|
||||
if command_string == "PRIVMSG"
|
||||
&& command_reciver == self.sender
|
||||
&& command_sender.starts_with(&format!(":{}", self.reciver))
|
||||
{
|
||||
messages.push(command.params[1].clone());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(messages.join("\n").as_bytes().read(buf)?)
|
||||
}
|
||||
}
|
|
@ -10,15 +10,15 @@ pub fn write_to_server(
|
|||
rx: mpsc::Receiver<String>,
|
||||
tx: mpsc::Sender<String>,
|
||||
) {
|
||||
tcp_stream
|
||||
.set_read_timeout(Some(Duration::from_millis(10)))
|
||||
.expect("Couldn't set timeout!!");
|
||||
|
||||
let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
|
||||
let mut stream = connector
|
||||
.connect(&server, &tcp_stream)
|
||||
.expect("Couldn't start TLS");
|
||||
|
||||
tcp_stream
|
||||
.set_read_timeout(Some(Duration::from_millis(10)))
|
||||
.expect("Couldn't set timeout!!");
|
||||
|
||||
loop {
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
let mut buf: [u8; 1] = [0];
|
||||
|
@ -31,6 +31,7 @@ pub fn write_to_server(
|
|||
}
|
||||
Err(_error) => match _error.io_error() {
|
||||
None => {
|
||||
dbg!(_error);
|
||||
panic!("TLS Error")
|
||||
}
|
||||
Some(error) => match error.kind() {
|
||||
|
|
Loading…
Reference in a new issue