Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        public static async void ReturnContext(HttpContext context, bool returnMsg, DnsMessage dnsMsg)
        {
            var queryDictionary = context.Request.Query;

            if (dnsMsg == null)
            {
                context.Response.StatusCode = StatusCodes.Status500InternalServerError;
                await context.Response.WriteAsync("Remote DNS server timeout");

                return;
            }

            if (returnMsg)
            {
                if (queryDictionary.ContainsKey("ct") && queryDictionary["ct"].ToString().Contains("json"))
                {
                    context.Response.ContentType = "application/json";
                    await context.Response.WriteAsync(DohJsonEncoder.Encode(dnsMsg).ToString(Formatting.None));
                }
                else
                {
                    context.Response.ContentType = "application/dns-message";
                    await context.Response.Body.WriteAsync(DnsEncoder.Encode(dnsMsg));
                }
            }
            else
            {
                if (queryDictionary.ContainsKey("ct") && queryDictionary["ct"].ToString().Contains("message"))
                {
                    context.Response.ContentType = "application/dns-message";
                    await context.Response.Body.WriteAsync(DnsEncoder.Encode(dnsMsg));
                }
                else
                {
                    context.Response.ContentType = "application/json";
                    await context.Response.WriteAsync(DohJsonEncoder.Encode(dnsMsg).ToString(Formatting.None));
                }
            }

            WriteLogCache(dnsMsg, context);
        }