Exemplo n.º 1
0
        DNSEntity GetDNS(String key, DNSEntity request)
        {
            // 批量请求父级代理
            var dic = DNSClient.QueryAll(_Clients, request);

            if (dic.Count == 0)
            {
                return(null);
            }

            DNSEntity rs = null;

            foreach (var item in dic)
            {
                rs = item.Value;
                var nc = item.Key.Client;

                WriteLog("{0} GetDNS {1}", nc.Remote, rs);

                if (OnNew != null)
                {
                    var e = new DNSEventArgs {
                        Request = request, Response = item.Value, Session = nc
                    };
                    OnNew(this, e);
                }
            }

            return(rs);
        }
Exemplo n.º 2
0
        /// <summary>处理响应</summary>
        /// <param name="session"></param>
        /// <param name="request"></param>
        /// <param name="response"></param>
        protected virtual void Response(ISocketSession session, DNSEntity request, DNSEntity response)
        {
            var isTcp = session.Local.IsTcp;

            if (OnResponse != null)
            {
                var e = new DNSEventArgs {
                    Request = request, Response = response, Session = session
                };
                OnResponse(this, e);
            }

            if (session != null && !session.Disposed)
            {
                session.Send(response.GetStream(isTcp));
            }
        }
Exemplo n.º 3
0
        /// <summary>处理响应</summary>
        /// <param name="session"></param>
        /// <param name="request"></param>
        /// <param name="response"></param>
        protected virtual void Response(INetSession session, DNSEntity request, DNSEntity response)
        {
            var ss = session?.Session;

            if (ss == null)
            {
                return;
            }

            var isTcp = ss.Local.IsTcp;

            if (OnResponse != null)
            {
                var e = new DNSEventArgs {
                    Request = request, Response = response, Session = ss
                };
                OnResponse(this, e);
            }

            session?.Send(response.GetStream(isTcp));
        }
Exemplo n.º 4
0
        /// <summary>处理请求</summary>
        /// <param name="session"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual DNSEntity Request(INetSession session, DNSEntity request)
        {
            var local = session.Session.Local;
            var isTcp = local.IsTcp;

            // 处理,修改
            WriteLog("{0} 请求 {1}", session.Session.Remote, request);

            // 请求事件,如果第二参数有值,则直接返回
            // 结合数据库缓存,可以在这里进行返回
            if (OnRequest != null)
            {
                var e = new DNSEventArgs
                {
                    Request = request
                };
                OnRequest(this, e);
                if (e.Response != null)
                {
                    return(e.Response);
                }
            }

            // 如果是PTR请求
            var rq = request.Questions[0];

            if (rq.Type == DNSQueryType.PTR)
            {
                var ptr = RequestPTR(request);
                if (ptr != null)
                {
                    return(ptr);
                }
            }

            // 读取缓存
            //var rs = cache.GetItem(request.ToString(), k => GetDNS(k, request));
            var key = request.ToString();
            var rs  = cache[key];

            if (rs == null)
            {
                cache[key] = rs = GetDNS(key, request);
            }

            // 返回给客户端
            if (rs != null)
            {
                // 如果是PTR请求
                if (rq.Type == DNSQueryType.PTR && rs.Questions[0].Type == DNSQueryType.PTR)
                {
                    var ptr = rq as DNS_PTR;
                    if (rs.GetAnswer() is DNS_PTR ptr2)
                    {
                        ptr2.Name       = ptr.Name;
                        ptr2.DomainName = DomainName;
                    }
                    if (rs.Answers != null && rs.Answers.Length > 0)
                    {
                        foreach (var item in rs.Answers)
                        {
                            if (item.Type == DNSQueryType.PTR)
                            {
                                item.Name = ptr.Name;
                            }
                        }
                    }
                }
            }

            return(rs);
        }
Exemplo n.º 5
0
        DNSEntity GetDNS(String key, DNSEntity request)
        {
            // 请求父级代理
            NetUri parent = null;

            Byte[]        data   = null;
            ISocketClient client = null;

            NetUri[] us = null;
            lock (Parents) { us = Parents.ToArray(); }

            foreach (var item in us)
            {
                WriteLog("GetDNS key={0} {1}", key, item);
                client = item.CreateRemote();
                // 如果是PTR请求
                if (request.IsPTR)
                {
                    // 复制一份,防止修改外部
                    request = new DNSEntity().CloneFrom(request);

                    var ptr = request.GetAnswer(true) as DNS_PTR;
                    if (ptr != null)
                    {
                        ptr.Address = item.Address;
                    }
                }

                try
                {
                    client.Timeout = 1000;
                    client.Send(request.GetStream(item.IsTcp));
                    data = client.Receive();

                    if (data != null && data.Length > 0)
                    {
                        parent = item;
                        break;
                    }
                }
                catch { }
            }
            if (data == null || data.Length < 1)
            {
                return(null);
            }

            // 调到第一位
            if (Parents.Count > 1 && Parents[0] != parent)
            {
                lock (Parents)
                {
                    if (Parents.Count > 1 && Parents[0] != parent)
                    {
                        Parents.Remove(parent);
                        Parents.Insert(0, parent);
                    }
                }
            }

            DNSEntity response = null;

            try
            {
                // 解析父级代理返回的数据
                response = DNSEntity.Read(data, parent.IsTcp);

                // 处理,修改
                WriteLog("{0} 返回 {1}", parent, response);
            }
            catch (Exception ex)
            {
                String file = String.Format("dns_{0:MMddHHmmss}.bin", DateTime.Now);
                XTrace.WriteLine("解析父级代理返回数据出错!数据保存于" + file + "。" + ex.Message);
                File.WriteAllBytes(file, data);
            }

            if (OnNew != null)
            {
                var e = new DNSEventArgs {
                    Request = request, Response = response, Session = client
                };
                OnNew(this, e);
            }

            return(response);
        }
Exemplo n.º 6
0
        /// <summary>处理请求</summary>
        /// <param name="session"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual DNSEntity Request(ISocketSession session, DNSEntity request)
        {
            var isTcp = session.Local.IsTcp;

            // 处理,修改
            WriteLog("{0} 请求 {1}", session.Local.Type, request);

            // 请求事件,如果第二参数有值,则直接返回
            // 结合数据库缓存,可以在这里进行返回
            if (OnRequest != null)
            {
                var e = new DNSEventArgs();
                e.Request = request;
                OnRequest(this, e);
                if (e.Response != null)
                {
                    return(e.Response);
                }
            }

            // 如果是PTR请求
            if (request.IsPTR)
            {
                var ptr = RequestPTR(request);
                if (ptr != null)
                {
                    return(ptr);
                }
            }

            // 读取缓存
            var rs = cache.GetItem <DNSEntity>(request.ToString(), request, GetDNS);

            // 返回给客户端
            if (rs != null)
            {
                // 如果是PTR请求
                if (request.IsPTR && rs.IsPTR)
                {
                    var ptr  = request.Questions[0] as DNS_PTR;
                    var ptr2 = rs.GetAnswer() as DNS_PTR;
                    if (ptr2 != null)
                    {
                        ptr2.Name       = ptr.Name;
                        ptr2.DomainName = DomainName;
                    }
                    if (rs.Answers != null && rs.Answers.Length > 0)
                    {
                        foreach (var item in rs.Answers)
                        {
                            if (item.Type == DNSQueryType.PTR)
                            {
                                item.Name = ptr.Name;
                            }
                        }
                    }
                }
            }

            return(rs);
        }