提问者:小点点

Swift-使用NEVPNManager连接到vpn


我从Android获得.ovpn文件,我有用户名和密码,我应该连接到vpn服务器,但我不确定如何做到这一点。我试过这样的东西:

let manager = NEVPNManager.sharedManager()
manager.loadFromPreferencesWithCompletionHandler { (error) -> Void in
  if manager.`protocol`  == nil {
    let newIPSec = NEVPNProtocolIKEv2()
    newIPSec.serverAddress = "xxx.xxx.xxx.xxx"
    newIPSec.username = "username"
    let keychain = Keychain(service: "com.app.ios")
    let data = keychain[data: "vpnpassword"]
    newIPSec.passwordReference = data
    newIPSec.authenticationMethod = NEVPNIKEAuthenticationMethod.None
    newIPSec.disconnectOnSleep = false

    manager.`protocol` = newIPSec
    manager.enabled = true

    manager.saveToPreferencesWithCompletionHandler({ (error) -> Void in
      print(error)
    })
  }
}

我不知道如何添加证书(in.ovpn)信息以及如何设置它。ovpn如下所示(我只是删除标记中的数据并更改服务器地址:

client
dev tun
proto udp
remote xx.xx.xx.xx 443
resolv-retry infinite
nobind
persist-key
persist-tun
verb 3

<auth-user-pass>
#username#
#userpass#
</auth-user-pass>

cipher AES-256-CBC
<ca>
-----BEGIN CERTIFICATE-----
xxx
-----END CERTIFICATE-----
</ca> 

谢谢你的帮助


共2个答案

匿名用户

这有点晚了,但你要明白这是两件完全不同的事情。

NetworkExtension API在撰写此答案时(我有iOS 9.3.4)支持两种协议:使用IKEv1-IPSec和证书(IKEv2)

您的服务器是OpenVPN服务器。目前没有用于OpenVVPN的API(如果您遇到任何API,请告诉我)。那么第一个问题,你的VPN服务器支持IKEv1还是V2?

有一个客户端OpenVPN-connect for iOS,但它是专有的,并且是封闭源码的。您仍然可以从应用程序内部调用它,它将弹出给用户,如下所示:```Swift func actConnectButtonPressed(Sender:AnyObject){let app:UIApplication=UIApplication.SharedApplication()

    let alert = UIAlertController(title: "Warning", message: "OpenVPN Connect needs to be installed to process .ovpn configuration files on your device. Go to OpenVPN Connect page in AppStore?", preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction!) in
        app.openURL(NSURL(string: "https://itunes.apple.com/app/id590379981?mt=8")!)
    }))

    alert.addAction(UIAlertAction(title: "No", style: .Default, handler: { (action: UIAlertAction!) in
        let server:Server = self.api.findServerByName(self.settings.loadValue(SettingsController.keyServerName, defValue: "") as! String)!
        let protocolPort:Int = self.settings.loadValue(SettingsController.keyProtocolPort, defValue: 0) as! Int
        app.openURL(NSURL(string: self.api.getOvpnConfigURL(server, proto: self.protocolAndPorts[protocolPort]))!)
    }))
    presentViewController(alert, animated: true, completion: nil)

}

```

此示例来自safejump客户端。在此查看https://github.com/proxysh/safejumper-for-iOS

我自己仍然在尝试让IPSec客户端以编程方式在iOS上运行,下面是我学到的--建议使用共享密钥方法而不是无--共享密钥也必须保存在密钥链中--如果使用共享密钥,那么也要启用ExtendedAuthentication--确保在启动连接之前保存配置

希望这有帮助

匿名用户

您正在使用NEVPNProtocolIKEv2连接到openVPN服务器。个人VPN仅支持带有ike v1和v2的ipsec(AEP和证书身份验证)