gcdevproxy 0.3.0

GoodCam Device Proxy library
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
//! # GoodCam Device Proxy
//!
//! This library simplifies creating HTTP proxies that can be used to communicate
//! with GoodCam devices in various networks. GoodCam devices contain a
//! [built-in client](https://goodcam.github.io/goodcam-api/#tag/cloud>) that
//! can be configured to connect automatically to a given proxy. Once
//! connected, the devices will wait for incoming HTTP requests. The proxy
//! simply forwards incoming HTTP requests to the connected devices.
//!
//! ## Usage example
//!
//! See the `examples` directory in the root of this repository for a
//! ready-to-use example.
//!
//! ```ignore
//! use gcdevproxy::{
//!     async_trait::async_trait,
//!     auth::BasicAuthorization,
//!     http::{Body, Request},
//!     ClientHandlerResult, DeviceHandlerResult, Error, RequestHandler,
//! };
//!
//! struct MyRequestHandler;
//!
//! #[async_trait]
//! impl RequestHandler for MyRequestHandler {
//!     async fn handle_device_request(
//!         &self,
//!         authorization: BasicAuthorization,
//!     ) -> Result<DeviceHandlerResult, Error> {
//!         ...
//!     }
//!
//!     async fn handle_client_request(
//!         &self,
//!         request: Request<Body>,
//!     ) -> Result<ClientHandlerResult, Error> {
//!         ...
//!     }
//! }
//!
//! let mut builder = ProxyBuilder::new();
//!
//! builder
//!     .hostname(hostname)
//!     .http_bind_address(SocketAddr::from((Ipv4Addr::UNSPECIFIED, 8080)));
//!
//! builder
//!     .build(MyRequestHandler)
//!     .await
//!     .unwrap()
//!     .await
//!     .unwrap();
//! ```

#[macro_use]
extern crate log;

mod acme;
mod binding;
mod body;
mod device;
mod error;
mod response;
mod shutdown;
mod tls;
mod utils;

#[cfg(feature = "c-api")]
mod exports;

pub mod auth;

use std::{
    future::Future,
    io,
    net::SocketAddr,
    pin::Pin,
    str::FromStr,
    sync::Arc,
    task::{Context, Poll},
    time::Duration,
};

use futures::{
    channel::mpsc::{self, UnboundedSender},
    future::{AbortHandle, Either},
    FutureExt, StreamExt,
};
use hyper::{body::Incoming, Request, Response};
use hyper_util::rt::{TokioExecutor, TokioIo, TokioTimer};
use uuid::Uuid;

pub use async_trait;
pub use hyper;
pub use hyper::http;

use self::{
    acme::{ChallengeRegistrations, Watchdog},
    auth::BasicAuthorization,
    binding::Bindings,
    device::{DeviceConnection, DeviceManager},
    error::{HttpError, Unauthorized},
    shutdown::{GracefulShutdown, UpgradeableConnectionExt},
    tls::{Identity, TlsAcceptor, TlsMode},
    utils::{AbortOnDrop, RequestExt},
};

pub use self::{binding::ConnectionInfo, body::Body, error::Error};

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

/// Possible results of a device connection handler.
pub enum DeviceHandlerResult {
    /// Accept the corresponding device connection.
    Accept,

    /// Reject the corresponding device connection.
    Unauthorized,

    /// Redirect the corresponding device to a given location.
    Redirect(String),
}

impl DeviceHandlerResult {
    /// Accept the corresponding device connection.
    pub fn accept() -> Self {
        Self::Accept
    }

    /// Reject the corresponding device connection.
    pub fn unauthorized() -> Self {
        Self::Unauthorized
    }

    /// Redirect the corresponding device to a given location.
    ///
    /// This can be used for example to implement load balancing by redirecting
    /// incoming devices to another service if the capacity of the current
    /// service is reached.
    pub fn redirect<T>(location: T) -> Self
    where
        T: ToString,
    {
        Self::Redirect(location.to_string())
    }
}

/// Possible results of a client handler.
pub enum ClientHandlerResult {
    /// Forward the request to a given device.
    Forward(String, Request<Body>),

    /// Block the corresponding request and return a given response back to the
    /// client.
    Block(Response<Body>),
}

impl ClientHandlerResult {
    /// Forward the request to a given device.
    pub fn forward<T>(device_id: T, request: Request<Body>) -> Self
    where
        T: ToString,
    {
        Self::Forward(device_id.to_string(), request)
    }

    /// Block the corresponding request and return a given response back to the
    /// client.
    pub fn block(response: Response<Body>) -> Self {
        Self::Block(response)
    }
}

