手机怎么更新dns缓存文件-(手机怎么刷新dns缓存)

如何更新手机?dns缓存文件。  (如何刷新手机dns缓存)。  作者:默默点滴。  出处:。  一、什么是DNSDNS(DomainNameSystem,域名系统),dns用于域名解析分析ip地址。 ......

  如何更新手机?dns缓存文件。

  (如何刷新手机dns缓存)。

  作者:默默点滴。

  出处:。

  一、什么是DNSDNS(DomainNameSystem,域名系统),dns用于域名解析分析ip地址。

  例如:给你你给主机名。

  我找到了对应的ip地址:163.177。151.109。

  例如:给你你给主机名。

  我找到了对应的ip地址:163.177。151.109。有些主机名也会有别名,比如就。

  有别名甚至不止一个别名,或者一个别名有两个ip地址。在linux机子上,运行nslookup(nameservicelookup)是域名分析。如下面:。

  ~$nslookup。

  DNS分为递归查询和迭代查询,具体可参考下图。

  DNS也可用于负载均衡、域名污染、防火墙,这里不讨论。

  二、DNS缓存。

  所谓DNS有两种缓存,如同步缓存和本地缓存缓存。对于手机来说,重点是本地缓存DNS缓存。

  二、DNS缓存。

  所谓DNS有两种缓存,如同步缓存和本地缓存缓存。对于手机来说,重点是本地缓存DNS缓存。Android基于Linux系统,对于AndroidApp这个缓存又多了,java层。

  2.1使用场景当然,我们需要理解AndroidApp这些场景需要进行,这是最重要的,有时没有必要更新缓存。综上所述,这里的场景无非是以下几种:场景一:存在多个运营商或者多个地区的分布式业务系统。

  例如,互联网分布式业务系统采用不同区域和运营商的方式,而不是业务系统。

  场景二:存在多个域名的业务系统,需要提前分析和缓存ip。

  。

  这是taobao网的dns-prefetchlink,这一步是加速其他页面dns场景三:ip地址是唯一的,但有多个子域名高并发请求综上所述,我们可以理解为,只有域名和ip地址之间的关系是一对多、多对多和多对一DNS缓存。

  2.2系统版本描述Android4.3之前的TTL(TimeToLive)正负有效期分为10分钟,最大缓存120个。TTL算法回收。

  //默认有效DNS缓存时间(TTL)。600seconds(10minutes)。privatestaticfinallongDEFAULT_POSITIVE_TTL_NANOS=600*L;//默认无效缓存时间(TTL)。10seconds。privatestaticfinallongDEFAULT_NEGATIVE_TTL_NANOS=10*L;Android4.3的系统,缓存修正为2秒,最大缓存为16个LRU算法和TTL回收算法。

  privatestaticfinallongTTL_NANOS=2*L;。

  注:见上述代码javaAddressCache。java三、AndroidDNS缓存更新。

  3.1。修改缓存过期时间在Android4.3之前,TTL可以用个System。setProperties设置,就可以了TTL修正为何Android4.3生存时间一致。

  Security。setProperty(“workaddress。cache。ttl”,String。valueOf(2*L));Security。setProperty(“workaddress。cache。negative。ttl”,String。valueOf(2*L))3.2实现DNS-Prefetch步骤3.1只缩短缓存过期时间,在一定程度上处理Android4.3之前系统的不足。但是,域名和存在ip一对多、多对多和多对一的分布式系统,如果有网络切换,下次将被授予。

  可能取还是比较费时的。但是,域名和存在ip一对多、多对多和多对一的分布式系统,如果有网络切换,下次将被授予取还是比较费时的。因此,预获取dns非常必要。那么如何实现呢?DNS-Prefetch呢首先,我们需要统一规范接口。

  publicinterfaceDns{DnsSYSTEM=newDns(){@OverridepublicListlookup(Stringhostname)throwsUnknownHostException{if(hostname==null)thrownewUnknownHostException(“hostname==null”);returnArrays。asList(IAddress。getAllByName(hostname));}};Listlookup(Stringhostname)throwsUnknownHostException;}。

  实现接口publilassDnsManagerimplementsDns{privatestaticDnsManagersingleInstance;privatefinalTreeSetHOST_SET=newTreeSet();publicstaticDnsManagergetDefault(){if(singleInstance==null){synchronized(DnsManager。class){if(singleInstance==null){singleInstance=newDnsManager();}}}returnsingleInstance;}@OverridepublicsynchronizedListlookup(Stringhostname)throwsUnknownHostException{try{if(TextUtils。isEmpty(hostname)||TextUtils。isEmpty(hostname。trim())){thrownewUnknownHostException(“hostname==null”);}Listlist=Dns。SYSTEM。lookup(hostname);。

  HOST_SET。add(hostname);returnlist;}catch(Exceptione){e。printStackTrace();returnArrays。asList(null);}}publicsynchronizedStringquickLookup(Stringhostname)throwsUnknownHostException{try{if(TextUtils。isEmpty(hostname)||TextUtils。isEmpty(hostname。trim())){thrownewUnknownHostException(“hostname==null”);}finalUriuri=Uri。parse(hostname);IAddressiAddress=IAddress。getByName(uri。getHost());if(iAddress==null){Throw。exception(“unkownhost”,UnknownHostException。class);}StringdnsIp=iAddress。getHostAddress();HOST_SET。add(hostname);returndnsIp;}catch(Exceptione){e。printStackTrace();returnLists。newArrayList();}}/***清除dns缓存*/publicsynchronizedvoidclearDnsCache(){try{ReflectUtils。invokeMethodByName(IAddress。class,“clearDnsCache”);}catch(Exceptione){e。printStackTrace();return;}}/***获取主机集合*@return*/publicsynchronizedTreeSetgetHostSet(){returnHOST_SET;}/***预加载DNS*@paramhosts*/publicsynchronizedvoidprefetchDns(Listhosts){if(hosts==null&&hosts。size()==0)return;for(Stringhostname:hosts){prefetchDns(hostname);}}/***预加载DNS*@paramhostname*/publicsynchronizedvoidprefetchDns(Stringhostname){try{IAddress。getAllByName(hostname);}catch(Exceptione){e。printStackTrace();return;}}}。

  使用时机通常网络切换后,并且下次联网成功时,我们prefetch时最好的时间,这里我们需要通过Broadcast+IntentService对于广播部分,我们需要监听如下两个Action(这里推荐使用动态广播)。

  广播实现代码。

  publilassStateChangeReceiverextendsBroadcastReceiver{privatestaticfinalStringTAG=StateChangeReceiver。class。getSimpleName();privateAtomicReferencependingworkState=null;privateAtomicReferencependingSSID=null;publietStateChangeReceiver(){pendingworkState=newAtomicReference();pendingSSID=newAtomicReference(); } @Override public void onReceive(Context context, Intent intent) { if (ConnectivityManager。CONNECTIVITY_ACTION。equals(intent。getAction())) { workType workType = workUtils。getworkType(context); notifyObservers(workType); } if(shouldStartDnsUpdateService(context,intent)) { Intent cloneFilter = intent。cloneFilter(); cloneFilter。setClass(context, DnsUpdateIntentService。class); context。startService(cloneFilter); } } //网络可用并且网络切换的情况下启动IntentService更新 public boolean shouldStartDnsUpdateService(Context context,Intent intent){ if(workUtils。isAvailable(context)){ workType type = workUtils。getworkType(context); if(type==null) return false ; String newState = type。toString(); String lastState = pendingworkState。get(); if(!TextUtils。isEmpty(lastState) && !lastState。equals(newState)) { pendingworkState。set(newState); return true; }else{ pendingworkState。set(newState); if(workUtils。isWifiConnected(context)){ WifiInfo wifiInfo= intent。getParcelableExtra(WifiManager。EXTRA_WIFI_INFO); if(wifiInfo!=null) { String nextSSID = wifiInfo。getSSID(); String lastSSID = pendingSSID。get(); if(nextSSID!=null && nextSSID。equals(lastSSID)) { return true; } } } } }else{ pendingworkState。set(workType。WORK_NO。toString()); } return false; }}。

  DnsUpdateIntentService代码如下。

  public class DnsUpdateIntentService extends IntentService { public DnsUpdateIntentService() { super(DnsUpdateIntentService。class。getName()); } @Override protected void onHandleIntent(@Nullable Intent intent) { runTask(); } private void runTask() { GFLog。d(DnsUpdateIntentService。class。getSimpleName(),“ startDns : 开始更新DNS ”); updateDnsCache(); GFLog。d(DnsUpdateIntentService。class。getSimpleName(),“ endDns : DNS更新完成 ”); } private void updateDnsCache() { try{ DnsManager dm = DnsManager。getDefault(); dm。clearDnsCache(); TreeSethostSet=dm。getHostSet();Listhosts=newArrayList();hosts。addAll(hostSet);dm。prefetchDns(hosts);}catch(Exceptione){e。printStackTrace();return;}}}。

  注意:DnsUpdateIntentService不可以注册为多进程,否则缓存无法更新。

  3.DNS防篡改与安全。

  Android4.3之前的DNS可能存在被污染的可能,如修改resolv。conf文件,在Android4.3+之后,统一使用d方式,安全性上有所提高。因此,对Android4.3之前的系统,建议使用HttpDNS等方案,此外采取HTTPS的通信方式,一定程度上几乎可以绝对避免此类问题的发生。

  此外,我们在。

  ip与域名对应数量不大的app中,可以在App中提前建立不同机房的域名映射也是一种放置篡改的方案。3.Android底层DNS更新Android基于linux,底层通过Libcore。so更新DNS,目前没有方式来更新Linux层面的DNS缓存。那么,我们的DNS-Prefetch功能是否有必要呢?这个问题我们需要明确,虽然我们不一定能更新底层DNS,但是,可以促进底层DNS更新,类似System。gc()的作用。参考链接AndroidDNS更新与DNS-Prefetch基于OkHttp3加入HttpDns功能作者:默默的点滴出处:。