菜鸟教程小白 发表于 2022-12-11 19:17:50

ios - 将所有 DNS 请求重定向到 iOS 应用程序中的自定义解析器


                                            <p><p>每个人。
我正在尝试<strong>在我的 iOS 应用中覆盖 dns 解析器设置</strong>。</p>

<p>我使用<strong>NEVPNManager</strong>安装了<strong>个人vpn</strong>,然后使用<strong>onDemandRules</strong>设置特定的dns服务器。
到目前为止,我的代码<strong>适用于某些域</strong>。
下面是我的代码。</p>

<p>当我将“*.com”放入 <strong>ma​​tchDomains</strong> 时,它可以完美运行。</p>

<p>但是<strong>我想要做的</strong>是<strong>将所有 dns 查询重定向</strong>到特定的 dns 服务器。
我尝试了空匹配域([])和空字符串([“”])。
我还尝试了 ["*"] 和 ["*.*] 之类的通配符表达式。
到目前为止,我没有成功。</p>

<p>已经好几天了,我还是想不通。
谁能告诉我我在这里想念什么?
提前致谢。</p>

<pre><code>    let manager = NEVPNManager.sharedManager()
    manager.loadFromPreferencesWithCompletionHandler { error in
    if let vpnError = error {
      print(&#34;vpn error in loading preferences : \(vpnError)&#34;)
      return
    }

    if manager.protocolConfiguration == nil {
      let myIPSec = NEVPNProtocolIPSec()
      myIPSec.username = &#34;username&#34;
      myIPSec.serverAddress = &#34;server address&#34;
      myIPSec.passwordReference = self.getPersistentRef()
      myIPSec.authenticationMethod = NEVPNIKEAuthenticationMethod.SharedSecret
      myIPSec.sharedSecretReference = self.getPersistentRef()
      myIPSec.useExtendedAuthentication = true

      manager.protocolConfiguration = myIPSec
      manager.localizedDescription = &#34;myDNS&#34;

      let evaluationRule = NEEvaluateConnectionRule(matchDomains: [&#34;*.com&#34;], andAction: NEEvaluateConnectionRuleAction.ConnectIfNeeded)
      evaluationRule.useDNSServers = [&#34;XXX.XXX.XXX.XXX&#34;]

      let onDemandRule = NEOnDemandRuleEvaluateConnection()
      onDemandRule.connectionRules =
      onDemandRule.interfaceTypeMatch = NEOnDemandRuleInterfaceType.Any

      manager.onDemandRules =
      manager.onDemandEnabled = true
      manager.enabled = true

      manager.saveToPreferencesWithCompletionHandler { error in
            if let vpnError = error {
                print(&#34;vpn error in saving preferences : \(vpnError)&#34;)
                return
            }
      }
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我发现即使在最新的 iOS (10.3.1) 和使用 NEVPNProtocolIKEv2 中,这也是错误的。前一刻它可以工作,下一刻它不想启动 VPN 连接,因为它似乎误解了按需规则并返回错误,说 VPN 配置文件未启用。我最终配置了 IKEv2 服务器 (Strongswan) 以使用/etc/ipsec.conf 中的“rightdns”选项推送 DNS 设置。这给了我将 DNS 请求重定向到自定义解析器的预期结果。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将所有 DNS 请求重定向到 iOS 应用程序中的自定义解析器,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42874507/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42874507/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将所有 DNS 请求重定向到 iOS 应用程序中的自定义解析器