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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! Shadowsocks tcp services.

use std::{
    io::{self, ErrorKind},
    net::SocketAddr,
    sync::Arc,
    time::Duration,
};

use tokio::{
    io::{AsyncRead, AsyncWrite, AsyncWriteExt},
    net::{TcpListener as TokioTcpListener, TcpStream as TokioTcpStream, ToSocketAddrs},
};

use crate::{
    context::Ctx,
    crypto::cipher::Method,
    net::{
        io::{lookup_host, transfer_between},
        stream::TcpStream as SsTcpStream,
    },
    socks::{self, socks5::Socks5Addr},
};

/// TCP Listener for incoming shadowsocks connection.
pub struct SsTcpListener {
    inner_listener: TokioTcpListener,
    cipher_method: Method,
    cipher_key: Vec<u8>,
    ctx: Arc<Ctx>,
}

impl SsTcpListener {
    /// Creates a new TcpListener for incoming shadowsocks connection,
    /// which will be bound to the specified address.
    pub async fn bind<A: ToSocketAddrs>(
        addr: A,
        cipher_method: Method,
        cipher_key: &[u8],
        ctx: Arc<Ctx>,
    ) -> io::Result<Self> {
        let inner_listener = TokioTcpListener::bind(addr).await?;
        Ok(SsTcpListener {
            inner_listener,
            cipher_method,
            cipher_key: cipher_key.to_owned(),
            ctx,
        })
    }

    /// Accepts a new incoming shadowsocks connection from this listener.
    pub async fn accept(&self) -> io::Result<(SsTcpStream<TokioTcpStream>, SocketAddr)> {
        let (stream, addr) = self.inner_listener.accept().await?;
        Ok((
            SsTcpStream::new(
                stream,
                self.cipher_method,
                &self.cipher_key,
                self.ctx.clone(),
            ),
            addr,
        ))
    }
}

/// Starts a shadowsocks remote server.
pub async fn ss_remote(
    addr: SocketAddr,
    method: Method,
    key: Vec<u8>,
    ctx: Arc<Ctx>,
) -> io::Result<()> {
    let listener = SsTcpListener::bind(addr, method, &key, ctx.clone()).await?;

    log::info!("ss-remote listening on {}", addr);

    loop {
        match listener.accept().await {
            Ok((encrypted_stream, peer)) => {
                log::debug!("Accept {}", peer);
                tokio::spawn(handle_ss_remote(encrypted_stream, peer, ctx.clone()));
            }
            Err(e) => log::warn!("Accept error: {}", e),
        }
    }
}

/// Starts a shadowsocks local server.
pub async fn ss_local(
    local_addr: SocketAddr,
    remote_addr: SocketAddr,
    method: Method,
    key: Vec<u8>,
    ctx: Arc<Ctx>,
) -> io::Result<()> {
    let listener = TokioTcpListener::bind(local_addr).await?;

    log::info!("ss-local listening on {}", local_addr);
    log::info!("The remote server address is {}", remote_addr);

    loop {
        match listener.accept().await {
            Ok((stream, peer)) => {
                log::debug!("Accept {}", peer);
                tokio::spawn(handle_ss_local(
                    stream,
                    peer,
                    remote_addr,
                    method,
                    key.clone(),
                    ctx.clone(),
                ));
            }
            Err(e) => log::warn!("Accept error: {}", e),
        }
    }
}

/// Handles incoming connection from ss-remote.
pub async fn handle_ss_remote<T>(mut stream: SsTcpStream<T>, peer: SocketAddr, ctx: Arc<Ctx>)
where
    T: AsyncRead + AsyncWrite + Unpin + Send,
{
    // 1. Checks whether or not to reject the client
    if ctx.is_bypass(peer.ip(), None) {
        log::warn!("Reject the client: peer {}", peer);
        return;
    }

    // 2. Constructs a socks5 address with timeout
    let result = tokio::time::timeout(Duration::from_secs(15), Socks5Addr::construct(&mut stream));
    let target_addr = match result.await {
        Ok(Ok(addr)) => addr,
        Ok(Err(e)) => {
            match e.kind() {
                ErrorKind::Other => log::warn!("Read target address failed: {}, peer {}", e, peer),
                _ => log::debug!("Read target address failed: {}, peer {}", e, peer),
            }
            return;
        }
        Err(e) => {
            log::debug!("Read target address timed out: {}, peer {}", e, peer);
            return;
        }
    };

    // 3. Resolves target socket address
    let target_socket_addr = match lookup_host(&target_addr.to_string()).await {
        Ok(addr) => addr,
        Err(e) => {
            log::warn!("Resolve {} failed: {}, peer {}", target_addr, e, peer);
            return;
        }
    };
    let target_ip = target_socket_addr.ip();

    // 4. Checks whether or not to block outbound
    if ctx.is_block_outbound(target_ip, Some(&target_addr.to_string())) {
        log::warn!(
            "Block outbound address: {} -> {} ({})",
            peer,
            target_addr,
            target_ip
        );
        return;
    }

    log::debug!(
        "Allow outbound address: {} -> {} ({})",
        peer,
        target_addr,
        target_ip
    );

    // 5. Connects to target address
    let target_stream = match TokioTcpStream::connect(target_socket_addr).await {
        Ok(stream) => stream,
        Err(e) => {
            log::debug!(
                "Unable to connect to {} ({}): {}",
                target_addr,
                target_ip,
                e
            );
            return;
        }
    };

    // 6. Establishes connection between ss-local and target
    let trans = format!("{} <=> {} ({})", peer, target_addr, target_ip);
    transfer(stream, target_stream, &trans).await;
}

