2

guys.

I'm trying to setup a VPN connection to my server, but something goes wrong.

I've tried setting it up manually and it works.

Here's my configuration code. The server object has a name and a correct host for sure and i tried disabling extended authentication. Afaik remote and local identifiers are just random strings, so i have no idea what i could have done wrong.

fileprivate func updateCurrentVPN(withServer server: Server, completion: ((Bool) -> Void)?) {

    let passwordRef = KeychainWrapper.standard.dataRef(forKey: self.account.username)
    let secretRef = KeychainWrapper.standard.dataRef(forKey: API.ipsecSecretKey())

    // checking if the strings are correct in a debugger
    let password = KeychainWrapper.standard.string(forKey: self.account.username)
    let secret = KeychainWrapper.standard.string(forKey: API.ipsecSecretKey())

    // VPN
    let manager = NEVPNManager.shared()
    manager.loadFromPreferences { (error) in
        if (error != nil)
        {
            print("Error: ", error.debugDescription)
            completion?(false)
        }
        else
        {
            let prtcl = NEVPNProtocolIPSec()
            prtcl.username = self.account.username
            prtcl.passwordReference = passwordRef
            prtcl.serverAddress = server.host
            prtcl.authenticationMethod = NEVPNIKEAuthenticationMethod.sharedSecret
            prtcl.sharedSecretReference = secretRef
            prtcl.localIdentifier = "Test iOS device"
            prtcl.remoteIdentifier = server.name
            prtcl.useExtendedAuthentication = true
            prtcl.disconnectOnSleep = false

            manager.isEnabled = true
            manager.protocolConfiguration = prtcl
            manager.isOnDemandEnabled = false
            manager.localizedDescription = "MyVPN Configuration"
            manager.saveToPreferences(completionHandler: { (error) in
                if (error != nil)
                {
                    print("Error: ", error.debugDescription)
                    completion?(false)
                }
                else
                {
                    self.serverButton.setTitle(server.name, for: UIControlState.normal)
                    print("VPN prefs saved")
                    completion?(true)
                }
            })
        }
    }

After config is saved i do:

 let manager = NEVPNManager.shared()
 do {
    try manager.connection.startVPNTunnel()
 } catch {
    print(error.localizedDescription)
    connectButton.setTitle("da failure :(", for: UIControlState.normal)
 }

But it never throws an error, just prompts me with "Negotiation with the VPN server has failed" in a system alert. Btw, is it possible to catch this kind of errors?

4

2 回答 2

3

显然,根据这个答案,iOS SDK 有一个错误。Afaik SDK 将本地标识符视为组名,并且在设置后,客户端会启动积极模式 IKE_SA。结果即使您在后端允许激进模式,它也不会发生太大变化(在我的情况下它没有)。

删除线

prtcl.localIdentifier = "Test iOS device"
prtcl.remoteIdentifier = server.name

解决了这个问题。

于 2017-02-14T22:25:28.750 回答
0

我有同样的错误,因为密码和 sharedSecret 钥匙串值已经过时了。

于 2018-06-12T09:43:31.597 回答