Skip to content

Commit

Permalink
Fix bug on trojan domain name dump that misses the length data; Fix b…
Browse files Browse the repository at this point in the history
…ug on socks protocol read domain name utility.
  • Loading branch information
cty123 committed Mar 7, 2023
1 parent d2d7c37 commit a3db5c5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/protocol/common/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ impl Into<std::io::Result<SocketAddr>> for IpAddrPort {
match self.ip {
IpAddress::IpAddr(addr) => Ok(SocketAddr::new(addr, self.port)),
IpAddress::Domain(domain) => {
let name = std::str::from_utf8(&domain.inner).unwrap_or("127.0.0.1");
let name = match std::str::from_utf8(&domain.inner) {
Ok(name) => name,
Err(_) => {
return Err(Error::new(
ErrorKind::InvalidData,
"request domain name contains non-utf8 character",
))
}
};

// to_socket_addrs function implicitly runs a DNS query to resolve the DomainName
let addrs = match (name, self.port).to_socket_addrs() {
Ok(a) => a,
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/socks5/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub async fn parse<T: AsyncRead + AsyncWrite + Unpin>(mut stream: T) -> Result<R
Atype::DomainName => {
// Read address size
let size = stream.read_u8().await? as usize;
let mut buf = Vec::with_capacity(size);
let mut buf = vec![0u8; size];

// Read address data
stream.read_exact(&mut buf).await?;
Expand Down
1 change: 1 addition & 0 deletions src/protocol/trojan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub async fn handshake<T: AsyncWrite + Unpin>(
stream.write_all(&ipv6.octets()).await?;
}
IpAddress::Domain(domain) => {
stream.write_u8(domain.as_bytes().len() as u8).await?;
stream.write_all(&domain.as_bytes()).await?;
}
}
Expand Down

0 comments on commit a3db5c5

Please sign in to comment.