/// Handles incoming connection from ss-local.
pub async fn handle_ss_local(
    mut stream: TokioTcpStream,
    peer: SocketAddr,
    remote_addr: SocketAddr,
    method: Method,
    key: Vec<u8>,
    ctx: Arc<Ctx>,
) {
    // 1. Constructs a socks5 address with timeout
    let result = tokio::time::timeout(Duration::from_secs(15), socks::handshake(&mut stream));
    let target_addr: Socks5Addr = match result.await {
        Ok(Ok(addr)) => addr.into(),
        Ok(Err(e)) => {
            match e.kind() {
                ErrorKind::Other => log::warn!("Read target address failed: {}, peer {}", e, peer),
                _ => log::debug!("Read target address failed: {}, peer {}", e, peer),
            }
            return;
        }
        Err(e) => {
            log::debug!("Read target address timed out: {}, peer {}", e, peer);
            return;
        }
    };

    // 2. Resolves target socket address
    let target_socket_addr = match lookup_host(&target_addr.to_string()).await {
        Ok(addr) => Some(addr),
        Err(e) => {
            log::debug!("Resolve {} failed: {}, peer {}", target_addr, e, peer);
            None
        }
    };

    // 3. Relays target address, bypass or proxy
    let trans: String;
    match target_socket_addr {
        Some(addr) if ctx.is_bypass(addr.ip(), Some(&target_addr.to_string())) => {
            trans = format!("{} <=> {} ({})", peer, target_addr, addr.ip());

            log::debug!(
                "Bypass target address: {} -> {} ({})",
                peer,
                target_addr,
                addr.ip()
            );

            // 3.1 Connects to target host
            let target_stream = match TokioTcpStream::connect(addr).await {
                Ok(stream) => stream,
                Err(e) => {
                    log::error!(
                        "Unable to connect to {} ({}): {}",
                        target_addr,
                        addr.ip(),
                        e
                    );
                    return;
                }
            };

            // 3.2 Establishes connection between ss-local and target
            transfer(stream, target_stream, &trans).await;
        }
        _ => {
            trans = format!("{} <=> {}", peer, target_addr);

            log::debug!("Proxy target address: {} -> {}", peer, target_addr);

            // 3.1 Connects to ss-remote
            let mut target_stream = match TokioTcpStream::connect(remote_addr).await {
                Ok(stream) => SsTcpStream::new(stream, method, &key, ctx),
                Err(e) => {
                    log::error!("Unable to connect to {}: {}", remote_addr, e);
                    return;
                }
            };

            // 3.2 Writes target address
            let target_addr_bytes = target_addr.get_raw_parts();
            match target_stream.write_all(&target_addr_bytes).await {
                Ok(_) => {}
                Err(e) => {
                    log::error!("Write target address to {} failed: {}", remote_addr, e);
                    return;
                }
            }

            // 3.3 Establishes connection between ss-local and ss-remote
            transfer(stream, target_stream, &trans).await;
        }
    }
}

async fn transfer<A, B>(a: A, b: B, trans: &str)
where
    A: AsyncRead + AsyncWrite + Send,
    B: AsyncRead + AsyncWrite + Send,
{
    match transfer_between(a, b, Duration::from_secs(90)).await {
        Ok((atob, btoa)) => log::trace!("{} done: ltor {} bytes, rtol {} bytes", trans, atob, btoa),
        Err(e) => match e.kind() {
            ErrorKind::Other => log::warn!("{} error: {}", trans, e),
            _ => log::debug!("{} error: {}", trans, e),
        },
    }
}