From d4c29a0b6d598cd133de5e13ffb0a0a8bd809713 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Thu, 6 Dec 2018 16:39:51 +0100 Subject: [PATCH] Option to resolve DNS for the statistics endpoint --- config/websockets.php | 6 ++++++ src/Console/StartWebSocketServer.php | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/config/websockets.php b/config/websockets.php index 40aaa24eeb..9c209434d1 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -65,6 +65,12 @@ * the number of days specified here will be deleted. */ 'delete_statistics_older_than_days' => 60, + + /* + * Use an DNS resolver to make the requests to the statistics logger + * default is to resolve everything to 127.0.0.1. + */ + 'perform_dns_lookup' => false, ], /* diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index c2b6678c16..c91feccdf8 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -5,7 +5,10 @@ use React\Socket\Connector; use Clue\React\Buzz\Browser; use Illuminate\Console\Command; +use React\Dns\Config\Config as DnsConfig; use React\EventLoop\Factory as LoopFactory; +use React\Dns\Resolver\Factory as DnsFactory; +use React\Dns\Resolver\Resolver as ReactDnsResolver; use BeyondCode\LaravelWebSockets\Statistics\DnsResolver; use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter; @@ -47,7 +50,7 @@ public function handle() protected function configureStatisticsLogger() { $connector = new Connector($this->loop, [ - 'dns' => new DnsResolver(), + 'dns' => $this->getDnsResolver(), 'tls' => [ 'verify_peer' => config('app.env') === 'production', 'verify_peer_name' => config('app.env') === 'production', @@ -123,4 +126,20 @@ protected function startWebSocketServer() ->createServer() ->run(); } + + protected function getDnsResolver(): ReactDnsResolver + { + if (! config('websockets.statistics.perform_dns_lookup')) { + return new DnsResolver; + } + + $dnsConfig = DnsConfig::loadSystemConfigBlocking(); + + return (new DnsFactory)->createCached( + $dnsConfig->nameservers + ? reset($dnsConfig->nameservers) + : '1.1.1.1', + $this->loop + ); + } }