diff options
author | Roopesh Chander <roop@roopc.net> | 2018-10-23 16:23:27 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-10-27 15:13:01 +0530 |
commit | e1b8b6789024b8b89057a49871daa92b5090f3a2 (patch) | |
tree | 6bb175ce9b350e10197c3c184944e9bdbc4d71d0 | |
parent | Model: Endpoint host should not have invalid characters (diff) | |
download | wireguard-apple-e1b8b6789024b8b89057a49871daa92b5090f3a2.tar.xz wireguard-apple-e1b8b6789024b8b89057a49871daa92b5090f3a2.zip |
Model for DNS server
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to '')
-rw-r--r-- | WireGuard/WireGuard.xcodeproj/project.pbxproj | 4 | ||||
-rw-r--r-- | WireGuard/WireGuard/Model/DNSServer.swift | 62 |
2 files changed, 66 insertions, 0 deletions
diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj index 253248f..99690b2 100644 --- a/WireGuard/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 6F628C3D217F09E9003482A3 /* TunnelViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F628C3C217F09E9003482A3 /* TunnelViewModel.swift */; }; + 6F628C3F217F3413003482A3 /* DNSServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F628C3E217F3413003482A3 /* DNSServer.swift */; }; 6F693A562179E556008551C1 /* Endpoint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F693A552179E556008551C1 /* Endpoint.swift */; }; 6F7774E1217181B1006A79B3 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774DF217181B1006A79B3 /* MainViewController.swift */; }; 6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F7774E0217181B1006A79B3 /* AppDelegate.swift */; }; @@ -23,6 +24,7 @@ /* Begin PBXFileReference section */ 6F628C3C217F09E9003482A3 /* TunnelViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelViewModel.swift; sourceTree = "<group>"; }; + 6F628C3E217F3413003482A3 /* DNSServer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DNSServer.swift; sourceTree = "<group>"; }; 6F693A552179E556008551C1 /* Endpoint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Endpoint.swift; sourceTree = "<group>"; }; 6F7774DF217181B1006A79B3 /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = "<group>"; }; 6F7774E0217181B1006A79B3 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; }; @@ -78,6 +80,7 @@ 6F7774E72172020C006A79B3 /* Configuration.swift */, 6F7774E9217229DB006A79B3 /* IPAddressRange.swift */, 6F693A552179E556008551C1 /* Endpoint.swift */, + 6F628C3E217F3413003482A3 /* DNSServer.swift */, ); path = Model; sourceTree = "<group>"; @@ -209,6 +212,7 @@ 6F7774EF21722D97006A79B3 /* TunnelsManager.swift in Sources */, 6F693A562179E556008551C1 /* Endpoint.swift in Sources */, 6F7774E2217181B1006A79B3 /* AppDelegate.swift in Sources */, + 6F628C3F217F3413003482A3 /* DNSServer.swift in Sources */, 6F628C3D217F09E9003482A3 /* TunnelViewModel.swift in Sources */, 6F7774EA217229DB006A79B3 /* IPAddressRange.swift in Sources */, 6F7774E82172020C006A79B3 /* Configuration.swift in Sources */, diff --git a/WireGuard/WireGuard/Model/DNSServer.swift b/WireGuard/WireGuard/Model/DNSServer.swift new file mode 100644 index 0000000..7eee007 --- /dev/null +++ b/WireGuard/WireGuard/Model/DNSServer.swift @@ -0,0 +1,62 @@ +// +// DNSServer.swift +// WireGuard +// +// Created by Roopesh Chander on 23/10/18. +// Copyright © 2018 WireGuard LLC. All rights reserved. +// + +import Foundation +import Network + +@available(OSX 10.14, iOS 12.0, *) +struct DNSServer { + let address: IPAddress +} + +// MARK: Converting to and from String +// For use in the UI + +extension DNSServer { + init?(from addressString: String) { + if let addr = IPv4Address(addressString) { + address = addr + } else if let addr = IPv6Address(addressString) { + address = addr + } else { + return nil + } + } + func stringRepresentation() -> String { + return "\(address)" + } +} + +// MARK: Codable +// For serializing to disk + +@available(OSX 10.14, iOS 12.0, *) +extension DNSServer: Codable { + public func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(address.rawValue) + } + public init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + var data = try container.decode(Data.self) + let ipAddressFromData: IPAddress? = { + switch (data.count) { + case 4: return IPv4Address(data) + case 16: return IPv6Address(data) + default: return nil + } + }() + guard let ipAddress = ipAddressFromData else { + throw DecodingError.invalidData + } + address = ipAddress + } + enum DecodingError: Error { + case invalidData + } +} |