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
#[cfg(feature = "ssl")]
use std::io::{Error, ErrorKind};
use std::io::{Read, Result, Write};
use std::net::{SocketAddr, TcpStream};
#[cfg(feature = "ssl")]
use openssl::ssl::{Ssl, SslContext, SslFiletype, SslMethod, SslOptions, SslStream, SslVerifyMode};
#[derive(Clone)]
pub enum StreamConnector {
Tcp,
#[cfg(feature = "ssl")]
Ssl {
ca_file: String,
certificate_file: Option<String>,
key_file: Option<String>,
verify_peer: bool,
},
}
impl Default for StreamConnector {
fn default() -> Self {
StreamConnector::Tcp
}
}
impl StreamConnector {
#[cfg(feature = "ssl")]
pub fn with_ssl(
ca_file: &str,
certificate_file: &str,
key_file: &str,
verify_peer: bool,
) -> Self {
StreamConnector::Ssl {
ca_file: String::from(ca_file),
certificate_file: Some(String::from(certificate_file)),
key_file: Some(String::from(key_file)),
verify_peer: verify_peer,
}
}
#[cfg(feature = "ssl")]
pub fn with_unauthenticated_ssl(ca_file: &str, verify_peer: bool) -> Self {
StreamConnector::Ssl {
ca_file: String::from(ca_file),
certificate_file: None,
key_file: None,
verify_peer: verify_peer,
}
}
pub fn connect(&self, hostname: &str, port: u16) -> Result<Stream> {
match *self {
StreamConnector::Tcp => TcpStream::connect((hostname, port)).map(Stream::Tcp),
#[cfg(feature = "ssl")]
StreamConnector::Ssl {
ref ca_file,
ref certificate_file,
ref key_file,
verify_peer,
} => {
let inner_stream = TcpStream::connect((hostname, port))?;
let mut ssl_context = SslContext::builder(SslMethod::tls())?;
ssl_context.set_cipher_list("ALL:!EXPORT:!eNULL:!aNULL:HIGH:@STRENGTH")?;
ssl_context.set_options(SslOptions::NO_SSLV2);
ssl_context.set_options(SslOptions::NO_SSLV3);
ssl_context.set_options(SslOptions::NO_COMPRESSION);
ssl_context.set_ca_file(ca_file)?;
if let &Some(ref file) = certificate_file {
ssl_context.set_certificate_file(file, SslFiletype::PEM)?;
}
if let &Some(ref file) = key_file {
ssl_context.set_private_key_file(file, SslFiletype::PEM)?;
}
let verify = if verify_peer {
SslVerifyMode::PEER
} else {
SslVerifyMode::NONE
};
ssl_context.set_verify(verify);
let mut ssl = Ssl::new(&ssl_context.build())?;
ssl.set_hostname(hostname)?;
match ssl.connect(inner_stream) {
Ok(s) => Ok(Stream::Ssl(s)),
Err(e) => Err(Error::new(ErrorKind::Other, e)),
}
}
}
}
}
pub enum Stream {
Tcp(TcpStream),
#[cfg(feature = "ssl")]
Ssl(SslStream<TcpStream>),
}
impl Read for Stream {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
match *self {
Stream::Tcp(ref mut s) => s.read(buf),
#[cfg(feature = "ssl")]
Stream::Ssl(ref mut s) => s.read(buf),
}
}
}
impl Write for Stream {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
match *self {
Stream::Tcp(ref mut s) => s.write(buf),
#[cfg(feature = "ssl")]
Stream::Ssl(ref mut s) => s.write(buf),
}
}
fn flush(&mut self) -> Result<()> {
match *self {
Stream::Tcp(ref mut s) => s.flush(),
#[cfg(feature = "ssl")]
Stream::Ssl(ref mut s) => s.flush(),
}
}
}
impl Stream {
pub fn peer_addr(&self) -> Result<SocketAddr> {
match *self {
Stream::Tcp(ref stream) => stream.peer_addr(),
#[cfg(feature = "ssl")]
Stream::Ssl(ref stream) => stream.get_ref().peer_addr(),
}
}
}