Added swiftlint and fixed all errors (and a bunch, but not all, warnings)
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
This commit is contained in:
@@ -28,13 +28,13 @@ class DNSResolver {
|
||||
}
|
||||
|
||||
var resolvedEndpoints: [Endpoint?] = Array<Endpoint?>(repeating: nil, count: endpoints.count)
|
||||
for (i, endpoint) in endpoints.enumerated() {
|
||||
for (index, endpoint) in endpoints.enumerated() {
|
||||
guard let endpoint = endpoint else { continue }
|
||||
if (endpoint.hasHostAsIPAddress()) {
|
||||
resolvedEndpoints[i] = endpoint
|
||||
resolvedEndpoints[index] = endpoint
|
||||
} else {
|
||||
let workItem = DispatchWorkItem {
|
||||
resolvedEndpoints[i] = DNSResolver.resolveSync(endpoint: endpoint)
|
||||
resolvedEndpoints[index] = DNSResolver.resolveSync(endpoint: endpoint)
|
||||
}
|
||||
DispatchQueue.global(qos: .userInitiated).async(group: dispatchGroup, execute: workItem)
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ class ErrorNotifier {
|
||||
switch (error) {
|
||||
case .savedProtocolConfigurationIsInvalid:
|
||||
return ("Activation failure", "Could not retrieve tunnel information from the saved configuration")
|
||||
case .dnsResolutionFailure(_):
|
||||
case .dnsResolutionFailure:
|
||||
return ("DNS resolution failure", "One or more endpoint domains could not be resolved")
|
||||
case .couldNotStartWireGuard:
|
||||
return ("Activation failure", "WireGuard backend could not be started")
|
||||
|
||||
@@ -21,7 +21,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||
// MARK: Properties
|
||||
|
||||
private var wgHandle: Int32?
|
||||
|
||||
|
||||
private var networkMonitor: NWPathMonitor?
|
||||
|
||||
// MARK: NEPacketTunnelProvider
|
||||
@@ -29,7 +29,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||
deinit {
|
||||
networkMonitor?.cancel()
|
||||
}
|
||||
|
||||
|
||||
/// Begin the process of establishing the tunnel.
|
||||
override func startTunnel(options: [String: NSObject]?,
|
||||
completionHandler startTunnelCompletionHandler: @escaping (Error?) -> Void) {
|
||||
@@ -89,9 +89,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||
}
|
||||
|
||||
let wireguardSettings = packetTunnelSettingsGenerator.uapiConfiguration()
|
||||
|
||||
|
||||
var handle: Int32 = -1
|
||||
|
||||
|
||||
networkMonitor = NWPathMonitor()
|
||||
networkMonitor?.pathUpdateHandler = { path in
|
||||
guard handle >= 0 else { return }
|
||||
@@ -99,18 +99,18 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||
wg_log(.debug, message: "Network change detected, re-establishing sockets and IPs: \(path.availableInterfaces)")
|
||||
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: wgGetListenPort(handle))
|
||||
let err = endpointString.withCString {
|
||||
wgSetConfig(handle, gostring_t(p: $0, n: endpointString.utf8.count))
|
||||
wgSetConfig(handle, gostring_t(p: $0, n: endpointString.utf8.count))
|
||||
}
|
||||
if err == -EADDRINUSE {
|
||||
let endpointString = packetTunnelSettingsGenerator.endpointUapiConfiguration(currentListenPort: 0)
|
||||
_ = endpointString.withCString {
|
||||
wgSetConfig(handle, gostring_t(p: $0, n: endpointString.utf8.count))
|
||||
wgSetConfig(handle, gostring_t(p: $0, n: endpointString.utf8.count))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
networkMonitor?.start(queue: DispatchQueue(label: "NetworkMonitor"))
|
||||
|
||||
|
||||
handle = connect(interfaceName: tunnelConfiguration.interface.name, settings: wireguardSettings, fd: fd)
|
||||
|
||||
if handle < 0 {
|
||||
@@ -141,7 +141,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
|
||||
networkMonitor?.cancel()
|
||||
networkMonitor = nil
|
||||
|
||||
|
||||
wg_log(.info, staticMessage: "Stopping tunnel")
|
||||
if let handle = wgHandle {
|
||||
wgTurnOff(handle)
|
||||
|
||||
@@ -18,17 +18,17 @@ class PacketTunnelSettingsGenerator {
|
||||
func endpointUapiConfiguration(currentListenPort: UInt16) -> String {
|
||||
var wgSettings = "listen_port=\(tunnelConfiguration.interface.listenPort ?? currentListenPort)\n"
|
||||
|
||||
for (i, peer) in tunnelConfiguration.peers.enumerated() {
|
||||
for (index, peer) in tunnelConfiguration.peers.enumerated() {
|
||||
wgSettings.append("public_key=\(peer.publicKey.hexEncodedString())\n")
|
||||
if let endpoint = resolvedEndpoints[i] {
|
||||
if let endpoint = resolvedEndpoints[index] {
|
||||
if case .name(_, _) = endpoint.host { assert(false, "Endpoint is not resolved") }
|
||||
wgSettings.append("endpoint=\(endpoint.stringRepresentation())\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return wgSettings
|
||||
}
|
||||
|
||||
|
||||
func uapiConfiguration() -> String {
|
||||
var wgSettings = ""
|
||||
let privateKey = tunnelConfiguration.interface.privateKey.hexEncodedString()
|
||||
@@ -40,12 +40,12 @@ class PacketTunnelSettingsGenerator {
|
||||
wgSettings.append("replace_peers=true\n")
|
||||
}
|
||||
assert(tunnelConfiguration.peers.count == resolvedEndpoints.count)
|
||||
for (i, peer) in tunnelConfiguration.peers.enumerated() {
|
||||
for (index, peer) in tunnelConfiguration.peers.enumerated() {
|
||||
wgSettings.append("public_key=\(peer.publicKey.hexEncodedString())\n")
|
||||
if let preSharedKey = peer.preSharedKey {
|
||||
wgSettings.append("preshared_key=\(preSharedKey.hexEncodedString())\n")
|
||||
}
|
||||
if let endpoint = resolvedEndpoints[i] {
|
||||
if let endpoint = resolvedEndpoints[index] {
|
||||
if case .name(_, _) = endpoint.host { assert(false, "Endpoint is not resolved") }
|
||||
wgSettings.append("endpoint=\(endpoint.stringRepresentation())\n")
|
||||
}
|
||||
@@ -200,10 +200,10 @@ class PacketTunnelSettingsGenerator {
|
||||
}
|
||||
|
||||
static func ipv4SubnetMaskString(of addressRange: IPAddressRange) -> String {
|
||||
let n: UInt8 = addressRange.networkPrefixLength
|
||||
assert(n <= 32)
|
||||
let length: UInt8 = addressRange.networkPrefixLength
|
||||
assert(length <= 32)
|
||||
var octets: [UInt8] = [0, 0, 0, 0]
|
||||
let subnetMask: UInt32 = n > 0 ? ~UInt32(0) << (32 - n) : UInt32(0)
|
||||
let subnetMask: UInt32 = length > 0 ? ~UInt32(0) << (32 - length) : UInt32(0)
|
||||
octets[0] = UInt8(truncatingIfNeeded: subnetMask >> 24)
|
||||
octets[1] = UInt8(truncatingIfNeeded: subnetMask >> 16)
|
||||
octets[2] = UInt8(truncatingIfNeeded: subnetMask >> 8)
|
||||
|
||||
Reference in New Issue
Block a user