Call startVPNTunnel
to kick off the proxy:
NETransparentProxyManager.loadAllFromPreferences { managers, error in
precondition(Thread.isMainThread)
if let nsError = error as NSError? {
// Handle error
return
}
let NEManagers = managers ?? []
guard let manager = NEManagers.first(where: Self.isOurManager(_:)) else {
return
}
do {
let options: [String : NSObject]? = ["Optional": "Value" as NSObject]
try manager.connection.startVPNTunnel(options: options)
// Handle Success
} catch {
// Handle error
}
}
Now, over in your provider, make sure you are using os_log
statements to make sure you can debug what is happening in the provider.
final class TransparentProxyProvider: NEAppProxyProvider {
static let log = OSLog(subsystem: "com.example.apple-samplecode.TransparentProxyTestBed.TransparentProxy", category: "provider")
private let queue = DispatchQueue(label: "TransparentProxyProvider", autoreleaseFrequency: .workItem)
private let log: OSLog
override init() {
self.log = Self.log
os_log(.debug, log: self.log, "init")
super.init()
}
override func startProxy(options: [String: Any]? = nil, completionHandler: @escaping (Error?) -> Void) {
os_log(.debug, log: self.log, "provider will start")
self.queue.async {
// Again, for testing only.
let settings = NETransparentProxyNetworkSettings(tunnelRemoteAddress: "127.0.0.1")
settings.includedNetworkRules = [
NENetworkRule(remoteNetwork: NWHostEndpoint(hostname: "example.com", port: "443"),
remotePrefix: 0,
localNetwork: nil,
localPrefix: 0,
protocol:.TCP,
direction: .outbound)
]
// If it succeeds, start the tunnel.
self.setTunnelNetworkSettings(settings) { error in
if let err = error as NSError? {
completionHandler(err)
// Handle error
return
}
// All is good.
completionHandler(nil)
}
}
}
override func handleNewFlow(_ flow: NEAppProxyFlow) -> Bool {
os_log(.debug, log: self.log, "provider will handle new flow, flow: %{public}@", flow.description)
return true
}
}
To use these os_log
statements log out the subsystem here:
$ log stream --level debug --predicate 'subsystem == "com.example.apple-samplecode.TransparentProxyTestBed.TransparentProxy"'
Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com