打开VPN无法正常工作。在configureVPN功能启动connection.startVpn部分运行后,什么都没发生,但是我会放在下面

我还检查了PacketTunnelProvider plist一切似乎都很完美。

readFile函数返回从ovpn文件接收的数据。

网络扩展权利也已附加。
任何帮助都会很棒。

<dict>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.networkextension.packet-tunnel</string>
    <key>NSExtensionPrincipalClass</key>
    <string>$(PRODUCT_MODULE_NAME).PacketTunnelProvider</string>
</dict>

class ViewController: UIViewController {

     var providerManager: NETunnelProviderManager!

     override func viewDidLoad() {
         super.viewDidLoad()
         self.loadProviderManager {
             self.configureVPN(serverAddress: "127.0.0.1", username: "uid", password: "pw123")
         }
      }

     func loadProviderManager(completion:@escaping () -> Void) {
        NETunnelProviderManager.loadAllFromPreferences { (managers, error) in
            if error == nil {
                self.providerManager = managers?.first ?? NETunnelProviderManager()
                completion()
            }
        }
     }


     func configureVPN(serverAddress: String, username: String, password: String) {
       guard let configData = self.readFile(path: "serverfile.ovpn") else { return }
        print("configData\(configData)")
       self.providerManager?.loadFromPreferences { error in
          if error == nil {
             let tunnelProtocol = NETunnelProviderProtocol()
             tunnelProtocol.username = username
             tunnelProtocol.serverAddress = serverAddress
             tunnelProtocol.providerBundleIdentifier = "com.bgnmobi.openDeneme.Packet" // bundle id of the network extension target
             tunnelProtocol.providerConfiguration = ["ovpn": configData, "username": username, "password": password]
             tunnelProtocol.disconnectOnSleep = false
             self.providerManager.protocolConfiguration = tunnelProtocol
             self.providerManager.localizedDescription = "GuardillaVPN" // the title of the VPN profile which will appear on Settings
             self.providerManager.isEnabled = true
             self.providerManager.saveToPreferences(completionHandler: { (error) in
                   if error == nil  {
                      self.providerManager.loadFromPreferences(completionHandler: { (error) in
                          do {
                            print("aSDFs34dasdsadsa")

                            try self.providerManager.connection.startVPNTunnel() // starts the VPN tunnel.
                          } catch let error {
                              print(error.localizedDescription)
                           }
                      })
                   } else {
                    print("asdasdsadsa")
                          print(error)
                }
             })
           }

         }
     }


  func readFile(path: String) -> Data? {
     let mypath = Bundle.main.path(forResource: "serverfile", ofType: ".ovpn")

    if let dir = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first{
        let fileURL = dir.appendingPathComponent(mypath!)
     //reading
    do{
        let text2 = try String(contentsOfFile: mypath!,encoding: String.Encoding.utf8)
        print(text2)
        let gerekliURL = URL(fileURLWithPath: mypath!)
        print(gerekliURL)
        return try Data(contentsOf: gerekliURL, options: .uncached)
       }catch{
    //    print("can t read")
    }
    }

    return nil

  }
 }

最佳答案

这些部分将与您的目标文件匹配。在这一点上要小心。就我而言,这是一个问题,也许也是您的问题。

private static let appGroup = "group.com.project.examplevpn"

private static let tunnelIdentifier = "com.project.examplevpn.Extension"

关于ios - openVPN网络扩展数据包隧道提供程序未启动到VPN隧道,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60705078/

10-13 05:33