OpenVPNAdapter icon indicating copy to clipboard operation
OpenVPNAdapter copied to clipboard

VPN not Connecting on iOS

Open sohilmemon opened this issue 6 years ago • 9 comments

I don't know where I am making mistake. Below is the code I am using to connect VPN in iOS app.

@objc private func establishVPNConnection() {
        let callback = { (error: Error?) -> Void in
            self.manager?.loadFromPreferences(completionHandler: { (error) in
                guard error == nil else {
                    print("\(error!.localizedDescription)")
                    return
                }
                
                 let options: [String : NSObject] = [
                 "username": "" as NSString,
                 "password": "" as NSString
                 ]
                
                do {
                    try self.manager?.connection.startVPNTunnel(options: options)
                } catch {
                    print("\(error.localizedDescription)")
                }
            })
        }
        
        configureVPN(callback: callback)
    }
extension ViewController {
    // MARK: -
    func configureVPN(callback: @escaping (Error?) -> Void) {
        NETunnelProviderManager.loadAllFromPreferences { (managers, error) in
            guard error == nil else {
                print("\(error!.localizedDescription)")
                callback(error)
                return
            }
            
            let passwordKey = "1111"
            
            do {
                try self.keychain.set("sohil", key: passwordKey)
            } catch {
                print("\(error.localizedDescription)")
                callback(error)
                return
            }
            
            guard let passwordReference = self.keychain[attributes: passwordKey]?.persistentRef else {
                fatalError()
            }
            
            self.manager = managers?.first ?? NETunnelProviderManager()
            
            self.manager?.loadFromPreferences(completionHandler: { (error) in
                guard error == nil else {
                    print("\(error!.localizedDescription)")
                    callback(error)
                    return
                }
                
                let configurationFile = Bundle.main.url(forResource: "[email protected]", withExtension: "ovpn")

                let configurationContent = try! Data(contentsOf: configurationFile!)
                
                let tunnelProtocol = NETunnelProviderProtocol()
                tunnelProtocol.serverAddress = ""
                tunnelProtocol.providerBundleIdentifier = "com.open.demo.tunnelprovider"
                tunnelProtocol.providerConfiguration = ["configuration": configurationContent]
                tunnelProtocol.username = "sohil"
                tunnelProtocol.passwordReference = passwordReference
                tunnelProtocol.disconnectOnSleep = false
                
                self.manager?.protocolConfiguration = tunnelProtocol
                self.manager?.localizedDescription = "OpenVPN iOS Client"
                
                self.manager?.isEnabled = true
                
                self.manager?.saveToPreferences(completionHandler: { (error) in
                    guard error == nil else {
                        print("\(error!.localizedDescription)")
                        callback(error)
                        return
                    }
                    
                    callback(nil)
                })
            })
        }
    }
}

I am using profile with username = sohil & password = sohil.

No luck, what am I doing wrong can anybody pls help?

sohilmemon avatar Jun 18 '18 18:06 sohilmemon

Hi @sohilmemon,

Have you properly configured the packet tunnel provider extension and have you tried to debug it step by step?

ss-abramchuk avatar Jun 19 '18 04:06 ss-abramchuk

Yes I have but under extension I cannot see any debug is going! and under main Controller there is no error responding.

Should I send the app to check at your end?

sohilmemon avatar Jun 19 '18 05:06 sohilmemon

I'm afraid I don't have time to check your app right now. But you can try this approach to debug, and also check system/device log. If something is wrong with the extension configuration you will see a error there.

ss-abramchuk avatar Jun 19 '18 05:06 ss-abramchuk

Sure, and I will update you with the log and errors if I am unable to solve.

sohilmemon avatar Jun 19 '18 08:06 sohilmemon

@sohilmemon Any luck?

roshit-omanakuttan avatar Jul 26 '18 12:07 roshit-omanakuttan

Yes, I managed to get it connected 👯‍♂️

sohilmemon avatar Jul 31 '18 01:07 sohilmemon

@sohilmemon so how did you manage to get it connected?

stajilov avatar Aug 06 '18 11:08 stajilov

@sohilmemon could you please share pointers / sample code that could help us get the connection up and running.

roshit-omanakuttan avatar Aug 21 '18 10:08 roshit-omanakuttan

@sohilmemon have you found the solution iam also facing same issue please help me out

sudarvizhi3055 avatar Jun 15 '20 09:06 sudarvizhi3055