Example #1
0
        public static DnsMessage FromDnsJson(HttpContext context, bool ActiveEcs = true, byte EcsDefaultMask = 24)
        {
            var queryDictionary = context.Request.Query;
            var dnsQuestion     = new DnsQuestion(DomainName.Parse(queryDictionary["name"]), RecordType.A,
                                                  RecordClass.INet);

            if (queryDictionary.ContainsKey("type"))
            {
                if (Enum.TryParse(queryDictionary["type"], true, out RecordType rType))
                {
                    dnsQuestion = new DnsQuestion(DomainName.Parse(queryDictionary["name"]), rType,
                                                  RecordClass.INet);
                }
            }

            var dnsQMsg = new DnsMessage
            {
                IsEDnsEnabled      = true,
                IsQuery            = true,
                IsRecursionAllowed = true,
                IsRecursionDesired = true,
                TransactionID      = Convert.ToUInt16(new Random(DateTime.Now.Millisecond).Next(1, 99))
            };

            dnsQMsg.Questions.Add(dnsQuestion);
            if (!Config.EcsEnable || !ActiveEcs || queryDictionary.ContainsKey("no-ecs"))
            {
                return(dnsQMsg);
            }

            if (queryDictionary.ContainsKey("edns_client_subnet"))
            {
                var ipStr     = queryDictionary["edns_client_subnet"].ToString();
                var ipNetwork = ipStr.Contains("/")
                    ? IPNetwork.Parse(ipStr)
                    : IPNetwork.Parse(ipStr, EcsDefaultMask);
                dnsQMsg.EDnsOptions.Options.Add(new ClientSubnetOption(
                                                    Equals(ipNetwork.Network, IPAddress.Any) ? (byte)0 : ipNetwork.Cidr, ipNetwork.Network));
            }
            else
            {
                var realIp = RealIP.Get(context);
                if (!Equals(realIp, IPAddress.Loopback))
                {
                    dnsQMsg.EDnsOptions.Options.Add(
                        new ClientSubnetOption(EcsDefaultMask,
                                               IPNetwork.Parse(realIp.ToString(), EcsDefaultMask).Network));
                }
            }

            return(dnsQMsg);
        }
Example #2
0
        public static DnsMessage FromWebBase64(HttpContext context, bool ActiveEcs = true)
        {
            var queryDictionary = context.Request.Query;
            var msg             = FromWebBase64(queryDictionary["dns"].ToString());

            if (!Config.EcsEnable || !ActiveEcs)
            {
                return(msg);
            }
            if (IsEcsEnable(msg))
            {
                return(msg);
            }
            if (!msg.IsEDnsEnabled)
            {
                msg.IsEDnsEnabled = true;
            }
            msg.EDnsOptions.Options.Add(new ClientSubnetOption(24, IPNetwork.Parse(RealIP.Get(context).ToString(), 24).Network));
            return(msg);
        }
Example #3
0
        public static DnsMessage FromWebBase64(HttpContext context, bool ActiveEcs = true, byte EcsDefaultMask = 24)
        {
            var msg = FromWebBase64(context.Request.Query["dns"].ToString());

            if (!Config.EcsEnable || !ActiveEcs || context.Request.Query.ContainsKey("no-ecs"))
            {
                return(msg);
            }
            if (IsEcsEnable(msg))
            {
                return(msg);
            }
            if (!msg.IsEDnsEnabled)
            {
                msg.IsEDnsEnabled = true;
            }
            msg.EDnsOptions.Options.Add(new ClientSubnetOption(EcsDefaultMask,
                                                               IPNetwork.Parse(RealIP.Get(context).ToString(), EcsDefaultMask).Network));
            return(msg);
        }
Example #4
0
        public static async Task <DnsMessage> FromPostByteAsync(HttpContext context, bool ActiveEcs = true,
                                                                byte EcsDefaultMask = 24)
        {
            var msg = DnsMessage.Parse((await context.Request.BodyReader.ReadAsync()).Buffer.ToArray());

            if (!Config.EcsEnable || !ActiveEcs || context.Request.Query.ContainsKey("no-ecs"))
            {
                return(msg);
            }
            if (IsEcsEnable(msg))
            {
                return(msg);
            }
            if (!msg.IsEDnsEnabled)
            {
                msg.IsEDnsEnabled = true;
            }
            msg.EDnsOptions.Options.Add(new ClientSubnetOption(EcsDefaultMask,
                                                               IPNetwork.Parse(RealIP.Get(context).ToString(), EcsDefaultMask).Network));
            return(msg);
        }