initial commit
This commit is contained in:
commit
abfd7fe4f4
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/target
|
||||||
|
/.idea
|
||||||
|
Cargo.lock
|
18
Cargo.toml
Normal file
18
Cargo.toml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
[package]
|
||||||
|
name = "ircproxy"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
futures= "*"
|
||||||
|
anyhow = "*"
|
||||||
|
tokio = { version = "*", features = ["full"] }
|
||||||
|
tokio-util = { version = "*", features = ["codec"] }
|
||||||
|
tokio-stream = { version = "*", features = ["net"] }
|
||||||
|
tokio-native-tls = { version = "*" }
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
lto = "thin"
|
||||||
|
opt-level = "z"
|
2
README.md
Normal file
2
README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
# ircproxy
|
||||||
|
a simple rust irc proxy for encrypted to unencrypted connections
|
84
src/main.rs
Normal file
84
src/main.rs
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use tokio::{
|
||||||
|
io::{copy, split, AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter, Lines},
|
||||||
|
net::{TcpListener, TcpStream},
|
||||||
|
try_join,
|
||||||
|
};
|
||||||
|
use tokio_native_tls::{TlsConnector, TlsStream};
|
||||||
|
use tokio_stream::{wrappers::TcpListenerStream, StreamExt};
|
||||||
|
|
||||||
|
async fn build_proxy<R: AsyncBufReadExt + Unpin>(
|
||||||
|
client_r: &mut Lines<R>,
|
||||||
|
) -> Result<TlsStream<TcpStream>> {
|
||||||
|
loop {
|
||||||
|
if let Some(line) = client_r.next_line().await? {
|
||||||
|
if line.starts_with("NICK") || line.starts_with("USER") {
|
||||||
|
break Err(anyhow!("Provide a server with PASS before authenticating"));
|
||||||
|
}
|
||||||
|
if let Some(server) = line.strip_prefix("PASS") {
|
||||||
|
let (domain, port) = server
|
||||||
|
.trim()
|
||||||
|
.split_once(':')
|
||||||
|
.ok_or_else(|| anyhow!("Unable to parse server and port"))?;
|
||||||
|
let cx = TlsConnector::from(
|
||||||
|
tokio_native_tls::native_tls::TlsConnector::builder()
|
||||||
|
.danger_accept_invalid_certs(true)
|
||||||
|
.build()?,
|
||||||
|
);
|
||||||
|
break Ok(cx
|
||||||
|
.connect(
|
||||||
|
domain,
|
||||||
|
TcpStream::connect(format!("{}:{}", domain, port)).await?,
|
||||||
|
)
|
||||||
|
.await?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_connection(mut client: TcpStream) -> Result<()> {
|
||||||
|
let (client_r, client_w) = client.split();
|
||||||
|
let mut client_r = BufReader::new(client_r).lines();
|
||||||
|
let mut client_w = BufWriter::new(client_w);
|
||||||
|
|
||||||
|
let socket = match build_proxy(&mut client_r).await {
|
||||||
|
Ok(socket) => socket,
|
||||||
|
Err(e) => {
|
||||||
|
client_w
|
||||||
|
.write_all(format!(":ircproxy 461 * PASS :{}\r\n", e).as_bytes())
|
||||||
|
.await?;
|
||||||
|
client_w.flush().await?;
|
||||||
|
client_w.shutdown().await?;
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut client_r = client_r.into_inner();
|
||||||
|
|
||||||
|
let (mut server_r, mut server_w) = split(socket);
|
||||||
|
|
||||||
|
let client_to_server = async {
|
||||||
|
copy(&mut client_r, &mut server_w).await?;
|
||||||
|
server_w.shutdown().await
|
||||||
|
};
|
||||||
|
let server_to_client = async {
|
||||||
|
copy(&mut server_r, &mut client_w).await?;
|
||||||
|
client_w.shutdown().await
|
||||||
|
};
|
||||||
|
|
||||||
|
try_join!(client_to_server, server_to_client)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
|
let addr = std::env::args().nth(1).unwrap_or("0.0.0.0:8081".to_owned());
|
||||||
|
println!("Hosting ircproxy on {}", addr);
|
||||||
|
let socket = TcpListener::bind(addr).await?;
|
||||||
|
|
||||||
|
|
||||||
|
let mut incoming = TcpListenerStream::new(socket);
|
||||||
|
while let Some(client) = incoming.next().await.transpose()? {
|
||||||
|
tokio::spawn(handle_connection(client));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user