Fixed ProxyTunnel

This commit is contained in:
johannesd3 2021-01-30 14:03:34 +01:00
parent a45fe85c27
commit c1d62d72a7

View file

@ -17,29 +17,40 @@ pub async fn connect<T: AsyncRead + AsyncWrite + Unpin>(
.into_bytes(); .into_bytes();
connection.write_all(buffer.as_ref()).await?; connection.write_all(buffer.as_ref()).await?;
buffer.clear(); buffer.resize(buffer.capacity(), 0);
connection.read_to_end(&mut buffer).await?;
if buffer.is_empty() {
return Err(io::Error::new(io::ErrorKind::Other, "Early EOF from proxy"));
}
let mut headers = [httparse::EMPTY_HEADER; 16]; let mut offset = 0;
let mut response = httparse::Response::new(&mut headers); loop {
let bytes_read = connection.read(&mut buffer[offset..]).await?;
response if bytes_read == 0 {
.parse(&buffer[..]) return Err(io::Error::new(io::ErrorKind::Other, "Early EOF from proxy"));
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?; }
offset += bytes_read;
match response.code {
Some(200) => Ok(connection), // Proxy says all is well let mut headers = [httparse::EMPTY_HEADER; 16];
Some(code) => { let mut response = httparse::Response::new(&mut headers);
let reason = response.reason.unwrap_or("no reason");
let msg = format!("Proxy responded with {}: {}", code, reason); let status = response
Err(io::Error::new(io::ErrorKind::Other, msg)) .parse(&buffer[..offset])
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
if status.is_complete() {
return match response.code {
Some(200) => Ok(connection), // Proxy says all is well
Some(code) => {
let reason = response.reason.unwrap_or("no reason");
let msg = format!("Proxy responded with {}: {}", code, reason);
Err(io::Error::new(io::ErrorKind::Other, msg))
}
None => Err(io::Error::new(
io::ErrorKind::Other,
"Malformed response from proxy",
)),
};
}
if offset >= buffer.len() {
buffer.resize(buffer.len() * 2, 0);
} }
None => Err(io::Error::new(
io::ErrorKind::Other,
"Malformed response from proxy",
)),
} }
} }