rama-socks5 0.4.0

SOCKS5 support for rama
Documentation
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use rama_core::extensions::ExtensionsRef;
use rama_core::io::BridgeIo;
use rama_core::rt::Executor;
use rama_core::telemetry::tracing::{self, Instrument, trace_span};
use rama_core::{
    Layer, Service,
    error::BoxError,
    io::Io,
    layer::{TimeoutLayer, timeout::DefaultTimeout},
};
#[cfg(feature = "dns")]
use rama_dns::client::DnsConnector;
use rama_net::address::HostWithPort;
use rama_net::client::{ConnectRequest, ConnectorService, ConnectorTarget};
use rama_net::{client::EstablishedClientConnection, proxy::IoForwardService, stream::SocketInfo};
use rama_tcp::client::service::TcpConnector;
use rama_tcp::proxy::IoToProxyBridgeIo;
use rama_utils::macros::generate_set_and_with;
use std::time::Duration;

use super::Error;
use crate::proto::{ReplyKind, server::Reply};

/// Types which can be used as socks5 [`Command::Connect`] drivers on the server side.
///
/// Typically used as a component part of a [`Socks5Acceptor`].
///
/// The actual underlying trait is sealed and not exposed for usage.
/// No custom connectors can be implemented. You can however customise
/// both the connection and actual stream proxy phase by using
/// your own matching [`Service`] implementations as part of the usage
/// of [`Connector`].
///
/// [`Socks5Acceptor`]: crate::server::Socks5Acceptor
/// [`Command::Connect`]: crate::proto::Command::Connect
pub trait Socks5Connector<S>: Socks5ConnectorSeal<S> {}

impl<S, C> Socks5Connector<S> for C where C: Socks5ConnectorSeal<S> {}

pub trait Socks5ConnectorSeal<S>: Send + Sync + 'static {
    fn accept_connect(
        &self,

        stream: S,
        destination: HostWithPort,
    ) -> impl Future<Output = Result<(), Error>> + Send + '_;
}

impl<S> Socks5ConnectorSeal<S> for ()
where
    S: Io + Unpin,
{
    async fn accept_connect(&self, mut stream: S, destination: HostWithPort) -> Result<(), Error> {
        tracing::trace!(
            "socks5 server w/ destination {destination}: abort: command not supported: Connect",
        );

        Reply::error_reply(ReplyKind::CommandNotSupported)
            .write_to(&mut stream)
            .await
            .map_err(|err| {
                Error::io(err).with_context("write server reply: command not supported (connect)")
            })?;
        Err(Error::aborted("command not supported: Connect")
            .with_context(ReplyKind::CommandNotSupported))
    }
}

/// Default [`Connector`] type.
#[cfg(feature = "dns")]
pub type DefaultConnector = Connector<DefaultTimeout<DnsConnector<TcpConnector>>, IoForwardService>;

/// Default [`Connector`] type.
#[cfg(not(feature = "dns"))]
pub type DefaultConnector = Connector<DefaultTimeout<TcpConnector>, IoForwardService>;

const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(60);

/// Proxy Forward [`Socks5Connector`] implementation,
/// which actually is able to accept connect requests and process them.
///
/// The [`Default`] implementation establishes a connection for the requested
/// destination [`HostWithPort`] and pipes the incoming [`Io`] with the established
/// outgoing [`Io`] by copying the bytes without doing anything else with them.
///
/// You can customise the [`Connector`] fully by creating it using
/// [`Connector::new`] or overwrite any of the default components using either or both of
/// [`Connector::with_connector`] and [`Connector::with_service`].
///
/// ## Lazy Connectors
///
/// Use [`LazyConnector`] only when application bytes are required before the
/// egress target can be selected. MITM proxies with a target from the SOCKS5
/// handshake should normally use this connector and inspect the resulting
/// [`BridgeIo`] with Rama's Relay/Peek services.
///
/// Connection policy belongs in the connector stack. Wrap a custom connector
/// in [`TimeoutLayer`] when its connection attempts should be bounded.
#[derive(Debug, Clone)]
pub struct Connector<C, S> {
    connector: C,
    service: S,

