利用EJS查找可用的Google搜索服务器IP,并缓存入系统Hosts文件内(改进版)

此博客为9925.org的镜像,登录9925.org可以查看到最新博文。

原文出处:http://ily.so/AfA3yu

EJS脚本运行环境下载:下载地址

由于http对象底层Socket的连接功能未提供超时机制,所以即使为http对象设置了Timeout属性在某些情况下也会等待20秒左右才能连接超时。

为了解决这个问题,我们采用Socket提供的异步连接方法测试服务器是否开通80端口。

第一步:新建一个名为“Socket.ejs”的脚本文件(下一个版本将集成到lib运行库目录内),键入下面代码:

/*
    指定 Socket 类的实例可以使用的寻址方案。
    AppleTalk   AppleTalk 地址。
    Atm 本机 ATM 服务地址。
    Banyan  Banyan 地址。
    Ccitt   CCITT 协议(如 X.25)的地址。
    Chaos   MIT CHAOS 协议的地址。
    Cluster Microsoft 群集产品的地址。
    DataKit Datakit 协议的地址。
    DataLink    直接数据链接接口地址。
    DecNet  DECnet 地址。
    Ecma    欧洲计算机制造商协会 (ECMA) 地址。
    FireFox FireFox 地址。
    HyperChannel    NSC Hyperchannel 地址。
    Ieee12844   IEEE 1284.4 工作组地址。
    ImpLink ARPANET IMP 地址。
    InterNetwork    IP 版本 4 的地址。
    InterNetworkV6  IP 版本 6 的地址。
    Ipx IPX 或 SPX 地址。
    Irda    IrDA 地址。
    Iso ISO 协议的地址。
    Lat LAT 地址。
    Max MAX 地址。
    NetBios NetBios 地址。
    NetworkDesigners    支持网络设计器 OSI 网关的协议的地址。
    NS  Xerox NS 协议的地址。
    Osi OSI 协议的地址。
    Pup PUP 协议的地址。
    Sna IBM SNA 地址。
    Unix    Unix 本地到主机地址。
    Unknown 未知的地址族。
    Unspecified 未指定的地址族。
    VoiceView   VoiceView 地址。
*/
var AddressFamily = System.Net.Sockets.AddressFamily;
/*
    指定 Socket 类的实例表示的套接字类型。
    Dgram   支持数据报,即最大长度固定(通常很小)的无连接、不可靠消息。 消息可能会丢失或重复并可能在到达时不按顺序排列。 Dgram 类型的 Socket 在发送和接收数据之前不需要任何连接,并且可以与多个对方主机进行通信。 Dgram 使用数据报协议 (Udp) 和 InterNetworkAddressFamily。
    Raw 支持对基础传输协议的访问。 通过使用 SocketTypeRaw,可以使用 Internet 控制消息协议 (Icmp) 和 Internet 组管理协议 (Igmp) 这样的协议来进行通信。 在发送时,您的应用程序必须提供完整的 IP 标头。 所接收的数据报在返回时会保持其 IP 标头和选项不变。
    Rdm 支持无连接、面向消息、以可靠方式发送的消息,并保留数据中的消息边界。 RDM(以可靠方式发送的消息)消息会依次到达,不会重复。 此外,如果消息丢失,将会通知发送方。 如果使用 Rdm 初始化 Socket,则在发送和接收数据之前无需建立远程主机连接。 利用 Rdm,您可以与多个对方主机进行通信。
    Seqpacket   在网络上提供排序字节流的面向连接且可靠的双向传输。 Seqpacket 不重复数据,它在数据流中保留边界。 Seqpacket 类型的 Socket 与单个对方主机通信,并且在通信开始之前需要建立远程主机连接。
    Stream  支持可靠、双向、基于连接的字节流,而不重复数据,也不保留边界。 此类型的 Socket 与单个对方主机通信,并且在通信开始之前需要建立远程主机连接。 Stream 使用传输控制协议 (Tcp) ProtocolType 和 InterNetworkAddressFamily。
    Unknown 指定未知的 Socket 类型。
*/
var SocketType = System.Net.Sockets.SocketType;
/*
    指定 Socket 类支持的协议。
    Ggp 网关到网关协议。
    Icmp    网际消息控制协议。
    IcmpV6  用于 IPv6 的 Internet 控制消息协议。
    Idp Internet 数据报协议。
    Igmp    网际组管理协议。
    IP  网际协议。
    IPSecAuthenticationHeader   IPv6 身份验证头。 有关详细信息,请参见位于 http://www.ietf.org 中的 RFC 2292 的 2.2.1 节。
    IPSecEncapsulatingSecurityPayload   IPv6 封装式安全措施负载头。
    IPv4    Internet 协议版本 4。
    IPv6    Internet 协议版本 6 (IPv6)。
    IPv6DestinationOptions  IPv6 目标选项头。
    IPv6FragmentHeader  IPv6 片段头。
    IPv6HopByHopOptions IPv6 逐跳选项头。
    IPv6NoNextHeader    IPv6 No Next 头。
    IPv6RoutingHeader   IPv6 路由头。
    Ipx Internet 数据包交换协议。
    ND  网络磁盘协议(非正式)。
    Pup PARC 通用数据包协议。
    Raw 原始 IP 数据包协议。
    Spx 顺序包交换协议。
    SpxII   顺序包交换协议第 2 版。
    Tcp 传输控制协议。
    Udp 用户数据报协议。
    Unknown 未知协议。
    Unspecified 未指定的协议。
*/
var ProtocolType = System.Net.Sockets.ProtocolType;
/*
    使用指定的地址族、套接字类型和协议初始化 Socket 类的新实例。
    addressFamily:AddressFamily 值之一。
    类型:AddressFamily
    socketType:SocketType 值之一。
    类型:SocketType
    protocolType:ProtocolType 值之一。
    类型:ProtocolType
*/
function Socket(addressFamily, socketType, protocolType)
{
    if(typeof(addressFamily) != "number")
    {
        throw new ArgumentException("addressFamily参数不能为空,并且必须为Number。");
    }
    if(typeof(socketType) != "number")
    {
        throw new ArgumentException("socketType参数不能为空,并且必须为Number。");
    }
    if(typeof(protocolType) != "number")
    {
        throw new ArgumentException("protocolType参数不能为空,并且必须为Number。");
    }
    this.author = "Easton";
    this.url = "http://www.9925.org";
    this.version = "1.0.0.0";
    this.baseObject = new System.Net.Sockets.Socket(addressFamily, socketType, protocolType);
}