/// Common trait for proxy request handlers.
#[async_trait::async_trait]
pub trait RequestHandler {
    /// Handle a given device request.
    ///
    /// The method is responsible for device authentication and (optionally)
    /// load balancing. It is called every time a GoodCam device connects to
    /// the proxy. The implementation should check the device ID and key in the
    /// authorization object.
    async fn handle_device_request(
        &self,
        authorization: BasicAuthorization,
    ) -> Result<DeviceHandlerResult, Error>;

    /// Handle a given client request.
    ///
    /// The method is responsible for authentication of a given client request.
    /// It is called every time a client is attempting to send an HTTP request
    /// to a GoodCam device. The implementation should verify the client
    /// identity and permission to access a given device. It is also
    /// responsible for extracting the target device ID from the request.
    async fn handle_client_request(
        &self,
        request: Request<Body>,
    ) -> Result<ClientHandlerResult, Error>;
}

/// Blocking version of the request handler trait.
///
/// See [`RequestHandler`] for more info.
pub trait BlockingRequestHandler {
    /// Handle a given device request.
    fn handle_device_request(
        &self,
        authorization: BasicAuthorization,
    ) -> Result<DeviceHandlerResult, Error>;

    /// Handle a given client request.
    fn handle_client_request(&self, request: Request<Body>) -> Result<ClientHandlerResult, Error>;
}

/// Adapter to make a [`RequestHandler`] from a given
/// [`BlockingRequestHandler`].
pub struct RequestHandlerAdapter<T> {
    inner: Arc<T>,
}

#[async_trait::async_trait]
impl<T> RequestHandler for RequestHandlerAdapter<T>
where
    T: BlockingRequestHandler + Send + Sync + 'static,
{
    async fn handle_device_request(
        &self,
        authorization: BasicAuthorization,
    ) -> Result<DeviceHandlerResult, Error> {
        let inner = self.inner.clone();

        let blocking =
            tokio::task::spawn_blocking(move || inner.handle_device_request(authorization));

        blocking
            .await
            .map_err(|_| Error::from_static_msg("terminating"))?
    }

    async fn handle_client_request(
        &self,
        request: Request<Body>,
    ) -> Result<ClientHandlerResult, Error> {
        let inner = self.inner.clone();

        let blocking = tokio::task::spawn_blocking(move || inner.handle_client_request(request));

        blocking
            .await
            .map_err(|_| Error::from_static_msg("terminating"))?
    }
}

impl<T> From<T> for RequestHandlerAdapter<T> {
    fn from(handler: T) -> Self {
        Self {
            inner: Arc::new(handler),
        }
    }
}

/// Proxy builder.
pub struct ProxyBuilder {
    hostname: String,
    http_bind_addresses: Vec<SocketAddr>,
    https_bind_addresses: Vec<SocketAddr>,
    tls_mode: TlsMode,
}

impl ProxyBuilder {
    /// Create a new builder.
    pub fn new() -> Self {
        Self {
            hostname: String::from("localhost"),
            http_bind_addresses: Vec::new(),
            https_bind_addresses: Vec::new(),
            tls_mode: TlsMode::None,
        }
    }

    /// Set the hostname where the proxy will be available.
    pub fn hostname<T>(&mut self, hostname: T) -> &mut Self
    where
        T: ToString,
    {
        self.hostname = hostname.to_string();
        self
    }

    /// Add a given HTTP binding.
    pub fn http_bind_address(&mut self, addr: SocketAddr) -> &mut Self {
        self.http_bind_addresses.push(addr);
        self
    }

    /// Add a given HTTPS binding.
    pub fn https_bind_address(&mut self, addr: SocketAddr) -> &mut Self {
        self.https_bind_addresses.push(addr);
        self
    }

    /// Set TLS identity (used for HTTPS).
    ///
    /// # Arguments
    /// * `key` - key in PEM format
    /// * `cert` - certificate chain in PEM format
    pub fn tls_identity(&mut self, key: &[u8], cert: &[u8]) -> Result<&mut Self, Error> {
        let identity = Identity::from_pkcs8(cert, key)?;

        self.tls_mode = TlsMode::Simple(identity);

        Ok(self)
    }

    /// Use Let's Encrypt to generate the TLS key and certificate chain
    /// automatically.
    ///
    /// Please note that Let's encrypt requires HTTP services to be available
    /// on a public domain name on TCP port 80 in order to issue a TLS
    /// certificate. Make sure that you set the proxy hostname and that you
    /// add at least the `0.0.0.0:80` HTTP binding.
    pub fn lets_encrypt(&mut self) -> &mut Self {
        self.tls_mode = TlsMode::LetsEncrypt;
        self
    }

