wireguard-apple/WireGuardNetworkExtension/PacketTunnelProvider.swift

95 lines
4.1 KiB
Swift
Raw Normal View History

//
// 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.
//
import NetworkExtension
import os.log
2018-08-03 20:24:41 +00:00
enum PacketTunnelProviderError: Error {
case tunnelSetupFailed
}
/// A packet tunnel provider object.
class PacketTunnelProvider: NEPacketTunnelProvider {
2018-08-03 20:24:41 +00:00
// MARK: Properties
/// A reference to the WireGuard wrapper object.
let wireGuardWrapper = WireGuardGoWrapper()
2018-08-03 20:24:41 +00:00
// MARK: NEPacketTunnelProvider
2018-08-03 20:24:41 +00:00
/// Begin the process of establishing the tunnel.
override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) {
os_log("Starting tunnel", log: Log.general, type: .info)
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: ",")
settings.split(separator: "\n").forEach {os_log("Tunnel config: %{public}s", log: Log.general, type: .info, String($0))}
if wireGuardWrapper.turnOn(withInterfaceName: interfaceName, settingsString: settings) {
// Success
2018-08-12 19:44:53 +00:00
//TODO: Hardcoded values for addresses
let ipv4Settings = NEIPv4Settings(addresses: ["10.50.10.171"], subnetMasks: ["255.255.224.0"])
//TODO: Hardcoded values for allowed ips
ipv4Settings.includedRoutes = [NEIPv4Route.default()]
2018-08-12 19:44:53 +00:00
ipv4Settings.excludedRoutes = endpoints.split(separator: ",").compactMap { $0.split(separator: ":").first}.map {NEIPv4Route(destinationAddress: String($0), subnetMask: "255.255.255.255")}
2018-08-12 19:44:53 +00:00
//TODO IPv6 settings
let newSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "149.248.160.60")
newSettings.ipv4Settings = ipv4Settings
2018-08-12 19:44:53 +00:00
newSettings.tunnelOverheadBytes = 80
2018-08-13 11:48:43 +00:00
if let dns = config.providerConfiguration?[PCKeys.dns.rawValue] as? String {
2018-08-12 19:44:53 +00:00
var splitDnsEntries = dns.split(separator: ",").map {String($0)}
let dnsSettings = NEDNSSettings(servers: splitDnsEntries)
newSettings.dnsSettings = dnsSettings
}
if let mtu = mtu, mtu.intValue > 0 {
2018-08-12 19:44:53 +00:00
newSettings.mtu = mtu
}
setTunnelNetworkSettings(newSettings) { [weak self](error) in
self?.wireGuardWrapper.packetFlow = self?.packetFlow
2018-08-12 19:44:53 +00:00
self?.wireGuardWrapper.configured = true
self?.wireGuardWrapper.startReadingPackets()
completionHandler(error)
2018-08-12 19:44:53 +00:00
}
2018-08-03 20:24:41 +00:00
} else {
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-08-03 20:24:41 +00:00
/// Begin the process of stopping the tunnel.
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
os_log("Stopping tunnel", log: Log.general, type: .info)
2018-08-03 20:24:41 +00:00
wireGuardWrapper.turnOff()
completionHandler()
}
2018-08-03 20:24:41 +00:00
/// Handle IPC messages from the app.
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-08-03 20:24:41 +00:00
os_log("Got a message from the app: %s", log: Log.general, type: .info, messageString)
2018-08-03 20:24:41 +00:00
let responseData = "Hello app".data(using: String.Encoding.utf8)
completionHandler?(responseData)
}
}