fn handshake(&self, socket: SrvStream) -> impl Future<Item = SrvStream, Error = HandlerError> {
        let secret_key = self.state.borrow().secret_key.to_string();
        let handshake = socket.into_future()
                              .map_err(|(err, _)| HandlerError::from(err))
                              .and_then(move |(m, io)| {
                                  m.map_or_else(
                    || {
                        Err(HandlerError::from(io::Error::from(
                            io::ErrorKind::UnexpectedEof,
                        )))
                    },
                    move |m| {
                        if m.message_id() != "Handshake" {
                            debug!("No handshake");
                            Err(HandlerError::from(io::Error::from(
                                io::ErrorKind::ConnectionAborted,
                            )))
                        } else if !m.is_transaction() {
                            Err(HandlerError::from(io::Error::from(
                                io::ErrorKind::ConnectionAborted,
                            )))
                        } else {
                            match m.parse::<protocol::ctl::Handshake>() {
                                Ok(decoded) => {
                                    trace!("Received handshake, {:?}", decoded);
                                    let decoded_key = decoded.secret_key.unwrap_or_default();
                                    Ok((m, crypto::secure_eq(decoded_key, secret_key), io))
                                }
                                Err(err) => {
                                    warn!("Handshake error, {:?}", err);
                                    Err(HandlerError::from(io::Error::from(
                                        io::ErrorKind::ConnectionAborted,
                                    )))
                                }
                            }
                        }
                    },
                )
                              })
                              .and_then(|(msg, success, socket)| {
                                  let mut reply = if success {
                                      SrvMessage::from(net::ok())
                                  } else {
                                      SrvMessage::from(net::err(ErrCode::Unauthorized,
                                                                "secret key mismatch"))
                                  };
                                  reply.reply_for(msg.transaction().unwrap(), true);
                                  socket.send(reply)
                                        .map_err(HandlerError::from)
                                        .and_then(move |io| Ok((io, success)))
                              });
        handshake.select2(self.timeout(REQ_TIMEOUT)).then(|res| {
                                                        match res {
                Ok(Either::A(((io, true), _to))) => future::ok(io),
                Ok(Either::A(((_, false), _to))) => future::err(HandlerError::from(
                    io::Error::new(io::ErrorKind::ConnectionAborted, "handshake failed"),
                )),
                Ok(Either::B((_to, _hs))) => future::err(HandlerError::from(io::Error::new(
                    io::ErrorKind::TimedOut,
                    "client timed out",
                ))),
                Err(Either::A((err, _))) => future::err(err),
                Err(Either::B((err, _))) => future::err(HandlerError::from(err)),
            }
                                                    })
    }
/// Initiate a handshake with the connected client before allowing future requests. A failed
/// handshake will close the connection.
Initiate a handshake with the connected client before allowing future requests. A failed
handshake will close the connection.
Initiate a handshake with the connected client before allowing future requests . A failed handshake will close the connection .
fn handshake(&self, socket: SrvStream) -> impl Future<Item = SrvStream, Error = HandlerError> {
        let secret_key = self.state.borrow().secret_key.to_string();
        let handshake = socket.into_future()
                              .map_err(|(err, _)| HandlerError::from(err))
                              .and_then(move |(m, io)| {
                                  m.map_or_else(
                    || {
                        Err(HandlerError::from(io::Error::from(
                            io::ErrorKind::UnexpectedEof,
                        )))
                    },
                    move |m| {
                        if m.message_id() != "Handshake" {
                            debug!("No handshake");
                            Err(HandlerError::from(io::Error::from(
                                io::ErrorKind::ConnectionAborted,
                            )))
                        } else if !m.is_transaction() {
                            Err(HandlerError::from(io::Error::from(
                                io::ErrorKind::ConnectionAborted,
                            )))
                        } else {
                            match m.parse::<protocol::ctl::Handshake>() {
                                Ok(decoded) => {
                                    trace!("Received handshake, {:?}", decoded);
                                    let decoded_key = decoded.secret_key.unwrap_or_default();
                                    Ok((m, crypto::secure_eq(decoded_key, secret_key), io))
                                }
                                Err(err) => {
                                    warn!("Handshake error, {:?}", err);
                                    Err(HandlerError::from(io::Error::from(
                                        io::ErrorKind::ConnectionAborted,
                                    )))
                                }
                            }
                        }
                    },
                )
                              })
                              .and_then(|(msg, success, socket)| {
                                  let mut reply = if success {
                                      SrvMessage::from(net::ok())
                                  } else {
                                      SrvMessage::from(net::err(ErrCode::Unauthorized,
                                                                "secret key mismatch"))
                                  };
                                  reply.reply_for(msg.transaction().unwrap(), true);
                                  socket.send(reply)
                                        .map_err(HandlerError::from)
                                        .and_then(move |io| Ok((io, success)))
                              });
        handshake.select2(self.timeout(REQ_TIMEOUT)).then(|res| {
                                                        match res {
                Ok(Either::A(((io, true), _to))) => future::ok(io),
                Ok(Either::A(((_, false), _to))) => future::err(HandlerError::from(
                    io::Error::new(io::ErrorKind::ConnectionAborted, "handshake failed"),
                )),
                Ok(Either::B((_to, _hs))) => future::err(HandlerError::from(io::Error::new(
                    io::ErrorKind::TimedOut,
                    "client timed out",
                ))),
                Err(Either::A((err, _))) => future::err(err),
                Err(Either::B((err, _))) => future::err(HandlerError::from(err)),
            }
                                                    })
    }
