Skip to content

Commit d683c58

Browse files
committed
Write main
1 parent a433c09 commit d683c58

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

socks4/src/main.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
1+
use std::net::TcpListener;
2+
use std::thread::Scope;
3+
use std::{io, thread};
4+
15
mod server;
26

3-
fn main() {
4-
println!("Hello, world!");
7+
fn main_loop<'handler, 'scope, 'env>(
8+
listener: TcpListener,
9+
handler: &'handler server::Handler,
10+
scope: &'scope Scope<'scope, 'env>,
11+
) -> io::Result<()>
12+
where
13+
'handler: 'scope,
14+
{
15+
eprintln!("Server start");
16+
for stream in listener.incoming() {
17+
match stream {
18+
Ok(stream) => {
19+
scope.spawn(move || {
20+
if let Err(e) = handler.handle(stream) {
21+
eprintln!("ERROR: {e}");
22+
}
23+
});
24+
}
25+
Err(e) => {
26+
eprintln!("ERROR: {e}");
27+
}
28+
}
29+
}
30+
Ok(())
31+
}
32+
33+
fn main() -> io::Result<()> {
34+
let listener = TcpListener::bind("[::]:1080")?;
35+
let handler = server::Handler::default();
36+
let handler_ref = &handler;
37+
thread::scope(move |scope| main_loop(listener, handler_ref, scope))
538
}

socks4/src/server.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,28 @@ const MAX_IDENT_LEN: u64 = 256;
88
const CODE_SUCCESS: u8 = 90;
99
const CODE_FAILURE: u8 = 91;
1010

11-
struct Handler {
12-
client_read_timeout: Duration,
13-
client_write_timeout: Duration,
14-
upstream_connect_timeout: Duration,
15-
upstream_read_timeout: Duration,
16-
upstream_write_timeout: Duration,
11+
pub struct Handler {
12+
pub client_read_timeout: Duration,
13+
pub client_write_timeout: Duration,
14+
pub upstream_connect_timeout: Duration,
15+
pub upstream_read_timeout: Duration,
16+
pub upstream_write_timeout: Duration,
17+
}
18+
19+
impl Default for Handler {
20+
fn default() -> Self {
21+
Self {
22+
client_read_timeout: Duration::from_secs(60),
23+
client_write_timeout: Duration::from_secs(60),
24+
upstream_connect_timeout: Duration::from_secs(10),
25+
upstream_read_timeout: Duration::from_secs(60),
26+
upstream_write_timeout: Duration::from_secs(60),
27+
}
28+
}
1729
}
1830

1931
impl Handler {
20-
fn handle(&self, mut client: TcpStream) -> io::Result<()> {
32+
pub fn handle(&self, mut client: TcpStream) -> io::Result<()> {
2133
client.set_read_timeout(Some(self.client_read_timeout))?;
2234
client.set_write_timeout(Some(self.client_write_timeout))?;
2335
let mut client_reader = BufReader::new(client.try_clone()?);

0 commit comments

Comments
 (0)