More linter warnings fixed, enabled more swiftlint rules, project cleanup
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
This commit is contained in:
@@ -66,25 +66,12 @@ extension DNSResolver {
|
||||
// Based on DNS resolution code by Jason Donenfeld <jason@zx2c4.com>
|
||||
// in parse_endpoint() in src/tools/config.c in the WireGuard codebase
|
||||
private static func resolveSync(endpoint: Endpoint) -> Endpoint? {
|
||||
var hints = addrinfo(
|
||||
ai_flags: 0,
|
||||
ai_family: AF_UNSPEC,
|
||||
ai_socktype: SOCK_DGRAM, // WireGuard is UDP-only
|
||||
ai_protocol: IPPROTO_UDP, // WireGuard is UDP-only
|
||||
ai_addrlen: 0,
|
||||
ai_canonname: nil,
|
||||
ai_addr: nil,
|
||||
ai_next: nil)
|
||||
var resultPointer = UnsafeMutablePointer<addrinfo>(OpaquePointer(bitPattern: 0))
|
||||
switch endpoint.host {
|
||||
case .name(let name, _):
|
||||
var resultPointer = UnsafeMutablePointer<addrinfo>(OpaquePointer(bitPattern: 0))
|
||||
|
||||
// The endpoint is a hostname and needs DNS resolution
|
||||
let returnValue = getaddrinfo(
|
||||
name.cString(using: .utf8), // Hostname
|
||||
"\(endpoint.port)".cString(using: .utf8), // Port
|
||||
&hints,
|
||||
&resultPointer)
|
||||
if returnValue == 0 {
|
||||
if addressInfo(for: name, port: endpoint.port, resultPointer: &resultPointer) == 0 {
|
||||
// getaddrinfo succeeded
|
||||
let ipv4Buffer = UnsafeMutablePointer<Int8>.allocate(capacity: Int(INET_ADDRSTRLEN))
|
||||
let ipv6Buffer = UnsafeMutablePointer<Int8>.allocate(capacity: Int(INET6_ADDRSTRLEN))
|
||||
@@ -115,9 +102,9 @@ extension DNSResolver {
|
||||
ipv6Buffer.deallocate()
|
||||
// We prefer an IPv4 address over an IPv6 address
|
||||
if let ipv4AddressString = ipv4AddressString, let ipv4Address = IPv4Address(ipv4AddressString) {
|
||||
return Endpoint(host: NWEndpoint.Host.ipv4(ipv4Address), port: endpoint.port)
|
||||
return Endpoint(host: .ipv4(ipv4Address), port: endpoint.port)
|
||||
} else if let ipv6AddressString = ipv6AddressString, let ipv6Address = IPv6Address(ipv6AddressString) {
|
||||
return Endpoint(host: NWEndpoint.Host.ipv6(ipv6Address), port: endpoint.port)
|
||||
return Endpoint(host: .ipv6(ipv6Address), port: endpoint.port)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
@@ -130,4 +117,22 @@ extension DNSResolver {
|
||||
return endpoint
|
||||
}
|
||||
}
|
||||
|
||||
private static func addressInfo(for name: String, port: NWEndpoint.Port, resultPointer: inout UnsafeMutablePointer<addrinfo>?) -> Int32 {
|
||||
var hints = addrinfo(
|
||||
ai_flags: 0,
|
||||
ai_family: AF_UNSPEC,
|
||||
ai_socktype: SOCK_DGRAM, // WireGuard is UDP-only
|
||||
ai_protocol: IPPROTO_UDP, // WireGuard is UDP-only
|
||||
ai_addrlen: 0,
|
||||
ai_canonname: nil,
|
||||
ai_addr: nil,
|
||||
ai_next: nil)
|
||||
|
||||
return getaddrinfo(
|
||||
name.cString(using: .utf8), // Hostname
|
||||
"\(port)".cString(using: .utf8), // Port
|
||||
&hints,
|
||||
&resultPointer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,6 @@ class ErrorNotifier {
|
||||
static func notify(_ error: PacketTunnelProviderError, from tunnelProvider: NEPacketTunnelProvider) {
|
||||
guard let (title, message) = ErrorNotifier.errorMessage(for: error) else { return }
|
||||
// displayMessage() is deprecated, but there's no better alternative to show the error to the user
|
||||
tunnelProvider.displayMessage("\(title): \(message)", completionHandler: { (_) in })
|
||||
tunnelProvider.displayMessage("\(title): \(message)") { _ in }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||
|
||||
// Bring up wireguard-go backend
|
||||
|
||||
let fileDescriptor = packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int32
|
||||
let fileDescriptor = packetFlow.value(forKeyPath: "socket.fileDescriptor") as! Int32 //swiftlint:disable:this force_cast
|
||||
if fileDescriptor < 0 {
|
||||
wg_log(.error, staticMessage: "Starting tunnel failed: Could not determine file descriptor")
|
||||
ErrorNotifier.notify(PacketTunnelProviderError.couldNotStartWireGuard, from: self)
|
||||
@@ -124,7 +124,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||
// Apply network settings
|
||||
|
||||
let networkSettings: NEPacketTunnelNetworkSettings = packetTunnelSettingsGenerator.generateNetworkSettings()
|
||||
setTunnelNetworkSettings(networkSettings) { (error) in
|
||||
setTunnelNetworkSettings(networkSettings) { error in
|
||||
if let error = error {
|
||||
wg_log(.error, staticMessage: "Starting tunnel failed: Error setting network settings.")
|
||||
wg_log(.error, message: "Error from setTunnelNetworkSettings: \(error.localizedDescription)")
|
||||
@@ -169,7 +169,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||
}
|
||||
|
||||
// Setup WireGuard logger
|
||||
wgSetLogger { (level, msgCStr) in
|
||||
wgSetLogger { level, msgCStr in
|
||||
let logType: OSLogType
|
||||
switch level {
|
||||
case 0:
|
||||
@@ -187,7 +187,7 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||
}
|
||||
|
||||
private func connect(interfaceName: String, settings: String, fileDescriptor: Int32) -> Int32 {
|
||||
return withStringsAsGoStrings(interfaceName, settings) { (nameGoStr, settingsGoStr) -> Int32 in
|
||||
return withStringsAsGoStrings(interfaceName, settings) { nameGoStr, settingsGoStr in
|
||||
return wgTurnOn(nameGoStr, settingsGoStr, fileDescriptor)
|
||||
}
|
||||
}
|
||||
@@ -206,9 +206,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
|
||||
}
|
||||
|
||||
private func withStringsAsGoStrings<R>(_ str1: String, _ str2: String, closure: (gostring_t, gostring_t) -> R) -> R {
|
||||
return str1.withCString { (s1cStr) -> R in
|
||||
return str1.withCString { s1cStr in
|
||||
let gstr1 = gostring_t(p: s1cStr, n: str1.utf8.count)
|
||||
return str2.withCString { (s2cStr) -> R in
|
||||
return str2.withCString { s2cStr in
|
||||
let gstr2 = gostring_t(p: s2cStr, n: str2.utf8.count)
|
||||
return closure(gstr1, gstr2)
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ class PacketTunnelSettingsGenerator {
|
||||
* a valid IP address that will actually route over the Internet.
|
||||
*/
|
||||
var remoteAddress: String = "0.0.0.0"
|
||||
let endpointsCompact = resolvedEndpoints.compactMap({ $0 })
|
||||
let endpointsCompact = resolvedEndpoints.compactMap { $0 }
|
||||
if endpointsCompact.count == 1 {
|
||||
switch endpointsCompact.first!.host {
|
||||
case .ipv4(let address):
|
||||
|
||||
Reference in New Issue
Block a user