PacketTunnelProvider
Hi
I'm developing OpenVPN in iOS. I follow your instruction. But I have one question.
I can't know how to use PacketTunnelProvider. Please let me know how can I use. If possible, please share your IM or Email.
Regards.
Hi @Richard1202, You can download Simple macOS OpenVPN Client source code and use it as the example. Despite it is written for macOS the iOS code will be almost the same.
Thank you for your reply.
OpenVPNAdapter framework is different in sample you sent, I have to use it for iPhone Project.
When can I download source code of them and compile it?
Hi @ss-abramchuk,
Thank you for creating a wonderful framework and sharing the above Mac Client. The Mac Client works like a charm with the OVPNs you shared as well as my Server OVPN.
I also created an iOS Client similar to the Mac Client you shared. Unfortunately, the iOS Client doesn't seem to be working.
The connection request doesn't seem to reach the server at all. It would very helpful if you could please help me in this regard. My codebase is here: https://drive.google.com/open?id=1RGF_KOJOc7BKI2NoJRwPEfXDUCdTSkah
Highly appreciate your time and effort.
Thanks and Regards, Roshit
Hi @ss-abramchuk I have downloaded Simple macOS OpenVPN Client source, run code, but can not connect the vpn. I have a issue, when PacketTunnelProvider exec, i have print some log in startTunnel func, but never exec
any help best regards.
@roshit-omanakuttan
I have solved this issue, I need config your info.plist
add
<key>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.networkextension.packet-tunnel</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).PacketTunnelProvider</string>
</dict>
Then, you will see your provider be called
Hi @googleliyang
Thank you for the response. The mentioned NSExtension key was always part of the plist file of the Tunnel Provider.
Should I also add to the plist of main project as well? I tried adding it to the plist of the project, but the app screen turned Black and non responsive.
@roshit-omanakuttan If you create packetTunnel plist will auto generate, info.plist just config at packetTunnel not in you main project. you can call provider code as below
class ViewController: NSViewController {
var manager: NETunnelProviderManager?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
@IBAction func establishVPNConnection(_ sender: Any) {
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": "freeopenvpn" as NSString,
"password": "819646439" as NSString
]
do {
// this will call providerTunnerProvider startTunnel method
try self.manager?.connection.startVPNTunnel(options: options)
} catch {
print("\(error.localizedDescription)")
}
})
}
configureVPN(callback: callback)
}
}
extension ViewController {
func configureVPN(callback: @escaping (Error?) -> Void) {
NETunnelProviderManager.loadAllFromPreferences { (managers, error) in
guard error == nil else {
print("\(error!.localizedDescription)")
callback(error)
return
}
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: "freeopenvpn_DE_udp", withExtension: "ovpn")
let configurationContent = try! Data(contentsOf: configurationFile!)
let tunnelProtocol = NETunnelProviderProtocol()
tunnelProtocol.serverAddress = "freeopenvpn.org"
tunnelProtocol.providerBundleIdentifier = "me.ss-abramchuk.openvpn-macos-client.tunnel-provider"
tunnelProtocol.providerConfiguration = ["configuration": configurationContent]
tunnelProtocol.disconnectOnSleep = false
self.manager?.protocolConfiguration = tunnelProtocol
self.manager?.localizedDescription = "OpenVPN macOS Client"
self.manager?.isEnabled = true
self.manager?.saveToPreferences(completionHandler: { (error) in
guard error == nil else {
print("\(error!.localizedDescription)")
callback(error)
return
}
callback(nil)
})
})
}
}
}
Notice: the log of packetTunnelProvider not show xocde console, you should cat in device log .
@roshit-omanakuttan This is a simple demo https://github.com/googleliyang/openvpn_adapter_ios
@googleliyang Thank you so much for sharing the Codebase. I ran the app on my device, but unfortunately I am getting the below logs:
exec connect
connecting
off
Hi @googleliyang I am developing OpenVPN in iOS. I followed instruction. But I am unable to achieve it. Can you please share sample project for iOS.
Regards
@roshit-omanakuttan This is a simple demo https://github.com/googleliyang/openvpn_adapter_ios
Above URL doesn't exist now. Can you please share updated URL?
Hi @ss-abramchuk Can you please re-share your Simple macOS OpenVPN Client source code download link, the project not there and we cant download it. Thanks a lot!