public static async Task ReturnContext(HttpContext context, bool returnMsg, DnsMessage dnsMsg, bool cache = true, bool transIdEnable = false, ushort id = 0) { try { var queryDictionary = context.Request.Query; if (dnsMsg == null) { await context.WriteResponseAsync("Remote DNS server timeout", StatusCodes.Status500InternalServerError); return; } if (returnMsg) { if (GetClientType(queryDictionary, "json")) { await context.WriteResponseAsync(DnsJsonEncoder.Encode(dnsMsg).ToString(Formatting.None), type : "application/json", headers : Startup.HeaderDict); } else { await context.WriteResponseAsync( DnsEncoder.Encode(dnsMsg, transIdEnable, id), type : "application/dns-message"); } } else { if (GetClientType(queryDictionary, "message")) { await context.WriteResponseAsync( DnsEncoder.Encode(dnsMsg, transIdEnable, id), type : "application/dns-message"); } else { await context.WriteResponseAsync(DnsJsonEncoder.Encode(dnsMsg).ToString(Formatting.None), type : "application/json", headers : Startup.HeaderDict); } } if (cache) { WriteLogCache(dnsMsg, context); } } catch (Exception e) { Console.WriteLine(e); } }
public static async Task <byte[]> DnsMsgToBytes(DnsMessage dnsMsg) { try { await using var memoryStream = new MemoryStream(); ReadFromDnsMessage(dnsMsg).WriteToUdp(memoryStream); return(memoryStream.ToArray()); } catch (Exception e) { Console.WriteLine(e); await using var memoryStream = new MemoryStream(); ReadFromJson(DnsJsonEncoder.Encode(dnsMsg)).WriteToUdp(memoryStream); return(memoryStream.ToArray()); } }