Search code examples
iosswiftconnectionvpn

iOS VPN auto disconnected, Algo VPN


I am create vpn profile used NEVPNManager based on NEVPNProtocolIKEv2 and when I am try connected to my vpn server(Algo VPN) I am connected only on a moment and then disconnected, but why? I am try examine this problem but don't know what undertake(.

Maybe anyone have an idea about that or you detection with this error in past?

This is server side log:

Oct 12 08:49:17 server charon: 09[IKE] 195.114.147.67 is initiating an IKE_SA
Oct 12 08:49:18 server charon: 11[IKE] IKE_SA ikev2-pubkey[91] established between 168.119.152.40[168.119.152.40]...195.114.147.67[[email protected]]
Oct 12 08:49:18 server charon: 11[IKE] CHILD_SA ikev2-pubkey{4} established with SPIs c2424671_i 07f5b8dd_o and TS 0.0.0.0/0 ::/0 === 10.19.48.1/32 2001:db8:4160::1/128
Oct 12 08:49:23 server charon: 13[IKE] deleting IKE_SA ikev2-pubkey[91] between 168.119.152.40[168.119.152.40]...195.114.147.67[[email protected]]
Oct 12 08:49:23 server charon: 13[IKE] IKE_SA deleted
Oct 12 08:54:57 server charon: 07[IKE] 195.114.147.67 is initiating an IKE_SA
Oct 12 08:54:57 server charon: 12[IKE] 195.114.147.67 is initiating an IKE_SA
Oct 12 08:57:42 server charon: 11[IKE] 195.114.147.67 is initiating an IKE_SA
Oct 12 08:57:42 server charon: 06[IKE] 195.114.147.67 is initiating an IKE_SA
Oct 12 08:57:44 server charon: 13[IKE] 195.114.147.67 is initiating an IKE_SA
Oct 12 08:57:44 server charon: 07[IKE] 195.114.147.67 is initiating an IKE_SA

This is example of my code.

import Foundation
import NetworkExtension

class VPNIKEv2Setup {
  
  /*
  VPN configuration was getting from file "client.mobileconfig" which was be generated on server(Algo VPN), 
  if VPN will be installing using this file all will be work well.
  */
  
  
    let p12Password = "*****" // password from certificate file "client.p12"
    let vpnServerAddress = "******" // vpn address
    let vpnRemoteIdentifier = "*****" // remote identifier, in my case same like a vpn server address
    let vpnLocalIdentifier = "client@935bfdbd-*********.algo" // local identifier name
    let vpnServerCertificateIssuerCommonName = "s****r" // sertificate server name
    
    static let shared = VPNIKEv2Setup()

    let vpnManager = NEVPNManager.shared()
    
