public static (List <DnsRecordBase> list, ReturnCode statusCode) ResolveOverHttpsByDnsMsg(string clientIpAddress, string domainName, string dohUrl,
                                                                                                  bool proxyEnable = false, IWebProxy wProxy = null, RecordType type = RecordType.A)
        {
            DnsMessage dnsMsg;
            var        dnsBase64String = Convert.ToBase64String(MyDnsSend.GetQuestionData(domainName.TrimEnd('.'), type)).TrimEnd('=')
                                         .Replace('+', '-').Replace('/', '_');

            try
            {
                var dnsDataBytes = MyCurl.GetData(
                    $"{dohUrl}?ct=application/dns-message&dns={dnsBase64String}",
                    DnsSettings.Http2Enable, proxyEnable, wProxy, DnsSettings.AllowAutoRedirect);
                dnsMsg = DnsMessage.Parse(dnsDataBytes);

                if (DnsSettings.Ipv4Disable || DnsSettings.Ipv6Disable)
                {
                    foreach (var item in dnsMsg.AnswerRecords.ToArray())
                    {
                        if (item.RecordType == RecordType.A && DnsSettings.Ipv4Disable)
                        {
                            dnsMsg.AnswerRecords.Remove(item);
                        }
                        if (item.RecordType == RecordType.Aaaa && DnsSettings.Ipv6Disable)
                        {
                            dnsMsg.AnswerRecords.Remove(item);
                        }
                    }
                }
            }
            catch (WebException e)
            {
                HttpWebResponse response = (HttpWebResponse)e.Response;
                try
                {
                    BackgroundLog(
                        $@"| - Catch WebException : {Convert.ToInt32(response.StatusCode)} {response.StatusCode} | {domainName} | {dohUrl} | {dnsBase64String}");

                    if (response.StatusCode == HttpStatusCode.BadRequest)
                    {
                        DnsSettings.DnsMsgEnable = false;
                        return(ResolveOverHttpsByDnsJson(clientIpAddress, domainName, DnsSettings.SecondHttpsDnsUrl,
                                                         proxyEnable, wProxy, type));
                    }
                }
                catch (Exception exception)
                {
                    BackgroundLog(
                        $@"| - Catch WebException : {exception.Message} | {domainName} | {dohUrl} | {dnsBase64String}");
                }

                if (dohUrl != DnsSettings.HttpsDnsUrl)
                {
                    return(new List <DnsRecordBase>(), ReturnCode.ServerFailure);
                }
                BackgroundLog($@"| -- SecondDoH : {DnsSettings.SecondHttpsDnsUrl}");
                return(ResolveOverHttpsByDnsMsg(clientIpAddress, domainName, DnsSettings.SecondHttpsDnsUrl,
                                                proxyEnable, wProxy, type));
            }
            return(dnsMsg.AnswerRecords, dnsMsg.ReturnCode);
        }
示例#2
0
        private static (List <DnsRecordBase> list, ReturnCode statusCode) ResolveOverHttpsByDnsMsg(string clientIpAddress, string domainName, string dohUrl,
                                                                                                   bool proxyEnable = false, IWebProxy wProxy = null, RecordType type = RecordType.A)
        {
            DnsMessage dnsMsg;
            MWebClient mWebClient = new MWebClient {
                Headers = { ["User-Agent"] = "AuroraDNSC/0.1" }
            };

            if (proxyEnable)
            {
                mWebClient.Proxy = wProxy;
            }

            var dnsBase64String = Convert.ToBase64String(MyDnsSend.GetQuestionData(domainName.TrimEnd('.'), type)).TrimEnd('=')
                                  .Replace('+', '-').Replace('/', '_');

            try
            {
                var dnsDataBytes = mWebClient.DownloadData(
                    $"{dohUrl}?ct=application/dns-message&dns={dnsBase64String}&edns_client_subnet={clientIpAddress}");
                dnsMsg = DnsMessage.Parse(dnsDataBytes);
            }
            catch (WebException e)
            {
                HttpWebResponse response = (HttpWebResponse)e.Response;
                try
                {
                    BackgroundLog($@"| - Catch WebException : {Convert.ToInt32(response.StatusCode)} {response.StatusCode} | {domainName} | {dohUrl} | {dnsBase64String}");

                    if (response.StatusCode == HttpStatusCode.BadRequest)
                    {
                        DnsSettings.DnsMsgEnable = false;
                        return(ResolveOverHttpsByDnsJson(clientIpAddress, domainName, DnsSettings.SecondHttpsDnsUrl,
                                                         proxyEnable, wProxy, type));
                    }
                }
                catch (Exception exception)
                {
                    BackgroundLog($@"| - Catch WebException : {exception.Message} | {domainName} | {dohUrl} | {dnsBase64String}");
                }

                if (dohUrl != DnsSettings.HttpsDnsUrl)
                {
                    return(new List <DnsRecordBase>(), ReturnCode.ServerFailure);
                }
                BackgroundLog($@"| -- SecondDoH : {DnsSettings.SecondHttpsDnsUrl}");
                return(ResolveOverHttpsByDnsMsg(clientIpAddress, domainName, DnsSettings.SecondHttpsDnsUrl,
                                                proxyEnable, wProxy, type));
            }
            return(dnsMsg.AnswerRecords, dnsMsg.ReturnCode);
        }