4
4
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
5
5
// modified, or distributed except according to those terms.
6
6
7
- //! Manages the connection between the API and your host.
8
-
9
7
use bytes:: { BufMut , BytesMut } ;
10
8
use errors:: * ;
11
9
use futures:: { future, Future } ;
12
- use { Executable , Runnable } ;
10
+ use Runnable ;
13
11
use serde:: Deserialize ;
14
12
use serde_json;
15
13
use std:: { io, result} ;
16
14
use std:: sync:: Arc ;
17
15
use std:: net:: SocketAddr ;
16
+ use super :: Host ;
18
17
use telemetry:: { self , Telemetry } ;
19
18
use tokio_core:: net:: TcpStream ;
20
19
use tokio_core:: reactor:: Handle ;
@@ -24,56 +23,16 @@ use tokio_proto::pipeline::{ClientProto, ClientService, ServerProto};
24
23
use tokio_proto:: TcpClient ;
25
24
use tokio_service:: Service ;
26
25
27
- pub trait Host {
28
- /// Retrieve Telemetry
29
- fn telemetry ( & self ) -> & Telemetry ;
30
-
31
- #[ doc( hidden) ]
32
- fn run < D : ' static > ( & self , Runnable ) -> Box < Future < Item = D , Error = Error > >
33
- where for < ' de > D : Deserialize < ' de > ;
34
- }
35
-
36
- pub struct LocalHost {
37
- telemetry : Option < Telemetry > ,
38
- }
39
-
40
- impl LocalHost {
41
- /// Create a new Host targeting the local machine.
42
- pub fn new ( ) -> Box < Future < Item = Arc < LocalHost > , Error = Error > > {
43
- let mut host = Arc :: new ( LocalHost {
44
- telemetry : None ,
45
- } ) ;
46
-
47
- Box :: new ( telemetry:: load ( & host) . map ( |t| {
48
- Arc :: get_mut ( & mut host) . unwrap ( ) . telemetry = Some ( t) ;
49
- host
50
- } ) )
51
- }
52
- }
53
-
54
- impl Host for LocalHost {
55
- fn telemetry ( & self ) -> & Telemetry {
56
- self . telemetry . as_ref ( ) . unwrap ( )
57
- }
58
-
59
- fn run < D : ' static > ( & self , provider : Runnable ) -> Box < Future < Item = D , Error = Error > >
60
- where for < ' de > D : Deserialize < ' de >
61
- {
62
- Box :: new ( provider. exec ( )
63
- . chain_err ( || "Could not run provider" )
64
- . and_then ( |s| {
65
- match serde_json:: to_value ( s) . chain_err ( || "Could not run provider" ) {
66
- Ok ( v) => match serde_json:: from_value :: < D > ( v) . chain_err ( || "Could not run provider" ) {
67
- Ok ( d) => future:: ok ( d) ,
68
- Err ( e) => future:: err ( e) ,
69
- } ,
70
- Err ( e) => future:: err ( e) ,
71
- }
72
- } ) )
73
- }
26
+ pub trait RemoteHost : Host {
27
+ fn connect ( addr : & str , handle : & Handle ) -> Box < Future < Item = Arc < Self > , Error = Error > > ;
74
28
}
75
29
76
- pub struct RemoteHost {
30
+ /// A Host type that uses an unencrypted socket.
31
+ ///
32
+ /// *Warning! This Host type is susceptible to eavesdropping and MITM
33
+ /// attacks, and ideally should only be used for testing on secure private
34
+ /// networks.*
35
+ pub struct Plain {
77
36
inner : ClientService < TcpStream , JsonProto > ,
78
37
telemetry : Option < Telemetry > ,
79
38
}
@@ -83,9 +42,9 @@ pub struct JsonCodec;
83
42
#[ doc( hidden) ]
84
43
pub struct JsonProto ;
85
44
86
- impl RemoteHost {
45
+ impl Plain {
87
46
/// Create a new Host connected to addr.
88
- pub fn connect ( addr : & str , handle : & Handle ) -> Box < Future < Item = Arc < RemoteHost > , Error = Error > > {
47
+ pub fn connect ( addr : & str , handle : & Handle ) -> Box < Future < Item = Arc < Plain > , Error = Error > > {
89
48
let addr: SocketAddr = match addr. parse ( ) . chain_err ( || "Invalid host address" ) {
90
49
Ok ( addr) => addr,
91
50
Err ( e) => return Box :: new ( future:: err ( e) ) ,
@@ -99,7 +58,7 @@ impl RemoteHost {
99
58
. and_then ( |client_service| {
100
59
info ! ( "Connected!" ) ;
101
60
102
- let mut host = Arc :: new ( RemoteHost {
61
+ let mut host = Arc :: new ( Plain {
103
62
inner : client_service,
104
63
telemetry : None ,
105
64
} ) ;
@@ -114,7 +73,7 @@ impl RemoteHost {
114
73
}
115
74
}
116
75
117
- impl Host for RemoteHost {
76
+ impl Host for Plain {
118
77
fn telemetry ( & self ) -> & Telemetry {
119
78
self . telemetry . as_ref ( ) . unwrap ( )
120
79
}
@@ -135,7 +94,7 @@ impl Host for RemoteHost {
135
94
}
136
95
}
137
96
138
- impl Service for RemoteHost {
97
+ impl Service for Plain {
139
98
type Request = serde_json:: Value ;
140
99
type Response = serde_json:: Value ;
141
100
type Error = io:: Error ;
0 commit comments