What happens when I try to run my app with the DNS proxy provider in network extension while another app with the same extension is already running? Will it throw an error?
Post
Replies
Boosts
Views
Activity
I've created a MacOS app project which contains System Network Extension (DNS proxy). With help of https://developer.apple.com/forums/thread/81103?answerId=246229022 and https://stackoverflow.com/questions/45508605/how-to-use-nednsproxyprovider-in-ios-11. I enabled DNSProxyProvider. When I built the app for the first time a prompt asked to allow the system extension. I allowed it and Under network in system preference, I can see DNS is not running (yellow dot).
I also changed my entitlements and info.plist as given in the https://stackoverflow.com/questions/45508605/how-to-use-nednsproxyprovider-in-ios-11
My APP delegate:
private func enable() {
NSLog("enabled already ",self.manager.isEnabled )
self.update {
self.manager.localizedDescription = "DNS"
let proto = NEDNSProxyProviderProtocol()
proto.providerBundleIdentifier = "com.procyon.ai.ProcyonDNS.SystemDNSProxyExtension"
self.manager.providerProtocol = proto
self.manager.isEnabled = true
}
// NEDNSProxyManager.conn
}
private func disable() {
self.update {
self.manager.isEnabled = false
}
}
private func update(_ body: @escaping () -> Void) {
self.manager.loadFromPreferences { (error) in
guard error == nil else {
NSLog("DNS Test App: load error")
return
}
body()
self.manager.saveToPreferences { (error) in
guard error == nil else {
NSLog("DNS Test App: save error")
return
}
NSLog("DNS Test App: saved")
}
}
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.enable()
// let DNSManager=NEDNSProxyManager.shared()
// DNSManager.shared().start() {}
// Insert code here to initialize your application
}
Extension :
import OSLog
class DNSProxyProvider: NEDNSProxyProvider {
static let log = OSLog(subsystem: "com.example.apple-samplecode.DNSTestBed.DNSExtension", category: "provider")
private let log: OSLog
override init() {
NSLog("QNEDNSProxy.Provider: 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) {
NSLog("QNEDNSProxy.Provider: start")
// Add code here to start the DNS proxy.
completionHandler(nil)
}
override func stopProxy(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
NSLog("QNEDNSProxy.Provider: stop")
// Add code here to stop the DNS proxy.
completionHandler()
}
override func sleep(completionHandler: @escaping () -> Void) {
// Add code here to get ready to sleep.
completionHandler()
}
override func wake() {
// Add code here to wake up.
}
override func handleNewFlow(_ flow: NEAppProxyFlow) -> Bool {
NSLog("QNEDNSProxy.Provider: new flow ")
NSLog("DNSProxyProvider: handleFlow")
if let tcpFlow = flow as? NEAppProxyTCPFlow {
let remoteHost = (tcpFlow.remoteEndpoint as! NWHostEndpoint).hostname
let remotePort = (tcpFlow.remoteEndpoint as! NWHostEndpoint).port
NSLog("DNSProxyProvider: handleFlow",remotePort," ", remoteHost)
// Do whatever I want with this data
} else if let udpFlow = flow as? NEAppProxyUDPFlow {
let localHost = (udpFlow.localEndpoint as! NWHostEndpoint).hostname
let localPort = (udpFlow.localEndpoint as! NWHostEndpoint).port
NSLog("DNSProxyProvider: handleFlow",localHost," ", localPort)
// Do whatever I want with this data
}
// Add code here to handle the incoming flow.
return false
}
}
As you can see I added logs in the extension code. But I don't those logs when I launch the app. start proxy is not executed and the main in the extension is also not executed (added logs there as well).
I want to start the system extension when the app is launched. Any help would be great.