Skip to content

Commit 65044a4

Browse files
committed
Socks4の宛先アドレスのバリデーションを簡潔にした
1 parent c40ecda commit 65044a4

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

local_proxy/src/outbound/socks4.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,16 @@ impl ProxyOutBound for Socks4Proxy {
4747
let ip;
4848
let mut hostname = None;
4949
match &addr.hostname {
50-
HostName::V4(v4) => ip = *v4,
50+
HostName::V4(v4) => {
51+
let v4_integer = u32::from_be_bytes(v4.octets());
52+
if v4_integer & 0xFFFFFF00 == 0 && v4_integer & 0xFF != 0 {
53+
return Err("".into());
54+
}
55+
ip = *v4
56+
}
5157
HostName::V6(v6) => {
5258
ip = Ipv4Addr::new(0, 0, 0, 1);
53-
hostname = Some(format!("[{}]", v6));
59+
hostname = Some(v6.to_string());
5460
}
5561
HostName::Domain(domain) => {
5662
ip = Ipv4Addr::new(0, 0, 0, 1);
@@ -61,16 +67,6 @@ impl ProxyOutBound for Socks4Proxy {
6167
}
6268
}
6369

64-
let ip_octet = ip.octets();
65-
if hostname.is_none()
66-
&& ip_octet[0] == 0
67-
&& ip_octet[1] == 0
68-
&& ip_octet[2] == 0
69-
&& ip_octet[3] != 0
70-
{
71-
return Err("".into());
72-
}
73-
7470
let mut server = proxies
7571
.next()
7672
.ok_or("")?
@@ -79,7 +75,7 @@ impl ProxyOutBound for Socks4Proxy {
7975

8076
server.write_all(&[4, 1]).await?;
8177
server.write_all(&addr.port.to_be_bytes()).await?;
82-
server.write_all(&ip_octet).await?;
78+
server.write_all(&ip.octets()).await?;
8379
if let Some(auth) = &self.auth {
8480
server.write_all(auth.as_bytes()).await?
8581
}

0 commit comments

Comments
 (0)