/// <summary> /// Acts as a Factory which reads QType from Immutable IResource param to return concrete type which implements IResource. /// </summary> /// <param name="data"></param> /// <param name="position"></param> /// <param name="resource"></param> /// <returns></returns> private static IResource ReadAnswer(ref byte[] data, int position, IResource resource) { switch (resource.Type) { case QType.TXT: return(TxtRecord.Parse(data, position, resource)); case QType.MX: return(MxRecord.Parse(data, position, resource)); case QType.A: return(ARecord.Parse(data, position, resource)); case QType.SOA: return(SoaRecord.Parse(data, position, resource)); case QType.NS: return(NsRecord.Parse(data, position, resource)); case QType.CNAME: return(CNameRecord.Parse(data, position, resource)); } return(null); //TODO: Thrown Exception Here }
private void _read() { _name = new DomainName(m_io, this, m_root); _type = ((DnsPacket.RecordType)m_io.ReadU2be()); _answerClass = ((DnsPacket.ClassType)m_io.ReadU2be()); _ttl = m_io.ReadS4be(); _rdlength = m_io.ReadU2be(); switch (Type) { case DnsPacket.RecordType.Aaaa: { _rdata = new AaaaRecord(m_io, this, m_root); break; } case DnsPacket.RecordType.A: { _rdata = new ARecord(m_io, this, m_root); break; } case DnsPacket.RecordType.Mx: { _rdata = new MxRecord(m_io, this, m_root); break; } case DnsPacket.RecordType.Cname: { _rdata = new CnameRecord(m_io, this, m_root); break; } case DnsPacket.RecordType.Ns: { _rdata = new NsRecord(m_io, this, m_root); break; } case DnsPacket.RecordType.Ptr: { _rdata = new PtrRecord(m_io, this, m_root); break; } } }
public DnsResourceRecord GetRecord(ResourceRecordInfo info) { if (info == null) { throw new ArgumentNullException(nameof(info)); } var oldIndex = _reader.Index; DnsResourceRecord result; switch (info.RecordType) { case ResourceRecordType.A: result = new ARecord(info, _reader.ReadIPAddress()); break; case ResourceRecordType.NS: result = new NsRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.CNAME: result = new CNameRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.SOA: result = ResolveSoaRecord(info); break; case ResourceRecordType.MB: result = new MbRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.MG: result = new MgRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.MR: result = new MrRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.NULL: result = new NullRecord(info, _reader.ReadBytes(info.RawDataLength).ToArray()); break; case ResourceRecordType.WKS: result = ResolveWksRecord(info); break; case ResourceRecordType.PTR: result = new PtrRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.HINFO: result = new HInfoRecord(info, _reader.ReadString(), _reader.ReadString()); break; case ResourceRecordType.MINFO: result = new MInfoRecord(info, _reader.ReadDnsName(), _reader.ReadDnsName()); break; case ResourceRecordType.MX: result = ResolveMXRecord(info); break; case ResourceRecordType.TXT: result = ResolveTXTRecord(info); break; case ResourceRecordType.RP: result = new RpRecord(info, _reader.ReadDnsName(), _reader.ReadDnsName()); break; case ResourceRecordType.AFSDB: result = new AfsDbRecord(info, (AfsType)_reader.ReadUInt16NetworkOrder(), _reader.ReadDnsName()); break; case ResourceRecordType.AAAA: result = new AaaaRecord(info, _reader.ReadIPv6Address()); break; case ResourceRecordType.SRV: result = ResolveSrvRecord(info); break; case ResourceRecordType.OPT: result = ResolveOptRecord(info); break; case ResourceRecordType.URI: result = ResolveUriRecord(info); break; case ResourceRecordType.CAA: result = ResolveCaaRecord(info); break; default: // update reader index because we don't read full data for the empty record _reader.Index += info.RawDataLength; result = new EmptyRecord(info); break; } // sanity check if (_reader.Index != oldIndex + info.RawDataLength) { throw new InvalidOperationException("Record reader index out of sync."); } return(result); }
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, 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); }
internal static RecordSetData DeserializeRecordSetData(JsonElement element) { Optional <string> etag = default; ResourceIdentifier id = default; string name = default; ResourceType type = default; SystemData systemData = default; Optional <IDictionary <string, string> > metadata = default; Optional <long> ttl = default; Optional <string> fqdn = default; Optional <string> provisioningState = default; Optional <WritableSubResource> targetResource = default; Optional <IList <ARecord> > aRecords = default; Optional <IList <AaaaRecord> > aaaaRecords = default; Optional <IList <MxRecord> > mxRecords = default; Optional <IList <NsRecord> > nsRecords = default; Optional <IList <PtrRecord> > ptrRecords = default; Optional <IList <SrvRecord> > srvRecords = default; Optional <IList <TxtRecord> > txtRecords = default; Optional <CnameRecord> cnameRecord = default; Optional <SoaRecord> soaRecord = default; Optional <IList <CaaRecord> > caaRecords = default; foreach (var property in element.EnumerateObject()) { if (property.NameEquals("etag")) { etag = property.Value.GetString(); continue; } if (property.NameEquals("id")) { id = new ResourceIdentifier(property.Value.GetString()); continue; } if (property.NameEquals("name")) { name = property.Value.GetString(); continue; } if (property.NameEquals("type")) { type = new ResourceType(property.Value.GetString()); continue; } if (property.NameEquals("systemData")) { systemData = JsonSerializer.Deserialize <SystemData>(property.Value.ToString()); continue; } if (property.NameEquals("properties")) { if (property.Value.ValueKind == JsonValueKind.Null) { property.ThrowNonNullablePropertyIsNull(); continue; } foreach (var property0 in property.Value.EnumerateObject()) { if (property0.NameEquals("metadata")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } Dictionary <string, string> dictionary = new Dictionary <string, string>(); foreach (var property1 in property0.Value.EnumerateObject()) { dictionary.Add(property1.Name, property1.Value.GetString()); } metadata = dictionary; continue; } if (property0.NameEquals("TTL")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } ttl = property0.Value.GetInt64(); continue; } if (property0.NameEquals("fqdn")) { fqdn = property0.Value.GetString(); continue; } if (property0.NameEquals("provisioningState")) { provisioningState = property0.Value.GetString(); continue; } if (property0.NameEquals("targetResource")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } targetResource = JsonSerializer.Deserialize <WritableSubResource>(property0.Value.ToString()); continue; } if (property0.NameEquals("ARecords")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } List <ARecord> array = new List <ARecord>(); foreach (var item in property0.Value.EnumerateArray()) { array.Add(ARecord.DeserializeARecord(item)); } aRecords = array; continue; } if (property0.NameEquals("AAAARecords")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } List <AaaaRecord> array = new List <AaaaRecord>(); foreach (var item in property0.Value.EnumerateArray()) { array.Add(AaaaRecord.DeserializeAaaaRecord(item)); } aaaaRecords = array; continue; } if (property0.NameEquals("MXRecords")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } List <MxRecord> array = new List <MxRecord>(); foreach (var item in property0.Value.EnumerateArray()) { array.Add(MxRecord.DeserializeMxRecord(item)); } mxRecords = array; continue; } if (property0.NameEquals("NSRecords")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } List <NsRecord> array = new List <NsRecord>(); foreach (var item in property0.Value.EnumerateArray()) { array.Add(NsRecord.DeserializeNsRecord(item)); } nsRecords = array; continue; } if (property0.NameEquals("PTRRecords")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } List <PtrRecord> array = new List <PtrRecord>(); foreach (var item in property0.Value.EnumerateArray()) { array.Add(PtrRecord.DeserializePtrRecord(item)); } ptrRecords = array; continue; } if (property0.NameEquals("SRVRecords")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } List <SrvRecord> array = new List <SrvRecord>(); foreach (var item in property0.Value.EnumerateArray()) { array.Add(SrvRecord.DeserializeSrvRecord(item)); } srvRecords = array; continue; } if (property0.NameEquals("TXTRecords")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } List <TxtRecord> array = new List <TxtRecord>(); foreach (var item in property0.Value.EnumerateArray()) { array.Add(TxtRecord.DeserializeTxtRecord(item)); } txtRecords = array; continue; } if (property0.NameEquals("CNAMERecord")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } cnameRecord = CnameRecord.DeserializeCnameRecord(property0.Value); continue; } if (property0.NameEquals("SOARecord")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } soaRecord = SoaRecord.DeserializeSoaRecord(property0.Value); continue; } if (property0.NameEquals("caaRecords")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } List <CaaRecord> array = new List <CaaRecord>(); foreach (var item in property0.Value.EnumerateArray()) { array.Add(CaaRecord.DeserializeCaaRecord(item)); } caaRecords = array; continue; } } continue; } } return(new RecordSetData(id, name, type, systemData, etag.Value, Optional.ToDictionary(metadata), Optional.ToNullable(ttl), fqdn.Value, provisioningState.Value, targetResource, Optional.ToList(aRecords), Optional.ToList(aaaaRecords), Optional.ToList(mxRecords), Optional.ToList(nsRecords), Optional.ToList(ptrRecords), Optional.ToList(srvRecords), Optional.ToList(txtRecords), cnameRecord.Value, soaRecord.Value, Optional.ToList(caaRecords))); }
public DnsResourceRecord GetRecord(ResourceRecordInfo info) { if (info == null) { throw new ArgumentNullException(nameof(info)); } var oldIndex = _reader.Index; DnsResourceRecord result; switch (info.RecordType) { case ResourceRecordType.A: result = new ARecord(info, _reader.ReadIPAddress()); break; case ResourceRecordType.NS: result = new NsRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.CNAME: result = new CNameRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.SOA: result = ResolveSoaRecord(info); break; case ResourceRecordType.MB: result = new MbRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.MG: result = new MgRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.MR: result = new MrRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.NULL: result = new NullRecord(info, _reader.ReadBytes(info.RawDataLength).ToArray()); break; case ResourceRecordType.WKS: result = ResolveWksRecord(info); break; case ResourceRecordType.PTR: result = new PtrRecord(info, _reader.ReadDnsName()); break; case ResourceRecordType.HINFO: result = new HInfoRecord(info, _reader.ReadStringWithLengthPrefix(), _reader.ReadStringWithLengthPrefix()); break; case ResourceRecordType.MINFO: result = new MInfoRecord(info, _reader.ReadDnsName(), _reader.ReadDnsName()); break; case ResourceRecordType.MX: result = ResolveMXRecord(info); break; case ResourceRecordType.TXT: result = ResolveTXTRecord(info); break; case ResourceRecordType.RP: result = new RpRecord(info, _reader.ReadDnsName(), _reader.ReadDnsName()); break; case ResourceRecordType.AFSDB: result = new AfsDbRecord(info, (AfsType)_reader.ReadUInt16NetworkOrder(), _reader.ReadDnsName()); break; case ResourceRecordType.AAAA: result = new AaaaRecord(info, _reader.ReadIPv6Address()); break; case ResourceRecordType.SRV: result = ResolveSrvRecord(info); break; case ResourceRecordType.OPT: result = ResolveOptRecord(info); break; case ResourceRecordType.URI: result = ResolveUriRecord(info); break; case ResourceRecordType.CAA: result = ResolveCaaRecord(info); break; case ResourceRecordType.SSHFP: result = ResolveSshfpRecord(info); break; default: result = new UnknownRecord(info, _reader.ReadBytes(info.RawDataLength).ToArray()); break; } // sanity check _reader.SanitizeResult(oldIndex + info.RawDataLength, info.RawDataLength); return(result); }
static string ParseRecord(IDnsQueryResponse dnsQueryResponse, bool isA = false, bool isMx = false) { string output = ""; string value = ""; string ttl = ""; string mxp = ""; foreach (DnsResourceRecord record in dnsQueryResponse.Answers) { switch (record.RecordType) { case ResourceRecordType.MX: { MxRecord mxRecord = record as MxRecord; if (mxRecord != null) { value += mxRecord.Exchange + "\n"; ttl += mxRecord.InitialTimeToLive + "\n"; mxp += mxRecord.Preference + "\n"; // output += $"{mxRecord.Exchange} | TTL {mxRecord.InitialTimeToLive}\n"; } break; } case ResourceRecordType.SOA: { SoaRecord soaRecord = record as SoaRecord; if (soaRecord != null) { value += soaRecord.ToString() + "\n"; ttl += soaRecord.Minimum + "\n"; // output += $"SOA TTL {soaRecord.Minimum}\n"; } break; } case ResourceRecordType.NS: { NsRecord nsRecord = record as NsRecord; if (nsRecord != null) { value += nsRecord.NSDName + "\n"; ttl += nsRecord.InitialTimeToLive + "\n"; // output += $"{nsRecord.NSDName} | TTL {nsRecord.InitialTimeToLive}\n"; } break; } case ResourceRecordType.A: { ARecord nsRecord = record as ARecord; if (nsRecord != null) { return($"{nsRecord.InitialTimeToLive}"); } return("No record"); } case ResourceRecordType.TXT: { TxtRecord txtRecord = record as TxtRecord; if (txtRecord != null) { foreach (string txt in txtRecord.Text) { value += "\"\"" + txt + "\"\"\n"; ttl += txtRecord.InitialTimeToLive + "\n"; // output += $"\"{txt}\" | TTL {txtRecord.InitialTimeToLive}\n"; } } break; } } } if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(ttl)) { if (string.IsNullOrEmpty(mxp)) { output = $"\"{value.Trim()}\",\"{ttl.Trim()}\""; } else { output = $"\"{value.Trim()}\",\"{mxp.Trim()}\",\"{ttl.Trim()}\""; } } else { if (isA) { output = "No record"; } else if (isMx) { output = "No record,No record,No record"; } else { output = "No record,No record"; } } if (string.IsNullOrEmpty(output)) { return("No record\n"); } return(output); }
internal static NsRecordSetData DeserializeNsRecordSetData(JsonElement element) { Optional <string> etag = default; ResourceIdentifier id = default; string name = default; ResourceType type = default; SystemData systemData = default; Optional <IDictionary <string, string> > metadata = default; Optional <long> ttl = default; Optional <string> fqdn = default; Optional <string> provisioningState = default; Optional <WritableSubResource> targetResource = default; Optional <IList <NsRecord> > nsRecords = default; foreach (var property in element.EnumerateObject()) { if (property.NameEquals("etag")) { etag = property.Value.GetString(); continue; } if (property.NameEquals("id")) { id = new ResourceIdentifier(property.Value.GetString()); continue; } if (property.NameEquals("name")) { name = property.Value.GetString(); continue; } if (property.NameEquals("type")) { type = property.Value.GetString(); continue; } if (property.NameEquals("systemData")) { systemData = JsonSerializer.Deserialize <SystemData>(property.Value.ToString()); continue; } if (property.NameEquals("properties")) { if (property.Value.ValueKind == JsonValueKind.Null) { property.ThrowNonNullablePropertyIsNull(); continue; } foreach (var property0 in property.Value.EnumerateObject()) { if (property0.NameEquals("metadata")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } Dictionary <string, string> dictionary = new Dictionary <string, string>(); foreach (var property1 in property0.Value.EnumerateObject()) { dictionary.Add(property1.Name, property1.Value.GetString()); } metadata = dictionary; continue; } if (property0.NameEquals("TTL")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } ttl = property0.Value.GetInt64(); continue; } if (property0.NameEquals("fqdn")) { fqdn = property0.Value.GetString(); continue; } if (property0.NameEquals("provisioningState")) { provisioningState = property0.Value.GetString(); continue; } if (property0.NameEquals("targetResource")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } targetResource = JsonSerializer.Deserialize <WritableSubResource>(property0.Value.ToString()); continue; } if (property0.NameEquals("NSRecords")) { if (property0.Value.ValueKind == JsonValueKind.Null) { property0.ThrowNonNullablePropertyIsNull(); continue; } List <NsRecord> array = new List <NsRecord>(); foreach (var item in property0.Value.EnumerateArray()) { array.Add(NsRecord.DeserializeNsRecord(item)); } nsRecords = array; continue; } } continue; } } return(new NsRecordSetData(id, name, type, systemData, etag.Value, Optional.ToDictionary(metadata), Optional.ToNullable(ttl), fqdn.Value, provisioningState.Value, targetResource, Optional.ToList(nsRecords))); }
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); }
internal virtual void AssertEquality(NsRecord expected, NsRecord actual) { Assert.AreEqual(expected.NsDomainName, actual.NsDomainName, "Should be equal"); }
public override void ExecuteCmdlet() { DnsRecordBase result = null; switch (this.ParameterSetName) { case ParameterSetA: { result = new ARecord { Ipv4Address = this.Ipv4Address }; break; } case ParameterSetAaaa: { result = new AaaaRecord { Ipv6Address = this.Ipv6Address }; break; } case ParameterSetMx: { result = new MxRecord { Preference = this.Preference, Exchange = this.Exchange }; break; } case ParameterSetNs: { result = new NsRecord { Nsdname = this.Nsdname }; break; } case ParameterSetSrv: { result = new SrvRecord { Priority = this.Priority, Port = this.Port, Target = this.Target, Weight = this.Weight }; break; } case ParameterSetTxt: { result = new TxtRecord { Value = this.Value }; break; } case ParameterSetCName: { result = new CnameRecord { Cname = this.Cname }; break; } case ParameterSetPtr: { result = new PtrRecord {Ptrdname = this.Ptrdname}; break; } default: { throw new PSArgumentException(string.Format(ProjectResources.Error_UnknownParameterSetName, this.ParameterSetName)); } } WriteObject(result); }