    // if true it uses the 0.0.0.0:0 bind address
    // instead of the actual local address used to connect
    hide_local_address: bool,
}

impl<C, S> Connector<C, S> {
    /// Create a new [`Connector`].
    ///
    /// In case you only wish to overwrite one of these components
    /// you can also use a [`Default`] [`Connector`] and overwrite the specific component
    /// using [`Connector::with_connector`] or [`Connector::with_service`].
    pub fn new(connector: C, service: S) -> Self {
        Self {
            connector,
            service,
            hide_local_address: false,
        }
    }

    generate_set_and_with! {
        /// Define whether or not the local address is exposed as the bind address in the reply,
        /// by default it is exposed.
        pub fn hide_local_address(mut self, hide: bool) -> Self {
            self.hide_local_address = hide;
            self
        }
    }
}

impl<C, S> Connector<C, S> {
    /// Overwrite the [`Connector`]'s connector [`Service`]
    /// used to establish the egress [`Io`] in the direction from target to source.
    ///
    /// Any [`Service`] can be used as long as it has the signature:
    ///
    /// ```plain
    /// (ConnectRequest)
    ///     -> (EstablishedConnection<T, ConnectRequest>, Into<BoxError>)
    /// ```
    ///
    /// Replacing a [`DefaultConnector`]'s connector removes its default
    /// connect timeout. Wrap the new connector in [`TimeoutLayer`] when its
    /// connection attempts should be bounded.
    pub fn with_connector<T>(self, connector: T) -> Connector<T, S> {
        Connector {
            connector,
            service: self.service,
            hide_local_address: self.hide_local_address,
        }
    }

    /// Overwrite the [`Connector`]'s [`Service`]
    /// used to actually do the proxy between the source and target [`Io`].
    ///
    /// Any [`Service`] can be used as long as it has the signature:
    ///
    /// ```plain
    /// (BridgeIo) -> ((), Into<BoxError>)
    /// ```
    pub fn with_service<T>(self, service: T) -> Connector<C, T> {
        Connector {
            connector: self.connector,
            service,
            hide_local_address: self.hide_local_address,
        }
    }
}

impl DefaultConnector {
    /// Create a [`DefaultConnector`] whose forward bridge observes graceful
    /// shutdown via the given [`Executor`].
    #[must_use]
    pub fn default_with_exec(exec: Executor) -> Self {
        #[cfg(feature = "dns")]
        let connector = DnsConnector::new(TcpConnector::default());
        #[cfg(not(feature = "dns"))]
        let connector = TcpConnector::default();
        let connector = TimeoutLayer::new(DEFAULT_CONNECT_TIMEOUT).into_layer(connector);

        Self {
            connector,
            service: IoForwardService::new(exec),
            hide_local_address: false,
        }
    }
}

impl Default for DefaultConnector {
    fn default() -> Self {
        Self::default_with_exec(Executor::default())
    }
}

impl<S, InnerConnector, StreamService> Socks5ConnectorSeal<S>
    for Connector<InnerConnector, StreamService>
