Skip to content

Commit

Permalink
Addressed bugs, and improved performance.
Browse files Browse the repository at this point in the history
* Using a set of buffers and trying to fill them as soon as possible.
* Remove closed sockets and tunnels.
* Fixed support for SOCKS Domain type addresses.
* Use the correct timer when running smoltcp's loop.
* Fixed other bugs like PPP parsing.
  • Loading branch information
zlogic committed Jul 21, 2024
1 parent 751cbb8 commit a5f4b2a
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 99 deletions.
36 changes: 24 additions & 12 deletions src/fortivpn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,30 @@ impl FortiVPNTunnel {
FortiVPNTunnel::send_ppp_packet(&mut self.socket, ppp::Protocol::IPV4, data).await
}

pub async fn try_next_ip_packet(
&mut self,
timeout: Option<Duration>,
) -> Result<usize, FortiError> {
// Peek header if not yet available - to get the protocol.
if let Some(timeout) = timeout {
match tokio::time::timeout(timeout, self.ppp_state.read_header(&mut self.socket)).await
{
Ok(res) => res,
Err(_) => return Ok(0),
}
} else {
self.ppp_state.read_header(&mut self.socket).await
}
.map_err(|err| {
debug!("Failed to read PPP header: {}", err);
"Failed to read PPP header"
})?;
match self.ppp_state.read_protocol() {
Some(ppp::Protocol::IPV4) => Ok(self.ppp_state.remaining_bytes()),
_ => Ok(0),
}
}

pub async fn try_read_packet(
&mut self,
dest: &mut [u8],
Expand Down Expand Up @@ -613,12 +637,10 @@ impl PPPState {
}

async fn read_header(&mut self, socket: &mut FortiTlsStream) -> Result<(), FortiError> {
println!("Reading packet");
if self.bytes_remaining > 0 {
return Ok(());
}
// If no data is available, this will return immediately.
//match tokio::time::timeout(Duration::from_millis(100), async {
while self.ppp_header_length < self.ppp_header.len() {
match socket
.read(&mut self.ppp_header[self.ppp_header_length..])
Expand All @@ -639,16 +661,6 @@ impl PPPState {
}
}
}
/*
.await
{
Ok(_) => {}
Err(_) => {
println!("Read timed out");
return Ok(0);
}
}
*/
println!("Packet ready");
if let Err(err) = self.validate_link(socket).await {
return Err(err);
Expand Down
4 changes: 2 additions & 2 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ where
format!(
"HTTP/1.1 200 OK\r\n\
Content-Type: text/html\r\n\
Cache-Control: no-store\r\n
Cache-Control: no-store\r\n\
Content-Length: {}\r\n\
\r\n",
data.len()
Expand Down Expand Up @@ -219,7 +219,7 @@ where
S: AsyncRead + AsyncWrite + Unpin,
{
stream: S,
buffer: Buffer<4096>,
buffer: Buffer<16000>,
}

impl<S> BufferedTlsStream<S>
Expand Down
Loading

0 comments on commit a5f4b2a

Please sign in to comment.