Encapsulate port() in Address

This commit is contained in:
zonyitoo
2020-03-01 14:42:38 +08:00
parent b5e7a77e2f
commit 42ef48be3d
2 changed files with 17 additions and 8 deletions
+7 -8
View File
@@ -272,23 +272,20 @@ impl AccessControl {
/// FIXME: This function may perform a DNS resolution
pub async fn check_target_bypassed(&self, context: &Context, addr: &Address) -> bool {
// Always redirect TCP DNS query (for android)
if cfg!(target_os = "android") {
let port = match *addr {
Address::SocketAddress(ref saddr) => saddr.port(),
Address::DomainNameAddress(ref _host, port) => port,
};
if port == 53 {
return false;
}
if cfg!(target_os = "android") && addr.port() == 53 {
return false;
}
// Addresses in bypass_list will be bypassed
if self.black_list.check_address_matched(addr) {
return true;
}
// Addresses in proxy_list will be proxied
if self.white_list.check_address_matched(addr) {
return false;
}
// Resolve hostname and check the list
if cfg!(not(target_os = "android")) {
if let Address::DomainNameAddress(ref host, port) = *addr {
@@ -297,6 +294,7 @@ impl AccessControl {
if self.black_list.check_ip_matched(&addr) {
return true;
}
if self.white_list.check_ip_matched(&addr) {
return false;
}
@@ -304,6 +302,7 @@ impl AccessControl {
}
}
}
// default rule
match self.mode {
Mode::BlackList => false,
+10
View File
@@ -214,6 +214,7 @@ pub enum Address {
}
impl Address {
/// Parse from a `AsyncRead`
pub async fn read_from<R>(stream: &mut R) -> Result<Address, Error>
where
R: AsyncRead + Unpin,
@@ -303,10 +304,19 @@ impl Address {
write_address(self, buf)
}
/// Get required buffer size for serializing
#[inline]
pub fn serialized_len(&self) -> usize {
get_addr_len(self)
}
/// Get associated port number
pub fn port(&self) -> u16 {
match *self {
Address::SocketAddress(addr) => addr.port(),
Address::DomainNameAddress(.., port) => port,
}
}
}
impl Debug for Address {