where
    S: Io + Unpin + ExtensionsRef,
    InnerConnector: ConnectorService<ConnectRequest, Connection: Io + Unpin + ExtensionsRef>,
    StreamService: Service<BridgeIo<S, InnerConnector::Connection>, Error: Into<BoxError>>,
{
    async fn accept_connect(
        &self,
        mut ingress_stream: S,
        destination: HostWithPort,
    ) -> Result<(), Error> {
        tracing::trace!(
            "socks5 server w/ destination {destination}: connect: try to establish connection",
        );

        ingress_stream
            .extensions()
            .insert(ConnectorTarget(destination.clone()));

        // Isolate connector-local routing metadata from the authoritative
        // target exposed by the ingress stream.
        let EstablishedClientConnection {
            conn: egress_stream,
            ..
        } = match self
            .connector
            .connect(ConnectRequest::new_with_extensions(
                destination.clone(),
                ingress_stream.extensions().fork(),
            ))
            .await
        {
            Ok(ecs) => ecs,
            Err(err) => {
                let err: BoxError = err.into();
                tracing::debug!(
                    "socks5 server w/ destination {destination}: abort: connect failed: {err:?}",
                );

                let reply_kind = (&err).into();
                Reply::error_reply(reply_kind)
                    .write_to(&mut ingress_stream)
                    .await
                    .map_err(|err| {
                        Error::io(err).with_context("write server reply: connect failed")
                    })?;
                return Err(Error::aborted("connect failed")
                    .with_context(reply_kind)
                    .with_source(err));
            }
        };

        let socket_info = egress_stream.extensions().get_ref::<SocketInfo>();
        let egress_addr_local = socket_info
            .and_then(SocketInfo::local_addr)
            .map(Into::into)
            .unwrap_or_else(|| {
                tracing::debug!(
                    "socks5 server w/ destination: {destination}: connect: established conn has no local SocketInfo addr, use default '0.0.0.0:0'",
                );
                HostWithPort::default_ipv4(0)
            });
        let egress_addr = socket_info.map(SocketInfo::peer_addr);

        tracing::trace!(
            "socks5 server w/ destination {destination}: connect: connection established, serve pipe: {egress_addr_local} <-> {egress_addr:?}",
        );

        Reply::new(if self.hide_local_address {
            HostWithPort::default_ipv4(0)
        } else {
            egress_addr_local.clone()
        })
        .write_to(&mut ingress_stream)
        .await
        .map_err(|err| Error::io(err).with_context("write server reply: connect succeeded"))?;

        tracing::trace!(
            "socks5 server w/ destination {destination}: connect: reply sent, start serving source-target pipe: {egress_addr_local} <-> {egress_addr:?}",
        );

        self.service
            .serve(BridgeIo(ingress_stream, egress_stream))
            .instrument(trace_span!("socks5::connect::proxy::serve"))
            .await
            .map(drop)
            .map_err(|err| Error::service(err).with_context("serve connect pipe"))
    }
}

/// Lazy [`Socks5Connector`] implementation,
/// which accepts a connection but delegates all the work
/// on the egress side to the inner (stream) service.
///
/// This connector is useful for proxy routers that need application bytes before
/// they can select an egress target. A regular MITM proxy should instead use
/// [`Connector`] and inspect its pre-established [`BridgeIo`].
///
/// ## Default Connectors
///
/// Please use [`Connector`] for the more common SOCKS5 proxy use case. It
/// establishes the destination connection before returning a successful reply,
/// ready for piping between the incoming and established streams.
#[derive(Debug, Clone)]
pub struct LazyConnector<S> {
    service: S,
}

impl<S> LazyConnector<S> {
    /// Create a new [`LazyConnector`].
    ///
    /// The default [`LazyConnector`] forwards the stream as-is to the
    /// received proxy target.
    pub fn new(service: S) -> Self {
        Self { service }
    }
}

impl LazyConnector<IoToProxyBridgeIo<IoForwardService>> {
    /// Create a [`LazyConnector`] whose forward bridge observes graceful
    /// shutdown via the given [`Executor`].
    #[must_use]
    pub fn default_with_exec(exec: Executor) -> Self {
        Self {
            service: IoToProxyBridgeIo::extension_connector_target(IoForwardService::new(exec)),
        }
    }
}

impl Default for LazyConnector<IoToProxyBridgeIo<IoForwardService>> {
    #[inline(always)]
    fn default() -> Self {
        Self::default_with_exec(Executor::default())
    }
}

