-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathsocks5_connect_proxy.rs
More file actions
72 lines (66 loc) · 2.3 KB
/
socks5_connect_proxy.rs
File metadata and controls
72 lines (66 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! An example to showcase how one can build an authenticated socks5 CONNECT proxy server.
//!
//! # Run the example
//!
//! ```sh
//! cargo run --example socks5_connect_proxy --features=dns,socks5
//! ```
//!
//! # Expected output
//!
//! The server will start and listen on `:62021`. You can use `curl` to interact with the service:
//!
//! ```sh
//! curl -v -x socks5://127.0.0.1:62021 --proxy-user 'john:secret' http://www.example.com/
//! curl -v -x socks5h://127.0.0.1:62021 --proxy-user 'john:secret' http://www.example.com/
//! curl -v -x socks5://127.0.0.1:62021 --proxy-user 'john:secret' https://www.example.com/
//! curl -v -x socks5h://127.0.0.1:62021 --proxy-user 'john:secret' https://www.example.com/
//! ```
//!
//! You should see in all the above examples the responses from the server.
//!
//! In case you use wrong credentials you'll see something like:
//!
//! ```sh
//! $ curl -v -x socks5://127.0.0.1:62021 --proxy-user 'john:foo' http://www.example.com/
//! * Trying 127.0.0.1:62021...
//! * Connected to 127.0.0.1 (127.0.0.1) port 62021
//! * User was rejected by the SOCKS5 server (1 1).
//! * Closing connection
//! curl: (97) User was rejected by the SOCKS5 server (1 1).
//! ```
use rama::{
net::user::credentials::basic,
proxy::socks5::Socks5Acceptor,
rt::Executor,
tcp::server::TcpListener,
telemetry::tracing::{
self,
level_filters::LevelFilter,
subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt},
},
};
use std::time::Duration;
#[tokio::main]
async fn main() {
tracing::subscriber::registry()
.with(fmt::layer())
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.from_env_lossy(),
)
.init();
let graceful = rama::graceful::Shutdown::default();
let exec = Executor::graceful(graceful.guard());
let tcp_service = TcpListener::bind("127.0.0.1:62021", exec)
.await
.expect("bind proxy to 127.0.0.1:62021");
let socks5_acceptor =
Socks5Acceptor::default().with_authorizer(basic!("john", "secret").into_authorizer());
graceful.spawn_task(tcp_service.serve(socks5_acceptor));
graceful
.shutdown_with_limit(Duration::from_secs(30))
.await
.expect("graceful shutdown");
}