NEVPNManager with L2TP Protocol

≡放荡痞女 提交于 2020-01-24 01:59:10

问题


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.

来源:https://stackoverflow.com/questions/53781609/nevpnmanager-with-l2tp-protocol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!