@ -6,9 +6,8 @@ use std::{
sync ::Arc ,
} ;
use structopt ::StructOpt ;
use zsittle ::futures_util ::future ;
use zsittle ::tokio ::{
io ::{ copy , split , stdin as tokio_stdin , stdout as tokio_stdout } ,
io ::{ split , stdin as tokio_stdin , stdout as tokio_stdout , AsyncBufReadExt , AsyncWriteEx t } ,
net ::TcpStream ,
runtime ,
} ;
@ -41,9 +40,11 @@ fn main() -> io::Result<()> {
let domain = options . domain . unwrap_or ( options . host ) ;
let mut runtime = runtime ::Builder ::new ( )
. basic _scheduler( )
. threaded _scheduler( )
. enable_io ( )
. build ( ) ? ;
let handle = runtime . handle ( ) . clone ( ) ;
let mut config = ClientConfig ::new ( ) ;
if let Some ( cafile ) = & options . cafile {
let mut pem = BufReader ::new ( File ::open ( cafile ) ? ) ;
@ -56,27 +57,32 @@ fn main() -> io::Result<()> {
. root_store
. add_server_trust_anchors ( & webpki_roots ::TLS_SERVER_ROOTS ) ;
}
let connector = TlsConnector ::from ( Arc ::new ( config ) ) ;
let domain = DNSNameRef ::try_from_ascii_str ( & domain )
. map_err ( | _ | io ::Error ::new ( io ::ErrorKind ::InvalidInput , "invalid dnsname" ) ) ? ;
let fut = async {
let stream = TcpStream ::connect ( & addr ) . await ? ;
let ( mut stdin , mut stdout ) = ( tokio_stdin ( ) , tokio_stdout ( ) ) ;
let domain = DNSNameRef ::try_from_ascii_str ( & domain )
. map_err ( | _ | io ::Error ::new ( io ::ErrorKind ::InvalidInput , "invalid dnsname" ) ) ? ;
let ( stdin , mut stdout ) = ( tokio_stdin ( ) , tokio_stdout ( ) ) ;
let stream = connector . connect ( domain , stream ) . await ? ;
let ( mut reader , mut writer ) = split ( stream ) ;
future ::select (
copy ( & mut reader , & mut stdout ) ,
copy ( & mut stdin , & mut writer ) ,
)
. await
. factor_first ( )
. 0 ? ;
let wtk : zsittle ::tokio ::task ::JoinHandle < Result < ( ) , zsittle ::ReadError > > =
handle . spawn ( async move {
loop {
let blb : zsittle ::Blob = zsittle ::read_blob ( & mut reader ) . await ? ;
stdout . write_all ( & blb . data [ . . ] ) . await ? ;
stdout . write_all ( b" \n " ) . await ? ;
}
} ) ;
let mut inps = zsittle ::tokio ::io ::BufReader ::new ( stdin ) . lines ( ) ;
while let Some ( line ) = inps . next_line ( ) . await ? {
zsittle ::write_blob ( & mut writer , 0 , line . as_bytes ( ) ) . await ? ;
}
let _ = wtk . await ? ;
Ok ( ( ) )
} ;