49 lines
2.1 KiB
Swift
49 lines
2.1 KiB
Swift
import Foundation
|
|
|
|
enum NetworkAddress {
|
|
static func tunnelUnderlayIPv4(_ remoteAddress: String) -> [String] {
|
|
let managementAddress = "47.104.134.190"
|
|
if isAuthorizedInternalIPv4(remoteAddress) {
|
|
// Preserve the management gateway and the three company relays.
|
|
return [remoteAddress, managementAddress, "8.216.53.107", "47.254.94.29", "192.236.188.22"]
|
|
}
|
|
guard isPublicIPv4(remoteAddress), remoteAddress != managementAddress else { return [managementAddress] }
|
|
return [remoteAddress, managementAddress]
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|