第二步:修改上一篇文章内提供的脚本,新增检测80端口是否启用的功能。

var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
    //检测80端口是否开通
    var result = socket.baseObject.BeginConnect(temp, 80, null, null);
    var success = result.AsyncWaitHandle.WaitOne(2000, true);
    if (!success)
    {
        console.error(url + "连接80端口超时。");
        continue;
    }
}
finally
{
    socket.baseObject.Close();
}

完整的脚本

clear();
console.info("/**************************版权声明**************************/");
console.info("/*                                                          */");
console.info("/*功能描述:查找可用的Google服务器,并将其缓存入系统DNS。   */");
console.info("/*脚本作者:Easton                                          */");
console.info("/*联系邮箱:[email protected]                           */");
console.info("/*完成时间:2015年2月27日                                   */");
console.info("/*                                                          */");
console.info("/************************************************************/");
var hostsMarkBegin = "#EJS GOOGLE HOSTS BEGIN";
var hostsMarkEnd = "#EJS GOOGLE HOSTS END";
var googleDomains = ["google.com", "www.google.com", "m.google.com", "scholar.google.com", "translate.google.com", "books.google.com", "appengine.google.com", "maps.google.com", "news.google.com", "images.google.com", "finance.google.com", "history.google.com", "drive.google.com", "docs.google.com", "plus.google.com", "play.google.com", "calendar.google.com", "domains.google.com", "code.google.com", "googlecode.com", "v8.googlecode.com", "gyp.googlecode.com", "cloud.google.com", "earth.google.com", "places.google.com", "adwords.google.com", "sites.google.com", "keep.google.com", "support.google.com", "developers.google.com", "spreadsheets.google.com", "video.google.com", "photos.google.com", "wallet.google.com", "groups.google.com", "gmail.com", "www.gmail.com", "m.gmail.com", "m.googlemail.com", "mail-attachment.googleusercontent.com", "www.chrome.com", "developer.chrome.com", "chrome.google.com", "picasa.google.com", "talkgadget.google.com", "toolbar.google.com", "tools.google.com", "inbox.google.com", "apis.google.com", "www.googleapis.com", "android.googleapis.com", "play.googleapis.com", "plus.googleapis.com", "ajax.googleapis.com", "chart.googleapis.com", "fonts.googleapis.com", "redirector-bigcache.googleapis.com", "translate.googleapis.com", "maps.googleapis.com", "content.googleapis.com", "storage.googleapis.com", "commondatastorage.googleapis.com", "static.panoramio.com.storage.googleapis.com", "www.gstatic.com", "ssl.gstatic.com", "csi.gstatic.com", "maps.gstatic.com", "fonts.gstatic.com", "encrypted-tbn0.gstatic.com", "encrypted-tbn1.gstatic.com", "encrypted-tbn2.gstatic.com", "encrypted-tbn3.gstatic.com", "encrypted-tbn4.gstatic.com", "encrypted-tbn5.gstatic.com", "encrypted-tbn6.gstatic.com", "g0.gstatic.com", "g1.gstatic.com", "g2.gstatic.com", "g3.gstatic.com", "g4.gstatic.com", "g5.gstatic.com", "g6.gstatic.com", "g7.gstatic.com", "mt0.gstatic.com", "mt1.gstatic.com", "mt2.gstatic.com", "mt3.gstatic.com", "mt4.gstatic.com", "mt5.gstatic.com", "mt6.gstatic.com", "mt7.gstatic.com", "t0.gstatic.com", "t1.gstatic.com", "t2.gstatic.com", "t3.gstatic.com", "t4.gstatic.com", "t5.gstatic.com", "t6.gstatic.com", "t7.gstatic.com", "1-ps.googleusercontent.com", "2-ps.googleusercontent.com", "3-ps.googleusercontent.com", "4-ps.googleusercontent.com", "www.googleusercontent.com", "oauth.googleusercontent.com", "feedback.googleusercontent.com", "translate.googleusercontent.com", "themes.googleusercontent.com", "webcache.googleusercontent.com", "books.googleusercontent.com", "video.googleusercontent.com", "music-onebox.googleusercontent.com", "music.googleusercontent.com", "blogger.googleusercontent.com", "newsstand.googleusercontent.com", "producer.googleusercontent.com", "code-opensocial.googleusercontent.com", "spreadsheets-opensocial.googleusercontent.com", "www-calENDar-opensocial.googleusercontent.com", "www-fc-opensocial.googleusercontent.com", "www-focus-opensocial.googleusercontent.com", "www-gm-opensocial.googleusercontent.com", "www-kix-opensocial.googleusercontent.com", "www-open-opensocial.googleusercontent.com", "www-opensocial.googleusercontent.com", "www-opensocial-sandbox.googleusercontent.com", "www-oz-opensocial.googleusercontent.com", "a-oz-opensocial.googleusercontent.com", "0-open-opensocial.googleusercontent.com", "1-open-opensocial.googleusercontent.com", "2-open-opensocial.googleusercontent.com", "3-open-opensocial.googleusercontent.com", "0-focus-opensocial.googleusercontent.com", "1-focus-opensocial.googleusercontent.com", "2-focus-opensocial.googleusercontent.com", "3-focus-opensocial.googleusercontent.com", "doc-00-7o-docs.googleusercontent.com", "doc-01-7o-docs.googleusercontent.com", "doc-02-7o-docs.googleusercontent.com", "doc-03-7o-docs.googleusercontent.com", "doc-04-7o-docs.googleusercontent.com", "doc-05-7o-docs.googleusercontent.com", "doc-06-7o-docs.googleusercontent.com", "doc-07-7o-docs.googleusercontent.com", "doc-08-7o-docs.googleusercontent.com", "doc-09-7o-docs.googleusercontent.com", "doc-10-7o-docs.googleusercontent.com", "doc-0a-7o-docs.googleusercontent.com", "doc-0b-7o-docs.googleusercontent.com", "doc-0c-7o-docs.googleusercontent.com", "doc-0d-7o-docs.googleusercontent.com", "doc-0e-7o-docs.googleusercontent.com", "doc-0f-7o-docs.googleusercontent.com", "doc-0g-7o-docs.googleusercontent.com", "doc-0h-7o-docs.googleusercontent.com", "doc-0i-7o-docs.googleusercontent.com", "doc-0j-7o-docs.googleusercontent.com", "doc-0k-7o-docs.googleusercontent.com", "doc-0l-7o-docs.googleusercontent.com", "doc-0m-7o-docs.googleusercontent.com", "doc-0n-7o-docs.googleusercontent.com", "doc-0o-7o-docs.googleusercontent.com", "doc-0p-7o-docs.googleusercontent.com", "doc-0q-7o-docs.googleusercontent.com", "doc-0r-7o-docs.googleusercontent.com", "doc-0s-7o-docs.googleusercontent.com", "doc-0t-7o-docs.googleusercontent.com", "doc-00-bc-docs.googleusercontent.com", "doc-01-bc-docs.googleusercontent.com", "doc-02-bc-docs.googleusercontent.com", "doc-03-bc-docs.googleusercontent.com", "doc-04-bc-docs.googleusercontent.com", "doc-05-bc-docs.googleusercontent.com", "doc-06-bc-docs.googleusercontent.com", "doc-07-bc-docs.googleusercontent.com", "doc-08-bc-docs.googleusercontent.com", "doc-09-bc-docs.googleusercontent.com", "doc-10-bc-docs.googleusercontent.com", "doc-0a-bc-docs.googleusercontent.com", "doc-0b-bc-docs.googleusercontent.com", "doc-0c-bc-docs.googleusercontent.com", "doc-0d-bc-docs.googleusercontent.com", "doc-0e-bc-docs.googleusercontent.com", "doc-0f-bc-docs.googleusercontent.com", "doc-0g-bc-docs.googleusercontent.com", "doc-0h-bc-docs.googleusercontent.com", "doc-0i-bc-docs.googleusercontent.com", "doc-0j-bc-docs.googleusercontent.com", "doc-0k-bc-docs.googleusercontent.com", "doc-0l-bc-docs.googleusercontent.com", "doc-0m-bc-docs.googleusercontent.com", "doc-0n-bc-docs.googleusercontent.com", "doc-0o-bc-docs.googleusercontent.com", "doc-0p-bc-docs.googleusercontent.com", "doc-0q-bc-docs.googleusercontent.com", "doc-0r-bc-docs.googleusercontent.com", "doc-0s-bc-docs.googleusercontent.com", "doc-0t-bc-docs.googleusercontent.com", "doc-00-1s-docs.googleusercontent.com", "doc-01-1s-docs.googleusercontent.com", "doc-02-1s-docs.googleusercontent.com", "doc-03-1s-docs.googleusercontent.com", "doc-04-1s-docs.googleusercontent.com", "doc-05-1s-docs.googleusercontent.com", "doc-06-1s-docs.googleusercontent.com", "doc-07-1s-docs.googleusercontent.com", "doc-08-1s-docs.googleusercontent.com", "doc-09-1s-docs.googleusercontent.com", "doc-10-1s-docs.googleusercontent.com", "doc-0a-1s-docs.googleusercontent.com", "doc-0b-1s-docs.googleusercontent.com", "doc-0c-1s-docs.googleusercontent.com", "doc-0d-1s-docs.googleusercontent.com", "doc-0e-1s-docs.googleusercontent.com", "doc-0f-1s-docs.googleusercontent.com", "doc-0g-1s-docs.googleusercontent.com", "doc-0h-1s-docs.googleusercontent.com", "doc-0i-1s-docs.googleusercontent.com", "doc-0j-1s-docs.googleusercontent.com", "doc-0k-1s-docs.googleusercontent.com", "doc-0l-1s-docs.googleusercontent.com", "doc-0m-1s-docs.googleusercontent.com", "doc-0n-1s-docs.googleusercontent.com", "doc-0o-1s-docs.googleusercontent.com", "doc-0p-1s-docs.googleusercontent.com", "doc-0q-1s-docs.googleusercontent.com", "doc-0r-1s-docs.googleusercontent.com", "doc-0s-1s-docs.googleusercontent.com", "doc-0t-1s-docs.googleusercontent.com", "doc-04-80-docs.googleusercontent.com", "doc-08-6g-docs.googleusercontent.com", "doc-0k-bk-docs.googleusercontent.com", "doc-10-6g-docs.googleusercontent.com", "doc-10-00-docs.googleusercontent.com", "images1-focus-opensocial.googleusercontent.com", "images2-focus-opensocial.googleusercontent.com", "images3-focus-opensocial.googleusercontent.com", "images4-focus-opensocial.googleusercontent.com", "images5-focus-opensocial.googleusercontent.com", "images6-focus-opensocial.googleusercontent.com", "images7-focus-opensocial.googleusercontent.com", "images8-focus-opensocial.googleusercontent.com", "images9-focus-opensocial.googleusercontent.com", "images1-esmobile-opensocial.googleusercontent.com", "images2-esmobile-opensocial.googleusercontent.com", "images3-esmobile-opensocial.googleusercontent.com", "images4-esmobile-opensocial.googleusercontent.com", "images5-esmobile-opensocial.googleusercontent.com", "images6-esmobile-opensocial.googleusercontent.com", "images7-esmobile-opensocial.googleusercontent.com", "images8-esmobile-opensocial.googleusercontent.com", "images9-esmobile-opensocial.googleusercontent.com", "images-docs-opensocial.googleusercontent.com", "images-oz-opensocial.googleusercontent.com", "images-lso-opensocial.googleusercontent.com", "images-blogger-opensocial.googleusercontent.com", "images-pos-opensocial.googleusercontent.com", "lh0.googleusercontent.com", "lh1.googleusercontent.com", "lh2.googleusercontent.com", "lh3.googleusercontent.com", "lh4.googleusercontent.com", "lh5.googleusercontent.com", "lh6.googleusercontent.com", "gp1.googleusercontent.com", "gp2.googleusercontent.com", "gp3.googleusercontent.com", "gp4.googleusercontent.com", "gp5.googleusercontent.com", "gp6.googleusercontent.com", "ci1.googleusercontent.com", "ci2.googleusercontent.com", "ci3.googleusercontent.com", "ci4.googleusercontent.com", "ci5.googleusercontent.com", "ci6.googleusercontent.com", "s1.googleusercontent.com", "s2.googleusercontent.com", "s3.googleusercontent.com", "s4.googleusercontent.com", "s5.googleusercontent.com", "s6.googleusercontent.com", "sp0.googleusercontent.com", "sp1.googleusercontent.com", "sp2.googleusercontent.com", "sp3.googleusercontent.com", "sp4.googleusercontent.com", "sp5.googleusercontent.com", "sp6.googleusercontent.com", "sp7.googleusercontent.com", "sp8.googleusercontent.com", "sp9.googleusercontent.com", "clients1.googleusercontent.com", "clients2.googleusercontent.com", "clients3.googleusercontent.com", "clients4.googleusercontent.com", "clients5.googleusercontent.com", "clients6.googleusercontent.com", "clients7.googleusercontent.com", "lh0.ggpht.com", "lh1.ggpht.com", "lh2.ggpht.com", "lh3.ggpht.com", "lh4.ggpht.com", "lh5.ggpht.com", "lh6.ggpht.com", "nt0.ggpht.com", "nt1.ggpht.com", "nt2.ggpht.com", "nt3.ggpht.com", "nt4.ggpht.com", "nt5.ggpht.com", "nt6.ggpht.com", "yt0.ggpht.com", "yt1.ggpht.com", "yt2.ggpht.com", "yt3.ggpht.com", "yt4.ggpht.com", "yt5.ggpht.com", "yt6.ggpht.com", "gm0.ggpht.com", "gm1.ggpht.com", "gm2.ggpht.com", "gm3.ggpht.com", "gm4.ggpht.com", "gm5.ggpht.com", "gm6.ggpht.com", "geo0.ggpht.com", "geo1.ggpht.com", "geo2.ggpht.com", "geo3.ggpht.com", "geo4.ggpht.com", "geo5.ggpht.com", "geo6.ggpht.com", "clients1.google.com", "clients2.google.com", "clients3.google.com", "clients4.google.com", "clients5.google.com", "clients6.google.com", "0.client-channel.google.com", "1.client-channel.google.com", "2.client-channel.google.com", "3.client-channel.google.com", "4.client-channel.google.com", "5.client-channel.google.com", "6.client-channel.google.com", "7.client-channel.google.com", "8.client-channel.google.com", "9.client-channel.google.com", "10.client-channel.google.com", "11.client-channel.google.com", "12.client-channel.google.com", "13.client-channel.google.com", "14.client-channel.google.com", "15.client-channel.google.com", "16.client-channel.google.com", "17.client-channel.google.com", "18.client-channel.google.com", "19.client-channel.google.com", "20.client-channel.google.com", "21.client-channel.google.com", "22.client-channel.google.com", "23.client-channel.google.com", "24.client-channel.google.com", "25.client-channel.google.com", "26.client-channel.google.com", "27.client-channel.google.com", "28.client-channel.google.com", "29.client-channel.google.com", "uploads.clients.google.com", "uploads.client.google.com", "upload.clients.google.com", "upload.client.google.com", "upload.google.com", "upload.docs.google.com", "upload.photos.google.com", "upload.drive.google.com", "upload.video.google.com", "uploads.code.google.com", "uploads.clients.google.com", "linkhelp.clients.google.com", "feedburner.google.com", "feedproxy.google.com", "0.talkgadget.google.com", "chromoting-host.talkgadget.google.com", "chromoting-oauth.talkgadget.google.com", "chromoting-client.talkgadget.google.com", "maps-api-ssl.google.com", "mapsengine.google.com", "bpui0.google.com", "i18napis.appspot.com", "safebrowsing.google.com", "safebrowsing.clients.google.com", "safebrowsing-cache.google.com", "admin.google.com", "appspot.l.google.com", "browserchannel-docs.l.google.com", "browserchannel-spreadsheets.l.google.com", "browsersync.google.com", "browsersync.l.google.com", "cache.l.google.com", "cache.pack.google.com", "cbk0.google.com", "cbk1.google.com", "cbk2.google.com", "cbk3.google.com", "cbks0.google.com", "cbks1.google.com", "cbks2.google.com", "cbks3.google.com", "dl.google.com", "dl.l.google.com", "dl-ssl.google.com", "encrypted.google.com", "encrypted-tbn.l.google.com", "encrypted-tbn0.google.com", "encrypted-tbn1.google.com", "encrypted-tbn2.google.com", "encrypted-tbn3.google.com", "gg.google.com", "ghs.google.com", "ghs.l.google.com", "ghs46.google.com", "ghs46.l.google.com", "googlecode.l.google.com", "google-public-dns-a.google.com", "google-public-dns-b.google.com", "groups.l.google.com", "investor.google.com", "kh.google.com", "kh.l.google.com", "khm.google.com", "khm.l.google.com", "khm0.google.com", "khm1.google.com", "khm2.google.com", "khm3.google.com", "khmdb.google.com", "khms.google.com", "khms.l.google.com", "khms0.google.com", "khms1.google.com", "khms2.google.com", "khms3.google.com", "lh2.google.com", "lh2.l.google.com", "lh3.google.com", "lh4.google.com", "lh5.google.com", "lh6.google.com", "mars.google.com", "mobilemaps.clients.google.com", "mt.google.com", "mt.l.google.com", "mt0.google.com", "mt1.google.com", "mt2.google.com", "mt3.google.com", "mts.google.com", "mts.l.google.com", "mts0.google.com", "mts1.google.com", "mts2.google.com", "mts3.google.com", "music.google.com", "mw1.google.com", "mw2.google.com", "picasaweb.google.com", "picasaweb.l.google.com", "services.google.com", "sandbox.google.com", "pack.google.com", "sb.google.com", "sb-ssl.google.com", "script.google.com", "security.google.com", "id.google.com", "id.l.google.com", "tbn0.google.com", "tbn1.google.com", "tbn2.google.com", "tbn3.google.com", "voice.google.com", "wifi.google.com", "g.co", "goo.gl", "goto.google.com", "0.docs.google.com", "1.docs.google.com", "2.docs.google.com", "3.docs.google.com", "4.docs.google.com", "5.docs.google.com", "6.docs.google.com", "7.docs.google.com", "8.docs.google.com", "9.docs.google.com", "10.docs.google.com", "0.drive.google.com", "1.drive.google.com", "2.drive.google.com", "3.drive.google.com", "4.drive.google.com", "5.drive.google.com", "6.drive.google.com", "7.drive.google.com", "8.drive.google.com", "9.drive.google.com", "10.drive.google.com", "bks0.books.google.com", "bks1.books.google.com", "bks2.books.google.com", "bks3.books.google.com", "bks4.books.google.com", "bks5.books.google.com", "bks6.books.google.com", "bks7.books.google.com", "bks8.books.google.com", "bks9.books.google.com", "bks10.books.google.com", "spreadsheets0.google.com", "spreadsheets1.google.com", "spreadsheets2.google.com", "spreadsheets3.google.com", "spreadsheets4.google.com", "spreadsheets5.google.com", "spreadsheets6.google.com", "spreadsheets7.google.com", "spreadsheets8.google.com", "spreadsheets9.google.com", "news.google.com.hk", "books.google.com.hk", "accounts.google.com.hk", "clients1.google.com.hk", "encrypted.google.com.hk", "groups.google.com.hk", "id.google.com.hk", "images.google.com.hk", "m.google.com.hk", "maps.google.com.hk", "picasaweb.google.com.hk", "scholar.google.com.hk", "toolbar.google.com.hk", "translate.google.com.hk", "wenda.google.com.hk", "www.google.com.hk", "www.google.com.tw", "accounts.google.com.tw", "maps.google.com.tw", "news.google.com.tw", "books.google.com.tw", "clients1.google.com.tw", "encrypted.google.com.tw", "groups.google.com.tw", "id.google.com.tw", "images.google.com.tw", "m.google.com.tw", "picasaweb.google.com.tw", "scholar.google.com.tw", "toolbar.google.com.tw", "toolbarqueries.google.com.tw", "translate.google.com.tw", "g.cn", "www.g.cn", "www.gstatic.cn", "ditu.google.cn", "maps.google.cn", "gg.google.cn", "id.google.cn", "maps.gstatic.cn", "m.google.cn", "mt.google.cn", "mt0.google.cn", "mt1.google.cn", "mt2.google.cn", "mt3.google.cn", "news.google.cn", "scholar.google.cn", "translate.google.cn", "www.google.cn", "accounts.l.google.com", "csi.l.google.com", "googleapis.l.google.com", "maps.l.google.com", "code.l.google.com", "news.l.google.com", "images.l.google.com", "mobile.l.google.com", "talkx.l.google.com", "talk.l.google.com", "large-uploads.l.google.com", "writely.l.google.com", "writely-china.l.google.com", "writely-com.l.google.com", "www.l.google.com", "www2.l.google.com", "www3.l.google.com", "www4.l.google.com", "ytstatic.l.google.com", "wildcard-talkgadget.l.google.com", "mobile-gtalk.l.google.com", "safebrowsing.cache.l.google.com", "spreadsheets-china.l.google.com", "spreadsheets.l.google.com", "suggestqueries.l.google.com", "video-stats.l.google.com", "sb.l.google.com", "sb-ssl.l.google.com", "scholar.l.google.com", "wifi.l.google.com", "wire.l.google.com", "tools.l.google.com", "photos-ugc.l.google.com", "music-streaming.l.google.com", "googlehosted.l.googleusercontent.com", "www.googledrive.com", "map.google.com", "mage.google.com", "plusone.google.com", "spreadsheet.google.com", "ditu.google.com", "local.google.com", "answers.google.com", "profiles.google.com", "writely.google.com", "buzz.google.com", "ssl.google-analytics.com", "www.google-analytics.com", "googlesource.com", "www.googlesource.com", "android.googlesource.com", "chromium.googlesource.com", "gerrit.googlesource.com", "kernel.googlesource.com", "gwt.googlesource.com"];
 