    /// Build the proxy and use a given request handler to handle incoming
    /// connections.
    pub async fn build<T>(&self, request_handler: T) -> Result<Proxy, Error>
    where
        T: RequestHandler + Send + Sync + 'static,
    {
        info!("starting GoodCam device proxy");

        let res = self.build_inner(request_handler).await;

        if let Err(err) = &res {
            warn!("unable to start the proxy: {err}");
        } else {
            info!("proxy started");
        }

        res
    }

    /// Build the proxy and use a given request handler to handle incoming
    /// connections.
    async fn build_inner<T>(&self, request_handler: T) -> Result<Proxy, Error>
    where
        T: RequestHandler + Send + Sync + 'static,
    {
        let tls_acceptor = self.tls_mode.create_tls_acceptor()?;

        let bindings = self.create_bindings(tls_acceptor.clone()).await?;

        info!("hostname: {}", self.hostname);

        let acme_challenges = ChallengeRegistrations::new();

        let mut acme_watchdog = None;

        if let Some(tls_acceptor) = tls_acceptor {
            if let Some(watchdog) = self
                .create_acme_watchdog(tls_acceptor, acme_challenges.clone())
                .await?
            {
                acme_watchdog = Some(watchdog);
            }
        }

        let handler = InternalRequestHandler {
            acme_challenges,
            devices: DeviceManager::new(),
            handler: request_handler.into(),
        };

        let (shutdown_tx, mut shutdown_rx) = mpsc::unbounded();

        let server = self.create_proxy(bindings, handler, async move {
            if shutdown_rx.next().await.is_none() {
                futures::future::pending().await
            }
        });

        self.start_proxy(server, shutdown_tx, acme_watchdog)
    }

    /// Create port bindings.
    async fn create_bindings(&self, tls_acceptor: Option<TlsAcceptor>) -> io::Result<Bindings> {
        let http_bind_addresses = self.http_bind_addresses.iter();
        let https_bind_addresses = self.https_bind_addresses.iter();

        let mut bindings = Bindings::new();

        for binding in http_bind_addresses.clone() {
            info!("HTTP binding: {binding}");
        }

        bindings
            .add_tcp_bindings(http_bind_addresses.copied())
            .await?;

        if let Some(acceptor) = tls_acceptor {
            for binding in https_bind_addresses.clone() {
                info!("HTTPS binding: {binding}");
            }

            bindings
                .add_tls_bindings(acceptor, https_bind_addresses.copied())
                .await?;
        }

        Ok(bindings)
    }

    /// Create ACME watchdog (if configured).
    async fn create_acme_watchdog(
        &self,
        tls_acceptor: TlsAcceptor,
        challenge_registrations: ChallengeRegistrations,
    ) -> Result<Option<Watchdog>, Error> {
        if let Some(acme_account) = self.tls_mode.create_acme_account().await? {
            let watchdog = Watchdog::new(
                acme_account,
                challenge_registrations,
                tls_acceptor,
                &self.hostname,
            );

            let res = watchdog.await?;

            Ok(Some(res))
        } else {
            Ok(None)
        }
    }

    /// Create the proxy service.
    fn create_proxy<T, F>(
        &self,
        mut bindings: Bindings,
        handler: InternalRequestHandler<T>,
        shutdown: F,
    ) -> impl Future<Output = Result<(), Error>>
    where
        T: RequestHandler + Send + Sync + 'static,
        F: Future,
    {
        let executor = TokioExecutor::new();
        let timer = TokioTimer::new();

        let mut builder = hyper_util::server::conn::auto::Builder::new(executor);

        builder
            .http1()
            .header_read_timeout(Duration::from_secs(60))
            .keep_alive(true)
            .timer(timer.clone());

        builder
            .http2()
            .keep_alive_interval(Some(Duration::from_secs(120)))
            .keep_alive_timeout(Duration::from_secs(20))
            .timer(timer);

        async move {
            let connections = GracefulShutdown::new();

            futures::pin_mut!(shutdown);

            loop {
                let next = bindings.next();

                let connection = match futures::future::select(&mut shutdown, next).await {
                    Either::Left((_, _)) => break,
                    Either::Right((res, _)) => match res.transpose() {
                        Ok(Some(c)) => c,
                        Ok(None) => return Err(Error::from_static_msg("server socket(s) closed")),
                        Err(err) => return Err(err.into()),
                    },
                };

                let shutdown_registration = connections.register_task();
                let info = connection.info();
                let handler = handler.clone();
                let builder = builder.clone();

                let io = TokioIo::new(connection);

                let service = hyper::service::service_fn(move |mut request| {
                    let extensions = request.extensions_mut();

                    extensions.insert(info);

                    let handler = handler.clone();

                    async move {
                        let response = handler.handle_request(request).await;

                        Ok(response) as Result<_, hyper::Error>
                    }
                });

                tokio::spawn(async move {
                    let serve = builder
                        .serve_connection_with_upgrades(io, service)
                        .with_graceful_shutdown(shutdown_registration);

                    if let Err(err) = serve.await {
                        debug!("connection error: {err}");
                    }
                });
            }

            connections.shutdown().await;

            Ok(())
        }
    }

