forked from driftluo/tentacle
-
Notifications
You must be signed in to change notification settings - Fork 26
Support proxy and onion #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use std::{io, sync::Arc, time::Duration}; | ||
|
||
use crate::service::config::TransformerContext; | ||
use nohash_hasher::IntMap; | ||
use tokio_util::codec::LengthDelimitedCodec; | ||
|
||
|
@@ -217,9 +218,49 @@ where | |
#[cfg(not(target_family = "wasm"))] | ||
pub fn tcp_config<F>(mut self, f: F) -> Self | ||
where | ||
F: Fn(TcpSocket) -> Result<TcpSocket, std::io::Error> + Send + Sync + 'static, | ||
F: Fn(TcpSocket, TransformerContext) -> Result<TcpSocket, std::io::Error> | ||
+ Send | ||
+ Sync | ||
+ 'static, | ||
{ | ||
self.config.tcp_config.tcp = Arc::new(f); | ||
self.config.tcp_config.tcp.socket_transformer = Arc::new(f); | ||
self | ||
} | ||
|
||
fn must_parse_proxy_url(url: String) -> url::Url { | ||
let url = url::Url::parse(&url) | ||
.unwrap_or_else(|err| panic!("parse {} to url::Url failed: {}", url, err)); | ||
if url.scheme().ne("socks5") { | ||
panic!("tentacle only support socks5 proxy"); | ||
} | ||
url | ||
} | ||
|
||
/// Proxy config for tcp | ||
/// | ||
/// Example: socks5://127.0.0.1:9050 | ||
/// Example: socks5://username:[email protected]:9050 | ||
#[cfg(not(target_family = "wasm"))] | ||
pub fn tcp_proxy_config(mut self, proxy_url: String) -> Self { | ||
let proxy_url: url::Url = Self::must_parse_proxy_url(proxy_url); | ||
self.config.tcp_config.tcp.proxy_url = Some(proxy_url); | ||
self | ||
} | ||
|
||
/// Onion config for tcp | ||
/// | ||
/// Example: socks5://127.0.0.1:9050 | ||
#[cfg(not(target_family = "wasm"))] | ||
pub fn tcp_onion_config(mut self, onion_url: String) -> Self { | ||
let onion_url: url::Url = Self::must_parse_proxy_url(onion_url); | ||
self.config.tcp_config.tcp.onion_url = Some(onion_url); | ||
self | ||
} | ||
|
||
/// Onion config for proxy, use random username/password is set for proxy connection | ||
#[cfg(not(target_family = "wasm"))] | ||
pub fn tcp_proxy_random_auth(mut self, proxy_random_auth: bool) -> Self { | ||
self.config.tcp_config.tcp.proxy_random_auth = proxy_random_auth; | ||
self | ||
} | ||
|
||
|
@@ -228,9 +269,12 @@ where | |
#[cfg_attr(docsrs, doc(cfg(feature = "ws")))] | ||
pub fn tcp_config_on_ws<F>(mut self, f: F) -> Self | ||
where | ||
F: Fn(TcpSocket) -> Result<TcpSocket, std::io::Error> + Send + Sync + 'static, | ||
F: Fn(TcpSocket, TransformerContext) -> Result<TcpSocket, std::io::Error> | ||
+ Send | ||
+ Sync | ||
+ 'static, | ||
{ | ||
self.config.tcp_config.ws = Arc::new(f); | ||
self.config.tcp_config.ws.socket_transformer = Arc::new(f); | ||
self | ||
} | ||
|
||
|
@@ -252,9 +296,12 @@ where | |
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))] | ||
pub fn tcp_config_on_tls<F>(mut self, f: F) -> Self | ||
where | ||
F: Fn(TcpSocket) -> Result<TcpSocket, std::io::Error> + Send + Sync + 'static, | ||
F: Fn(TcpSocket, TransformerContext) -> Result<TcpSocket, std::io::Error> | ||
+ Send | ||
+ Sync | ||
+ 'static, | ||
{ | ||
self.config.tcp_config.tls = Arc::new(f); | ||
self.config.tcp_config.tls.socket_transformer = Arc::new(f); | ||
self | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[cfg(not(target_family = "wasm"))] | ||
pub(crate) mod socks5; | ||
#[cfg(not(target_family = "wasm"))] | ||
pub(crate) mod socks5_config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use fast_socks5::{ | ||
AuthenticationMethod, Socks5Command, | ||
client::{Config as ConnectConfig, Socks5Stream}, | ||
}; | ||
use tokio::net::TcpStream; | ||
|
||
pub async fn connect( | ||
socks_server: url::Url, | ||
target_addr: String, | ||
target_port: u16, | ||
) -> Result<TcpStream, fast_socks5::SocksError> { | ||
let auth = { | ||
if socks_server.username().is_empty() { | ||
AuthenticationMethod::None | ||
} else { | ||
AuthenticationMethod::Password { | ||
username: socks_server.username().to_string(), | ||
password: socks_server.password().unwrap_or_default().to_string(), | ||
} | ||
} | ||
}; | ||
let socks_server_str = format!( | ||
"{}:{}", | ||
socks_server.host_str().ok_or_else(|| { | ||
fast_socks5::SocksError::ArgumentInputError("socks_server should have host") | ||
})?, | ||
socks_server.port().ok_or_else(|| { | ||
fast_socks5::SocksError::ArgumentInputError("socks_server should have port") | ||
})? | ||
); | ||
Socks5Stream::connect_raw( | ||
Socks5Command::TCPConnect, | ||
socks_server_str, | ||
target_addr, | ||
target_port, | ||
Some(auth), | ||
ConnectConfig::default(), | ||
) | ||
.await | ||
.map(|socket| socket.get_socket()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use rand::Rng; | ||
|
||
pub(crate) fn random_auth() -> (String, String) { | ||
let username = rand::thread_rng() | ||
.sample_iter(&rand::distributions::Alphanumeric) | ||
.take(8) | ||
.map(char::from) | ||
.collect(); | ||
let password = rand::thread_rng() | ||
.sample_iter(&rand::distributions::Alphanumeric) | ||
.take(16) | ||
.map(char::from) | ||
.collect(); | ||
(username, password) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.