public async Task <DnsResult> GetDnsTxtRecord(string domain, string dnsServer) { DnsResult result = new DnsResult(); var dns = new MyDns(); if (!await dns.Search(domain, QueryType.TXT, dnsServer, null)) { result.Success = false; result.RecordValue = string.Empty; switch (dns.header.ResultCode) { case ResultCode.OK: result.ErrorMessage = "ok"; break; case ResultCode.FormatErr: result.ErrorMessage = "格式错误"; break; case ResultCode.DNSErr: result.ErrorMessage = "DNS出错"; break; case ResultCode.DomainNoExist: result.ErrorMessage = "域名不存在"; break; case ResultCode.DNSNoSuppot: result.ErrorMessage = "DNS不支持这类查询"; break; case ResultCode.DNSRefuse: result.ErrorMessage = "DNS拒绝查询"; break; default: result.ErrorMessage = "未知错误"; break; } return(result); } else { result.Success = true; result.RecordValue = dns.record?.Records?.FirstOrDefault()?.RDDate?.ToString(); } return(result); }
public void Parse(byte[] data, int offset) { while (offset < data.Length) { int labelLen; MyDnsRecord RecordItem = new MyDnsRecord(); RecordItem.Name = MyDns.GetLabelName(data, offset, out labelLen); offset += labelLen; // offset++; RecordItem.QType = (QueryType)data[++offset]; // offset++; RecordItem.QClass = (QueryClass)data[++offset]; // offset++; RecordItem.TTL = data[offset++] * 256 * 256 * 256 + data[offset++] * 256 * 256 + data[offset++] * 256 + data[offset++]; // RecordItem.RDLength = data[offset++] * 256 + data[offset++]; // switch (RecordItem.QType) { case QueryType.A: RecordItem.RDDate = new A_RR(data, offset, RecordItem.RDLength); break; case QueryType.CNAME: RecordItem.RDDate = new CNAME_RR(data, offset, RecordItem.RDLength); break; case QueryType.MX: RecordItem.RDDate = new MX_RR(data, offset, RecordItem.RDLength); break; case QueryType.NS: RecordItem.RDDate = new NS_RR(data, offset, RecordItem.RDLength); break; case QueryType.SOA: RecordItem.RDDate = new SOA_RR(data, offset, RecordItem.RDLength); break; case QueryType.TXT: RecordItem.RDDate = new TXT_RR(data, offset, RecordItem.RDLength); break; } Records.Add(RecordItem); offset += RecordItem.RDLength; } }