    /// Start the proxy service.
    fn start_proxy<F>(
        &self,
        proxy: F,
        shutdown: UnboundedSender<()>,
        acme_watchdog: Option<Watchdog>,
    ) -> Result<Proxy, Error>
    where
        F: Future<Output = Result<(), Error>> + Send + 'static,
    {
        let (server, server_handle) = futures::future::abortable(proxy);

        let server = AbortOnDrop::from(tokio::spawn(server));

        let watchdog = if let Some(watchdog) = acme_watchdog {
            AbortOnDrop::from(tokio::spawn(watchdog.watch()))
        } else {
            AbortOnDrop::from(tokio::spawn(futures::future::pending()))
        };

        let join = async move {
            let res = match server.await {
                Ok(Ok(Ok(()))) => Ok(()),
                Ok(Ok(Err(err))) => Err(err),
                Ok(Err(_)) => Ok(()),
                Err(_) => Ok(()),
            };

            watchdog.abort();

            let _ = watchdog.await;

            res
        };

        let handle = ProxyHandle {
            shutdown,
            abort: server_handle,
        };

        let proxy = Proxy {
            join: Box::pin(join),
            handle,
        };

        Ok(proxy)
    }
}

impl Default for ProxyBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// GoodCam device proxy.
///
/// The proxy itself is a future that will be resolved when the proxy stops.
/// The proxy runs in a background task, so the future does not have to be
/// polled in order to run the proxy. However, dropping the future will also
/// abort the background task.
pub struct Proxy {
    join: Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>,
    handle: ProxyHandle,
}

impl Proxy {
    /// Get a proxy builder.
    pub fn builder() -> ProxyBuilder {
        ProxyBuilder::new()
    }

    /// Get a proxy handle.
    pub fn handle(&self) -> ProxyHandle {
        self.handle.clone()
    }
}

impl Future for Proxy {
    type Output = Result<(), Error>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.join.poll_unpin(cx)
    }
}

/// Proxy handle.
#[derive(Clone)]
pub struct ProxyHandle {
    shutdown: UnboundedSender<()>,
    abort: AbortHandle,
}

impl ProxyHandle {
    /// Gracefully stop the proxy.
    pub fn stop(&self) {
        let _ = self.shutdown.unbounded_send(());
    }

    /// Abort the proxy execution.
    pub fn abort(&self) {
        self.abort.abort();
    }
}

/// Internal request handler.
struct InternalRequestHandler<T> {
    acme_challenges: acme::ChallengeRegistrations,
    devices: DeviceManager,
    handler: Arc<T>,
}

