39 lines
1.5 KiB
Swift
39 lines
1.5 KiB
Swift
import Foundation
|
|
|
|
enum NetworkAddress {
|
|
static func isAuthorizedInternalIPv4(_ value: String) -> Bool {
|
|
let parts = value.split(separator: ".", omittingEmptySubsequences: false)
|
|
guard parts.count == 4 else { return false }
|
|
let octets = parts.compactMap { UInt8($0) }
|
|
guard octets.count == 4,
|
|
octets.map({ String($0) }).joined(separator: ".") == value,
|
|
octets[0] == 100,
|
|
(64...127).contains(octets[1])
|
|
else { return false }
|
|
return value != "100.64.0.0"
|
|
&& value != "100.127.255.255"
|
|
&& value != "100.100.100.100"
|
|
}
|
|
|
|
static func isPublicIPv4(_ value: String) -> Bool {
|
|
let parts = value.split(separator: ".", omittingEmptySubsequences: false)
|
|
guard parts.count == 4,
|
|
let a = UInt8(parts[0]),
|
|
let b = UInt8(parts[1]),
|
|
let c = UInt8(parts[2]),
|
|
UInt8(parts[3]) != nil
|
|
else { return false }
|
|
|
|
if a == 0 || a == 10 || a == 127 || a >= 224 { return false }
|
|
if a == 100 && (64...127).contains(b) { return false }
|
|
if a == 169 && b == 254 { return false }
|
|
if a == 172 && (16...31).contains(b) { return false }
|
|
if a == 192 && b == 168 { return false }
|
|
if a == 192 && b == 0 && (c == 0 || c == 2) { return false }
|
|
if a == 198 && (b == 18 || b == 19) { return false }
|
|
if a == 198 && b == 51 && c == 100 { return false }
|
|
if a == 203 && b == 0 && c == 113 { return false }
|
|
return true
|
|
}
|
|
}
|