private static (List <dynamic> list, ReturnCode statusCode) ResolveOverHttps(string clientIpAddress, string domainName, bool proxyEnable = false, IWebProxy wProxy = null, RecordType type = RecordType.A) { string dnsStr; List <dynamic> recordList = new List <dynamic>(); using (MWebClient webClient = new MWebClient()) { webClient.Headers["User-Agent"] = "AuroraDNSC/0.1"; // webClient.AllowAutoRedirect = false; if (proxyEnable) { webClient.Proxy = wProxy; } try { dnsStr = webClient.DownloadString( DnsSettings.HttpsDnsUrl + @"?ct=application/dns-json&" + $"name={domainName}&type={type.ToString().ToUpper()}&edns_client_subnet={clientIpAddress}"); } catch (WebException e) { HttpWebResponse response = (HttpWebResponse)e.Response; try { BgwLog($@"| - Catch WebException : {Convert.ToInt32(response.StatusCode)} {response.StatusCode} | {domainName}"); } catch (Exception exception) { BgwLog($@"| - Catch WebException : {exception.Message} | {domainName}"); //MainWindow.NotifyIcon.ShowBalloonTip(360, "AuroraDNS - 错误", // $"异常 : {exception.Message} {Environment.NewLine} {domainName}", ToolTipIcon.Warning); } return(new List <dynamic>(), ReturnCode.ServerFailure); } } JsonValue dnsJsonValue = Json.Parse(dnsStr); int statusCode = dnsJsonValue.AsObjectGetInt("Status"); if (statusCode != 0) { return(new List <dynamic>(), (ReturnCode)statusCode); } if (dnsStr.Contains("\"Answer\"")) { var dnsAnswerJsonList = dnsJsonValue.AsObjectGetArray("Answer"); foreach (var itemJsonValue in dnsAnswerJsonList) { string answerAddr = itemJsonValue.AsObjectGetString("data"); string answerDomainName = itemJsonValue.AsObjectGetString("name"); int answerType = itemJsonValue.AsObjectGetInt("type"); int ttl = itemJsonValue.AsObjectGetInt("TTL"); switch (type) { case RecordType.A: { if (Convert.ToInt32(RecordType.A) == answerType) { ARecord aRecord = new ARecord( DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr)); recordList.Add(aRecord); } else if (Convert.ToInt32(RecordType.CName) == answerType) { CNameRecord cRecord = new CNameRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(cRecord); //recordList.AddRange(ResolveOverHttps(clientIpAddress,answerAddr)); //return recordList; } break; } case RecordType.Aaaa: { if (Convert.ToInt32(RecordType.Aaaa) == answerType) { AaaaRecord aaaaRecord = new AaaaRecord( DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr)); recordList.Add(aaaaRecord); } else if (Convert.ToInt32(RecordType.CName) == answerType) { CNameRecord cRecord = new CNameRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(cRecord); } break; } case RecordType.CName when answerType == Convert.ToInt32(RecordType.CName): { CNameRecord cRecord = new CNameRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(cRecord); break; } case RecordType.Ns when answerType == Convert.ToInt32(RecordType.Ns): { NsRecord nsRecord = new NsRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(nsRecord); break; } case RecordType.Mx when answerType == Convert.ToInt32(RecordType.Mx): { MxRecord mxRecord = new MxRecord( DomainName.Parse(answerDomainName), ttl, ushort.Parse(answerAddr.Split(' ')[0]), DomainName.Parse(answerAddr.Split(' ')[1])); recordList.Add(mxRecord); break; } case RecordType.Txt when answerType == Convert.ToInt32(RecordType.Txt): { TxtRecord txtRecord = new TxtRecord(DomainName.Parse(answerDomainName), ttl, answerAddr); recordList.Add(txtRecord); break; } case RecordType.Ptr when answerType == Convert.ToInt32(RecordType.Ptr): { PtrRecord ptrRecord = new PtrRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(ptrRecord); break; } default: { statusCode = Convert.ToInt32(ReturnCode.ServerFailure); break; } } } } return(recordList, (ReturnCode)statusCode); }
public static (List <DnsRecordBase> list, ReturnCode statusCode) ResolveOverHttpsByDnsJson(string clientIpAddress, string domainName, string dohUrl, bool proxyEnable = false, IWebProxy wProxy = null, RecordType type = RecordType.A) { string dnsStr; List <DnsRecordBase> recordList = new List <DnsRecordBase>(); try { dnsStr = MyCurl.GetString(dohUrl + @"?ct=application/dns-json&" + $"name={domainName}&type={type.ToString().ToUpper()}&edns_client_subnet={clientIpAddress}", DnsSettings.Http2Enable, proxyEnable, wProxy, DnsSettings.AllowAutoRedirect); } catch (WebException e) { HttpWebResponse response = (HttpWebResponse)e.Response; try { BackgroundLog($@"| - Catch WebException : {Convert.ToInt32(response.StatusCode)} {response.StatusCode} | {e.Status} | {domainName} | {response.ResponseUri}"); if (DnsSettings.HTTPStatusNotify) { MainWindow.NotifyIcon.ShowBalloonTip(360, "AuroraDNS - 错误", $"异常 :{Convert.ToInt32(response.StatusCode)} {response.StatusCode} {Environment.NewLine} {domainName}", ToolTipIcon.Warning); } if (response.StatusCode == HttpStatusCode.BadRequest) { DnsSettings.DnsMsgEnable = true; } } catch (Exception exception) { BackgroundLog($@"| - Catch WebException : {exception.Message} | {e.Status} | {domainName} | {dohUrl}" + @"?ct=application/dns-json&" + $"name={domainName}&type={type.ToString().ToUpper()}&edns_client_subnet={clientIpAddress}"); if (DnsSettings.HTTPStatusNotify) { MainWindow.NotifyIcon.ShowBalloonTip(360, "AuroraDNS - 错误", $"异常 : {exception.Message} {Environment.NewLine} {domainName}", ToolTipIcon.Warning); } } if (dohUrl != DnsSettings.HttpsDnsUrl) { return(new List <DnsRecordBase>(), ReturnCode.ServerFailure); } BackgroundLog($@"| -- SecondDoH : {DnsSettings.SecondHttpsDnsUrl}"); return(ResolveOverHttpsByDnsJson(clientIpAddress, domainName, DnsSettings.SecondHttpsDnsUrl, proxyEnable, wProxy, type)); } JsonValue dnsJsonValue = Json.Parse(dnsStr); int statusCode = dnsJsonValue.AsObjectGetInt("Status"); if (statusCode != 0) { return(new List <DnsRecordBase>(), (ReturnCode)statusCode); } if (dnsStr.Contains("\"Answer\"")) { var dnsAnswerJsonList = dnsJsonValue.AsObjectGetArray("Answer"); foreach (var itemJsonValue in dnsAnswerJsonList) { string answerAddr = itemJsonValue.AsObjectGetString("data"); string answerDomainName = itemJsonValue.AsObjectGetString("name"); int answerType = itemJsonValue.AsObjectGetInt("type"); int ttl = itemJsonValue.AsObjectGetInt("TTL"); switch (type) { case RecordType.A when Convert.ToInt32(RecordType.A) == answerType && !DnsSettings.Ipv4Disable: { ARecord aRecord = new ARecord( DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr)); recordList.Add(aRecord); break; } case RecordType.A: { if (Convert.ToInt32(RecordType.CName) == answerType) { CNameRecord cRecord = new CNameRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(cRecord); //recordList.AddRange(ResolveOverHttps(clientIpAddress,answerAddr)); //return recordList; } break; } case RecordType.Aaaa when Convert.ToInt32(RecordType.Aaaa) == answerType && !DnsSettings.Ipv6Disable: { AaaaRecord aaaaRecord = new AaaaRecord( DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr)); recordList.Add(aaaaRecord); break; } case RecordType.Aaaa: { if (Convert.ToInt32(RecordType.CName) == answerType) { CNameRecord cRecord = new CNameRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(cRecord); } break; } case RecordType.CName when answerType == Convert.ToInt32(RecordType.CName): { CNameRecord cRecord = new CNameRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(cRecord); break; } case RecordType.Ns when answerType == Convert.ToInt32(RecordType.Ns): { NsRecord nsRecord = new NsRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(nsRecord); break; } case RecordType.Mx when answerType == Convert.ToInt32(RecordType.Mx): { MxRecord mxRecord = new MxRecord( DomainName.Parse(answerDomainName), ttl, ushort.Parse(answerAddr.Split(' ')[0]), DomainName.Parse(answerAddr.Split(' ')[1])); recordList.Add(mxRecord); break; } case RecordType.Txt when answerType == Convert.ToInt32(RecordType.Txt): { TxtRecord txtRecord = new TxtRecord(DomainName.Parse(answerDomainName), ttl, answerAddr); recordList.Add(txtRecord); break; } case RecordType.Ptr when answerType == Convert.ToInt32(RecordType.Ptr): { PtrRecord ptrRecord = new PtrRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(ptrRecord); break; } default: statusCode = Convert.ToInt32(ReturnCode.ServerFailure); break; } } } return(recordList, (ReturnCode)statusCode); }
private static (List <dynamic> list, int statusCode) ResolveOverHttps(string clientIpAddress, string domainName, bool proxyEnable = false, IWebProxy wProxy = null, RecordType type = RecordType.A) { string dnsStr; List <dynamic> recordList = new List <dynamic>(); using (WebClient webClient = new WebClient()) { webClient.Headers["User-Agent"] = "AuroraDNSC/0.1"; if (proxyEnable) { webClient.Proxy = wProxy; } dnsStr = webClient.DownloadString( ADnsSetting.HttpsDnsUrl + @"?ct=application/dns-json&" + $"name={domainName}&type={type.ToString().ToUpper()}&edns_client_subnet={clientIpAddress}"); } JsonValue dnsJsonValue = Json.Parse(dnsStr); int statusCode = dnsJsonValue.AsObjectGetInt("Status"); if (statusCode != 0) { return(new List <dynamic>(), statusCode); } if (dnsStr.Contains("\"Answer\"")) { var dnsAnswerJsonList = dnsJsonValue.AsObjectGetArray("Answer"); foreach (var itemJsonValue in dnsAnswerJsonList) { string answerAddr = itemJsonValue.AsObjectGetString("data"); string answerDomainName = itemJsonValue.AsObjectGetString("name"); int answerType = itemJsonValue.AsObjectGetInt("type"); int ttl = itemJsonValue.AsObjectGetInt("TTL"); if (type == RecordType.A) { if (Convert.ToInt32(RecordType.A) == answerType) { ARecord aRecord = new ARecord( DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr)); recordList.Add(aRecord); } else if (Convert.ToInt32(RecordType.CName) == answerType) { CNameRecord cRecord = new CNameRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(cRecord); //recordList.AddRange(ResolveOverHttps(clientIpAddress,answerAddr)); //return recordList; } } else if (type == RecordType.Aaaa && ADnsSetting.IPv6Enable) { if (Convert.ToInt32(RecordType.Aaaa) == answerType) { AaaaRecord aaaaRecord = new AaaaRecord( DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr)); recordList.Add(aaaaRecord); } else if (Convert.ToInt32(RecordType.CName) == answerType) { CNameRecord cRecord = new CNameRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(cRecord); } } else if (type == RecordType.CName && answerType == Convert.ToInt32(RecordType.CName)) { CNameRecord cRecord = new CNameRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(cRecord); } else if (type == RecordType.Ns && answerType == Convert.ToInt32(RecordType.Ns)) { NsRecord nsRecord = new NsRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(nsRecord); } else if (type == RecordType.Mx && answerType == Convert.ToInt32(RecordType.Mx)) { MxRecord mxRecord = new MxRecord( DomainName.Parse(answerDomainName), ttl, ushort.Parse(answerAddr.Split(' ')[0]), DomainName.Parse(answerAddr.Split(' ')[1])); recordList.Add(mxRecord); } else if (type == RecordType.Txt && answerType == Convert.ToInt32(RecordType.Txt)) { TxtRecord txtRecord = new TxtRecord(DomainName.Parse(answerDomainName), ttl, answerAddr); recordList.Add(txtRecord); } else if (type == RecordType.Ptr && answerType == Convert.ToInt32(RecordType.Ptr)) { PtrRecord ptrRecord = new PtrRecord( DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr)); recordList.Add(ptrRecord); } } } return(recordList, statusCode); }