NEVPNManager with L2TP Protocol

2019-06-08 19:45发布

I'm working with VPN and I ask this question but now I would like to create a VPN profile using L2TP Protocol, not the IPSec protocol.

I have all the information I need (user, server, password, pre sharedKey) and the service is correctly ON. I'm trying so to create an App that simply connect to the VPN by creating the right Setting Profile like the app '1.1.1.1' in the App Store.

I'm using the NEVPNProtocolIPSec Class but I think is wrong. The is no class for L2PT protocol?

In the device setting I'm able to manually configure the VPN for L2PT but how ca I do it using NEVPNManager??

Here my code:

class VPN {

let vpnManager = NEVPNManager.shared();

private var vpnLoadHandler: (Error?) -> Void { return
{ (error:Error?) in
    if ((error) != nil) {
        print("Could not load VPN Configurations")
        return;
    }
    let p = NEVPNProtocolIPSec()
    p.username = "myUsername"
    p.serverAddress = "myAddressServer"
    p.authenticationMethod = NEVPNIKEAuthenticationMethod.sharedSecret

    let kcs = KeychainService();
    kcs.save(key: "SHARED", value: "sharedPsw")
    kcs.save(key: "VPN_PASSWORD", value: "password")
        p.sharedSecretReference = kcs.load(key: "SHARED")
        p.passwordReference = kcs.load(key: "VPN_PASSWORD")
        p.useExtendedAuthentication = true
        p.disconnectOnSleep = false
        self.vpnManager.protocolConfiguration = p
        self.vpnManager.localizedDescription = "myDescription"
        self.vpnManager.isEnabled = true
        self.vpnManager.isOnDemandEnabled = true
        self.vpnManager.saveToPreferences(completionHandler: self.vpnSaveHandler)
        }

}

private var vpnSaveHandler: (Error?) -> Void { return
{ (error:Error?) in
    if (error != nil) {
        print("Could not save VPN Configurations")
        return
    } else {
        do {
            try self.vpnManager.connection.startVPNTunnel()
        } catch let error {
            print("Error starting VPN Connection \(error.localizedDescription)");
        }
    }
    }
}

public func connectVPN() {
    //For no known reason the process of saving/loading the VPN configurations fails.On the 2nd time it works
    do {
        try self.vpnManager.loadFromPreferences(completionHandler: self.vpnLoadHandler)

    } catch let error {
        print("Could not start VPN Connection: \(error.localizedDescription)" )
    }
}

public func disconnectVPN() ->Void {
    vpnManager.connection.stopVPNTunnel()
}

I't working but it create a VPN Configuration for IPSec but I want the L2PT. Can somebody else help me please?

Maybe somebody face the same problem.

0条回答
登录 后发表回答