add socks proxy support

This commit is contained in:
xfnw 2024-04-12 19:05:09 -04:00
parent 3a60ee1b28
commit 879d14073b
3 changed files with 95 additions and 5 deletions

69
Cargo.lock generated
View File

@ -100,6 +100,36 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
[[package]]
name = "either"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a"
[[package]]
name = "futures-core"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
[[package]]
name = "futures-task"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004"
[[package]]
name = "futures-util"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
dependencies = [
"futures-core",
"futures-task",
"pin-project-lite",
"pin-utils",
]
[[package]]
name = "getrandom"
version = "0.2.12"
@ -137,6 +167,7 @@ dependencies = [
"rustls-pemfile",
"tokio",
"tokio-rustls",
"tokio-socks",
]
[[package]]
@ -196,6 +227,12 @@ version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "proc-macro2"
version = "1.0.79"
@ -308,6 +345,26 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio"
version = "1.36.0"
@ -347,6 +404,18 @@ dependencies = [
"tokio",
]
[[package]]
name = "tokio-socks"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51165dfa029d2a65969413a6cc96f354b86b464498702f174a4efa13608fd8c0"
dependencies = [
"either",
"futures-util",
"thiserror",
"tokio",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"

View File

@ -12,6 +12,7 @@ clap = { version = "4.5.2", default-features = false, features = ["derive", "std
rustls-pemfile = "2.1.1"
tokio = { version = "1.36.0", features = ["rt-multi-thread", "macros", "net", "io-util", "io-std"]}
tokio-rustls = { version = "0.25.0", default-features = false, features = ["ring", "tls12"] }
tokio-socks = "0.5.1"
[profile.smol]
inherits = "release"

View File

@ -1,5 +1,5 @@
use clap::Parser;
use std::{io::Write, path::PathBuf, sync::Arc};
use std::{io::Write, net::SocketAddr, path::PathBuf, sync::Arc};
use tokio::{
io::{self, AsyncBufReadExt, AsyncWriteExt, BufReader},
net::TcpStream,
@ -8,6 +8,7 @@ use tokio_rustls::{
rustls::{self, pki_types},
TlsConnector,
};
use tokio_socks::tcp::Socks5Stream;
mod danger;
@ -29,6 +30,10 @@ struct Opt {
#[arg(long, default_value = "/etc/ssl/cert.pem")]
cafile: PathBuf,
/// connect via socks5 proxy
#[arg(short = 's', long)]
socks: Option<SocketAddr>,
#[arg(required = true)]
host: String,
@ -91,10 +96,25 @@ async fn main() {
6667
};
let stream = TcpStream::connect((opt.host.as_ref(), port))
.await
.expect("failed to connect");
let target = (opt.host.as_ref(), port);
if let Some(sock) = opt.socks {
let stream = Socks5Stream::connect(sock, target)
.await
.expect("failed to sock");
handle_tls(opt, stream).await;
} else {
let stream = TcpStream::connect(target).await.expect("failed to connect");
handle_tls(opt, stream).await;
}
}
async fn handle_tls<T: io::AsyncReadExt + io::AsyncWriteExt + std::marker::Unpin>(
opt: Opt,
stream: T,
) {
if opt.tls {
let config = rustls::ClientConfig::builder();
let config = if opt.insecure {
@ -152,7 +172,7 @@ async fn main() {
}
}
async fn handle_irc(stream: impl io::AsyncReadExt + io::AsyncWriteExt) {
async fn handle_irc<T: io::AsyncReadExt + io::AsyncWriteExt>(stream: T) {
let (read, mut write) = io::split(stream);
let mut read = BufReader::new(read);
let mut stdin = BufReader::new(io::stdin());