Compare commits
25 commits
Author | SHA1 | Date | |
---|---|---|---|
vanten-s | e4fd3224cf | ||
vanten-s | 5c24ae5f96 | ||
vanten-s | c895210cea | ||
vanten-s | 4aed403c97 | ||
vanten-s | 247f35994b | ||
vanten-s | 6eeeb4a2e8 | ||
vanten-s | 2783cc9ca8 | ||
vanten-s | bdb14a5b6d | ||
vanten-s | 805caebe35 | ||
vanten-s | b9af753fc7 | ||
vanten-s | e26db6d68b | ||
vanten-s | 7c5bc94ade | ||
vanten-s | d38da05d05 | ||
vanten-s | 83b10eb3d5 | ||
vanten-s | b480ce09e5 | ||
vanten-s | e19e3b0d25 | ||
vanten-s | d185ceb7c0 | ||
vanten-s | 0a7b931bb9 | ||
vanten-s | cb87ebe318 | ||
vanten-s | 620ad761fd | ||
vanten-s | ca79a1e513 | ||
vanten-s | e83cf5bfb5 | ||
vanten-s | 72945e8b94 | ||
vanten-s | 6626110758 | ||
vanten-s | 34b2e0e5a9 |
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,2 @@
|
||||||
/target
|
/target
|
||||||
secret.gpg
|
Cargo.lock
|
||||||
public.gpg
|
|
||||||
|
|
1399
Cargo.lock
generated
1399
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
13
Cargo.toml
13
Cargo.toml
|
@ -1,14 +1,21 @@
|
||||||
[package]
|
[package]
|
||||||
name = "irc-e2e"
|
name = "e2e-irc"
|
||||||
version = "0.1.0"
|
version = "3.0.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
license = "GPL-3.0"
|
||||||
|
keywords = ["irc", "encryption"]
|
||||||
|
description = "An IRC bouncer that can send encrypted messages"
|
||||||
|
repository = "https://forgejo.vanten-s.com/vanten-s/e2e-irc/"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
argparse = "0.2.2"
|
||||||
base64 = "0.21.4"
|
base64 = "0.21.4"
|
||||||
|
dirs = "5.0.1"
|
||||||
eyre = "0.6.8"
|
eyre = "0.6.8"
|
||||||
ircparser = "0.2.1"
|
ircparser-vanten = "0.2.1"
|
||||||
openssl = "0.10"
|
openssl = "0.10"
|
||||||
pgp = "0.10.2"
|
pgp = "0.10.2"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
toml = "0.8.2"
|
||||||
|
|
35
README.md
Normal file
35
README.md
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
# e2e-irc
|
||||||
|
This is an IRC bouncer that supports end-to-end encryption and is horribly written.
|
||||||
|
|
||||||
|
# Configuration
|
||||||
|
e2e-irc uses a config file in `~/.config/e2e-irc/config.toml` on linux.
|
||||||
|
The configuration file is a toml file with two or three fields:
|
||||||
|
- public_key the location of the public pgp key
|
||||||
|
- secret_key the location of the secret pgp key
|
||||||
|
- passwd the password of the pgp key
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
```
|
||||||
|
e2e-irc [OPTIONS] SERVER
|
||||||
|
|
||||||
|
Encrypted IRC Bouncer
|
||||||
|
|
||||||
|
Positional arguments:
|
||||||
|
server The Address Of The Server The Bouncer Connects To
|
||||||
|
|
||||||
|
Optional arguments:
|
||||||
|
-h,--help Show this help message and exit
|
||||||
|
-p,--port PORT The Port The Bouncer Binds To
|
||||||
|
--sp,--server-port SERVER_PORT
|
||||||
|
The TLS Enabled Port Of The Server
|
||||||
|
```
|
||||||
|
|
||||||
|
# Install
|
||||||
|
```bash
|
||||||
|
cargo install e2e-irc
|
||||||
|
```
|
||||||
|
|
||||||
|
# Run
|
||||||
|
```bash
|
||||||
|
e2e-irc [OPTIONS] SERVER
|
||||||
|
```
|
|
@ -1,9 +1,40 @@
|
||||||
use crate::helpers::bytes_to_privmsg_base64;
|
use crate::helpers::bytes_to_privmsg_base64;
|
||||||
use crate::{encryption, helpers};
|
use crate::{encryption, helpers, State};
|
||||||
|
use eyre::Result;
|
||||||
use pgp::{Deserializable, SignedPublicKey};
|
use pgp::{Deserializable, SignedPublicKey};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::mpsc::{Receiver, Sender};
|
use std::sync::mpsc::{Receiver, Sender};
|
||||||
use eyre::Result;
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct InvalidCommand;
|
||||||
|
|
||||||
|
impl std::fmt::Display for InvalidCommand {
|
||||||
|
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for InvalidCommand {}
|
||||||
|
|
||||||
|
fn parse_bouncer_command(message: String, state: &mut State) -> Result<()> {
|
||||||
|
macro_rules! unwrap_option {
|
||||||
|
($t:expr) => {
|
||||||
|
match $t {
|
||||||
|
Some(val) => val,
|
||||||
|
None => return Err(InvalidCommand.into()),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut splitted = message.split(' ');
|
||||||
|
match unwrap_option!(splitted.next()) {
|
||||||
|
"ALLOW_UNENCRYPTED" => state
|
||||||
|
.nicks_without_encryption
|
||||||
|
.push(unwrap_option!(splitted.next()).to_string().to_lowercase()),
|
||||||
|
_ => return Err(InvalidCommand.into()),
|
||||||
|
};
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn handle_message_from_client(
|
pub fn handle_message_from_client(
|
||||||
recieved: &str,
|
recieved: &str,
|
||||||
|
@ -14,10 +45,34 @@ pub fn handle_message_from_client(
|
||||||
writer_channel_rx: &Receiver<String>,
|
writer_channel_rx: &Receiver<String>,
|
||||||
listener_channel_tx: &Sender<String>,
|
listener_channel_tx: &Sender<String>,
|
||||||
_listener_channel_rx: &Receiver<String>,
|
_listener_channel_rx: &Receiver<String>,
|
||||||
|
state: &mut State,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let command = &ircparser::parse(recieved).expect("Got an invalid IRC instruction")[0];
|
let mut recieved = recieved.to_string();
|
||||||
|
|
||||||
if command.command == "PRIVMSG" && !command.params[0].starts_with("#") {
|
if recieved.split(' ').count() == 1 {
|
||||||
|
recieved += " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
let parsed = ircparser::parse(&recieved);
|
||||||
|
let command = match parsed {
|
||||||
|
Ok(val) => val[0].clone(),
|
||||||
|
Err(_) => {
|
||||||
|
writer_channel_tx.send(recieved)?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if command.command == "PRIVMSG" && !command.params[0].starts_with('#') {
|
||||||
|
if command.params[0] == "BOUNCER" {
|
||||||
|
return parse_bouncer_command(command.params[1].clone(), state);
|
||||||
|
}
|
||||||
|
if state
|
||||||
|
.nicks_without_encryption
|
||||||
|
.contains(&command.params[0].to_lowercase())
|
||||||
|
{
|
||||||
|
writer_channel_tx.send(recieved)?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
let other = &command.params[0];
|
let other = &command.params[0];
|
||||||
|
|
||||||
if !keys.contains_key(other) {
|
if !keys.contains_key(other) {
|
||||||
|
@ -27,7 +82,7 @@ pub fn handle_message_from_client(
|
||||||
listener_channel_tx,
|
listener_channel_tx,
|
||||||
"127.0.0.1",
|
"127.0.0.1",
|
||||||
server,
|
server,
|
||||||
&other,
|
other,
|
||||||
"END_KEY",
|
"END_KEY",
|
||||||
)?;
|
)?;
|
||||||
let key = SignedPublicKey::from_bytes(key.as_slice())?;
|
let key = SignedPublicKey::from_bytes(key.as_slice())?;
|
||||||
|
@ -38,12 +93,12 @@ pub fn handle_message_from_client(
|
||||||
|
|
||||||
writer_channel_tx.send(format!("PRIVMSG {other} START_MESSAGE\r\n"))?;
|
writer_channel_tx.send(format!("PRIVMSG {other} START_MESSAGE\r\n"))?;
|
||||||
writer_channel_tx.send(bytes_to_privmsg_base64(
|
writer_channel_tx.send(bytes_to_privmsg_base64(
|
||||||
&encryption::encrypt(&foreign_key, &command.params[1])?,
|
&encryption::encrypt(foreign_key, &command.params[1])?,
|
||||||
other,
|
other,
|
||||||
))?;
|
))?;
|
||||||
writer_channel_tx.send(format!("PRIVMSG {other} END_MESSAGE\r\n"))?;
|
writer_channel_tx.send(format!("PRIVMSG {other} END_MESSAGE\r\n"))?;
|
||||||
} else {
|
} else {
|
||||||
writer_channel_tx.send(recieved.replace("127.0.0.1", &server))?;
|
writer_channel_tx.send(recieved.replace("127.0.0.1", server))?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
|
use eyre::Result;
|
||||||
use pgp::crypto::sym::SymmetricKeyAlgorithm;
|
use pgp::crypto::sym::SymmetricKeyAlgorithm;
|
||||||
use pgp::ser::Serialize;
|
use pgp::ser::Serialize;
|
||||||
use pgp::{Deserializable, Message, SignedPublicKey, SignedSecretKey};
|
use pgp::{Deserializable, Message, SignedPublicKey, SignedSecretKey};
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use eyre::Result;
|
|
||||||
|
|
||||||
pub fn encrypt(key: &SignedPublicKey, message: &str) -> Result<Vec<u8>, pgp::errors::Error> {
|
pub fn encrypt(key: &SignedPublicKey, message: &str) -> Result<Vec<u8>, pgp::errors::Error> {
|
||||||
let message = Message::new_literal("none", message);
|
let message = Message::new_literal("none", message);
|
||||||
|
@ -10,15 +10,15 @@ pub fn encrypt(key: &SignedPublicKey, message: &str) -> Result<Vec<u8>, pgp::err
|
||||||
|
|
||||||
let message = message.encrypt_to_keys(&mut rng, SymmetricKeyAlgorithm::AES128, &[key])?;
|
let message = message.encrypt_to_keys(&mut rng, SymmetricKeyAlgorithm::AES128, &[key])?;
|
||||||
|
|
||||||
Ok(message.to_bytes()?)
|
message.to_bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decrypt<'a>(
|
pub fn decrypt<'a>(
|
||||||
key: &'a SignedSecretKey,
|
key: &'a SignedSecretKey,
|
||||||
message: Vec<u8>,
|
message: &[u8],
|
||||||
password: &'a str,
|
password: &'a str,
|
||||||
) -> Result<String, pgp::errors::Error> {
|
) -> Result<String, pgp::errors::Error> {
|
||||||
let message = Message::from_bytes(message.as_slice())?; // Convert message bytes to message object
|
let message = Message::from_bytes(message)?; // Convert message bytes to message object
|
||||||
let message = message.decrypt(|| password.to_string(), &[key])?.0; // Decrypt
|
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.map(|x| x.unwrap()).collect::<Vec<Message>>(); // Get all messages
|
||||||
let message = &message[0]; // Get first message
|
let message = &message[0]; // Get first message
|
||||||
|
|
|
@ -1,20 +1,69 @@
|
||||||
use base64::{engine::general_purpose, Engine as _};
|
use base64::{engine::general_purpose, Engine as _};
|
||||||
use std::sync::mpsc::{self, Receiver, Sender};
|
|
||||||
use eyre::Result;
|
use eyre::Result;
|
||||||
use ircparser;
|
use std::sync::mpsc::{self, Receiver, Sender};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct IrcParseError;
|
||||||
|
|
||||||
|
impl std::fmt::Display for IrcParseError {
|
||||||
|
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for IrcParseError {}
|
||||||
|
|
||||||
static MAX_LENGTH: usize = 300;
|
static MAX_LENGTH: usize = 300;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! unwrap_or_return_result {
|
||||||
|
($e:expr) => {
|
||||||
|
match $e {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(_) => return Ok(()),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! unwrap_or_return_option {
|
||||||
|
($e:expr) => {
|
||||||
|
match $e {
|
||||||
|
Some(val) => val,
|
||||||
|
None => return Ok(()),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct State {
|
||||||
|
pub nicks_without_encryption: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
State {
|
||||||
|
nicks_without_encryption: vec![
|
||||||
|
"nickserv".to_string(),
|
||||||
|
"chanserv".to_string(),
|
||||||
|
"hostserv".to_string(),
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn forward(
|
fn forward(
|
||||||
message: &str,
|
message: String,
|
||||||
stream: &Sender<String>,
|
stream: &Sender<String>,
|
||||||
server_local: &str,
|
server_local: &str,
|
||||||
server_forward: &str,
|
server_forward: &str,
|
||||||
) -> Result<(), mpsc::SendError<String>> {
|
) -> Result<(), mpsc::SendError<String>> {
|
||||||
if ircparser::parse(message).unwrap()[0].command == "PRIVMSG" {
|
match ircparser::parse(&message) {
|
||||||
return Ok(())
|
Ok(val) => match val[0].command.as_str() {
|
||||||
|
"PRIVMSG" => stream.send(message),
|
||||||
|
_ => stream.send(message.replace(server_local, server_forward)),
|
||||||
|
},
|
||||||
|
Err(_) => stream.send(message.replace(server_local, server_forward)),
|
||||||
}
|
}
|
||||||
stream.send(message.replace(&server_local, server_forward))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bytes_to_privmsg_base64(message: &Vec<u8>, reciever: &str) -> String {
|
pub fn bytes_to_privmsg_base64(message: &Vec<u8>, reciever: &str) -> String {
|
||||||
|
@ -37,11 +86,7 @@ pub fn bytes_to_privmsg_base64(message: &Vec<u8>, reciever: &str) -> String {
|
||||||
command
|
command
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_key(
|
pub fn send_key(sender: &Sender<String>, reciever: &str, key: &Vec<u8>) -> Result<()> {
|
||||||
sender: &Sender<String>,
|
|
||||||
reciever: &str,
|
|
||||||
key: &Vec<u8>,
|
|
||||||
) -> Result<()> {
|
|
||||||
sender.send(format!("PRIVMSG {reciever} :START_KEY\r\n"))?;
|
sender.send(format!("PRIVMSG {reciever} :START_KEY\r\n"))?;
|
||||||
sender.send(bytes_to_privmsg_base64(key, reciever))?;
|
sender.send(bytes_to_privmsg_base64(key, reciever))?;
|
||||||
sender.send(format!("PRIVMSG {reciever} :END_KEY\r\n"))?;
|
sender.send(format!("PRIVMSG {reciever} :END_KEY\r\n"))?;
|
||||||
|
@ -49,11 +94,11 @@ pub fn send_key(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_nick(userstring: &str) -> String {
|
pub fn get_nick(userstring: &str) -> Option<String> {
|
||||||
let userstring = userstring.chars().collect::<Vec<char>>();
|
let userstring = userstring.chars().collect::<Vec<char>>();
|
||||||
let start_pos = userstring.iter().position(|&x| x == ':').unwrap() + 1;
|
let start_pos = userstring.iter().position(|&x| x == ':')? + 1;
|
||||||
let end_pos = userstring.iter().position(|&x| x == '!').unwrap();
|
let end_pos = userstring.iter().position(|&x| x == '!')?;
|
||||||
userstring[start_pos..end_pos].iter().collect::<String>()
|
Some(userstring[start_pos..end_pos].iter().collect::<String>())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn recieve_message_base64(
|
pub fn recieve_message_base64(
|
||||||
|
@ -69,16 +114,23 @@ pub fn recieve_message_base64(
|
||||||
while !message.contains(&end.to_string()) {
|
while !message.contains(&end.to_string()) {
|
||||||
let recieved_raw = writer_channel_rx.recv()?;
|
let recieved_raw = writer_channel_rx.recv()?;
|
||||||
|
|
||||||
let recieved = &ircparser::parse(&recieved_raw).unwrap()[0];
|
let parse_result = ircparser::parse(&recieved_raw);
|
||||||
|
|
||||||
|
let recieved = match parse_result {
|
||||||
|
Ok(mut val) => val.pop_back().unwrap(),
|
||||||
|
Err(_) => return Err(IrcParseError.into()),
|
||||||
|
};
|
||||||
|
|
||||||
let begin_source_reciever = format!(":{sender}!");
|
let begin_source_reciever = format!(":{sender}!");
|
||||||
if recieved.command != "PRIVMSG"
|
if recieved.command != "PRIVMSG"
|
||||||
|| !recieved
|
|| !recieved
|
||||||
.source
|
.source
|
||||||
.clone()
|
.clone()
|
||||||
.unwrap_or("".to_string())
|
.unwrap_or("".to_string())
|
||||||
.starts_with(&begin_source_reciever) || recieved.params[0].starts_with("#")
|
.starts_with(&begin_source_reciever)
|
||||||
|
|| recieved.params[0].starts_with('#')
|
||||||
{
|
{
|
||||||
forward(&recieved_raw, forward_stream, server_local, server_forward)?;
|
forward(recieved_raw, forward_stream, server_local, server_forward)?;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,10 @@
|
||||||
use std::io::{ErrorKind, Read, Write};
|
use std::io::{ErrorKind, Read, Write};
|
||||||
use std::net::TcpListener;
|
use std::net::{TcpListener, TcpStream};
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc::{self, TryRecvError};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
pub fn listen_to_client(tx: mpsc::Sender<String>, rx: mpsc::Receiver<String>, port: &str) {
|
fn stream_handler(tx: &mpsc::Sender<String>, rx: &mpsc::Receiver<String>, mut stream: TcpStream) {
|
||||||
let listener = TcpListener::bind("127.0.0.1:".to_string() + port).unwrap();
|
|
||||||
let mut stream = listener.accept().unwrap().0;
|
|
||||||
|
|
||||||
stream
|
|
||||||
.set_nonblocking(true)
|
|
||||||
.expect("Couldn't set nonblocking");
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut buffer: Vec<u8> = Vec::new();
|
let mut buffer: Vec<u8> = Vec::new();
|
||||||
let mut buf: [u8; 1] = [0];
|
let mut buf: [u8; 1] = [0];
|
||||||
|
@ -24,6 +17,7 @@ pub fn listen_to_client(tx: mpsc::Sender<String>, rx: mpsc::Receiver<String>, po
|
||||||
ErrorKind::WouldBlock => {}
|
ErrorKind::WouldBlock => {}
|
||||||
_ => {
|
_ => {
|
||||||
dbg!(_error);
|
dbg!(_error);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -31,14 +25,35 @@ pub fn listen_to_client(tx: mpsc::Sender<String>, rx: mpsc::Receiver<String>, po
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
match stream.write_all(value.as_bytes()) {
|
match stream.write_all(value.as_bytes()) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(_e) => println!("Couldn't send {value}"),
|
Err(_e) => {
|
||||||
|
dbg!(_e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Err(_e) => {}
|
Err(TryRecvError::Empty) => {}
|
||||||
|
Err(TryRecvError::Disconnected) => return,
|
||||||
}
|
}
|
||||||
|
thread::sleep(Duration::from_micros(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = tx.send(String::from_utf8_lossy(&buffer).to_string());
|
let _ = tx.send(dbg!(String::from_utf8_lossy(&buffer).to_string()));
|
||||||
thread::sleep(Duration::from_millis(100));
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn listen_to_client(tx: mpsc::Sender<String>, rx: mpsc::Receiver<String>, port: String) {
|
||||||
|
let listener = TcpListener::bind("127.0.0.1:".to_string() + &port)
|
||||||
|
.expect(&("Couldn't start listener on 127.0.0.1 port ".to_string() + &port));
|
||||||
|
loop {
|
||||||
|
let (stream, ip) = listener.accept().unwrap();
|
||||||
|
println!("Got connection from {ip}");
|
||||||
|
|
||||||
|
stream
|
||||||
|
.set_nonblocking(true)
|
||||||
|
.expect("Couldn't set nonblocking");
|
||||||
|
|
||||||
|
stream_handler(&tx, &rx, stream);
|
||||||
|
println!("Closed connection with {ip}");
|
||||||
|
let _ = tx.send("DUMMY CLOSE_CONNECTION".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
118
src/main.rs
118
src/main.rs
|
@ -1,11 +1,14 @@
|
||||||
|
use argparse::{ArgumentParser, Store};
|
||||||
|
use dirs::config_local_dir;
|
||||||
|
use eyre::Result;
|
||||||
|
use helpers::State;
|
||||||
use pgp::{Deserializable, SignedPublicKey, SignedSecretKey};
|
use pgp::{Deserializable, SignedPublicKey, SignedSecretKey};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::{Shutdown, TcpStream};
|
use std::fs;
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{env, fs};
|
use toml::Table;
|
||||||
use eyre::Result;
|
|
||||||
|
|
||||||
mod client_handler;
|
mod client_handler;
|
||||||
mod encryption;
|
mod encryption;
|
||||||
|
@ -15,35 +18,61 @@ mod server_handler;
|
||||||
mod writer_client;
|
mod writer_client;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut args = env::args().collect::<Vec<String>>();
|
let config_file = config_local_dir()
|
||||||
|
.expect("Couldn't get config directory")
|
||||||
|
.join("e2e-irc/config.toml");
|
||||||
|
|
||||||
let server = args[1].clone();
|
if !config_file.exists() {
|
||||||
let port = args[2].clone();
|
panic!("Create a config file at {}", config_file.display());
|
||||||
|
|
||||||
let default_password = String::new();
|
|
||||||
let passwd = args.pop().unwrap_or(default_password);
|
|
||||||
|
|
||||||
let stream = TcpStream::connect(format!("{server}:6697"))?;
|
|
||||||
|
|
||||||
let public_key = fs::read("public.gpg")?;
|
|
||||||
let secret_key = SignedSecretKey::from_bytes(fs::read("secret.gpg")?.as_slice())?;
|
|
||||||
|
|
||||||
let reader_stream = match stream.try_clone() {
|
|
||||||
Ok(stream) => stream,
|
|
||||||
Err(_error) => {
|
|
||||||
let _ = stream.shutdown(Shutdown::Both);
|
|
||||||
panic!("Failed to create the reader stream")
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
let writer_stream = match stream.try_clone() {
|
let parsed_config = String::from_utf8_lossy(&fs::read(config_file)?).parse::<Table>()?;
|
||||||
Ok(stream) => stream,
|
|
||||||
Err(_error) => {
|
let public_key_location = parsed_config
|
||||||
let _ = stream.shutdown(Shutdown::Both);
|
.get("public_key")
|
||||||
let _ = reader_stream.shutdown(Shutdown::Both);
|
.expect("Coudln't get public_key. Try creating it in the config")
|
||||||
panic!("Failed to create the writer stream")
|
.as_str()
|
||||||
|
.expect("Couldn't convert public_key to str");
|
||||||
|
let secret_key_location = parsed_config
|
||||||
|
.get("secret_key")
|
||||||
|
.expect("Coudln't get secret_key. Try creating it in the config")
|
||||||
|
.as_str()
|
||||||
|
.expect("Couldn't convert secret_key to str");
|
||||||
|
|
||||||
|
let default_password = toml::Value::String(String::new());
|
||||||
|
|
||||||
|
let passwd = parsed_config
|
||||||
|
.get("passwd")
|
||||||
|
.unwrap_or(&default_password)
|
||||||
|
.as_str()
|
||||||
|
.expect("Coudln't convert passwd to str");
|
||||||
|
|
||||||
|
let mut server = "irc.vanten-s.com".to_string();
|
||||||
|
let mut port = "6666".to_string();
|
||||||
|
let mut server_port = "6697".to_string();
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut ap = ArgumentParser::new();
|
||||||
|
ap.set_description("Encrypted IRC Bouncer");
|
||||||
|
ap.refer(&mut server)
|
||||||
|
.add_argument(
|
||||||
|
"server",
|
||||||
|
Store,
|
||||||
|
"The Address Of The Server The Bouncer Connects To",
|
||||||
|
)
|
||||||
|
.required();
|
||||||
|
ap.refer(&mut port)
|
||||||
|
.add_option(&["-p", "--port"], Store, "The Port The Bouncer Binds To");
|
||||||
|
ap.refer(&mut server_port).add_option(
|
||||||
|
&["--sp", "--server-port"],
|
||||||
|
Store,
|
||||||
|
"The TLS Enabled Port Of The Server",
|
||||||
|
);
|
||||||
|
ap.parse_args_or_exit();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
let public_key = fs::read(public_key_location)?;
|
||||||
|
let secret_key = SignedSecretKey::from_bytes(fs::read(secret_key_location)?.as_slice())?;
|
||||||
|
|
||||||
let (listener_channel_send_tx, listener_channel_rx) = mpsc::channel();
|
let (listener_channel_send_tx, listener_channel_rx) = mpsc::channel();
|
||||||
let (listener_channel_tx, listener_channel_recv_rx) = mpsc::channel();
|
let (listener_channel_tx, listener_channel_recv_rx) = mpsc::channel();
|
||||||
|
@ -51,24 +80,32 @@ fn main() -> Result<()> {
|
||||||
let (writer_channel_tx, writer_channel_send_rx) = mpsc::channel();
|
let (writer_channel_tx, writer_channel_send_rx) = mpsc::channel();
|
||||||
let (writer_channel_recv_tx, writer_channel_rx) = mpsc::channel();
|
let (writer_channel_recv_tx, writer_channel_rx) = mpsc::channel();
|
||||||
|
|
||||||
|
let tmp_port = port.clone();
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
listener_server::listen_to_client(listener_channel_send_tx, listener_channel_recv_rx, &port)
|
listener_server::listen_to_client(
|
||||||
|
listener_channel_send_tx,
|
||||||
|
listener_channel_recv_rx,
|
||||||
|
tmp_port,
|
||||||
|
)
|
||||||
});
|
});
|
||||||
|
let tmp_port = server_port.clone();
|
||||||
let tmp_server = server.clone();
|
let tmp_server = server.clone();
|
||||||
thread::spawn(|| {
|
thread::spawn(move || {
|
||||||
writer_client::write_to_server(
|
writer_client::write_to_server(
|
||||||
writer_stream,
|
&tmp_server,
|
||||||
tmp_server,
|
&tmp_port,
|
||||||
writer_channel_send_rx,
|
writer_channel_send_rx,
|
||||||
writer_channel_recv_tx,
|
writer_channel_recv_tx,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut keys: HashMap<String, SignedPublicKey> = HashMap::new();
|
let mut keys: HashMap<String, SignedPublicKey> = HashMap::new();
|
||||||
|
let mut state = State::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match listener_channel_rx.try_recv() {
|
match listener_channel_rx.try_recv() {
|
||||||
Ok(message) => client_handler::handle_message_from_client(
|
Ok(message) => {
|
||||||
|
let _ = client_handler::handle_message_from_client(
|
||||||
&message,
|
&message,
|
||||||
&public_key,
|
&public_key,
|
||||||
&server,
|
&server,
|
||||||
|
@ -77,7 +114,9 @@ fn main() -> Result<()> {
|
||||||
&writer_channel_rx,
|
&writer_channel_rx,
|
||||||
&listener_channel_tx,
|
&listener_channel_tx,
|
||||||
&listener_channel_rx,
|
&listener_channel_rx,
|
||||||
)?,
|
&mut state,
|
||||||
|
);
|
||||||
|
}
|
||||||
Err(error) => match error {
|
Err(error) => match error {
|
||||||
mpsc::TryRecvError::Empty => {}
|
mpsc::TryRecvError::Empty => {}
|
||||||
mpsc::TryRecvError::Disconnected => panic!("listener_channel_rx disconnected"),
|
mpsc::TryRecvError::Disconnected => panic!("listener_channel_rx disconnected"),
|
||||||
|
@ -85,24 +124,27 @@ fn main() -> Result<()> {
|
||||||
};
|
};
|
||||||
|
|
||||||
match writer_channel_rx.try_recv() {
|
match writer_channel_rx.try_recv() {
|
||||||
Ok(message) => server_handler::handle_message_from_server(
|
Ok(message) => {
|
||||||
|
let _ = server_handler::handle_message_from_server(
|
||||||
&message,
|
&message,
|
||||||
&public_key,
|
&public_key,
|
||||||
&secret_key,
|
&secret_key,
|
||||||
&server,
|
&server,
|
||||||
&passwd,
|
passwd,
|
||||||
&mut keys,
|
&mut keys,
|
||||||
&writer_channel_tx,
|
&writer_channel_tx,
|
||||||
&writer_channel_rx,
|
&writer_channel_rx,
|
||||||
&listener_channel_tx,
|
&listener_channel_tx,
|
||||||
&listener_channel_rx,
|
&listener_channel_rx,
|
||||||
)?,
|
&state,
|
||||||
|
);
|
||||||
|
}
|
||||||
Err(error) => match error {
|
Err(error) => match error {
|
||||||
mpsc::TryRecvError::Empty => {}
|
mpsc::TryRecvError::Empty => {}
|
||||||
mpsc::TryRecvError::Disconnected => panic!("writer_channel_rx disconnected"),
|
mpsc::TryRecvError::Disconnected => panic!("writer_channel_rx disconnected"),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
thread::sleep(Duration::from_millis(100));
|
thread::sleep(Duration::from_millis(1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
|
use crate::unwrap_or_return_option;
|
||||||
|
use crate::unwrap_or_return_result;
|
||||||
|
use crate::State;
|
||||||
use crate::{encryption, helpers};
|
use crate::{encryption, helpers};
|
||||||
|
use eyre::Result;
|
||||||
use pgp::{Deserializable, SignedPublicKey, SignedSecretKey};
|
use pgp::{Deserializable, SignedPublicKey, SignedSecretKey};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::mpsc::{self, Receiver, Sender};
|
use std::sync::mpsc::{self, Receiver, Sender};
|
||||||
use eyre::Result as Result;
|
|
||||||
|
|
||||||
fn forward(
|
fn forward(
|
||||||
message: &str,
|
message: &str,
|
||||||
listener_channel_tx: &Sender<String>,
|
listener_channel_tx: &Sender<String>,
|
||||||
server: &str,
|
server: &str,
|
||||||
) -> Result<(), mpsc::SendError<String>> {
|
) -> Result<(), mpsc::SendError<String>> {
|
||||||
listener_channel_tx.send(message.replace(&server, "127.0.0.1"))
|
listener_channel_tx.send(message.replace(server, "127.0.0.1"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_message_from_server(
|
pub fn handle_message_from_server(
|
||||||
|
@ -23,15 +26,20 @@ pub fn handle_message_from_server(
|
||||||
writer_channel_rx: &Receiver<String>,
|
writer_channel_rx: &Receiver<String>,
|
||||||
listener_channel_tx: &Sender<String>,
|
listener_channel_tx: &Sender<String>,
|
||||||
_listener_channel_rx: &Receiver<String>,
|
_listener_channel_rx: &Receiver<String>,
|
||||||
|
state: &State,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let recieved_parsed = &ircparser::parse(&recieved).unwrap()[0];
|
let recieved_parsed = &unwrap_or_return_result!(ircparser::parse(recieved))[0];
|
||||||
|
|
||||||
|
let default_reciever = String::new();
|
||||||
|
|
||||||
|
let reciever = match recieved_parsed.params.get(0) {
|
||||||
|
Some(val) => val,
|
||||||
|
None => &default_reciever,
|
||||||
|
};
|
||||||
|
|
||||||
if recieved_parsed.command != "PRIVMSG"
|
if recieved_parsed.command != "PRIVMSG"
|
||||||
|| recieved_parsed
|
|| reciever.starts_with('#')
|
||||||
.params
|
|| state.nicks_without_encryption.contains(reciever)
|
||||||
.get(0)
|
|
||||||
.unwrap_or(&String::new())
|
|
||||||
.starts_with("#")
|
|
||||||
{
|
{
|
||||||
forward(recieved, listener_channel_tx, server)?;
|
forward(recieved, listener_channel_tx, server)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -39,30 +47,32 @@ pub fn handle_message_from_server(
|
||||||
|
|
||||||
dbg!(&recieved_parsed);
|
dbg!(&recieved_parsed);
|
||||||
|
|
||||||
|
let source = unwrap_or_return_option!(&recieved_parsed.source);
|
||||||
|
|
||||||
if recieved_parsed.params[1] == "START_MESSAGE" {
|
if recieved_parsed.params[1] == "START_MESSAGE" {
|
||||||
let sender = helpers::get_nick(&recieved_parsed.source.clone().unwrap());
|
let sender = unwrap_or_return_option!(helpers::get_nick(source));
|
||||||
|
|
||||||
let message = helpers::recieve_message_base64(
|
let message = helpers::recieve_message_base64(
|
||||||
writer_channel_rx,
|
writer_channel_rx,
|
||||||
listener_channel_tx,
|
listener_channel_tx,
|
||||||
&server,
|
server,
|
||||||
"127.0.0.1",
|
"127.0.0.1",
|
||||||
&sender,
|
&sender,
|
||||||
"END_MESSAGE",
|
"END_MESSAGE",
|
||||||
)?; // Get
|
)?; // Get
|
||||||
let message = encryption::decrypt(&secret_key, message, &passwd)?; // Decrypt
|
let message = encryption::decrypt(secret_key, &message, passwd)?; // Decrypt
|
||||||
|
|
||||||
listener_channel_tx.send(recieved.replace("START_MESSAGE", &message))?; // Send
|
listener_channel_tx.send(recieved.replace("START_MESSAGE", &message))?; // Send
|
||||||
} else if recieved_parsed.params[1] == "START_KEY" {
|
} else if recieved_parsed.params[1] == "START_KEY" {
|
||||||
let sender = helpers::get_nick(&recieved_parsed.source.clone().unwrap());
|
let sender = unwrap_or_return_option!(helpers::get_nick(source));
|
||||||
let to_send = helpers::bytes_to_privmsg_base64(&public_key, &sender);
|
let to_send = helpers::bytes_to_privmsg_base64(public_key, &sender);
|
||||||
writer_channel_tx.send(to_send)?;
|
writer_channel_tx.send(to_send)?;
|
||||||
writer_channel_tx.send(format!("PRIVMSG {sender} END_KEY\r\n"))?;
|
writer_channel_tx.send(format!("PRIVMSG {sender} END_KEY\r\n"))?;
|
||||||
|
|
||||||
let foreign_key = helpers::recieve_message_base64(
|
let foreign_key = helpers::recieve_message_base64(
|
||||||
writer_channel_rx,
|
writer_channel_rx,
|
||||||
listener_channel_tx,
|
listener_channel_tx,
|
||||||
&server,
|
server,
|
||||||
"127.0.0.1",
|
"127.0.0.1",
|
||||||
&sender,
|
&sender,
|
||||||
"END_KEY",
|
"END_KEY",
|
||||||
|
|
|
@ -6,14 +6,19 @@ use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
pub fn write_to_server(
|
pub fn write_to_server(
|
||||||
tcp_stream: TcpStream,
|
server: &str,
|
||||||
server: String,
|
port: &str,
|
||||||
rx: mpsc::Receiver<String>,
|
rx: mpsc::Receiver<String>,
|
||||||
tx: mpsc::Sender<String>,
|
tx: mpsc::Sender<String>,
|
||||||
) {
|
) {
|
||||||
|
'big: loop {
|
||||||
|
println!("Connecting to {server}:{port}");
|
||||||
|
let tcp_stream =
|
||||||
|
TcpStream::connect(format!("{server}:{port}")).expect("Couldn't connect to server");
|
||||||
|
|
||||||
let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
|
let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
|
||||||
let mut stream = connector
|
let mut stream = connector
|
||||||
.connect(&server, tcp_stream)
|
.connect(server, &tcp_stream)
|
||||||
.expect("Couldn't start TLS");
|
.expect("Couldn't start TLS");
|
||||||
|
|
||||||
stream
|
stream
|
||||||
|
@ -35,25 +40,34 @@ pub fn write_to_server(
|
||||||
}
|
}
|
||||||
Err(_error) => match _error.io_error() {
|
Err(_error) => match _error.io_error() {
|
||||||
None => {
|
None => {
|
||||||
dbg!(_error);
|
dbg!(_error.ssl_error());
|
||||||
|
continue 'big;
|
||||||
}
|
}
|
||||||
Some(error) => match error.kind() {
|
Some(error) => match error.kind() {
|
||||||
ErrorKind::WouldBlock => {}
|
ErrorKind::WouldBlock => {}
|
||||||
_ => {
|
_ => {
|
||||||
dbg!(error.kind());
|
dbg!(error.kind());
|
||||||
println!("Couldn't read the stream");
|
println!("Couldn't read the stream");
|
||||||
|
continue 'big;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
let value = rx.try_recv().unwrap_or("".to_string());
|
let value = rx.try_recv().unwrap_or("".to_string());
|
||||||
|
match value.as_str() {
|
||||||
|
"DUMMY CLOSE_CONNECTION" => {
|
||||||
|
continue 'big;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
match stream.write_all(value.as_bytes()) {
|
match stream.write_all(value.as_bytes()) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(_e) => println!("Couldn't send {value}"),
|
Err(_e) => println!("Couldn't send {value}"),
|
||||||
};
|
};
|
||||||
|
thread::sleep(Duration::from_micros(100));
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = tx.send(dbg!(String::from_utf8_lossy(&buffer).to_string()));
|
let _ = tx.send(dbg!(String::from_utf8_lossy(&buffer).to_string()));
|
||||||
thread::sleep(Duration::from_millis(100));
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue