diff --git a/Benchmark/Benchmark.csproj b/Benchmark/Benchmark.csproj index bdd47b3..fd63373 100644 --- a/Benchmark/Benchmark.csproj +++ b/Benchmark/Benchmark.csproj @@ -16,7 +16,7 @@ - + diff --git a/EchoAgent/EchoAgent.csproj b/EchoAgent/EchoAgent.csproj index c575a62..9aa6618 100644 --- a/EchoAgent/EchoAgent.csproj +++ b/EchoAgent/EchoAgent.csproj @@ -14,8 +14,8 @@ - - + + diff --git a/EchoTest/EchoTest.csproj b/EchoTest/EchoTest.csproj index 9f655f4..d9027c3 100644 --- a/EchoTest/EchoTest.csproj +++ b/EchoTest/EchoTest.csproj @@ -14,7 +14,7 @@ - + diff --git a/HandlerTest/HandlerTest.csproj b/HandlerTest/HandlerTest.csproj index 2566a94..0f6230b 100644 --- a/HandlerTest/HandlerTest.csproj +++ b/HandlerTest/HandlerTest.csproj @@ -14,7 +14,7 @@ - + diff --git a/NewLife.Net/DNS/DNSServer.cs b/NewLife.Net/DNS/DNSServer.cs index e9021e6..96ee7c0 100644 --- a/NewLife.Net/DNS/DNSServer.cs +++ b/NewLife.Net/DNS/DNSServer.cs @@ -1,320 +1,315 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using NewLife.Caching; -using NewLife.Collections; +using NewLife.Caching; using NewLife.Data; using NewLife.Log; #if !NET40 using TaskEx = System.Threading.Tasks.Task; #endif -namespace NewLife.Net.DNS +namespace NewLife.Net.DNS; + +/// DNS服务器 +public class DNSServer : NetServer { - /// DNS服务器 - public class DNSServer : NetServer - { - #region 属性 - /// 域名 - public String DomainName { get; set; } + #region 属性 + /// 域名 + public String DomainName { get; set; } - /// 上级DNS地址 - public List Parents { get; set; } = new List(); - #endregion + /// 上级DNS地址 + public List Parents { get; set; } = new List(); + #endregion - #region 构造 - /// 实例化一个DNS服务器 - public DNSServer() - { - //Name = "DNS"; - Port = 53; + #region 构造 + /// 实例化一个DNS服务器 + public DNSServer() + { + //Name = "DNS"; + Port = 53; - DomainName = "dns.NewLifeX.com"; + DomainName = "dns.NewLifeX.com"; - SocketLog = Logger.Null; - SessionLog = Logger.Null; - } - #endregion + SocketLog = Logger.Null; + SessionLog = Logger.Null; + } + #endregion - #region 父级DNS - /// 获取本机DNS列表 - /// - public virtual List GetLocalDNS() + #region 父级DNS + /// 获取本机DNS列表 + /// + public virtual List GetLocalDNS() + { + var list = new List(); + foreach (var item in NetHelper.GetDns()) { - var list = new List(); - foreach (var item in NetHelper.GetDns()) - { - if (!item.IsIPv4()) continue; + if (!item.IsIPv4()) continue; - if (item.IsAny()) - { - WriteLog("取得的本地DNS[{0}]有误,任意地址不能作为父级DNS地址。", item); - continue; - } - var uri = new NetUri(NetType.Udp, item, 53); - WriteLog("使用本地地址作为父级DNS:{0}", uri); - list.Add(uri); + if (item.IsAny()) + { + WriteLog("取得的本地DNS[{0}]有误,任意地址不能作为父级DNS地址。", item); + continue; } - - return list; + var uri = new NetUri(NetType.Udp, item, 53); + WriteLog("使用本地地址作为父级DNS:{0}", uri); + list.Add(uri); } - /// 设置父级DNS - /// - public virtual void SetParents(String parents) - { - var ss = parents.Split(","); - if (ss == null || ss.Length < 1) return; + return list; + } + + /// 设置父级DNS + /// + public virtual void SetParents(String parents) + { + var ss = parents.Split(","); + if (ss == null || ss.Length < 1) return; - var ps = Parents; - var list = new HashSet(ps.Select(p => p.ToString()), StringComparer.OrdinalIgnoreCase); - //ps.Clear(); + var ps = Parents; + var list = new HashSet(ps.Select(p => p.ToString()), StringComparer.OrdinalIgnoreCase); + //ps.Clear(); - for (var i = ss.Length - 1; i >= 0; i--) + for (var i = ss.Length - 1; i >= 0; i--) + { + try { - try + var uri = new NetUri(ss[i]); + if (uri.Port <= 0) uri.Port = 53; + if (!list.Contains(uri.ToString())) { - var uri = new NetUri(ss[i]); - if (uri.Port <= 0) uri.Port = 53; - if (!list.Contains(uri.ToString())) + if (uri.Address.IsAny()) { - if (uri.Address.IsAny()) - { - WriteLog("配置的父级DNS[{0}]有误,任意地址不能作为父级DNS地址。", uri); - continue; - } - ps.Insert(0, uri); - list.Add(uri.ToString()); + WriteLog("配置的父级DNS[{0}]有误,任意地址不能作为父级DNS地址。", uri); + continue; } + ps.Insert(0, uri); + list.Add(uri.ToString()); } - catch (Exception ex) - { - WriteLog("配置的父级DNS[{0}]有误,{1}", ss[i], ex.Message); - } + } + catch (Exception ex) + { + WriteLog("配置的父级DNS[{0}]有误,{1}", ss[i], ex.Message); } } + } - private List _Clients; - #endregion + private List _Clients; + #endregion - #region 方法 - /// 启动服务 - protected override void OnStart() - { - // 如果没有设置父级DNS,则使用本地DNS - var ps = Parents; - if (ps.Count == 0) ps.AddRange(GetLocalDNS()); + #region 方法 + /// 启动服务 + protected override void OnStart() + { + // 如果没有设置父级DNS,则使用本地DNS + var ps = Parents; + if (ps.Count == 0) ps.AddRange(GetLocalDNS()); - base.OnStart(); + base.OnStart(); - // 准备连接 - _Clients = new List(); - foreach (var item in Parents.ToArray()) + // 准备连接 + _Clients = new List(); + foreach (var item in Parents.ToArray()) + { + var nc = new DNSClient(item); + TaskEx.Run(() => { - var nc = new DNSClient(item); - TaskEx.Run(() => + if (nc.Open()) { - if (nc.Open()) - { - WriteLog("已连接父级DNS:{0}", nc.Client.Remote); - lock (_Clients) { _Clients.Add(nc); } - } - }); - } + WriteLog("已连接父级DNS:{0}", nc.Client.Remote); + lock (_Clients) { _Clients.Add(nc); } + } + }); } + } - /// 停止服务 - protected override void OnStop() - { - base.OnStop(); - - _Clients.TryDispose(); - _Clients = null; - } + /// 停止服务 + /// + protected override void OnStop(String reason) + { + base.OnStop(reason); - readonly ICache _cache = new MemoryCache { Expire = 600/*, Asynchronous = true, CacheDefault = false*/ }; + _Clients.TryDispose(); + _Clients = null; + } - /// 接收处理 - /// - /// - protected override void OnReceive(INetSession session, Packet pk) - { - var isTcp = session.Session.Local.IsTcp; + readonly ICache _cache = new MemoryCache { Expire = 600/*, Asynchronous = true, CacheDefault = false*/ }; - // 解析 - var request = DNSEntity.Read(pk.GetStream(), isTcp); + /// 接收处理 + /// + /// + protected override void OnReceive(INetSession session, Packet pk) + { + var isTcp = session.Session.Local.IsTcp; - var response = Request(session, request); - if (response != null) - { - response.Header.ID = request.Header.ID; - Response(session, request, response); - } + // 解析 + var request = DNSEntity.Read(pk.GetStream(), isTcp); - session.Dispose(); + var response = Request(session, request); + if (response != null) + { + response.Header.ID = request.Header.ID; + Response(session, request, response); } - /// 处理请求 - /// - /// - /// - protected virtual DNSEntity Request(INetSession session, DNSEntity request) - { - var local = session.Session.Local; - var isTcp = local.IsTcp; + session.Dispose(); + } - // 处理,修改 - WriteLog("{0} 请求 {1}", session.Session.Remote, request); + /// 处理请求 + /// + /// + /// + protected virtual DNSEntity Request(INetSession session, DNSEntity request) + { + var local = session.Session.Local; + var isTcp = local.IsTcp; - // 请求事件,如果第二参数有值,则直接返回 - // 结合数据库缓存,可以在这里进行返回 - if (OnRequest != null) - { - var e = new DNSEventArgs - { - Request = request - }; - OnRequest(this, e); - if (e.Response != null) return e.Response; - } + // 处理,修改 + WriteLog("{0} 请求 {1}", session.Session.Remote, request); - // 如果是PTR请求 - var rq = request.Questions[0]; - if (rq.Type == DNSQueryType.PTR) + // 请求事件,如果第二参数有值,则直接返回 + // 结合数据库缓存,可以在这里进行返回 + if (OnRequest != null) + { + var e = new DNSEventArgs { - var ptr = RequestPTR(request); - if (ptr != null) return ptr; - } + 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 (!_cache.TryGetValue(key,out var rs)) _cache[key] = rs = GetDNS(key, request); + // 读取缓存 + //var rs = cache.GetItem(request.ToString(), k => GetDNS(k, request)); + var key = request.ToString(); + //var rs = _cache[key]; + if (!_cache.TryGetValue(key, out var rs)) _cache[key] = rs = GetDNS(key, request); - // 返回给客户端 - if (rs != null) + // 返回给客户端 + if (rs != null) + { + // 如果是PTR请求 + if (rq.Type == DNSQueryType.PTR && rs.Questions[0].Type == DNSQueryType.PTR) { - // 如果是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) { - 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) + ptr2.Name = ptr.Name; + ptr2.DomainName = DomainName; + } + if (rs.Answers != null && rs.Answers.Length > 0) + { + foreach (var item in rs.Answers) { - foreach (var item in rs.Answers) - { - if (item.Type == DNSQueryType.PTR) item.Name = ptr.Name; - } + if (item.Type == DNSQueryType.PTR) item.Name = ptr.Name; } } } - - return rs; } - /// 处理PTR请求 - /// - /// - protected virtual DNSEntity RequestPTR(DNSEntity request) + return rs; + } + + /// 处理PTR请求 + /// + /// + protected virtual DNSEntity RequestPTR(DNSEntity request) + { + var rq = request.Questions[0]; + var ptr = rq as DNS_PTR; + if (ptr == null) ptr = new DNS_PTR { Name = rq.Name }; + // 对本地的请求马上返回 + var addr = ptr.Address; + if (addr != null && addr.IsLocal()) { - var rq = request.Questions[0]; - var ptr = rq as DNS_PTR; - if (ptr == null) ptr = new DNS_PTR { Name = rq.Name }; - // 对本地的请求马上返回 - var addr = ptr.Address; - if (addr != null && addr.IsLocal()) + var ptr2 = new DNS_PTR { - var ptr2 = new DNS_PTR - { - Name = ptr.Name, - DomainName = DomainName - }; + Name = ptr.Name, + DomainName = DomainName + }; - var rs = new DNSEntity - { - Questions = request.Questions, - Answers = new DNSRecord[] { ptr2 } - }; + var rs = new DNSEntity + { + Questions = request.Questions, + Answers = new DNSRecord[] { ptr2 } + }; - rs.Header.ID = request.Header.ID; - return rs; - } - return null; + rs.Header.ID = request.Header.ID; + return rs; } + return null; + } - /// 处理响应 - /// - /// - /// - protected virtual void Response(INetSession session, DNSEntity request, DNSEntity response) - { - var ss = session?.Session; - if (ss == null) return; - - var isTcp = ss.Local.IsTcp; + /// 处理响应 + /// + /// + /// + protected virtual void Response(INetSession session, DNSEntity request, DNSEntity response) + { + var ss = session?.Session; + if (ss == null) return; - if (OnResponse != null) - { - var e = new DNSEventArgs { Request = request, Response = response, Session = ss }; - OnResponse(this, e); - } + var isTcp = ss.Local.IsTcp; - session?.Send(response.GetStream(isTcp)); + if (OnResponse != null) + { + var e = new DNSEventArgs { Request = request, Response = response, Session = ss }; + OnResponse(this, e); } - DNSEntity GetDNS(String key, DNSEntity request) - { - // 批量请求父级代理 - var dic = DNSClient.QueryAll(_Clients, request); - if (dic.Count == 0) return null; + session?.Send(response.GetStream(isTcp)); + } - DNSEntity rs = null; - foreach (var item in dic) - { - rs = item.Value; - var nc = item.Key.Client; + DNSEntity GetDNS(String key, DNSEntity request) + { + // 批量请求父级代理 + var dic = DNSClient.QueryAll(_Clients, request); + if (dic.Count == 0) return null; - WriteLog("{0} GetDNS {1}", nc.Remote, rs); + DNSEntity rs = null; + foreach (var item in dic) + { + rs = item.Value; + var nc = item.Key.Client; - if (OnNew != null) - { - var e = new DNSEventArgs { Request = request, Response = item.Value, Session = nc }; - OnNew(this, e); - } - } + WriteLog("{0} GetDNS {1}", nc.Remote, rs); - return rs; + if (OnNew != null) + { + var e = new DNSEventArgs { Request = request, Response = item.Value, Session = nc }; + OnNew(this, e); + } } - #endregion - #region 事件 - /// 请求时触发。 - public event EventHandler OnRequest; + return rs; + } + #endregion - /// 响应时触发。 - public event EventHandler OnResponse; + #region 事件 + /// 请求时触发。 + public event EventHandler OnRequest; - /// 取得新DNS时触发。 - public event EventHandler OnNew; - #endregion - } + /// 响应时触发。 + public event EventHandler OnResponse; - /// DNS事件参数 - public class DNSEventArgs : EventArgs - { - /// 请求 - public DNSEntity Request { get; set; } + /// 取得新DNS时触发。 + public event EventHandler OnNew; + #endregion +} - /// 响应 - public DNSEntity Response { get; set; } +/// DNS事件参数 +public class DNSEventArgs : EventArgs +{ + /// 请求 + public DNSEntity Request { get; set; } - /// 网络会话 - public ISocketRemote Session { get; set; } - } + /// 响应 + public DNSEntity Response { get; set; } + + /// 网络会话 + public ISocketRemote Session { get; set; } } \ No newline at end of file diff --git a/NewLife.Net/NewLife.Net.csproj b/NewLife.Net/NewLife.Net.csproj index f6f54c8..ff1c695 100644 --- a/NewLife.Net/NewLife.Net.csproj +++ b/NewLife.Net/NewLife.Net.csproj @@ -1,11 +1,11 @@  - net40;net45;net461;netstandard2.0;netstandard2.1 + net45;net461;netstandard2.0;netstandard2.1 新生命网络库 - 网络通讯基础框架及各种协议实现。旧版NET45请找2021年 + 网络通讯基础框架及各种协议实现。 新生命开发团队 - 版权所有(C) 新生命开发团队 2002~2023 + 版权所有(C) 新生命开发团队 2002~2024 4.3 $([System.DateTime]::Now.ToString(`yyyy.MMdd`)) $(VersionPrefix).$(VersionSuffix) @@ -47,7 +47,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -101,12 +101,9 @@ - - - - - - + + + \ No newline at end of file diff --git a/RpcTest/RpcTest.csproj b/RpcTest/RpcTest.csproj index 8d81439..bda5e50 100644 --- a/RpcTest/RpcTest.csproj +++ b/RpcTest/RpcTest.csproj @@ -12,8 +12,8 @@ - - + + diff --git a/Test/Test.csproj b/Test/Test.csproj index 96594b5..67a72cd 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -14,7 +14,7 @@ - +