| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | pub fn is_public(address: IpAddr) -> bool { |
| 32 | match address { |
| 33 | |
| 34 | |
| 35 | |
| 36 | IpAddr::V6(address) => match address.to_ipv4_mapped() { |
| 37 | Some(mapped) => is_public_v4(mapped), |
| 38 | None => is_public_v6(address), |
| 39 | }, |
| 40 | IpAddr::V4(address) => is_public_v4(address), |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | const LOCAL_NAMES: [&str; 3] = ["localhost", "localhost.localdomain", "ip6-localhost"]; |
| 46 | |
| 47 | |
| 48 | |
| 49 | const LOCAL_SUFFIXES: [&str; 4] = [".localhost", ".local", ".internal", ".localdomain"]; |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | pub fn names_local_network(host: &str) -> bool { |
| 63 | let lowered = host.trim_end_matches('.').to_ascii_lowercase(); |
| 64 | |
| 65 | LOCAL_NAMES.contains(&lowered.as_str()) |
| 66 | || LOCAL_SUFFIXES |
| 67 | .iter() |
| 68 | .any(|suffix| lowered.ends_with(suffix)) |
| 69 | } |
| 70 | |
| 71 | fn is_public_v4(address: Ipv4Addr) -> bool { |
| 72 | let [a, b, ..] = address.octets(); |
| 73 | |
| 74 | !(address.is_unspecified() |
| 75 | || address.is_loopback() |
| 76 | || address.is_private() |
| 77 | || address.is_link_local() |
| 78 | || address.is_multicast() |
| 79 | || address.is_broadcast() |
| 80 | || address.is_documentation() |
| 81 | |
| 82 | || a == 0 |
| 83 | |
| 84 | |
| 85 | |
| 86 | || (a == 100 && (64..128).contains(&b)) |
| 87 | |
| 88 | || address.octets()[..3] == [192, 0, 0] |
| 89 | |
| 90 | || (a == 198 && (b == 18 || b == 19)) |
| 91 | |
| 92 | |
| 93 | || a >= 240) |
| 94 | } |
| 95 | |
| 96 | fn is_public_v6(address: Ipv6Addr) -> bool { |
| 97 | let first = address.segments()[0]; |
| 98 | |
| 99 | !(address.is_unspecified() |
| 100 | || address.is_loopback() |
| 101 | || address.is_multicast() |
| 102 | |
| 103 | || (first & 0xfe00) == 0xfc00 |
| 104 | |
| 105 | || (first & 0xffc0) == 0xfe80) |
| 106 | } |
| 107 | |
| 108 | #[cfg(test)] |
| 109 | mod tests { |
| 110 | use super::*; |
| 111 | |
| 112 | fn v4(value: &str) -> IpAddr { |
| 113 | IpAddr::V4(value.parse::<Ipv4Addr>().expect("a v4 address")) |
| 114 | } |
| 115 | |
| 116 | fn v6(value: &str) -> IpAddr { |
| 117 | IpAddr::V6(value.parse::<Ipv6Addr>().expect("a v6 address")) |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | #[test] |
| 124 | fn nothing_on_this_machine_or_its_own_network_is_public() { |
| 125 | for address in [ |
| 126 | "0.0.0.0", |
| 127 | "0.1.2.3", |
| 128 | "127.0.0.1", |
| 129 | "127.1.2.3", |
| 130 | "10.0.0.5", |
| 131 | "172.16.4.1", |
| 132 | "192.168.1.1", |
| 133 | |
| 134 | "169.254.169.254", |
| 135 | "100.64.0.1", |
| 136 | "100.127.255.255", |
| 137 | "192.0.0.1", |
| 138 | "198.18.0.1", |
| 139 | "198.19.255.255", |
| 140 | "192.0.2.1", |
| 141 | "224.0.0.1", |
| 142 | "240.0.0.1", |
| 143 | "255.255.255.255", |
| 144 | ] { |
| 145 | assert!( |
| 146 | !is_public(v4(address)), |
| 147 | "{address} is not on the public internet" |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | for address in ["::", "::1", "fd00::1", "fe80::1", "ff02::1"] { |
| 152 | assert!( |
| 153 | !is_public(v6(address)), |
| 154 | "{address} is not on the public internet" |
| 155 | ); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn names_that_mean_this_machine_or_its_network_are_local() { |
| 161 | for host in [ |
| 162 | "localhost", |
| 163 | "LOCALHOST", |
| 164 | "localhost.", |
| 165 | "localhost.localdomain", |
| 166 | "ip6-localhost", |
| 167 | "api.localhost", |
| 168 | "steid.local", |
| 169 | "Steid.Local.", |
| 170 | "db.internal", |
| 171 | "metadata.google.internal", |
| 172 | "box.localdomain", |
| 173 | ] { |
| 174 | assert!(names_local_network(host), "{host} names the local network"); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn ordinary_names_are_not_local() { |
| 180 | for host in [ |
| 181 | "example.com", |
| 182 | "github.com", |
| 183 | |
| 184 | "local.example.com", |
| 185 | "internal.example.com", |
| 186 | "notlocal", |
| 187 | "mylocalhost.com", |
| 188 | ] { |
| 189 | assert!(!names_local_network(host), "{host} is an ordinary name"); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn an_ipv4_address_in_an_ipv6_costume_answers_the_same_way() { |
| 195 | assert!(!is_public(v6("::ffff:127.0.0.1"))); |
| 196 | assert!(!is_public(v6("::ffff:10.0.0.1"))); |
| 197 | assert!(is_public(v6("::ffff:1.1.1.1"))); |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn ordinary_public_addresses_are_public() { |
| 202 | for address in ["1.1.1.1", "8.8.8.8", "140.82.121.4", "100.63.255.255"] { |
| 203 | assert!(is_public(v4(address)), "{address} is on the internet"); |
| 204 | } |
| 205 | |
| 206 | for address in ["2606:4700::1111", "2001:4860:4860::8888"] { |
| 207 | assert!(is_public(v6(address)), "{address} is on the internet"); |
| 208 | } |
| 209 | } |
| 210 | } |