#include ~/io/file.ejs
#include ~/io/textfile.ejs
#include Socket.ejs
 
/*
    IP辅助对象
*/
var IP =
{
    toInt: function(ip)
    {
        var ipTest =/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
        var xH = "", result = ipTest.exec(ip);
        if(!result) return -1;
        return (parseInt(result[1]) << 24 
            | parseInt(result[2]) << 16
            | parseInt(result[3]) << 8
            | parseInt(result[4]));
    },
    toString: function(ip)
    {
        if(ip < 0 || ip > 0xFFFFFFFF){
            throw new ArgumentException("参数超过IPV4地址范围。");
        }
        return (ip >>> 24) + "." + (ip >> 16 & 0xFF) + "." + (ip >> 8 & 0xFF) + "." + (ip & 0xFF);
    }
}
var main = function()
{
    //开始搜索的IP地址
    var start = 1089054986;
    //结束搜索的IP地址
    var end = 1089077247;
    //使用对话框接收值
    while(true)
    {
        var _start = prompt("请输入开始搜索的IP(默认从" + IP.toString(start) + "开始搜索):");
        if(_start !== null)
        {
            _start = IP.toInt(_start);
            if(_start === -1)
            {
                alert("请输入正确的IP地址。");
                continue;
            }
            start = _start;
        }
        break;
    }
    console.log("从" + IP.toString(start) + "开始检索。");
    while(true)
    {
        var _count = prompt("请输入要检索的IP个数(默认检索到" + IP.toString(end) + "):");
        if(_count !== null)
        {
            _count = parseInt(_count);
            if(isNaN(_count))
            {
                alert("请输入阿拉伯数字作为要检索的IP个数!");
                continue;
            }
            end = start + _count - 1;
        }
        break;
    }
    console.log("检索到" + IP.toString(end) + "结束。");
    //为了避免底部http协议视图区域显示http数据封包,加快检索速度这里避免使用内置“http”对象,而是实例化新的http对象。
    var h = new Http();
    var ipaddr, lastTime = 9999999999;
    var ipTotal = (end - start) + 1;    //总数
    if(!confirm("需要搜索" + ipTotal + "个IP地址,搜索可能会消耗较长的时间,是否继续?"))
    {
        console.error("用户停止执行脚本。");
        return; //退出服务
    }
    var count = 0;
    //开始检索
    for(var ip = start; ip <= end; ip++)
    {
        //计算运行时间
        var beginTime = new Date().getTime();
        try
        {
            var temp = IP.toString(ip);
            var url = "http://" + temp;
            //延时超过300毫秒的服务器都丢掉
            var timeout = h.TestIP(temp, 300);
            if(timeout != -1)
            {
                var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    //检测80端口是否开通
                    var result = socket.baseObject.BeginConnect(temp, 80, null, null);
                    //500毫秒超时,如果可以忍受更大的数值可以修改
                    var success = result.AsyncWaitHandle.WaitOne(500, true);
                    if (!success)
                    {
                        console.error(url + "连接80端口超时。");
                        continue;
                    }
                }
                finally
                {
                    socket.baseObject.Close();
                }
                var htmlText = h.get(url, "utf-8");
                if(htmlText.indexOf("按回车键进行搜索。") > -1)
                {
                    var endTime = new Date().getTime();
                    var time = endTime - beginTime;
                    if(lastTime > time)
                    {
                        lastTime = time;
                        ipaddr = temp;
                    }
                    console.info(url + "地址测试耗时:" + time + "毫秒。");
                }
                else
                {
                    console.error(url + "不是Google搜索服务首页。");
                }
            }
            else
            {
                console.error(url + "服务器ping超时。");
            }
        }
        catch(e)
        {
            var endTime = new Date().getTime();
            var time = endTime - beginTime;
            console.error(e.message + "\r\n耗时:" + time + "毫秒。");
        }
        finally
        {
            count++;
            console.info("搜索进度:完成" + (count / ipTotal * 100).toFixed(2) + "%。");
        }
    }
    if(ipaddr === undefined)
    {
        console.error("未能搜索到可用的Google服务器IP地址。");
        return;
    }
    console.info("最优Google搜索服务器:http://" + ipaddr);
    //将最优IP缓存入系统hosts文件
    if(confirm("是否将" + ipaddr + "缓存入系统hosts文件?\r\n\r\n警告:缓存过程中请勿停止执行脚本,否则可能造成hosts缓存数据丢失!!!"))
    {
        var env = System.Environment;
        var hosts = env.SystemDirectory + "\\drivers\\etc\\hosts";
        if(File.exists(hosts))
        {
            console.info("当前系统hosts文件路径:" + hosts);
            var lines = [];
            var i = 0;
            //先读取hosts文件
            var reader = new TextReader(hosts, "utf-8");
            try
            {
                var skip = false;
                while(reader.peek() > -1)
                {
                    var line = reader.readLine();
                    if(line.indexOf(hostsMarkBegin) === 0)
                    {
                        skip = true;
                    }
                    if(!skip)
                    {
                        lines[i++] = line;
                    }
                    if(line.indexOf(hostsMarkEnd) === 0)
                    {
                        skip = false;
                    }
                }
                lines[i++] = hostsMarkBegin;
                for(var j = 0; j < googleDomains.length; j++)
                {
                    lines[i++] = ipaddr + "    " + googleDomains[j];
                }
                lines[i++] = hostsMarkEnd;
            }
            catch(e)
            {
                console.error(e.message);
            }
            finally
            {
                reader.close();
            }
            var hosts_backup = env.SystemDirectory + "\\drivers\\etc\\hosts_ejs_backup";
            //先备份一份hosts数据
            File.copy(hosts, hosts_backup, true);
            console.info("已备份hosts文件至:" + hosts_backup);
            //写新的hosts文件
            File.writeAllLines(hosts, lines, "utf-8");
            console.info("已将" + ipaddr + "地址缓存入系统hosts文件。\r\n友情提示:如果未能及时生效请在命令行环境中键入“ipconfig /flushdns”指令刷新DNS。在刷新DNS后还是无效请检查下当前系统的hosts文件是否存在非EJS脚本生成的Google域名DNS缓存,如果存在删除非EJS脚本生成的Google域名DNS缓存即可生效。");
        }
        else
        {
            console.error("未找到hosts文件。");
        }
    }
}
//程序开始执行
main();

注意:上述脚本需要引用“Socket.ejs”脚本文件,这里是将两个脚本文件放在同一目录内的。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。