2

我正在开发一个 VPN 应用程序,VPN 工作正常,但 15-20 分钟后,它会自动断开连接。这是我正在使用的配置

        let vpnProtocol = NEVPNProtocolIKEv2()
        vpnProtocol.username = CredentialsManager.shared.accessToken
        vpnProtocol.localIdentifier = CredentialsManager.shared.accessToken

        print("VPN Connecting to \(self.region.name ?? "Error! Must be a valid region name!")")

        if let region = self.region {
          f
            vpnProtocol.serverAddress = region.serverAddress
            vpnProtocol.remoteIdentifier = region.serverAddress
        }

        let encodedIdentifier = "Secret Password".data(using: .utf8)!
        let item = [kSecClass: kSecClassGenericPassword,
                    kSecAttrGeneric: encodedIdentifier,
                    kSecAttrAccount: encodedIdentifier,
                    kSecMatchLimit: kSecMatchLimitOne,
                    kSecReturnPersistentRef: kCFBooleanTrue as Any,
                    kSecAttrService: "XYZ"] as [CFString : Any]

        var passwordReference: CFTypeRef?
        SecItemCopyMatching(item as CFDictionary, &passwordReference)

        vpnProtocol.passwordReference = passwordReference as? Data
        vpnProtocol.authenticationMethod = .none
        vpnProtocol.useExtendedAuthentication = true

        vpnProtocol.ikeSecurityAssociationParameters.encryptionAlgorithm =
            .algorithmAES256GCM
        vpnProtocol.ikeSecurityAssociationParameters.integrityAlgorithm = .SHA384
        vpnProtocol.ikeSecurityAssociationParameters.diffieHellmanGroup = .group14

        vpnProtocol.childSecurityAssociationParameters.encryptionAlgorithm = .algorithmAES256GCM
        vpnProtocol.childSecurityAssociationParameters.integrityAlgorithm = .SHA384
        vpnProtocol.childSecurityAssociationParameters.diffieHellmanGroup = .group14

        vpnProtocol.disconnectOnSleep = false

        self.vpnManager.protocolConfiguration = vpnProtocol
        let connectRule = NEOnDemandRuleConnect()
        connectRule.interfaceTypeMatch = .any

        self.vpnManager.onDemandRules = [connectRule]
        self.vpnManager.isOnDemandEnabled = self.connectOnDemand
        self.vpnManager.localizedDescription = "XYZ VPN"
        self.vpnManager.isEnabled = true

请帮助我,如何识别导致自动断开连接的问题。

4

2 回答 2

0

用这个改变你的 VPN 协议:NEVPNProtocolIPSec 也许它会帮助你。

于 2019-12-23T10:41:04.650 回答
0

您已经配置了“按需连接”,所以下次访问资源时它会自动连接回来。这就是 iOS 上 VPN 的工作方式,它总是会在空闲时关闭连接。

于 2019-12-23T14:41:08.193 回答