Skip to content

Commit

Permalink
fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
r-n-o committed Jun 7, 2024
1 parent 0202416 commit 21a2858
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 151 deletions.
2 changes: 1 addition & 1 deletion src/integration/src/bin/pivot_remote_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl RequestProcessor for Processor {
);

tls.write_all(http_request.as_bytes()).unwrap();
let ciphersuite = tls.conn.negotiated_cipher_suite().unwrap();
let _ciphersuite = tls.conn.negotiated_cipher_suite().unwrap();

let mut response_bytes = Vec::new();
let read_to_end_result: usize =
Expand Down
13 changes: 7 additions & 6 deletions src/qos_core/src/io/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl Stream {
send_timeout.set(fd, &timeout)?;

match connect(stream.fd, &*addr.addr()) {
Ok(_) => return Ok(stream),
Ok(()) => return Ok(stream),
Err(e) => err = IOError::ConnectNixError(e),
}

Expand Down Expand Up @@ -201,7 +201,7 @@ impl Stream {
&mut buf[received_bytes..len],
MsgFlags::empty(),
) {
Ok(size) if size == 0 => {
Ok(0) => {
return Err(IOError::RecvConnectionClosed);
}
Ok(size) => size,
Expand Down Expand Up @@ -234,7 +234,7 @@ impl Stream {
&mut buf[received_bytes..length],
MsgFlags::empty(),
) {
Ok(size) if size == 0 => {
Ok(0) => {
return Err(IOError::RecvConnectionClosed);
}
Ok(size) => size,
Expand All @@ -257,7 +257,7 @@ impl Stream {
impl Read for Stream {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
match recv(self.fd, buf, MsgFlags::empty()) {
Ok(size) if size == 0 => Err(std::io::Error::new(
Ok(0) => Err(std::io::Error::new(
ErrorKind::ConnectionAborted,
"read 0 bytes",
)),
Expand All @@ -270,7 +270,7 @@ impl Read for Stream {
impl Write for Stream {
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
match send(self.fd, buf, MsgFlags::empty()) {
Ok(size) if size == 0 => Err(std::io::Error::new(
Ok(0) => Err(std::io::Error::new(
ErrorKind::ConnectionAborted,
"wrote 0 bytes",
)),
Expand Down Expand Up @@ -398,7 +398,8 @@ mod test {
assert_eq!(data, resp);
}

// TODO: replace this test with something simpler. Local socket which does a simple echo?
// TODO: replace this test with something simpler. Local socket which does a
// simple echo?
#[test]
fn stream_implement_read_write_traits() {
let host = "api.turnkey.com";
Expand Down
6 changes: 3 additions & 3 deletions src/qos_net/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ pub enum QosNetError {
ConnectionClosed,
/// Happens when a socket `read` results in no data
EmptyRead,
/// Happens when a socket `read` returns too much data for the provided buffer
/// and the data doesn't fit. The first `usize` is the size of the received
/// data, the second `usize` is the size of the buffer.
/// Happens when a socket `read` returns too much data for the provided
/// buffer and the data doesn't fit. The first `usize` is the size of the
/// received data, the second `usize` is the size of the buffer.
ReadOverflow(usize, usize),
}

Expand Down
5 changes: 3 additions & 2 deletions src/qos_net/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! This crate contains a simple proxy server which binds to a local socket and opens TCP connection
//! It exposes a simple protocol for remote clients who connect to let them manipulate these connections (read/write/flush)
//! This crate contains a simple proxy server which binds to a local socket and
//! opens TCP connection It exposes a simple protocol for remote clients who
//! connect to let them manipulate these connections (read/write/flush)
#![deny(clippy::all, unsafe_code)]
pub mod cli;
Expand Down
122 changes: 42 additions & 80 deletions src/qos_net/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ impl Proxy {
}
}

fn get_connection(
&mut self,
id: u32,
) -> Option<&mut ProxyConnection> {
fn get_connection(&mut self, id: u32) -> Option<&mut ProxyConnection> {
self.connections.iter_mut().find(|c| c.id == id)
}

Expand All @@ -68,10 +65,9 @@ impl Proxy {
let connection_id = conn.id;
let remote_ip = conn.ip.clone();
match self.save_connection(conn) {
Ok(()) => ProxyMsg::ConnectResponse {
connection_id,
remote_ip,
},
Ok(()) => {
ProxyMsg::ConnectResponse { connection_id, remote_ip }
}
Err(e) => ProxyMsg::ProxyError(e),
}
}
Expand All @@ -87,10 +83,9 @@ impl Proxy {
let connection_id = conn.id;
let remote_ip = conn.ip.clone();
match self.save_connection(conn) {
Ok(()) => ProxyMsg::ConnectResponse {
connection_id,
remote_ip,
},
Ok(()) => {
ProxyMsg::ConnectResponse { connection_id, remote_ip }
}
Err(e) => ProxyMsg::ProxyError(e),
}
}
Expand All @@ -99,19 +94,13 @@ impl Proxy {
}

/// Performs a Read on a connection
pub fn read(
&mut self,
connection_id: u32,
size: usize,
) -> ProxyMsg {
pub fn read(&mut self, connection_id: u32, size: usize) -> ProxyMsg {
if let Some(conn) = self.get_connection(connection_id) {
let mut buf: Vec<u8> = vec![0; size];
match conn.read(&mut buf) {
Ok(size) => {
if size == 0 {
ProxyMsg::ProxyError(
QosNetError::ConnectionClosed,
)
ProxyMsg::ProxyError(QosNetError::ConnectionClosed)
} else {
ProxyMsg::ReadResponse {
connection_id,
Expand All @@ -123,29 +112,23 @@ impl Proxy {
Err(e) => ProxyMsg::ProxyError(e.into()),
}
} else {
ProxyMsg::ProxyError(
QosNetError::ConnectionIdNotFound(connection_id),
)
ProxyMsg::ProxyError(QosNetError::ConnectionIdNotFound(
connection_id,
))
}
}

/// Performs a Write on an existing connection
pub fn write(
&mut self,
connection_id: u32,
data: Vec<u8>,
) -> ProxyMsg {
pub fn write(&mut self, connection_id: u32, data: Vec<u8>) -> ProxyMsg {
if let Some(conn) = self.get_connection(connection_id) {
match conn.write(&data) {
Ok(size) => {
ProxyMsg::WriteResponse { connection_id, size }
}
Ok(size) => ProxyMsg::WriteResponse { connection_id, size },
Err(e) => ProxyMsg::ProxyError(e.into()),
}
} else {
ProxyMsg::ProxyError(
QosNetError::ConnectionIdNotFound(connection_id),
)
ProxyMsg::ProxyError(QosNetError::ConnectionIdNotFound(
connection_id,
))
}
}

Expand All @@ -157,21 +140,19 @@ impl Proxy {
Err(e) => ProxyMsg::ProxyError(e.into()),
}
} else {
ProxyMsg::ProxyError(
QosNetError::ConnectionIdNotFound(connection_id),
)
ProxyMsg::ProxyError(QosNetError::ConnectionIdNotFound(
connection_id,
))
}
}
}

impl server::RequestProcessor for Proxy {
fn process(&mut self, req_bytes: Vec<u8>) -> Vec<u8> {
if req_bytes.len() > MAX_ENCODED_MSG_LEN {
return ProxyMsg::ProxyError(
QosNetError::OversizedPayload,
)
.try_to_vec()
.expect("ProtocolMsg can always be serialized. qed.");
return ProxyMsg::ProxyError(QosNetError::OversizedPayload)
.try_to_vec()
.expect("ProtocolMsg can always be serialized. qed.");
}

let resp = match ProxyMsg::try_from_slice(&req_bytes) {
Expand Down Expand Up @@ -203,43 +184,28 @@ impl server::RequestProcessor for Proxy {
self.flush(connection_id)
}
ProxyMsg::ProxyError(_) => {
ProxyMsg::ProxyError(
QosNetError::InvalidMsg,
)
ProxyMsg::ProxyError(QosNetError::InvalidMsg)
}
ProxyMsg::StatusResponse(_) => {
ProxyMsg::ProxyError(
QosNetError::InvalidMsg,
)
ProxyMsg::ProxyError(QosNetError::InvalidMsg)
}
ProxyMsg::ConnectResponse {
connection_id: _,
remote_ip: _,
} => ProxyMsg::ProxyError(
QosNetError::InvalidMsg,
),
ProxyMsg::WriteResponse {
connection_id: _,
size: _,
} => ProxyMsg::ProxyError(
QosNetError::InvalidMsg,
),
} => ProxyMsg::ProxyError(QosNetError::InvalidMsg),
ProxyMsg::WriteResponse { connection_id: _, size: _ } => {
ProxyMsg::ProxyError(QosNetError::InvalidMsg)
}
ProxyMsg::FlushResponse { connection_id: _ } => {
ProxyMsg::ProxyError(
QosNetError::InvalidMsg,
)
ProxyMsg::ProxyError(QosNetError::InvalidMsg)
}
ProxyMsg::ReadResponse {
connection_id: _,
size: _,
data: _,
} => ProxyMsg::ProxyError(
QosNetError::InvalidMsg,
),
} => ProxyMsg::ProxyError(QosNetError::InvalidMsg),
},
Err(_) => {
ProxyMsg::ProxyError(QosNetError::InvalidMsg)
}
Err(_) => ProxyMsg::ProxyError(QosNetError::InvalidMsg),
};

resp.try_to_vec()
Expand Down Expand Up @@ -282,7 +248,8 @@ pub enum ProxyMsg {
},
/// Response for `ConnectByNameRequest` and `ConnectByIpRequest`
ConnectResponse {
/// Connection ID to reference the opened connection in later messages (`Read`, `Write`, `Flush`)
/// Connection ID to reference the opened connection in later messages
/// (`Read`, `Write`, `Flush`)
connection_id: u32,
/// The remote host IP, e.g. "1.2.3.4"
remote_ip: String,
Expand Down Expand Up @@ -345,7 +312,7 @@ mod test {
fn simple_status_request() {
let mut proxy = Proxy::new();
let request = ProxyMsg::StatusRequest.try_to_vec().unwrap();
let response = proxy.process(request.try_into().unwrap());
let response = proxy.process(request);
let msg = ProxyMsg::try_from_slice(&response).unwrap();
assert_eq!(msg, ProxyMsg::StatusResponse(0));
}
Expand All @@ -361,7 +328,7 @@ mod test {
}
.try_to_vec()
.unwrap();
let response = proxy.process(request.try_into().unwrap());
let response = proxy.process(request);
let msg = ProxyMsg::try_from_slice(&response).unwrap();
let connection_id = match msg {
ProxyMsg::ConnectResponse { connection_id, remote_ip: _ } => {
Expand All @@ -379,25 +346,20 @@ mod test {
}
.try_to_vec()
.unwrap();
let response = proxy.process(request.try_into().unwrap());
let response = proxy.process(request);
let msg: ProxyMsg = ProxyMsg::try_from_slice(&response).unwrap();
assert!(matches!(
msg,
ProxyMsg::WriteResponse { connection_id: _, size: _ }
));

let request =
ProxyMsg::ReadRequest { connection_id, size: 512 }
.try_to_vec()
.unwrap();
let response = proxy.process(request.try_into().unwrap());
let request = ProxyMsg::ReadRequest { connection_id, size: 512 }
.try_to_vec()
.unwrap();
let response = proxy.process(request);
let msg: ProxyMsg = ProxyMsg::try_from_slice(&response).unwrap();
let data = match msg {
ProxyMsg::ReadResponse {
connection_id: _,
size: _,
data,
} => data,
ProxyMsg::ReadResponse { connection_id: _, size: _, data } => data,
_ => {
panic!("test failure: msg is not ReadResponse")
}
Expand Down
2 changes: 1 addition & 1 deletion src/qos_net/src/proxy_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ mod test {
|| (read_to_end_result
.is_err_and(|e| e.kind() == ErrorKind::UnexpectedEof))
);

let response_text = std::str::from_utf8(&response_bytes).unwrap();
assert!(response_text.contains("HTTP/1.1 200 OK"));
assert!(response_text.contains("currentTime"));
Expand Down
Loading

0 comments on commit 21a2858

Please sign in to comment.