    func initVPNTunnelProviderManager() {

        print("CALL LOAD TO PREFERENCES...")
        self.vpnManager.loadFromPreferences { [self] (error) -> Void in
            
            if((error) != nil) {

                print("VPN Preferences error: 1 - \(String(describing: error))")
            } else {

                let IKEv2Protocol = NEVPNProtocolIKEv2()
                
                IKEv2Protocol.authenticationMethod = .certificate
                IKEv2Protocol.serverAddress = self.vpnServerAddress
                IKEv2Protocol.remoteIdentifier = self.vpnRemoteIdentifier
                IKEv2Protocol.localIdentifier = self.vpnLocalIdentifier

                IKEv2Protocol.useExtendedAuthentication = true
                IKEv2Protocol.ikeSecurityAssociationParameters.encryptionAlgorithm = .algorithmAES256GCM
                IKEv2Protocol.ikeSecurityAssociationParameters.diffieHellmanGroup = .group20
                IKEv2Protocol.ikeSecurityAssociationParameters.integrityAlgorithm = .SHA512
                IKEv2Protocol.ikeSecurityAssociationParameters.lifetimeMinutes = 1440

                IKEv2Protocol.childSecurityAssociationParameters.encryptionAlgorithm = .algorithmAES256GCM
                IKEv2Protocol.childSecurityAssociationParameters.diffieHellmanGroup = .group20
                IKEv2Protocol.childSecurityAssociationParameters.integrityAlgorithm = .SHA512
                IKEv2Protocol.childSecurityAssociationParameters.lifetimeMinutes = 1440

                IKEv2Protocol.deadPeerDetectionRate = .medium
                IKEv2Protocol.disableRedirect = true
                IKEv2Protocol.disableMOBIKE = false
                IKEv2Protocol.enableRevocationCheck = false
                IKEv2Protocol.enablePFS = true
                IKEv2Protocol.useConfigurationAttributeInternalIPSubnet = false

                IKEv2Protocol.serverCertificateIssuerCommonName = self.vpnServerCertificateIssuerCommonName
                IKEv2Protocol.disconnectOnSleep = false
                IKEv2Protocol.certificateType = .ECDSA384
                IKEv2Protocol.identityDataPassword = self.p12Password
                IKEv2Protocol.identityData = self.dataFromFile()

                self.vpnManager.protocolConfiguration = IKEv2Protocol
                self.vpnManager.localizedDescription = "My VPN"
                self.vpnManager.isEnabled = true

                self.vpnManager.isOnDemandEnabled = true

                //Set rules
                var rules = [NEOnDemandRule]()
                let rule = NEOnDemandRuleConnect()
                rule.interfaceTypeMatch = .any
                rules.append(rule)

                print("SAVE TO PREFERENCES...")
                //SAVE TO PREFERENCES...
                self.vpnManager.saveToPreferences(completionHandler: { (error) -> Void in
                    if((error) != nil) {

                        print("VPN Preferences error: 2 - \(String(describing: error))")
                    } else {

                        print("CALL LOAD TO PREFERENCES AGAIN...")
                        //CALL LOAD TO PREFERENCES AGAIN...
                        self.vpnManager.loadFromPreferences(completionHandler: { (error) in
                            if ((error) != nil) {
                                print("VPN Preferences error: 2 - \(String(describing: error))")
                            } else {
                                var startError: NSError?

                                do {
                                    //START THE CONNECTION...
                                    try self.vpnManager.connection.startVPNTunnel()
                                } catch let error as NSError {

                                    startError = error
                                    print(startError.debugDescription)
                                } catch {

                                    print("Fatal Error")
                                    fatalError()
                                }
                                if ((startError) != nil) {
                                    print("VPN Preferences error: 3 - \(String(describing: error))")

                                    //Show alert here
                                    print("title: Oops.., message: Something went wrong while connecting to the VPN. Please try again.")

                                    print(startError.debugDescription)
                                } else {
                                    //self.VPNStatusDidChange(nil)
                                    print("Starting VPN...")
                                }
                            }
                        })
                    }
                })
            }
        } //END OF .loadFromPreferences //
    }

    //MARK:- Connect VPN
    static func connectVPN() {
        VPNIKEv2Setup().initVPNTunnelProviderManager()
    }

    //MARK:- Disconnect VPN
    static func disconnectVPN() {
        VPNIKEv2Setup().vpnManager.connection.stopVPNTunnel()
    }
    
    //MARK:- Disconnect VPN
    static func testConnect() {
        do {
            try VPNIKEv2Setup().vpnManager.connection.startVPNTunnel()
        } catch let error {
            print(error)
        }
    }

    //MARK:- check connection staatus
    static func checkStatus() {

        let status = VPNIKEv2Setup().vpnManager.connection.status
        print("VPN connection status = \(status.rawValue)")

        switch status {
        case NEVPNStatus.connected:

            print("Connected")

        case NEVPNStatus.invalid, NEVPNStatus.disconnected :

            print("Disconnected")

        case NEVPNStatus.connecting , NEVPNStatus.reasserting:

            print("Connecting")

        case NEVPNStatus.disconnecting:

            print("Disconnecting")

        default:
            print("Unknown VPN connection status")
        }
    }
    
    func dataFromFile() -> Data? {
        let rootCertPath = Bundle.main.url(forResource: "client", withExtension: "p12")
        print(rootCertPath?.absoluteURL)
        return try? Data(contentsOf: rootCertPath!.absoluteURL)
    }    
}

Solution

  • I am solved problem, was need reinstall server with AlgoVPN, then use logs of server to change few preferences, check this thread to more detail info - https://developer.apple.com/forums/thread/662799