PHP code example of tourze / dns-server-bundle

1. Go to this page and download the library: Download tourze/dns-server-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / dns-server-bundle example snippets


return [
    // ...
    DnsServerBundle\DnsServerBundle::class => ['all' => true],
];

// Example upstream server configuration
$upstream = new UpstreamDnsServer();
$upstream->setName('Cloudflare DNS');
$upstream->setHost('1.1.1.1');
$upstream->setPort(53);
$upstream->setProtocol(DnsProtocolEnum::UDP);
$upstream->setMatchStrategy(MatchStrategy::SUFFIX);
$upstream->setMatchPattern('*.example.com');

use DnsServerBundle\Service\DnsQueryService;
use DnsServerBundle\Service\DnsResolver;

// Resolve DNS queries
$resolver = new DnsResolver($upstreamServers);
$result = $resolver->resolve('example.com', RecordType::A);

// Handle DNS queries programmatically
$queryService = new DnsQueryService($resolver, $logger);
$response = $queryService->handleQuery($dnsMessage, $clientAddress, $server);

use DnsServerBundle\Enum\DnsProtocolEnum;
use DnsServerBundle\Enum\MatchStrategy;
use DnsServerBundle\Entity\UpstreamDnsServer;

// Configure DNS over TLS upstream
$dotServer = new UpstreamDnsServer();
$dotServer->setName('Cloudflare DoT')
    ->setHost('1.1.1.1')
    ->setPort(853)
    ->setProtocol(DnsProtocolEnum::DOT)
    ->setCertPath('/path/to/client.crt')
    ->setKeyPath('/path/to/client.key')
    ->setVerifyCert(true);

// Configure DNS over HTTPS upstream
$dohServer = new UpstreamDnsServer();
$dohServer->setName('Google DoH')
    ->setHost('dns.google')
    ->setPort(443)
    ->setProtocol(DnsProtocolEnum::DOH)
    ->setPath('/dns-query');

use DnsServerBundle\Entity\DnsQueryLog;

// Query logs / - Client IP address
// - Response code
// - Response time
// - Upstream server used
// - Query timestamp

use DnsServerBundle\Service\DnsResolver;
use DnsServerBundle\Service\DnsMatcherService;

$matcher = new DnsMatcherService();
$resolver = new DnsResolver($upstreamServers, $matcher, $logger);

// Add custom resolution logic
$resolver->resolve('example.com', RecordType::A);
bash
php bin/console doctrine:migrations:migrate
bash
php bin/console dns:worker:start --host=0.0.0.0 --port=53