Android11 设置备用DNS

在Android11 版本的rom 产品开发过程中,遇到一个问题,发现分配的DNS不可用的情况,所以需要设置备用DNS。

直接上代码

//frameworks/base/services/core/java/com/android/server/ConnectivityService
//ConnectivityService.java -->
//updateLinkProperties() -->
//updateDnses(newLp, oldLp, netId);

private void updateDnses(LinkProperties newLp, LinkProperties oldLp, int netId) {
        if (oldLp != null && newLp.isIdenticalDnses(oldLp)) {
            return;  // no updating necessary
        }

        final NetworkAgentInfo defaultNai = getDefaultNetwork();
        final boolean isDefaultNetwork = (defaultNai != null && defaultNai.network.netId == netId);

        if (DBG) {
            final Collection<InetAddress> dnses = newLp.getDnsServers();
            log("Setting DNS servers for network " + netId + " to " + dnses);
        }
        try {
            mDnsManager.noteDnsServersForNetwork(netId, newLp);
            // TODO: netd should listen on [::1]:53 and proxy queries to the current
            // default network, and we should just set net.dns1 to ::1, not least
            // because applications attempting to use net.dns resolvers will bypass
            // the privacy protections of things like DNS-over-TLS.
            if (isDefaultNetwork) mDnsManager.setDefaultDnsSystemProperties(newLp.getDnsServers());
            mDnsManager.flushVmDnsCache();
        } catch (Exception e) {
            loge("Exception in setDnsConfigurationForNetwork: " + e);
        }
    }

在这里,我们添加如下代码:

 private void updateDnses(LinkProperties newLp, LinkProperties oldLp, int netId) {
        if (oldLp != null && newLp.isIdenticalDnses(oldLp)) {
            return;  // no updating necessary
        }

        final NetworkAgentInfo defaultNai = getDefaultNetwork();
        final boolean isDefaultNetwork = (defaultNai != null && defaultNai.network.netId == netId);

        if (DBG) {
            final Collection<InetAddress> dnses = newLp.getDnsServers();
            log("Setting DNS servers for network " + netId + " to " + dnses);
        }
        try {
            //add code here
            List<InetAddress> dnsServers = newLp.getDnsServers();
            dnsServers.add(InetAddress.getByName("xxxxxxxxxx"));
            dnsServers.add(InetAddress.getByName("xxxxxxxxxx"));
            //-----------
            mDnsManager.noteDnsServersForNetwork(netId, newLp);
            // TODO: netd should listen on [::1]:53 and proxy queries to the current
            // default network, and we should just set net.dns1 to ::1, not least
            // because applications attempting to use net.dns resolvers will bypass
            // the privacy protections of things like DNS-over-TLS.
            if (isDefaultNetwork) mDnsManager.setDefaultDnsSystemProperties(newLp.getDnsServers());
            mDnsManager.flushVmDnsCache();
        } catch (Exception e) {
            loge("Exception in setDnsConfigurationForNetwork: " + e);
        }
    }

添加完,单编services,发现报错:Exception in setDnsConfigurationForNetwork: java.lang.UnsupportedOperationException

发现报错原因在于:

   /**
     * Returns all the {@link InetAddress} for DNS servers on this link.
     *
     * @return An unmodifiable {@link List} of {@link InetAddress} for DNS servers on
     *         this link.
     */
    public @NonNull List<InetAddress> getDnsServers() {
        return Collections.unmodifiableList(mDnses);
    }

既然不能对原有的List做修改,那就曲线来一把,如下:

  List<InetAddress> oriServers = newLp.getDnsServers();
  List<InetAddress> dnsServers = new ArrayList<InetAddress>();
  dnsServers.addAll(oriServers);
  dnsServers.add(InetAddress.getByName("xxxxxxxxxx"));
  dnsServers.add(InetAddress.getByName("xxxxxxxxxx"));
  newLp.setDnsServers(dnsServers);

编译验证,可以看到,如果分配的DNS不可用的时候,系统就会使用备用的DNS!!!

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值