Oct 23

这个脚本功能很简单,如果你有境外 PHP 主机空间,利用这个脚本可以查询到被 DNS 污染的一些站点的真实 IP 。缓存的结果可以直接拷贝到系统 Hosts 文件中使用,也可以转换为 Unbound 的 localzone 格式供 Unbound 调用。hosts.txt 中包含需要查询的站点主机名,一行一个,ipcache.txt 则为查询结果缓存文件,标准的 Windows 系统 Hosts 文件格式。

可以到本站: gethosts 页面体验效果。


<?php
/**
 * gethosts
 *
 * @link       https://www.quakemachinex.com/gethosts/
 * @copyright  Copyright (c) 2011 AvP
 * @license    MIT Style License
 * @version    1.0
 */

$hostsLock = true; //用户提交的查询数据是否写入 hosts.txt,默认:不写入

$isPost = false;

if (isset($_POST['hosts'])){
    $hosts_txt = $_POST['hosts'];
    $isPost = true;
	if (!$hostsLock) file_put_contents('hosts.txt',$hosts_txt);
} else {
	$hosts_txt = file_get_contents('hosts.txt');
}
$hosts_array = preg_split('/[\s,]+/', $hosts_txt, -1, PREG_SPLIT_NO_EMPTY);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>gethosts</title>
<script src="ZeroClipboard.js" type="text/javascript"></script>
<script type="text/javascript">
//类似 PHP 中的 trim
    function trim(str) {
        var str = str.replace(/^\s\s*/, ''),
            ws = /\s/,
            i = str.length;
        while (ws.test(str.charAt(--i)));
        return str.slice(0, i + 1);
    }
//基于 ZeroClipboard 的跨浏览器复制到剪贴板
    function ctoc() {
        var txt = document.getElementById('hostsMap').value;

        if (window.clipboardData) {
            window.clipboardData.setData("text", txt);
            alert("Copied text to clipboard:\n" + txt);
        } else {
            var clip = new ZeroClipboard.Client();
            clip.glue('clickme');
            clip.setText(txt);
            clip.addEventListener('complete', function(client, text) {
                alert("Copied text to clipboard:\n" + text);
            });
            }
    }
//转换 Hosts 文件格式到 Unbound 的 localzone 格式
    function htol() {
        var i;
        var localz = new Array();
        var hosts = document.getElementById('hostsMap').value;
        var localzs='';
        hostdomain = hosts.split("\n");
        for (i=0;i<hostdomain.length;i++) {
            if (trim(hostdomain[i]) != '') {
                localz[i] = hostdomain[i].split("\t");
                localzs += 'local-data: "'+localz[i][1]+' A '+localz[i][0]+'"\n';
            }
        }
        document.getElementById('hostsMap').value = localzs;
    }
</script>
</head>
<body>
<?php
//判定字符串是否是 IP 地址
function IsIPAdress($value){

    if (preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $value)){
        return true;
    }
    return false;
}
//获取真实 IP,并且缓存,用户提交数据不缓存
function getRealIP(){
    global $hosts_array, $isPost;
    if($isPost) {
        foreach($hosts_array as $hostname){
            $ip = gethostbyname($hostname);
            if (IsIPAdress($ip)) {
                $i = $i + 1;
                echo "$ip\t$hostname";
                echo "\n";
                if ($i > 25) return;
            }
        }
    } else {
        $cache_days = 1; //缓存多少天
        $fz = filesize('ipcache.txt');
        $ipcache_create_time = filectime('ipcache.txt');
        $time_now = date("Y-m-d H:i:s");
        $ipcache_stay_days = (strtotime($time_now)-strtotime(date("Y-m-d H:i:s",$ipcache_create_time)))/86400;
        $ipcache_stay_days = floor($ipcache_stay_days);

        if ($fz && $fz != 0 && $ipcache_stay_days < $cache_days) {
            $ipcache = file_get_contents('ipcache.txt');
            echo $ipcache;
         } else {
            foreach($hosts_array as $hostname){
                $ip = gethostbyname($hostname);
                if (IsIPAdress($ip)) {
                    $ipcacheall .= "$ip\t$hostname\n";
                }
            }
            $local_ipcache = 'ipcache.txt';
            $local_ipcache_actual = fopen($local_ipcache, 'w+');
            fwrite($local_ipcache_actual, $ipcacheall);
            fclose($local_ipcache_actual);
            $ipcache = file_get_contents('ipcache.txt');
            echo $ipcache;
         }
     }
}
?>
    <div style="width:1024px; margin:0 auto;">
        <div style="float:left;margin:0 10px;">
            <h2>Blocked Hostnames</h2>
            <form method="post">
                <button type="submit" style="margin: 5px auto; display: block;">Get Hosts</button>
                <textarea name="hosts" style="width:400px;height:550px;margin:0;padding:3px;display:block;"><?php echo $hosts_txt;?></textarea>
            </form>
        </div>
        <div style="float:left;margin:0 10px;">
            <h2>hosts</h2>
            <div style="margin: 5px auto; display: block;">
            <button type="button" id="clickme" onclick="ctoc();">CopyToClipboard(Click Twice!)</button>
            <button type="button" id="hosttolocal" onclick="htol();">ConventerToLocalzone</button>
            </div>
            <textarea id="hostsMap" onclick="this.focus();this.select()"style="width:500px;height:550px;margin:0;padding:3px;display:block;"><?php getRealIP(); ?></textarea>
        </div>
    </div>
</body>
</html>

ZeroClipboard 可到官方下载。 :!: