예제 #1
0
        void Server_OnResponse(object sender, DNSEventArgs e)
        {
            var rs = e.Response;

            if (rs == null)
            {
                return;
            }

            var remote = e.Session?.Remote;
            var rq     = rs.Questions[0];

            // 记录历史
            var hi = new History();

            hi.Type = (Int32)rq.Type;
            hi.Name = rq.Name;
            if (remote != null)
            {
                hi.UserIP       = remote.EndPoint.Address + "";
                hi.ProtocolType = remote.Type;
            }
            if (rs.Answers != null && rs.Answers.Length > 0)
            {
                //entity.Address = rs.Answers[0].Text;

                foreach (var item in rs.Answers)
                {
                    //var dr = hi.CloneEntity(true);
                    var dr = new History();
                    dr.UserIP       = hi.UserIP;
                    dr.ProtocolType = hi.ProtocolType;
                    dr.Type         = (Int32)item.Type;
                    dr.Name         = item.Name;
                    dr.Address      = item.Text;
                    dr.SaveAsync();
                }
            }
            else
            {
                hi.SaveAsync();
            }

            // 记录访问者
            var vt = Visitor.Check(remote?.Host);

            if (vt != null)
            {
                vt.LastDomainName = rq.Name;
                vt.Hits++;
                vt.LastVisit = DateTime.Now;
                // 单对象缓存会自动保存
                vt.SaveAsync();
            }
        }
예제 #2
0
        void Server_OnNew(object sender, DNSEventArgs e)
        {
            var rs = e.Response;

            if (rs == null)
            {
                return;
            }

            var list = new List <DNSRecord>();

            if (rs.Answers != null)
            {
                list.AddRange(rs.Answers);
            }
            if (rs.Authoritis != null)
            {
                list.AddRange(rs.Authoritis);
            }
            if (rs.Additionals != null)
            {
                list.AddRange(rs.Additionals);
            }

            var rq = rs.Questions[0];

            var now = DateTime.Now;

            foreach (var dr in list)
            {
                var entity = Record.FindByQueryTypeAndNameAndAddress((Int32)dr.Type, rq.Name, dr.Text);
                if (entity == null)
                {
                    entity         = new Record();
                    entity.Name    = rq.Name;
                    entity.Type    = (Int32)dr.Type;
                    entity.Address = dr.Text;
                }

                entity.TTL        = now.Add(dr.TTL);
                entity.Parent     = e.Session.Remote + "";
                entity.UpdateTime = DateTime.Now;

                entity.SaveAsync();
            }
        }
예제 #3
0
        void Server_OnRequest(object sender, DNSEventArgs e)
        {
            var dns = e.Request;

            if (dns == null)
            {
                return;
            }

            // 查询规则
            var rs = CheckRule(dns);

            // 查询记录
            if (rs == null)
            {
                rs = CheckRecord(dns);
            }

            if (rs != null)
            {
                e.Response = rs;
            }
        }