最近经常查询域名注册状态,主要用的网站是tld-list或者gandi,都可以多域名查询的,通过excel表,批量做一批域名查询,但是最近网络不怎么稳定,网站经常打不开,或者加载没有样式。

因此等着不如动手,域名查询的原理只是利用 Whois 服务地址的 43 端口反馈结果。

网站现有的查询方式也多是这么实现的。只不过美化查询页面和反馈的结果。

现在搜集到全球可以注册的域名1552个。其中有 whois server 的域名有1213个。

所有可注册域名列表从这里,点击另存下载,39K的TXT文档,压缩到11KB,再转成 Base64(数据来源:https://www.iana.org/domains/root/db

所有域名可以访问https://gongju.cool/tld/,查询所有域名所属的地区,可以分类查询是否支持备案。

提供PHP版查询功能代码

HTML代码:
index.html

    <h1>域名查询</h1>
    <form id="whoisForm">
        <label for="domainNames">请输入要查询的域名 (用逗号分隔):</label>
        <textarea id="domainNames" name="domainNames" rows="20" cols="50"></textarea>
        <button type="submit">查询</button>
    </form>

    <div id="result"></div>

    <script>
    document.getElementById('whoisForm').addEventListener('submit', function (e) {
        e.preventDefault(); // 阻止表单的默认提交行为

        const domainNamesTextarea = document.getElementById('domainNames');
        //使用 .split('\n') 将文本拆分为单独的行,并使用 .map(domain => domain.trim()) 去除每行的前后空白,最后使用 .filter(domain => domain) 过滤掉空行。
        const domainNames = domainNamesTextarea.value.split('\n')
        .map(domain => domain.trim())
        .filter(domain => domain);
        const resultContainer = document.getElementById('result');

        resultContainer.innerHTML = ''; // 清空之前的结果

        domainNames.forEach(domainName => {
            // 发起查询请求
            fetch('whois.php?domainNames=' + domainName)
                .then(response => response.text())
                .then(data => {
                    const resultDiv = document.createElement('div');
                    resultDiv.innerHTML = data;
                    resultContainer.appendChild(resultDiv);
                })
                .catch(error => {
                    resultContainer.innerHTML = '查询失败: ' + error;
                });
        });
    });
    </script>

PHP代码:
whois.php

<?php
//需要自行从txt中转成php数组的格式
$whoisServer=array(
'.aaa'=>'',
'.zip'=>'whois.nic.google',
'.zippo'=>'',
'.zm'=>'whois.zicta.zm',
'.zone'=>'whois.nic.zone',
'.zuerich'=>'whois.nic.zuerich',
'.zw'=>''
);
$domainNames="";
if (isset($_GET['domainNames'])) {
    //从GET请求中获取域名数据并分割成行数组
    $domainNames = explode("\n", $_GET['domainNames']);

    //遍历每个域名并查询其注册状态
    foreach ($domainNames as $domainName) {
        $domainName = trim($domainName); //去除每行域名前后的空白

        if (!empty($domainName)) { //检查域名是否为空
            $domainParts = explode('.', $domainName);
            $domainExtension = '.' . end($domainParts);

            if (array_key_exists($domainExtension, $whoisServer)) {
                $whoisServerAddress = $whoisServer[$domainExtension];

                if (!empty($whoisServerAddress)) {
                    //连接到 Whois 服务器的 43 端口
                    $socket = fsockopen($whoisServerAddress, 43, $errno, $errstr, 30);

                    if ($socket) {
                        fwrite($socket, $domainName . "\r\n");
                        $response = '';
                        while (!feof($socket)) {
                            $response .= fgets($socket, 128);
                        }

                        //别忘记关闭连接
                        fclose($socket);

                        //本代码指示查询响应中是否包含 'No match for',来判断是否已经被注册,如果需要展示更多的,需要跟前端配合一下。
                        if (strpos($response, 'No match for') !== false) {
                            echo $domainName . ' 未被注册<br>';
                        } else {
                            echo $domainName . ' 已被注册<br>';
                        }
                    } else {
                        echo "无法连接到 Whois 服务器 $whoisServerAddress: $errstr ($errno)<br>";
                    }
                } else {
                    echo "找不到 $domainExtension 的 Whois 服务器地址,请检查配置<br>";
                }
            } else {
                echo "不支持的域名后缀: $domainExtension<br>";
            }
        }
    }
} else {
    echo '未收到域名数据';
}
?>

以上代码可以查询所有的有whois server的域名是否注册。

也可以一次把要查询的域名Post到 PHP 处理,但是如果太多的话,容易超时。
因此本次选用的是每个域名一个POST进程。
以上代码还是有BUG,如:没加判断是否一行只有一个域名,可以带二级域名查询等等。

--- EOF ---
本文链接:
订阅本站:feed
声明:博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

标签: none

添加新评论