简体   繁体   English

NetworkExtension connectOnDemand规则不起作用

[英]NetworkExtension connectOnDemand rules doesn't work

I have an app with VPN config created by the new NetworkExtension framework. 我有一个带有由新NetworkExtension框架创建的VPN配置的应用程序。 It works just fine, but now I need to add some rules to turn this VPN only when I'm trying to connect to a specific URL. 它工作得很好,但是现在我仅需要在尝试连接到特定URL时就需要添加一些规则来启用此VPN。 I planed to use NEVPNManager's connectOnDemand feature, but it does`t seem to be working for me. 我计划使用NEVPNManager的connectOnDemand功能,但似乎对我没有用。 When I'm opening http://some-site.com in safari my VPN connection should establish, but for some reason it does't. 当我在野生动物园中打开http://some-site.com时 ,我的VPN连接应该建立,但是由于某种原因却没有。 I tried a different type of configurations as well as using generated .mobileconfig files to make connectOnDemand work, but with no luck. 我尝试了不同类型的配置,以及使用生成的.mobileconfig文件使connectOnDemand正常工作,但是没有运气。 Whats wrong with it? 它出什么问题了? I'm testing on code like this: 我正在测试这样的代码:

let manager = NEVPNManager.sharedManager()

manager.enabled = true

manager.loadFromPreferencesWithCompletionHandler { (err) -> Void in
    manager.removeFromPreferencesWithCompletionHandler { (err0) -> Void in
                print("err0 \(err0)")
                print("err \(err)")
                let config = NEVPNProtocolIPSec()
                config.localIdentifier = "NEVPNProtocolIPSec"
                config.remoteIdentifier = "NEVPNProtocolIPSecRemote"
                config.disconnectOnSleep = true
                config.serverAddress = server
                config.authenticationMethod = .Certificate
                //configurating here
                manager.protocolConfiguration = config
                let onDemandRule1 = NEOnDemandRuleConnect()
                onDemandRule1.DNSSearchDomainMatch = ["some-site.com", "*.some-site.com"]

                manager.onDemandRules = [onDemandRule1]
                manager.onDemandEnabled = true
                manager.saveToPreferencesWithCompletionHandler({ (err2) -> Void in
                    print("err2 \(err2)")
                })
            }
        }

I made it work with the next rules: 我使它符合以下规则:

let onDemandRule = NEOnDemandRuleEvaluateConnection()
let evaluateRule = NEEvaluateConnectionRule(matchDomains: ["*.some-site.com"], andAction: .ConnectIfNeeded)
evaluateRule.probeURL = NSURL(string: "https://a.url.accecable.only.from.vpn")

onDemandRule.connectionRules = [evaluateRule]
manager.protocolConfiguration = config
manager.onDemandRules = [onDemandRule]

您需要添加行

manager.onDemandEnabled = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM