@@ -17,9 +17,7 @@ impl Connection {
1717 /// Initializes a connection to a NUT server (upsd).
1818 pub fn new ( config : & Config ) -> crate :: Result < Self > {
1919 match & config. host {
20- Host :: Tcp ( socket_addr) => {
21- Ok ( Self :: Tcp ( TcpConnection :: new ( config. clone ( ) , socket_addr) ?) )
22- }
20+ Host :: Tcp ( host) => Ok ( Self :: Tcp ( TcpConnection :: new ( config. clone ( ) , & host. addr ) ?) ) ,
2321 }
2422 }
2523
@@ -62,7 +60,7 @@ impl Connection {
6260/// A blocking TCP NUT client connection.
6361pub struct TcpConnection {
6462 config : Config ,
65- pipeline : ConnectionStream ,
63+ stream : ConnectionStream ,
6664}
6765
6866impl TcpConnection {
@@ -71,7 +69,7 @@ impl TcpConnection {
7169 let tcp_stream = TcpStream :: connect_timeout ( socket_addr, config. timeout ) ?;
7270 let mut connection = Self {
7371 config,
74- pipeline : ConnectionStream :: Plain ( tcp_stream) ,
72+ stream : ConnectionStream :: Plain ( tcp_stream) ,
7573 } ;
7674
7775 // Initialize SSL connection
@@ -98,19 +96,37 @@ impl TcpConnection {
9896 } ) ?
9997 . expect_ok ( ) ?;
10098
101- let mut config = rustls:: ClientConfig :: new ( ) ;
102- config
103- . dangerous ( )
104- . set_certificate_verifier ( std:: sync:: Arc :: new (
105- crate :: ssl:: NutCertificateValidator :: new ( & self . config ) ,
106- ) ) ;
99+ let mut ssl_config = rustls:: ClientConfig :: new ( ) ;
100+ let sess = if self . config . ssl_insecure {
101+ ssl_config
102+ . dangerous ( )
103+ . set_certificate_verifier ( std:: sync:: Arc :: new (
104+ crate :: ssl:: InsecureCertificateValidator :: new ( & self . config ) ,
105+ ) ) ;
106+
107+ let dns_name = webpki:: DNSNameRef :: try_from_ascii_str ( "www.google.com" ) . unwrap ( ) ;
108+
109+ rustls:: ClientSession :: new ( & std:: sync:: Arc :: new ( ssl_config) , dns_name)
110+ } else {
111+ // Try to get hostname as given (e.g. localhost can be used for strict SSL, but not 127.0.0.1)
112+ let hostname = self
113+ . config
114+ . host
115+ . hostname ( )
116+ . ok_or ( ClientError :: Nut ( NutError :: SslInvalidHostname ) ) ?;
117+
118+ let dns_name = webpki:: DNSNameRef :: try_from_ascii_str ( & hostname)
119+ . map_err ( |_| ClientError :: Nut ( NutError :: SslInvalidHostname ) ) ?;
120+
121+ ssl_config
122+ . root_store
123+ . add_server_trust_anchors ( & webpki_roots:: TLS_SERVER_ROOTS ) ;
107124
108- // todo: this DNS name is temporary; should get from connection hostname? (#8)
109- let dns_name = webpki:: DNSNameRef :: try_from_ascii_str ( "www.google.com" ) . unwrap ( ) ;
110- let sess = rustls:: ClientSession :: new ( & std:: sync:: Arc :: new ( config) , dns_name) ;
125+ rustls:: ClientSession :: new ( & std:: sync:: Arc :: new ( ssl_config) , dns_name)
126+ } ;
111127
112128 // Wrap and override the TCP stream
113- self . pipeline = self . pipeline . upgrade_ssl ( sess) ?;
129+ self . stream = self . stream . upgrade_ssl ( sess) ?;
114130
115131 // Send a test command
116132 self . get_network_version ( ) ?;
@@ -179,8 +195,8 @@ impl TcpConnection {
179195 if self . config . debug {
180196 eprint ! ( "DEBUG -> {}" , line) ;
181197 }
182- self . pipeline . write_all ( line. as_bytes ( ) ) ?;
183- self . pipeline . flush ( ) ?;
198+ self . stream . write_all ( line. as_bytes ( ) ) ?;
199+ self . stream . flush ( ) ?;
184200 Ok ( ( ) )
185201 }
186202
@@ -203,19 +219,19 @@ impl TcpConnection {
203219 }
204220
205221 fn read_response ( & mut self ) -> crate :: Result < Response > {
206- let mut reader = BufReader :: new ( & mut self . pipeline ) ;
222+ let mut reader = BufReader :: new ( & mut self . stream ) ;
207223 let args = Self :: parse_line ( & mut reader, self . config . debug ) ?;
208224 Response :: from_args ( args)
209225 }
210226
211227 fn read_plain_response ( & mut self ) -> crate :: Result < String > {
212- let mut reader = BufReader :: new ( & mut self . pipeline ) ;
228+ let mut reader = BufReader :: new ( & mut self . stream ) ;
213229 let args = Self :: parse_line ( & mut reader, self . config . debug ) ?;
214230 Ok ( args. join ( " " ) )
215231 }
216232
217233 fn read_list ( & mut self , query : & [ & str ] ) -> crate :: Result < Vec < Response > > {
218- let mut reader = BufReader :: new ( & mut self . pipeline ) ;
234+ let mut reader = BufReader :: new ( & mut self . stream ) ;
219235 let args = Self :: parse_line ( & mut reader, self . config . debug ) ?;
220236
221237 Response :: from_args ( args) ?. expect_begin_list ( query) ?;
0 commit comments