Example #1
0
        public static void Add(DnsMessage dnsMessage, HttpContext context)
        {
            if (dnsMessage.AnswerRecords.Count <= 0)
            {
                return;
            }
            var dnsRecordBase = dnsMessage.AnswerRecords.FirstOrDefault();

            if (RealIP.TryGetFromDns(dnsMessage, out var ipAddress))
            {
                Add(new CacheItem(
                        $"DNS:{GeoIP.GetGeoStr(ipAddress)}{dnsRecordBase.Name}:{dnsRecordBase.RecordType}",
                        new CacheEntity
                {
                    List        = dnsMessage.AnswerRecords.ToList(),
                    Time        = DateTime.Now,
                    ExpiresTime = DateTime.Now.AddSeconds(dnsRecordBase.TimeToLive)
                }),
                    dnsRecordBase.TimeToLive);
            }
            else
            {
                Add(new CacheItem($"DNS:{dnsRecordBase.Name}:{dnsRecordBase.RecordType}",
                                  new CacheEntity
                {
                    List        = dnsMessage.AnswerRecords.ToList(),
                    Time        = DateTime.Now,
                    ExpiresTime = DateTime.Now.AddSeconds(dnsRecordBase.TimeToLive)
                }),
                    dnsRecordBase.TimeToLive);
            }
        }
Example #2
0
 public static bool Contains(DnsMessage dnsQMsg, HttpContext context = null)
 {
     return(context == null
         ? MemoryCache.Default.Contains(
                $"DNS:{dnsQMsg.Questions.FirstOrDefault().Name}:{dnsQMsg.Questions.FirstOrDefault().RecordType}")
         : MemoryCache.Default.Contains(
                $"DNS:{GeoIP.GetGeoStr(RealIP.GetFromDns(dnsQMsg, context))}{dnsQMsg.Questions.FirstOrDefault().Name}:{dnsQMsg.Questions.FirstOrDefault().RecordType}"));
 }
Example #3
0
        public static DnsMessage Get(DnsMessage dnsQMessage, HttpContext context = null)
        {
            var dCacheMsg = new DnsMessage
            {
                IsRecursionAllowed = true,
                IsRecursionDesired = true,
                TransactionID      = dnsQMessage.TransactionID
            };
            var getName = context != null
                ? $"DNS:{GeoIP.GetGeoStr(RealIP.GetFromDns(dnsQMessage, context))}{dnsQMessage.Questions.FirstOrDefault().Name}:{dnsQMessage.Questions.FirstOrDefault().RecordType}"
                : $"DNS:{dnsQMessage.Questions.FirstOrDefault().Name}:{dnsQMessage.Questions.FirstOrDefault().RecordType}";
            var cacheEntity = Get(getName);

            foreach (var item in cacheEntity.List)
            {
                if (item is ARecord aRecord)
                {
                    dCacheMsg.AnswerRecords.Add(new ARecord(aRecord.Name,
                                                            Convert.ToInt32((cacheEntity.Time.AddSeconds(item.TimeToLive) - DateTime.Now)
                                                                            .TotalSeconds), aRecord.Address));
                }
                else if (item is AaaaRecord aaaaRecord)
                {
                    dCacheMsg.AnswerRecords.Add(new AaaaRecord(aaaaRecord.Name,
                                                               Convert.ToInt32((cacheEntity.Time.AddSeconds(item.TimeToLive) - DateTime.Now)
                                                                               .TotalSeconds), aaaaRecord.Address));
                }
                else if (item is CNameRecord cNameRecord)
                {
                    dCacheMsg.AnswerRecords.Add(new CNameRecord(cNameRecord.Name,
                                                                Convert.ToInt32((cacheEntity.Time.AddSeconds(item.TimeToLive) - DateTime.Now)
                                                                                .TotalSeconds), cNameRecord.CanonicalName));
                }
                else
                {
                    dCacheMsg.AnswerRecords.Add(item);
                }
            }

            dCacheMsg.Questions.AddRange(dnsQMessage.Questions);
            dCacheMsg.AnswerRecords.Add(new TxtRecord(DomainName.Parse("cache.arashi-msg"), 0,
                                                      cacheEntity.ExpiresTime.ToString("r")));
            return(dCacheMsg);
        }
