25 lines
966 B
Swift
25 lines
966 B
Swift
import Foundation
|
|
|
|
enum NetworkAddress {
|
|
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
|
|
}
|
|
}
|