-
Notifications
You must be signed in to change notification settings - Fork 16
/
traits.rs
132 lines (118 loc) · 3.89 KB
/
traits.rs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license OR Apache 2.0
use crate::util::tunnel_stream::{TunnelStream, WrappedStream};
use downcast_rs::{impl_downcast, Downcast, DowncastSync};
use futures::{
future::{BoxFuture, FutureExt},
TryFutureExt,
};
use std::{
any::Any,
backtrace::Backtrace,
collections::BTreeMap,
fmt::{Debug, Display},
sync::{Arc, Weak},
};
use super::{
tunnel::{registry::TunnelRegistry, Tunnel, TunnelId, TunnelName},
RouteAddress,
};
use crate::common::protocol::tunnel::TunnelError;
#[derive(thiserror::Error, Debug)]
#[error(bound = std::fmt::Debug)]
pub enum ServiceError<InternalError> {
#[error("Address refused by client")]
Refused,
#[error("Unexpected end of stream with remote")]
UnexpectedEnd,
#[error("Illegal response from remote")]
IllegalResponse,
#[error("Invalid address provided by remote client")]
AddressError,
#[error("An internal dependency failed")]
DependencyFailure,
#[error("An internal dependency failed with a backtrace")]
BacktraceDependencyFailure(Backtrace),
#[error("Internal service error")]
InternalError(InternalError),
#[error(transparent)]
InternalFailure(anyhow::Error),
}
impl<InternalError> ServiceError<InternalError> {
pub fn map_internal<TNewError, F>(self, f: F) -> ServiceError<TNewError>
where
F: Fn(InternalError) -> TNewError,
{
match self {
ServiceError::Refused => ServiceError::Refused,
ServiceError::UnexpectedEnd => ServiceError::UnexpectedEnd,
ServiceError::IllegalResponse => ServiceError::IllegalResponse,
ServiceError::AddressError => ServiceError::AddressError,
ServiceError::DependencyFailure => ServiceError::DependencyFailure,
ServiceError::BacktraceDependencyFailure(e) => ServiceError::BacktraceDependencyFailure(e),
ServiceError::InternalError(e) => ServiceError::InternalError(f(e)),
ServiceError::InternalFailure(e) => ServiceError::InternalFailure(e),
}
}
pub fn err_into<TNewError>(self) -> ServiceError<TNewError>
where
TNewError: From<InternalError>,
{
self.map_internal(From::from)
}
}
impl<InternalError: std::fmt::Debug + std::fmt::Display> From<InternalError>
for ServiceError<InternalError>
{
fn from(e: InternalError) -> Self {
Self::InternalError(e)
}
}
pub trait Service {
type Error;
fn accepts(&self, addr: &RouteAddress, tunnel_id: &TunnelId) -> bool;
// fn protocol_id() -> String where Self: Sized;
fn handle<'a>(
&'a self,
addr: RouteAddress,
stream: Box<dyn TunnelStream + Send + 'static>,
tunnel_id: TunnelId,
) -> BoxFuture<'a, Result<(), ServiceError<Self::Error>>>;
}
pub trait MappedService<TIntoError: std::fmt::Debug + std::fmt::Display> {
fn accepts_mapped(&self, addr: &RouteAddress, tunnel_id: &TunnelId) -> bool;
// fn protocol_id() -> String where Self: Sized;
fn handle_mapped<'a>(
&'a self,
addr: RouteAddress,
stream: Box<dyn TunnelStream + Send + 'static>,
tunnel_id: TunnelId,
) -> BoxFuture<'a, Result<(), ServiceError<TIntoError>>>;
}
impl<TInnerService, TIntoError> MappedService<TIntoError> for TInnerService
where
TInnerService: Service,
TIntoError: From<<Self as Service>::Error> + std::fmt::Debug + std::fmt::Display,
{
fn accepts_mapped(&self, addr: &RouteAddress, tunnel_id: &TunnelId) -> bool {
Service::accepts(self, addr, tunnel_id)
}
fn handle_mapped<'a>(
&'a self,
addr: RouteAddress,
stream: Box<dyn TunnelStream + Send + 'static>,
tunnel_id: TunnelId,
) -> BoxFuture<'a, Result<(), ServiceError<TIntoError>>> {
Service::handle(self, addr, stream, tunnel_id)
.map_err(|e| ServiceError::err_into(e))
.boxed()
}
}
pub trait ServiceRegistry {
type Error: std::fmt::Debug + std::fmt::Display;
fn find_service(
self: Arc<Self>,
addr: &RouteAddress,
tunnel_id: &TunnelId,
) -> Option<Arc<dyn MappedService<Self::Error> + Send + Sync + 'static>>;
}