import Foundation
import Network
-struct DNSServer {
- let address: IPAddress
+public struct DNSServer {
+ public let address: IPAddress
- init(address: IPAddress) {
+ public init(address: IPAddress) {
self.address = address
}
}
extension DNSServer: Equatable {
- static func == (lhs: DNSServer, rhs: DNSServer) -> Bool {
+ public static func == (lhs: DNSServer, rhs: DNSServer) -> Bool {
return lhs.address.rawValue == rhs.address.rawValue
}
}
extension DNSServer {
- var stringRepresentation: String {
+ public var stringRepresentation: String {
return "\(address)"
}
- init?(from addressString: String) {
+ public init?(from addressString: String) {
if let addr = IPv4Address(addressString) {
address = addr
} else if let addr = IPv6Address(addressString) {
import Foundation
import Network
-struct Endpoint {
- let host: NWEndpoint.Host
- let port: NWEndpoint.Port
+public struct Endpoint {
+ public let host: NWEndpoint.Host
+ public let port: NWEndpoint.Port
- init(host: NWEndpoint.Host, port: NWEndpoint.Port) {
+ public init(host: NWEndpoint.Host, port: NWEndpoint.Port) {
self.host = host
self.port = port
}
}
extension Endpoint: Equatable {
- static func == (lhs: Endpoint, rhs: Endpoint) -> Bool {
+ public static func == (lhs: Endpoint, rhs: Endpoint) -> Bool {
return lhs.host == rhs.host && lhs.port == rhs.port
}
}
extension Endpoint: Hashable {
- func hash(into hasher: inout Hasher) {
+ public func hash(into hasher: inout Hasher) {
hasher.combine(host)
hasher.combine(port)
}
}
extension Endpoint {
- var stringRepresentation: String {
+ public var stringRepresentation: String {
switch host {
case .name(let hostname, _):
return "\(hostname):\(port)"
}
}
- init?(from string: String) {
+ public init?(from string: String) {
// Separation of host and port is based on 'parse_endpoint' function in
// https://git.zx2c4.com/wireguard-tools/tree/src/config.c
guard !string.isEmpty else { return nil }
}
extension Endpoint {
- func hasHostAsIPAddress() -> Bool {
+ public func hasHostAsIPAddress() -> Bool {
switch host {
case .name:
return false
}
}
- func hostname() -> String? {
+ public func hostname() -> String? {
switch host {
case .name(let hostname, _):
return hostname
import Foundation
import Network
-struct IPAddressRange {
- let address: IPAddress
- var networkPrefixLength: UInt8
+public struct IPAddressRange {
+ public let address: IPAddress
+ public let networkPrefixLength: UInt8
init(address: IPAddress, networkPrefixLength: UInt8) {
self.address = address
}
extension IPAddressRange: Equatable {
- static func == (lhs: IPAddressRange, rhs: IPAddressRange) -> Bool {
+ public static func == (lhs: IPAddressRange, rhs: IPAddressRange) -> Bool {
return lhs.address.rawValue == rhs.address.rawValue && lhs.networkPrefixLength == rhs.networkPrefixLength
}
}
extension IPAddressRange: Hashable {
- func hash(into hasher: inout Hasher) {
+ public func hash(into hasher: inout Hasher) {
hasher.combine(address.rawValue)
hasher.combine(networkPrefixLength)
}
}
extension IPAddressRange {
- var stringRepresentation: String {
+ public var stringRepresentation: String {
return "\(address)/\(networkPrefixLength)"
}
- init?(from string: String) {
+ public init?(from string: String) {
guard let parsed = IPAddressRange.parseAddressString(string) else { return nil }
address = parsed.0
networkPrefixLength = parsed.1
import Foundation
import Network
-struct InterfaceConfiguration {
- var privateKey: Data
- var addresses = [IPAddressRange]()
- var listenPort: UInt16?
- var mtu: UInt16?
- var dns = [DNSServer]()
+public struct InterfaceConfiguration {
+ public var privateKey: Data
+ public var addresses = [IPAddressRange]()
+ public var listenPort: UInt16?
+ public var mtu: UInt16?
+ public var dns = [DNSServer]()
- init(privateKey: Data) {
+ public init(privateKey: Data) {
if privateKey.count != TunnelConfiguration.keyLength {
fatalError("Invalid private key")
}
}
extension InterfaceConfiguration: Equatable {
- static func == (lhs: InterfaceConfiguration, rhs: InterfaceConfiguration) -> Bool {
+ public static func == (lhs: InterfaceConfiguration, rhs: InterfaceConfiguration) -> Bool {
let lhsAddresses = lhs.addresses.filter { $0.address is IPv4Address } + lhs.addresses.filter { $0.address is IPv6Address }
let rhsAddresses = rhs.addresses.filter { $0.address is IPv4Address } + rhs.addresses.filter { $0.address is IPv6Address }
import Foundation
-struct PeerConfiguration {
- var publicKey: Data
- var preSharedKey: Data? {
+public struct PeerConfiguration {
+ public var publicKey: Data
+ public var preSharedKey: Data? {
didSet(value) {
if let value = value {
if value.count != TunnelConfiguration.keyLength {
}
}
}
- var allowedIPs = [IPAddressRange]()
- var endpoint: Endpoint?
- var persistentKeepAlive: UInt16?
- var rxBytes: UInt64?
- var txBytes: UInt64?
- var lastHandshakeTime: Date?
+ public var allowedIPs = [IPAddressRange]()
+ public var endpoint: Endpoint?
+ public var persistentKeepAlive: UInt16?
+ public var rxBytes: UInt64?
+ public var txBytes: UInt64?
+ public var lastHandshakeTime: Date?
- init(publicKey: Data) {
+ public init(publicKey: Data) {
self.publicKey = publicKey
if publicKey.count != TunnelConfiguration.keyLength {
fatalError("Invalid public key")
}
extension PeerConfiguration: Equatable {
- static func == (lhs: PeerConfiguration, rhs: PeerConfiguration) -> Bool {
+ public static func == (lhs: PeerConfiguration, rhs: PeerConfiguration) -> Bool {
return lhs.publicKey == rhs.publicKey &&
lhs.preSharedKey == rhs.preSharedKey &&
Set(lhs.allowedIPs) == Set(rhs.allowedIPs) &&
}
extension PeerConfiguration: Hashable {
- func hash(into hasher: inout Hasher) {
+ public func hash(into hasher: inout Hasher) {
hasher.combine(publicKey)
hasher.combine(preSharedKey)
hasher.combine(Set(allowedIPs))
import Foundation
-final class TunnelConfiguration {
- var name: String?
- var interface: InterfaceConfiguration
- let peers: [PeerConfiguration]
+public final class TunnelConfiguration {
+ public var name: String?
+ public var interface: InterfaceConfiguration
+ public let peers: [PeerConfiguration]
- static let keyLength = 32
+ public static let keyLength = 32
- init(name: String?, interface: InterfaceConfiguration, peers: [PeerConfiguration]) {
+ public init(name: String?, interface: InterfaceConfiguration, peers: [PeerConfiguration]) {
self.interface = interface
self.peers = peers
self.name = name
}
extension TunnelConfiguration: Equatable {
- static func == (lhs: TunnelConfiguration, rhs: TunnelConfiguration) -> Bool {
+ public static func == (lhs: TunnelConfiguration, rhs: TunnelConfiguration) -> Bool {
return lhs.name == rhs.name &&
lhs.interface == rhs.interface &&
Set(lhs.peers) == Set(rhs.peers)