public static byte[] GetDataByMWebClient(string url, bool proxyEnable = false, IWebProxy wProxy = null, bool allowRedirect = true) { MWebClient mWebClient = new MWebClient { Headers = { ["User-Agent"] = "AuroraDNSC/0.1" } }; mWebClient.AllowAutoRedirect = allowRedirect; mWebClient.Proxy = proxyEnable ? wProxy : new WebProxy(); return(mWebClient.DownloadData(url)); }
public static string GetStringByMWebClient(string url, bool proxyEnable = false, IWebProxy wProxy = null, bool allowRedirect = true) { MWebClient mWebClient = new MWebClient { Headers = { ["User-Agent"] = "AuroraDNSC/0.1" }, AllowAutoRedirect = allowRedirect, Proxy = proxyEnable ? wProxy : new WebProxy() }; return(mWebClient.DownloadString(url)); }
public static byte[] GetDataByMWebClient(string url, bool proxyEnable = false, IWebProxy wProxy = null) { MWebClient mWebClient = new MWebClient { Headers = { ["User-Agent"] = "AuroraDNSC/0.1" } }; //if (bool) webClient.AllowAutoRedirect = false; if (proxyEnable) { mWebClient.Proxy = wProxy; } return(mWebClient.DownloadData(url)); }
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); }
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); }