An Authoritative DNS Server written purely in PHP.
This is an Authoritative DNS Server written in pure PHP. It will listen to DNS request on the default port (Default: port 53) and give answers about any domain that it has DNS records for. This class can be used to give DNS responses dynamically based on your pre-existing PHP code.
Requirements
PHP 7.2+- Needs either
socketsorsocket_createPHP extension loaded (which they are by default)
Example
Here is an example of DNS server usage:
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();
Running example
- Run
composer installto install dependencies - Run
php example/example.phpto run the server
Query server using
dig command to ensure proper functioning$ 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
Zone File Storage
PHP DNS Server supports four zone file formats out-of-the-box: Bind, JSON, XML, and YAML; each file format is supported by a specialised
Resolver class: BindResolver, JsonResolver, XmlResolver, and YamlResolver, respectively. Example files are in the example/ directory.JSON zone example
{
"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"
}
]
}
Running Tests
Unit tests using PHPUnit are provided. A simple script is located in the root.
- run
composer installto install PHPUnit and dependencies - run
vendor/bin/phpunitfrom the root to run the tests
Building .phar
- run
composer run-script build-serverto build the phpdnsserver.phar file, outputs in the bin folder. - run
composer run-script build-consoleto build the phpdnscli.phar file, outputs in the bin folder. - run
composer run-script build-installerto build the installer. Windows support for the installer is currently limited.
Running the .phar files
To run the new .phar files, download them from the release and move them to the desired folder.
phpdnsserver.pharto run the phpdnsserver, uses the new filesystem by defaultphpdnscli.pharto run cli commandsphpdnsinstaller.pharas root to create required folders and default config.
Supported command line switches
--bind:b- bind to a specific ip. Uses0.0.0.0by default--port:p- bind to a specific port. Uses port53by default--config:c- specify the config file. Usesphpdns.jsonon windows and/etc/phpdns.jsonon unix systems by default--storage:s- specify the path to the storage for zones, and logs. Uses/etc/phpdnsserveron unix, and current working directory on windows.
Supported Record Types
- A
- NS
- CNAME
- SOA
- PTR
- MX
- TXT
- AAAA
- AXFR
- ANY
- SRV
No comments:
Post a Comment