fn handshake ( & self , socket : SrvStream ) -> impl Future < Item = SrvStream , Error = HandlerError > { let secret_key = self . state . borrow ( ) . secret_key . to_string ( ) ; let handshake = socket . into_future ( ) . map_err ( | ( err , _ ) | HandlerError :: from ( err ) ) . and_then ( move | ( m , io ) | { m . map_or_else ( | | { Err ( HandlerError :: from ( io :: Error :: from ( io :: ErrorKind :: UnexpectedEof , ) ) ) } , move | m | { if m . message_id ( ) != " " { debug ! ( " " ) ; Err ( HandlerError :: from ( io :: Error :: from ( io :: ErrorKind :: ConnectionAborted , ) ) ) } else if ! m . is_transaction ( ) { Err ( HandlerError :: from ( io :: Error :: from ( io :: ErrorKind :: ConnectionAborted , ) ) ) } else { match m . parse :: < protocol :: ctl :: Handshake > ( ) { Ok ( decoded ) => { trace ! ( " " decoded ) ; let decoded_key = decoded . secret_key . unwrap_or_default ( ) ; Ok ( ( m , crypto :: secure_eq ( decoded_key , secret_key ) , io ) ) } Err ( err ) => { warn ! ( " " err ) ; Err ( HandlerError :: from ( io :: Error :: from ( io :: ErrorKind :: ConnectionAborted , ) ) ) } } } } , ) } ) . and_then ( | ( msg , success , socket ) | { let mut reply = if success { SrvMessage :: from ( net :: ok ( ) ) } else { SrvMessage :: from ( net :: err ( ErrCode :: Unauthorized , " " ) ) } ; reply . reply_for ( msg . transaction ( ) . unwrap ( ) , true ) ; socket . send ( reply ) . map_err ( HandlerError :: from ) . and_then ( move | io | Ok ( ( io , success ) ) ) } ) ; handshake . select2 ( self . timeout ( REQ_TIMEOUT ) ) . then ( | res | { match res { Ok ( Either :: A ( ( ( io , true ) , _to ) ) ) => future :: ok ( io ) , Ok ( Either :: A ( ( ( _ , false ) , _to ) ) ) => future :: err ( HandlerError :: from ( io :: Error :: new ( io :: ErrorKind :: ConnectionAborted , " " ) , ) ) , Ok ( Either :: B ( ( _to , _hs ) ) ) => future :: err ( HandlerError :: from ( io :: Error :: new ( io :: ErrorKind :: TimedOut , " " , ) ) ) , Err ( Either :: A ( ( err , _ ) ) ) => future :: err ( err ) , Err ( Either :: B ( ( err , _ ) ) ) => future :: err ( HandlerError :: from ( err ) ) , } } ) }
Initiate a handshake with the connected client before allowing future requests.
Initiate a handshake with the connected client before allowing future requests .
&self
socket
SrvStream
&self
socket
SrvStream