Example #4
0
        public static void Add(DnsMessage dnsMessage, HttpContext context)
        {
            if (dnsMessage.AnswerRecords.Count <= 0)
            {
                return;
            }
            var dnsRecordBase = dnsMessage.AnswerRecords.FirstOrDefault();

            if (RealIP.TryGetFromDns(dnsMessage, out var ipAddress))
            {
                Add(new CacheItem(
                        $"{GeoIP.GetGeoStr(ipAddress)}:{dnsRecordBase.Name}:{dnsRecordBase.RecordType}",
                        dnsMessage.AnswerRecords.ToList()), dnsRecordBase.TimeToLive);
            }
            else
            {
                Add(new CacheItem($"{dnsRecordBase.Name}:{dnsRecordBase.RecordType}",
                                  dnsMessage.AnswerRecords.ToList()), dnsRecordBase.TimeToLive);
            }
        }
Example #5
0
        public static void Add(DnsMessage dnsMessage, HttpContext context)
        {
            foreach (var item in new List <DnsRecordBase>(dnsMessage.AnswerRecords).Where(item =>
                                                                                          (item.Name.IsSubDomainOf(DomainName.Parse("arashi-msg")) ||
                                                                                           item.Name.IsSubDomainOf(DomainName.Parse("nova-msg"))) && item.RecordType == RecordType.Txt))
            {
                dnsMessage.AnswerRecords.Remove(item);
            }

            if (dnsMessage.AnswerRecords.Count <= 0)
            {
                return;
            }
            var dnsRecordBase = dnsMessage.AnswerRecords.FirstOrDefault();

            if (RealIP.TryGetFromDns(dnsMessage, out var ipAddress))
            {
                Add(new CacheItem(
                        $"DNS:{GeoIP.GetGeoStr(ipAddress)}{dnsRecordBase.Name}:{dnsRecordBase.RecordType}",
                        new CacheEntity
                {
                    List        = dnsMessage.AnswerRecords.ToList(),
                    Time        = DateTime.Now,
                    ExpiresTime = DateTime.Now.AddSeconds(dnsRecordBase.TimeToLive)
                }),
                    dnsRecordBase.TimeToLive);
            }
            else
            {
                Add(new CacheItem($"DNS:{dnsRecordBase.Name}:{dnsRecordBase.RecordType}",
                                  new CacheEntity
                {
                    List        = dnsMessage.AnswerRecords.ToList(),
                    Time        = DateTime.Now,
                    ExpiresTime = DateTime.Now.AddSeconds(dnsRecordBase.TimeToLive)
                }),
                    dnsRecordBase.TimeToLive);
            }
        }
Example #6
0
        public static DnsMessage Get(DnsMessage dnsQMessage, HttpContext context = null)
        {
            var dCacheMsg = new DnsMessage
            {
                IsRecursionAllowed = true,
                IsRecursionDesired = true,
                TransactionID      = dnsQMessage.TransactionID
            };

            if (context != null)
            {
                dCacheMsg.AnswerRecords.AddRange(Get(
                                                     $"{GeoIP.GetGeoStr(RealIP.GetFromDns(dnsQMessage, context))}{dnsQMessage.Questions.FirstOrDefault().Name}:{dnsQMessage.Questions.FirstOrDefault().RecordType}"));
            }
            else
            {
                dCacheMsg.AnswerRecords.AddRange(Get(
                                                     $"{dnsQMessage.Questions.FirstOrDefault().Name}:{dnsQMessage.Questions.FirstOrDefault().RecordType}"));
            }
            dCacheMsg.Questions.AddRange(dnsQMessage.Questions);
            dCacheMsg.AnswerRecords.Add(new TxtRecord(DomainName.Parse("cache.doh.pp.ua"), 0,
                                                      "ArashiDNS.P Cached"));
            return(dCacheMsg);
        }