impl<S, StreamService> Socks5ConnectorSeal<S> for LazyConnector<StreamService>
where
    S: Io + Unpin + ExtensionsRef,
    StreamService: Service<S, Error: Into<BoxError>>,
{
    async fn accept_connect(&self, mut stream: S, destination: HostWithPort) -> Result<(), Error> {
        tracing::trace!(
            "socks5 server w/ destination {destination}: lazy connect: acknowledge without establishing egress connection",
        );

        Reply::new(HostWithPort::default_ipv4(0))
            .write_to(&mut stream)
            .await
            .map_err(|err| Error::io(err).with_context("write server reply: connect succeeded"))?;

        tracing::trace!(
            "socks5 server w/ destination {destination}: lazy connect: reply sent, delegate to inner stream service",
        );

        stream.extensions().insert(ConnectorTarget(destination));

        self.service
            .serve(stream)
            .instrument(trace_span!("socks5::connect::lazy::serve"))
            .await
            .map(drop)
            .map_err(|err| Error::service(err).with_context("inner stream (proxy) service"))
    }
}

#[cfg(test)]
pub(crate) use test::MockConnector;

#[cfg(test)]
mod test {
    #![expect(
        clippy::unreachable,
        reason = "test fixtures: arms gated on the mock variants the test sets up"
    )]

    use super::*;
    use rama_net::address::HostWithPort;
    use std::{ops::DerefMut, sync::Arc};
    use tokio::sync::Mutex;

    #[derive(Debug)]
    pub(crate) struct MockConnector {
        reply: MockReply,
    }

    #[derive(Debug)]
    enum MockReply {
        Success {
            local_addr: HostWithPort,
            target: Option<Arc<Mutex<tokio_test::io::Mock>>>,
        },
        Error(ReplyKind),
    }

    impl MockConnector {
        pub(crate) fn new(local_addr: HostWithPort) -> Self {
            Self {
                reply: MockReply::Success {
                    local_addr,
                    target: None,
                },
            }
        }
        pub(crate) fn new_err(reply: ReplyKind) -> Self {
            Self {
                reply: MockReply::Error(reply),
            }
        }

        pub(crate) fn with_proxy_data(mut self, target: tokio_test::io::Mock) -> Self {
            self.reply = match self.reply {
                MockReply::Success { local_addr, .. } => MockReply::Success {
                    local_addr,
                    target: Some(Arc::new(Mutex::new(target))),
                },
                MockReply::Error(_) => unreachable!(),
            };
            self
        }
    }

    impl<S> Socks5ConnectorSeal<S> for MockConnector
    where
        S: Io + Unpin,
    {
        async fn accept_connect(
            &self,
            mut stream: S,
            _destination: HostWithPort,
        ) -> Result<(), Error> {
            match &self.reply {
                MockReply::Success { local_addr, target } => {
                    Reply::new(local_addr.clone())
                        .write_to(&mut stream)
                        .await
                        .map_err(Error::io)?;

                    if let Some(target) = target.as_ref() {
                        let mut target = target.lock().await;
                        match tokio::io::copy_bidirectional(&mut stream, target.deref_mut()).await {
                            Ok((bytes_copied_north, bytes_copied_south)) => {
                                tracing::trace!(
                                    "(proxy) I/O stream forwarder finished: bytes north = {}; bytes south = {}",
                                    bytes_copied_north,
                                    bytes_copied_south,
                                );
                                Ok(())
                            }
                            Err(err) => {
                                if rama_net::conn::is_connection_error(&err) {
                                    Ok(())
                                } else {
                                    Err(Error::io(err))
                                }
                            }
                        }
                    } else {
                        Ok(())
                    }
                }
                MockReply::Error(reply_kind) => {
                    Reply::error_reply(*reply_kind)
                        .write_to(&mut stream)
                        .await
                        .map_err(Error::io)?;
                    Err(Error::aborted("mock abort").with_context(*reply_kind))
                }
            }
        }
    }
}