【问题标题】:PHP Query a SRV recordPHP 查询一条 SRV 记录
【发布时间】:2013-07-01 20:07:04
【问题描述】:

我运行一个 Minecraft 网站,目前在使用查询协议时,它无法处理 SRV 记录。

我只是想知道有没有办法获取 SRV 记录指向的 ip 和端口。

E.g: mc.lunarphase.co.uk => 192.198.91.238:64759

【问题讨论】:

  • 所以基本上你有主机名并且需要解析ip?还是需要先查找 SRV 记录,然后从返回的主机名中查找 ip?
  • 我只需要从srv记录中获取ip和端口

标签: php dns minecraft srv


【解决方案1】:

您可以使用 dns_get_record。

$result = dns_get_record("_http._tcp.mxtoolbox.com", DNS_SRV);
print_r($result);

打印出来的:

Array
(
    [0] => Array
        (
            [host] => _http._tcp.mxtoolbox.com
            [class] => IN
            [ttl] => 2409
            [type] => SRV
            [pri] => 10
            [weight] => 100
            [port] => 80
            [target] => mxtoolbox.com
        )

)

【讨论】:

    【解决方案2】:

    最简单的方法可能是使用 dig。您可以直接使用套接字,但这样更容易(恕我直言):

    function getDNS($hostname, $type='') {
            $records=`dig +noall +answer $hostname $type`;
            $records=explode("\n",$records);
            $res_hostname='';
            $port=0;
    
            foreach ($records as $record) {
                    preg_match_all('/([^\s]+)\s*/',$record, $matches);
                    if (is_array($matches) && is_array($matches[1]) && count($matches[1]) > 3) {
                            switch (strtoupper($matches[1][3])) {
                            case 'SRV':
                                    if (count($matches[1]) > 6) {
                                            $port=$matches[1][6];
                                            $res_hostname=$matches[1][7];
                                    }
                                    break;
                            case 'A':
                            case 'CNAME':
                                    if (count($matches[1]) > 3) {
                                            $res_hostname=$matches[1][4];
                                    }
                                    break;
                            }
                            if (!empty($res_hostname) && substr($res_hostname, -1) == '.') break; // if it's a cname, we've probably already got the ip, so keep going just in case (but we might not so don't count on it!)
                    }
            }
            if (substr($res_hostname, -1) == '.') { // we have more resolving to do
                    $res_hostname=getDNS(trim($res_hostname, '. '));
            }
    
            if (empty($res_hostname)) die('Failed to lookup IP for ' . (!empty($type) ? '(' . $type .' record) ' : '' ) . $hostname . PHP_EOL);
            if (empty($port)) return $res_hostname;
            return $res_hostname . ':' . $port;
    }
    $hostIPPair=getDNS('example.com', 'srv');
    

    【讨论】:

    • 嗯,刚试过,它回复了;查找(srv 记录)mc.lunarphase.co.uk 的 IP 失败
    • 服务器的正确记录是_minecraft._tcp.mc.lunarphase.co.uk;但是,我只是查看了该 srv 记录,它指向 192.198.91.238。这是错误的(SRV 记录不能指向 IPS;正确的做法是创建一个指向子域上该 ip 的 A 记录,然后将 srv 记录指向所述子域)我建议添加一个mc的唱片。并将其指向该 IP,然后将 SRV 指向 mc.lunarphase.co.uk。
    • 服务器的主机让我这样设置~这是我的世界的SRV记录seyup的方式
    • 你被误导了...... Minecraft 使用 DNS 的方式与任何其他服务相同。
    • 我目前正在使用通过 mc.lunarphase.co.uk 连接到我的服务器的方法 A 记录将要求我还使用域末尾的端口,例如:mc.lunarphase。 co.uk:64759
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多