Skip to content

Commit f338466

Browse files
committed
Add onion_random_socks_auth
Signed-off-by: Eval EXEC <[email protected]>
1 parent a341d27 commit f338466

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

tentacle/src/builder.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ where
241241
self
242242
}
243243

244+
/// Onion config for tcp
245+
#[cfg(not(target_family = "wasm"))]
246+
pub fn tcp_onion_random_socks_auth(mut self, onion_random_socks_auth: bool) -> Self {
247+
self.config.tcp_config.tcp.onion_random_socks_auth = onion_random_socks_auth;
248+
self
249+
}
250+
244251
/// The same as tcp config, but use on ws transport
245252
#[cfg(feature = "ws")]
246253
#[cfg_attr(docsrs, doc(cfg(feature = "ws")))]

tentacle/src/runtime/proxy/socks5_config.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
use std::io;
22

3+
use rand::Rng;
4+
35
#[derive(Debug)]
46
pub(crate) struct Socks5Config {
57
pub(crate) proxy_url: String,
68
pub(crate) auth: Option<(String, String)>,
79
}
810

911
// parse proxy url like "socks5://username:password@localhost:1080" to Socks5Config
10-
pub(crate) fn parse(proxy_url: &str) -> io::Result<Socks5Config> {
11-
let parsed_url = url::Url::parse(proxy_url)
12-
.map_err(|err| io::Error::other(format!("parse proxy_url {} failed, {}", proxy_url, err)))?;
12+
pub(crate) fn parse(proxy_url: &str, onion_random_socks_auth: bool) -> io::Result<Socks5Config> {
13+
let parsed_url = url::Url::parse(proxy_url).map_err(|err| {
14+
io::Error::other(format!("parse proxy_url {} failed, {}", proxy_url, err))
15+
})?;
1316
let scheme = parsed_url.scheme();
1417
match scheme {
1518
"socks5" => {
1619
let auth = match parsed_url.username() {
17-
"" => None,
20+
"" => {
21+
if onion_random_socks_auth {
22+
let username = rand::thread_rng()
23+
.sample_iter(&rand::distributions::Alphanumeric)
24+
.take(8)
25+
.map(char::from)
26+
.collect();
27+
let password = rand::thread_rng()
28+
.sample_iter(&rand::distributions::Alphanumeric)
29+
.take(16)
30+
.map(char::from)
31+
.collect();
32+
Some((username, password))
33+
} else {
34+
None
35+
}
36+
}
1837
username => Some((
1938
username.to_string(),
2039
parsed_url.password().unwrap_or("").to_string(),

tentacle/src/runtime/tokio_runtime/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,12 @@ async fn connect_by_proxy<A>(
146146
target_addr: A,
147147
socket_transformer: TcpSocketTransformer,
148148
proxy_url: String,
149+
onion_random_socks_auth: bool,
149150
) -> io::Result<TcpStream>
150151
where
151152
A: Into<shadowsocks::relay::Address>,
152153
{
153-
let socks5_config = socks5_config::parse(&proxy_url)?;
154+
let socks5_config = socks5_config::parse(&proxy_url, onion_random_socks_auth)?;
154155

155156
let dial_addr: SocketAddr = socks5_config.proxy_url.parse().map_err(io::Error::other)?;
156157
let stream = connect_direct(dial_addr, socket_transformer).await?;
@@ -168,10 +169,13 @@ pub(crate) async fn connect(
168169
socket_transformer,
169170
proxy_url,
170171
onion_url: _,
172+
onion_random_socks_auth: _,
171173
} = tcp_config;
172174

173175
match proxy_url {
174-
Some(proxy_url) => connect_by_proxy(target_addr, socket_transformer, proxy_url).await,
176+
Some(proxy_url) => {
177+
connect_by_proxy(target_addr, socket_transformer, proxy_url, false).await
178+
}
175179
None => connect_direct(target_addr, socket_transformer).await,
176180
}
177181
}
@@ -184,6 +188,7 @@ pub(crate) async fn connect_onion(
184188
socket_transformer,
185189
proxy_url,
186190
onion_url,
191+
onion_random_socks_auth,
187192
} = tcp_config;
188193
let onion_url = onion_url.or(proxy_url).ok_or(io::Error::other(
189194
"need tor proxy server to connect to onion address",
@@ -204,5 +209,11 @@ pub(crate) async fn connect_onion(
204209
let onion_address =
205210
shadowsocks::relay::Address::from_str(&onion_str).map_err(std::io::Error::other)?;
206211

207-
connect_by_proxy(onion_address, socket_transformer, onion_url).await
212+
connect_by_proxy(
213+
onion_address,
214+
socket_transformer,
215+
onion_url,
216+
onion_random_socks_auth,
217+
)
218+
.await
208219
}

tentacle/src/service/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub(crate) struct TcpSocketConfig {
124124
pub(crate) socket_transformer: TcpSocketTransformer,
125125
pub(crate) proxy_url: Option<String>,
126126
pub(crate) onion_url: Option<String>,
127+
pub(crate) onion_random_socks_auth: bool,
127128
}
128129

129130
impl Default for TcpSocketConfig {
@@ -132,6 +133,7 @@ impl Default for TcpSocketConfig {
132133
socket_transformer: Arc::new(|tcp_socket, _| Ok(tcp_socket)),
133134
proxy_url: None,
134135
onion_url: None,
136+
onion_random_socks_auth: false,
135137
}
136138
}
137139
}

0 commit comments

Comments
 (0)