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
|
|
|
/// The completion handler to call when the tunnel is fully established.
|
|
|
|
var pendingStartCompletion: ((Error?) -> Void)?
|
|
|
|
|
|
|
|
/// The completion handler to call when the tunnel is fully disconnected.
|
|
|
|
var pendingStopCompletion: (() -> Void)?
|
2018-07-07 20:54:44 +00:00
|
|
|
|
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-03 20:24:41 +00:00
|
|
|
//TODO tunnel settings
|
|
|
|
if wireGuardWrapper.turnOn(withInterfaceName: "test", settingsString: "") {
|
|
|
|
// Success
|
|
|
|
completionHandler(nil)
|
|
|
|
} else {
|
|
|
|
completionHandler(PacketTunnelProviderError.tunnelSetupFailed)
|
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
|
|
|
}
|
|
|
|
}
|