ss-abramchuk / OpenVPNAdapter

Objective-C wrapper for OpenVPN library. Compatible with iOS and macOS.
GNU Affero General Public License v3.0
481 stars 215 forks source link

VPN not Connecting on iOS #70

Open sohilmemon opened 6 years ago

sohilmemon commented 6 years ago

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: "sohil@mandybeuno123.ddns.net", 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?

ss-abramchuk commented 6 years ago

Hi @sohilmemon,

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

sohilmemon commented 6 years ago

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?

ss-abramchuk commented 6 years ago

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.

sohilmemon commented 6 years ago

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

roshit-omanakuttan commented 6 years ago

@sohilmemon Any luck?

sohilmemon commented 6 years ago

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

stajilov commented 6 years ago

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

roshit-omanakuttan commented 6 years ago

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

sudarvizhi3055 commented 4 years ago

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