gekkone/php-dns-server

基于 yswery/dns 分支。一个纯PHP实现的DNS服务器。

1.5.3 2024-05-24 14:31 UTC

This package is auto-updated.

Last update: 2024-09-24 15:09:59 UTC


README

Build Status Coverage Status Scrutinizer Code Quality

PHP DNS服务器

这是一个纯PHP编写的权威DNS服务器。它将在默认端口(默认:端口53)上监听DNS请求,并对其拥有的DNS记录的任何域名提供答案。此类可以用来自动根据您现有的PHP代码提供DNS响应。

需求

  • PHP 7.2+
  • 需要加载socketssocket_create PHP扩展(默认情况下已加载)

示例

以下是一个DNS服务器使用的示例

require_once __DIR__.'/../vendor/autoload.php';

// JsonResolver created and provided with path to file with json dns records
$jsonResolver = new yswery\DNS\Resolver\JsonResolver([
    '/path/to/zones/example.com.json',
    '/path/to/zone/test.com.json',
]);

// System resolver acting as a fallback to the JsonResolver
$systemResolver = new yswery\DNS\Resolver\SystemResolver();

// StackableResolver will try each resolver in order and return the first match
$stackableResolver = new yswery\DNS\Resolver\StackableResolver([$jsonResolver, $systemResolver]);

// Create the eventDispatcher and add the event subscribers
$eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
$eventDispatcher->addSubscriber(new \yswery\DNS\Event\Subscriber\EchoLogger());

// Create a new instance of Server class
$server = new yswery\DNS\Server($stackableResolver, $eventDispatcher);

// Start DNS server
$server->start();

运行示例

  • 运行composer install以安装依赖项
  • 运行php example/example.php以运行服务器

使用dig命令查询服务器以确保其正常工作

$ dig @127.0.0.1 test.com A +short
111.111.111.111

$ dig @127.0.0.1 test.com TXT +short
"Some text."

$ dig @127.0.0.1 test2.com A +short
111.111.111.111
112.112.112.112

区域文件存储

PHP DNS服务器支持四种区域文件格式:Bind、JSON、XML和YAML;每种文件格式都有一个专门的Resolver类:分别对应BindResolverJsonResolverXmlResolverYamlResolver。示例文件位于example/目录中。

JSON区域示例

{
  "domain": "example.com.",
  "default-ttl": 7200,
  "resource-records": [
    {
      "name": "@",
      "ttl": 10800,
      "type": "SOA",
      "class": "IN",
      "mname": "example.com.",
      "rname": "postmaster",
      "serial": 2,
      "refresh": 3600,
      "retry": 7200,
      "expire": 10800,
      "minimum": 3600
    }, {
      "type": "A",
      "address": "12.34.56.78"
    },{
      "type": "A",
      "address": "90.12.34.56"
    }, {
      "type": "AAAA",
      "address": "2001:acad:ad::32"
    }, {
      "name": "www",
      "type": "cname",
      "target": "@"
    }, {
      "name": "@",
      "type": "MX",
      "preference": 15,
      "exchange": "mail"
    }, {
      "name": "*.subdomain",
      "ttl": 3600,
      "type": "A",
      "address": "192.168.1.42"
    }
  ]
}

运行测试

提供了使用PHPUnit的单元测试。一个简单的脚本位于根目录。

  • 运行composer install以安装PHPUnit和依赖项
  • 从根目录运行vendor/bin/phpunit以运行测试

构建.phar

  • 运行composer run-script build-server以构建phpdnsserver.phar文件,输出在bin目录中。
  • 运行composer run-script build-console以构建phpdnscli.phar文件,输出在bin目录中。
  • 运行composer run-script build-installer以构建安装程序。安装程序的Windows支持目前有限。

运行.phar文件

要运行新的.phar文件,从发行版下载它们,并将它们移动到目标文件夹。

  • phpdnsserver.phar用于运行phpdnsserver,默认使用新的文件系统
  • phpdnscli.phar用于运行命令行命令
  • 作为root运行phpdnsinstaller.phar以创建所需的文件夹和默认配置。

支持的命令行开关

  • --bind:b - 绑定到特定的IP。默认使用0.0.0.0
  • --port:p - 绑定到特定的端口。默认使用端口53
  • --config:c - 指定配置文件。在Windows上默认使用phpdns.json,在Unix系统上默认使用/etc/phpdns.json
  • --storage:s - 指定区域和日志存储的路径。在Unix上默认使用/etc/phpdnsserver,在Windows上默认使用当前工作目录

支持的记录类型

  • A
  • NS
  • CNAME
  • SOA
  • PTR
  • MX
  • TXT
  • AAAA
  • AXFR
  • ANY
  • SRV
  • CAA(自版本1.5.0起)

许可证

MIT许可证(MIT)

版权所有(c) 2016-2017 Yif Swery

特此授予任何人免费获得此软件及其相关文档文件(“软件”)副本的权利,无论以何种形式,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本,以及允许向获得软件的人提供上述行为,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何形式的质量保证,无论是明示的、暗示的还是与特定目的相关的,包括但不限于适销性、特定用途适用性和非侵权性。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论其责任基于合同、侵权或其他法律行为,以及软件本身、使用或对软件的任何其他处理引起的、产生的或与之相关的。