2018-06-22 06:23:39 +00:00
|
|
|
//
|
|
|
|
// PacketTunnelProvider.swift
|
|
|
|
// WireGuardNetworkExtension
|
|
|
|
//
|
|
|
|
// Created by Jeroen Leenarts on 19-06-18.
|
2018-07-15 09:55:41 +00:00
|
|
|
// Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All rights reserved.
|
2018-06-22 06:23:39 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import NetworkExtension
|
2018-07-07 20:54:44 +00:00
|
|
|
import os.log
|
2018-06-22 06:23:39 +00:00
|
|
|
|
2018-08-03 20:24:41 +00:00
|
|
|
enum PacketTunnelProviderError: Error {
|
|
|
|
case tunnelSetupFailed
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A packet tunnel provider object.
|
2018-06-22 06:23:39 +00:00
|
|
|
class PacketTunnelProvider: NEPacketTunnelProvider {
|
2018-08-03 20:24:41 +00:00
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
/// A reference to the WireGuard wrapper object.
|
2018-07-07 20:54:44 +00:00
|
|
|
let wireGuardWrapper = WireGuardGoWrapper()
|
|
|
|
|
2018-08-03 20:24:41 +00:00
|
|
|
// MARK: NEPacketTunnelProvider
|
2018-06-22 06:23:39 +00:00
|
|
|
|
2018-08-03 20:24:41 +00:00
|
|
|
/// Begin the process of establishing the tunnel.
|
2018-06-22 06:23:39 +00:00
|
|
|
override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) {
|
2018-07-07 20:54:44 +00:00
|
|
|
os_log("Starting tunnel", log: Log.general, type: .info)
|
2018-06-22 06:23:39 +00:00
|
|
|
|
2018-08-08 05:04:42 +00:00
|
|
|
let config = self.protocolConfiguration as! NETunnelProviderProtocol // swiftlint:disable:this force_cast
|
2018-08-12 19:37:56 +00:00
|
|
|
let interfaceName = config.providerConfiguration![PCKeys.title.rawValue]! as! String // swiftlint:disable:this force_cast
|
|
|
|
let mtu = config.providerConfiguration![PCKeys.mtu.rawValue] as? NSNumber
|
|
|
|
let settings = config.providerConfiguration![PCKeys.settings.rawValue]! as! String // swiftlint:disable:this force_cast
|
|
|
|
let endpoints = config.providerConfiguration?[PCKeys.endpoints.rawValue] as? String ?? ""
|
|
|
|
let addresses = (config.providerConfiguration?[PCKeys.addresses.rawValue] as? String ?? "").split(separator: ",")
|
|
|
|
|
2018-08-16 19:26:24 +00:00
|
|
|
let validatedEndpoints = endpoints.split(separator: ",").compactMap { try? Endpoint(endpointString: String($0)) }.compactMap {$0}
|
|
|
|
let validatedAddresses = addresses.compactMap { try? CIDRAddress(stringRepresentation: String($0)) }.compactMap { $0 }
|
2018-08-05 20:58:48 +00:00
|
|
|
|
2018-08-08 05:04:42 +00:00
|
|
|
if wireGuardWrapper.turnOn(withInterfaceName: interfaceName, settingsString: settings) {
|
2018-08-16 19:26:24 +00:00
|
|
|
//TODO: Hardcoded values for tunnelRemoteAddress
|
|
|
|
let newSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "149.248.160.60")
|
|
|
|
newSettings.tunnelOverheadBytes = 80
|
|
|
|
|
2018-08-15 20:57:40 +00:00
|
|
|
// IPv4 settings
|
2018-08-16 19:26:24 +00:00
|
|
|
let validatedIPv4Addresses = validatedAddresses.filter { $0.addressType == .IPv4}
|
|
|
|
if validatedIPv4Addresses.count > 0 {
|
|
|
|
let ipv4Settings = NEIPv4Settings(addresses: validatedIPv4Addresses.map { $0.ipAddress }, subnetMasks: validatedIPv4Addresses.map { $0.subnetString })
|
|
|
|
ipv4Settings.includedRoutes = [NEIPv4Route.default()]
|
|
|
|
ipv4Settings.excludedRoutes = validatedEndpoints.filter { $0.addressType == .IPv4}.map {
|
|
|
|
NEIPv4Route(destinationAddress: $0.ipAddress, subnetMask: "255.255.255.255")}
|
|
|
|
|
|
|
|
newSettings.ipv4Settings = ipv4Settings
|
|
|
|
}
|
2018-08-05 20:58:48 +00:00
|
|
|
|
2018-08-15 20:57:40 +00:00
|
|
|
// IPv6 settings
|
2018-08-16 19:26:24 +00:00
|
|
|
let validatedIPv6Addresses = validatedAddresses.filter { $0.addressType == .IPv6}
|
|
|
|
if validatedIPv6Addresses.count > 0 {
|
|
|
|
let ipv6Settings = NEIPv6Settings(addresses: validatedIPv6Addresses.map { $0.ipAddress }, networkPrefixLengths: validatedIPv6Addresses.map { NSNumber(value: $0.subnet) })
|
|
|
|
ipv6Settings.includedRoutes = [NEIPv6Route.default()]
|
|
|
|
ipv6Settings.excludedRoutes = validatedEndpoints.filter { $0.addressType == .IPv6}.map { NEIPv6Route(destinationAddress: $0.ipAddress, networkPrefixLength: 0)}
|
2018-08-15 20:57:40 +00:00
|
|
|
|
2018-08-16 19:26:24 +00:00
|
|
|
newSettings.ipv6Settings = ipv6Settings
|
|
|
|
}
|
2018-08-15 20:57:40 +00:00
|
|
|
|
2018-08-13 11:48:43 +00:00
|
|
|
if let dns = config.providerConfiguration?[PCKeys.dns.rawValue] as? String {
|
2018-08-16 19:26:24 +00:00
|
|
|
let splitDnsEntries = dns.split(separator: ",").map {String($0)}
|
2018-08-12 19:44:53 +00:00
|
|
|
let dnsSettings = NEDNSSettings(servers: splitDnsEntries)
|
|
|
|
newSettings.dnsSettings = dnsSettings
|
|
|
|
}
|
2018-08-13 11:46:56 +00:00
|
|
|
if let mtu = mtu, mtu.intValue > 0 {
|
2018-08-12 19:44:53 +00:00
|
|
|
newSettings.mtu = mtu
|
|
|
|
}
|
|
|
|
|
|
|
|
setTunnelNetworkSettings(newSettings) { [weak self](error) in
|
2018-08-14 19:40:20 +00:00
|
|
|
self?.wireGuardWrapper.packetFlow = self?.packetFlow
|
2018-08-12 19:44:53 +00:00
|
|
|
self?.wireGuardWrapper.configured = true
|
2018-08-13 11:48:08 +00:00
|
|
|
self?.wireGuardWrapper.startReadingPackets()
|
2018-08-14 19:40:20 +00:00
|
|
|
completionHandler(error)
|
2018-08-12 19:44:53 +00:00
|
|
|
}
|
2018-08-05 20:58:48 +00:00
|
|
|
|
2018-08-03 20:24:41 +00:00
|
|
|
} else {
|
2018-08-14 19:40:20 +00:00
|
|
|
self.wireGuardWrapper.packetFlow = self.packetFlow
|
2018-08-03 20:24:41 +00:00
|
|
|
completionHandler(PacketTunnelProviderError.tunnelSetupFailed)
|
2018-08-12 19:44:53 +00:00
|
|
|
wireGuardWrapper.configured = false
|
2018-07-07 20:54:44 +00:00
|
|
|
}
|
2018-06-22 06:23:39 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 20:24:41 +00:00
|
|
|
/// Begin the process of stopping the tunnel.
|
2018-06-22 06:23:39 +00:00
|
|
|
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
|
2018-07-07 20:54:44 +00:00
|
|
|
os_log("Stopping tunnel", log: Log.general, type: .info)
|
2018-08-03 20:24:41 +00:00
|
|
|
|
|
|
|
wireGuardWrapper.turnOff()
|
2018-06-22 06:23:39 +00:00
|
|
|
completionHandler()
|
|
|
|
}
|
|
|
|
|
2018-08-03 20:24:41 +00:00
|
|
|
/// Handle IPC messages from the app.
|
2018-06-22 06:23:39 +00:00
|
|
|
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
|
2018-08-03 20:24:41 +00:00
|
|
|
guard let messageString = NSString(data: messageData, encoding: String.Encoding.utf8.rawValue) else {
|
|
|
|
completionHandler?(nil)
|
|
|
|
return
|
2018-07-07 20:54:44 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 20:24:41 +00:00
|
|
|
os_log("Got a message from the app: %s", log: Log.general, type: .info, messageString)
|
2018-06-22 06:23:39 +00:00
|
|
|
|
2018-08-03 20:24:41 +00:00
|
|
|
let responseData = "Hello app".data(using: String.Encoding.utf8)
|
|
|
|
completionHandler?(responseData)
|
2018-06-22 06:23:39 +00:00
|
|
|
}
|
|
|
|
}
|