diff options
author | Jeroen Leenarts <jeroen.leenarts@gmail.com> | 2018-08-15 22:52:37 +0200 |
---|---|---|
committer | Jeroen Leenarts <jeroen.leenarts@gmail.com> | 2018-08-15 22:52:37 +0200 |
commit | 91daed0c8061f0bd02bc171942933423f26c9fc1 (patch) | |
tree | b1ac389699506b07e04d8be06f32d357f294a4c8 /Shared/Validators.swift | |
parent | Assign correct copyright. (diff) | |
download | wireguard-apple-91daed0c8061f0bd02bc171942933423f26c9fc1.tar.xz wireguard-apple-91daed0c8061f0bd02bc171942933423f26c9fc1.zip |
Improve validator for IPv6.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'Shared/Validators.swift')
-rw-r--r-- | Shared/Validators.swift | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/Shared/Validators.swift b/Shared/Validators.swift index 231f802..17c8368 100644 --- a/Shared/Validators.swift +++ b/Shared/Validators.swift @@ -34,15 +34,18 @@ struct Endpoint { var addressType: AddressType init?(endpointString: String) throws { - let parts = endpointString.split(separator: ":") - guard parts.count == 2 else { + guard let range = endpointString.range(of: ":", options: .backwards, range: nil, locale: nil) else { throw EndpointValidationError.noIpAndPort(endpointString) } - guard let port = Int32(parts[1]), port > 0 else { - throw EndpointValidationError.invalidPort(String(parts[1])) + + let ipString = endpointString[..<range.lowerBound].replacingOccurrences(of: "[", with: "").replacingOccurrences(of: "]", with: "") + let portString = endpointString[range.upperBound...] + + guard let port = Int32(portString), port > 0 else { + throw EndpointValidationError.invalidPort(String(portString/*parts[1]*/)) } - ipAddress = String(parts[0]) + ipAddress = String(ipString) let addressType = validateIpAddress(ipToValidate: ipAddress) guard addressType == .IPv4 || addressType == .IPv6 else { throw EndpointValidationError.invalidIP(ipAddress) |