rocket
rocket

Reputation: 51

PacketTunnelProvider network extension not called Swift 3

I am trying to add a PacketTunnerProvider network extension to my project. The method startTunnelWithOptions(options: [String : NSObject]?, completionHandler: (NSError?) -> Void) Never gets called

However, I am able to succesfully establish a VPN connection using the network extensions bundle id for the providerBundleIdentifier

This is my code used to establish a connection

let vpnManager = NETunnelProviderManager.shared()

 func initVPNTunnelProviderManager() {

    let config = NETunnelProviderProtocol()

    config.providerBundleIdentifier = self.tunnelBundleId
    config.providerConfiguration = ["lol": 1]
    config.serverAddress = self.serverAddress
    config.username = self.username
    config.passwordReference = passwordRef

    vpnManager.loadFromPreferences {
        (error: Error?) in

        self.vpnManager.protocolConfiguration = vpnProtocol
        self.vpnManager.localizedDescription = "Connect_1.0.0"
        self.vpnManager.isEnabled = true

        self.vpnManager.saveToPreferences {
            (error: Error?) in
            do {
                try self.vpnManager.connection.startVPNTunnel()
            } catch let error as NSError {
                print("Error: \(error.localizedDescription)")
            }
        }
    }
}

This is my PacketTunnel entitlements file

`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.developer.networking.vpn.api</key>
    <array>
        <string>allow-vpn</string>
    </array>
    <key>com.apple.security.application-groups</key>
    <array>
        <string>group.touchcore.Connectionapp</string>
    </array>
    <key>keychain-access-groups</key>
    <array>
        <string>$(AppIdentifierPrefix)touchcore.Connectionapp.PacketTunnel</string>
    </array>
    <key>com.apple.developer.networking.networkextension</key>
    <array>
        <string>packet-tunnel-provider</string>
        <string>app-proxy-provider</string>
        <string>content-filter-provider</string>
    </array>
</dict>
</plist>`

Upvotes: 4

Views: 3450

Answers (1)

Nathan F.
Nathan F.

Reputation: 3469

The method startTunnelWithOptions(options: [String : NSObject]?, completionHandler: (NSError?) -> Void) Never gets called

However, I am able to succesfully establish a VPN connection using the network extensions bundle id for the providerBundleIdentifier

What exactly do you mean it never gets called? If you're able to successfully establish a connection then startTunnelWithOptions is being called.

If you're trying to determine that it' being called by using an NSLog(), keep in mind that that will only show in the debug log if you attatch the debugger to the provider instead of your container application.

This will be difficult as the provider is initialized and the startTunnelWithOptions function is called before you get a chance to attach the debugger.

A useful work around in this situation is to sleep in order to give the debugger time to attach.

- (void) startTunnelWithOptions:(NSDictionary *) options
          completionHandler:(void (^)(NSError *)) completionHandler
{
    
    // Give debugger time to attach, 10 seconds is usually enough
    // Comment this out before you release the app or else you 
    // will be stuck with a 10 second delay on all connections.
    sleep(10); 

    // Continue with execution
    . . .
}

Then, when you initialize your PacketTunnelProvider it will wait 10 seconds before fully entering your logic inside of the startTunnelWithOptions function.

So, during this time in XCode you can go to Debug->Attach To Process->YourVPNProviderProcess and wait for it to fully initialize.

Attach the debugger to a running process

Upvotes: 5

Related Questions