impl<T> InternalRequestHandler<T>
where
    T: RequestHandler + Send + Sync + 'static,
{
    /// Handle a given request.
    async fn handle_request(&self, request: Request<Incoming>) -> Response<Body> {
        self.handle_request_inner(request.map(Body::from))
            .await
            .unwrap_or_else(|err| {
                if let Some(response) = err.to_response() {
                    return response;
                }

                warn!("internal server error: {err}");

                response::internal_server_error()
            })
    }

    /// Handle a given request.
    async fn handle_request_inner(
        &self,
        request: Request<Body>,
    ) -> Result<Response<Body>, HttpError> {
        if let Some(token) = request.get_acme_challenge_token() {
            if let Some(response) = self.acme_challenges.create_response(token) {
                return Ok(response);
            }
        }

        if request.is_device_request() {
            self.handle_device_request(request).await
        } else {
            self.handle_client_request(request).await
        }
    }

    /// Handle a given device request.
    async fn handle_device_request(
        &self,
        request: Request<Body>,
    ) -> Result<Response<Body>, HttpError> {
        let session_id = Uuid::new_v4();

        info!("received device connection request (session_id: {session_id})");

        let authorization = request
            .headers()
            .get("authorization")
            .map(|auth| auth.to_str())
            .and_then(|res| res.ok())
            .map(BasicAuthorization::from_str)
            .and_then(|res| res.ok());

        if authorization.is_none() {
            warn!("unable to process device connection request (session_id: {session_id}): missing or invalid authorization header");
        }

        let authorization = authorization.ok_or(Unauthorized)?;

        let res = self
            .handle_device_request_inner(session_id, authorization, request)
            .await;

        if let Err(err) = &res {
            warn!("unable to process device connection request (session_id: {session_id}): {err}");
        }

        res.map_err(HttpError::from)
    }

    /// Handle a given device request.
    async fn handle_device_request_inner(
        &self,
        session_id: Uuid,
        authorization: BasicAuthorization,
        request: Request<Body>,
    ) -> Result<Response<Body>, Error> {
        let device_id = String::from(authorization.username());

        match self.handler.handle_device_request(authorization).await? {
            DeviceHandlerResult::Accept => {
                let this = self.clone();

                tokio::spawn(async move {
                    if let Err(err) = this
                        .handle_device_connection(&device_id, session_id, request)
                        .await
                    {
                        warn!("unable to upgrade device connection (device_id: {device_id}, session_id: {session_id}): {err}");
                    }
                });

                let res = Response::builder()
                    .status(101)
                    .header("Upgrade", "goodcam-device-proxy")
                    .body(Body::empty())
                    .unwrap();

                Ok(res)
            }
            DeviceHandlerResult::Unauthorized => {
                info!("unauthorized device (device_id: {device_id}, session_id: {session_id})");

                Ok(response::unauthorized())
            }
            DeviceHandlerResult::Redirect(location) => {
                info!("redirecting device (device_id: {device_id}, session_id: {session_id}, location: {location})");

                Ok(response::temporary_redirect(location))
            }
        }
    }

    /// Handle a new device connection.
    async fn handle_device_connection(
        &self,
        device_id: &str,
        session_id: Uuid,
        request: Request<Body>,
    ) -> Result<(), Error> {
        let handshake = DeviceConnection::new(hyper::upgrade::on(request));

        let (connection, handle) = tokio::time::timeout(DEVICE_HANDSHAKE_TIMEOUT, handshake)
            .await
            .map_err(|_| Error::from_static_msg("device handshake timeout"))??;

        if let Some(old) = self.devices.add(device_id, session_id, handle) {
            old.close();
        }

        info!("device connected (device_id: {device_id}, session_id: {session_id})");

        if let Err(err) = connection.await {
            warn!(
                "device connection error (device_id: {device_id}, session_id: {session_id}): {err}"
            );
        } else {
            info!("device disconnected (device_id: {device_id}, session_id: {session_id})");
        }

        self.devices.remove(device_id, Some(session_id));

        Ok(())
    }

    /// Handle a given client request.
    async fn handle_client_request(
        &self,
        request: Request<Body>,
    ) -> Result<Response<Body>, HttpError> {
        let request_id = Uuid::new_v4();

        let method = request.method();
        let uri = request.uri();
        let path = uri.path();

        info!("received client {method} request at {path} (request_id: {request_id})");

        let res = self.handle_client_request_inner(request_id, request).await;

        if let Err(err) = &res {
            warn!("unable to complete client request (request_id: {request_id}): {err}");
        }

        res.map_err(HttpError::from)
    }

    /// Handle a given client request.
    async fn handle_client_request_inner(
        &self,
        request_id: Uuid,
        request: Request<Body>,
    ) -> Result<Response<Body>, Error> {
        match self.handler.handle_client_request(request).await? {
            ClientHandlerResult::Block(response) => {
                info!("client request blocked (request_id: {request_id})");

                Ok(response)
            }
            ClientHandlerResult::Forward(device_id, request) => {
                info!(
                    "forwarding client request (request_id: {request_id}, device_id: {device_id})"
                );

                if let Some(mut device) = self.devices.get(&device_id) {
                    match device.send_request(request).await {
                        Ok(response) => {
                            let status = response.status();

                            info!("forwarding device response (request_id: {request_id}, device_id: {device_id}, status: {status})");

                            Ok(response)
                        }
                        Err(err) => {
                            info!("unable to forward client request (request_id: {request_id}, device_id: {device_id}): {err}");

                            Ok(response::bad_gateway())
                        }
                    }
                } else {
                    info!("unable to forward client request (request_id: {request_id}, device_id: {device_id}): device not connected");

                    Ok(response::bad_gateway())
                }
            }
        }
    }
}

impl<T> Clone for InternalRequestHandler<T> {
    fn clone(&self) -> Self {
        Self {
            acme_challenges: self.acme_challenges.clone(),
            devices: self.devices.clone(),
            handler: self.handler.clone(),
        }
    }
}