From bb246c79e45188207c65abe4535780e572d58111 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 19 Sep 2020 16:58:55 +0530 Subject: [PATCH] WebService: fixed issue in GetRequestRemoteEndPoint() for reading X-Real-IP from reverse proxy headers. Minor fix in syntax of parsing PTR response. --- DnsServerCore/WebService.cs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/DnsServerCore/WebService.cs b/DnsServerCore/WebService.cs index c7f3bbc3..bd153d34 100644 --- a/DnsServerCore/WebService.cs +++ b/DnsServerCore/WebService.cs @@ -522,16 +522,19 @@ namespace DnsServerCore { try { - string xRealIp = request.Headers["X-Real-IP"]; - if (IPAddress.TryParse(xRealIp, out IPAddress address)) - { - //get the real IP address of the requesting client from X-Real-IP header set in nginx proxy_pass block - return new IPEndPoint(address, 0); - } - if (request.RemoteEndPoint == null) return new IPEndPoint(IPAddress.Any, 0); + if (NetUtilities.IsPrivateIP(request.RemoteEndPoint.Address)) + { + string xRealIp = request.Headers["X-Real-IP"]; + if (IPAddress.TryParse(xRealIp, out IPAddress address)) + { + //get the real IP address of the requesting client from X-Real-IP header set in nginx proxy_pass block + return new IPEndPoint(address, 0); + } + } + return request.RemoteEndPoint; } catch @@ -1502,10 +1505,12 @@ namespace DnsServerCore DnsDatagram ptrResponse = await _dnsServer.DirectQueryAsync(new DnsQuestionRecord(address, DnsClass.IN), 200); if ((ptrResponse != null) && (ptrResponse.Answer.Count > 0)) { - string ptrDomain = DnsClient.ParseResponsePTR(ptrResponse); - - jsonWriter.WritePropertyName("domain"); - jsonWriter.WriteValue(ptrDomain); + IReadOnlyList ptrDomains = DnsClient.ParseResponsePTR(ptrResponse); + if (ptrDomains != null) + { + jsonWriter.WritePropertyName("domain"); + jsonWriter.WriteValue(ptrDomains[0]); + } } } catch