diff --git a/Texnomic.SecureDNS.Servers/GlobalUsings.cs b/Texnomic.SecureDNS.Servers/GlobalUsings.cs new file mode 100644 index 0000000..e8e8521 --- /dev/null +++ b/Texnomic.SecureDNS.Servers/GlobalUsings.cs @@ -0,0 +1,21 @@ +// Global using directives + +global using System.Buffers.Binary; +global using System.Diagnostics; +global using System.Net; +global using System.Net.Sockets; +global using System.Runtime.InteropServices; +global using System.Text.Json.Serialization; +global using System.Threading.Tasks.Dataflow; +global using Microsoft.Extensions.Hosting; +global using Microsoft.Extensions.Options; +global using Nethereum.Util; +global using PipelineNet.MiddlewareResolver; +global using Serilog; +global using Texnomic.SecureDNS.Abstractions; +global using Texnomic.SecureDNS.Abstractions.Enums; +global using Texnomic.SecureDNS.Core; +global using Texnomic.SecureDNS.Extensions; +global using Texnomic.SecureDNS.Serialization; +global using Texnomic.SecureDNS.Servers.Proxy.Options; +global using Texnomic.SecureDNS.Servers.Proxy.ResponsibilityChain; \ No newline at end of file diff --git a/Texnomic.SecureDNS.Servers/Proxy/Options/ProxyResponsibilityChainOptions.cs b/Texnomic.SecureDNS.Servers/Proxy/Options/ProxyResponsibilityChainOptions.cs index fe1654b..4fb5391 100644 --- a/Texnomic.SecureDNS.Servers/Proxy/Options/ProxyResponsibilityChainOptions.cs +++ b/Texnomic.SecureDNS.Servers/Proxy/Options/ProxyResponsibilityChainOptions.cs @@ -1,5 +1,4 @@ using PipelineNet.Middleware; -using Texnomic.SecureDNS.Abstractions; namespace Texnomic.SecureDNS.Servers.Proxy.Options; diff --git a/Texnomic.SecureDNS.Servers/Proxy/Options/ProxyServerOptions.cs b/Texnomic.SecureDNS.Servers/Proxy/Options/ProxyServerOptions.cs index 1c83553..8251d6a 100644 --- a/Texnomic.SecureDNS.Servers/Proxy/Options/ProxyServerOptions.cs +++ b/Texnomic.SecureDNS.Servers/Proxy/Options/ProxyServerOptions.cs @@ -1,7 +1,4 @@ -using System.Net; -using System.Text.Json.Serialization; - -namespace Texnomic.SecureDNS.Servers.Proxy.Options; +namespace Texnomic.SecureDNS.Servers.Proxy.Options; public class ProxyServerOptions { @@ -10,7 +7,7 @@ public class ProxyServerOptions public int Port { get; set; } = 53; [JsonIgnore] - public IPEndPoint IPEndPoint => new IPEndPoint(IPAddress.Parse(Address), Port); + public IPEndPoint IPEndPoint => new(IPAddress.Parse(Address), Port); - public static int Threads { get; set; } = Environment.ProcessorCount; + public int Threads { get; set; } = Environment.ProcessorCount; } \ No newline at end of file diff --git a/Texnomic.SecureDNS.Servers/Proxy/ResponsibilityChain/ProxyResponsibilityChain.cs b/Texnomic.SecureDNS.Servers/Proxy/ResponsibilityChain/ProxyResponsibilityChain.cs index f2cc557..f67a9e7 100644 --- a/Texnomic.SecureDNS.Servers/Proxy/ResponsibilityChain/ProxyResponsibilityChain.cs +++ b/Texnomic.SecureDNS.Servers/Proxy/ResponsibilityChain/ProxyResponsibilityChain.cs @@ -1,9 +1,4 @@ -using Microsoft.Extensions.Options; -using PipelineNet.ChainsOfResponsibility; -using PipelineNet.MiddlewareResolver; -using Texnomic.SecureDNS.Abstractions; -using Texnomic.SecureDNS.Abstractions.Enums; -using Texnomic.SecureDNS.Servers.Proxy.Options; +using PipelineNet.ChainsOfResponsibility; namespace Texnomic.SecureDNS.Servers.Proxy.ResponsibilityChain; diff --git a/Texnomic.SecureDNS.Servers/Proxy/TCPServer.cs b/Texnomic.SecureDNS.Servers/Proxy/TCPServer.cs index bbc207b..964b142 100644 --- a/Texnomic.SecureDNS.Servers/Proxy/TCPServer.cs +++ b/Texnomic.SecureDNS.Servers/Proxy/TCPServer.cs @@ -1,78 +1,33 @@ -using System.Buffers.Binary; -using System.Net.Sockets; -using System.Threading.Tasks.Dataflow; - -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Options; - -using Nethereum.Util; - -using PipelineNet.MiddlewareResolver; - -using Serilog; - -using Texnomic.SecureDNS.Abstractions; -using Texnomic.SecureDNS.Abstractions.Enums; -using Texnomic.SecureDNS.Core; -using Texnomic.SecureDNS.Extensions; -using Texnomic.SecureDNS.Serialization; -using Texnomic.SecureDNS.Servers.Proxy.Options; -using Texnomic.SecureDNS.Servers.Proxy.ResponsibilityChain; - -namespace Texnomic.SecureDNS.Servers.Proxy; - -public sealed class TCPServer : IHostedService, IDisposable +namespace Texnomic.SecureDNS.Servers.Proxy; + +public sealed class TCPServer( + IOptionsMonitor ProxyResponsibilityChainOptions, + IOptionsMonitor ProxyServerOptions, + IMiddlewareResolver MiddlewareResolver, + ILogger Logger) + : IHostedService, IDisposable { - private readonly ILogger Logger; - private readonly List Workers; - private readonly IOptionsMonitor Options; - private readonly IMiddlewareResolver MiddlewareResolver; - private readonly IOptionsMonitor ProxyResponsibilityChainOptions; - private readonly BufferBlock<(IMessage, Socket)> IncomingQueue; - private readonly BufferBlock<(IMessage, Socket)> OutgoingQueue; + private readonly List Workers = []; + private readonly BufferBlock<(IMessage, Socket)> IncomingQueue = new(); + private readonly BufferBlock<(IMessage, Socket)> OutgoingQueue = new(); + private readonly TcpListener TcpListener = new(ProxyServerOptions.CurrentValue.IPEndPoint); private CancellationToken CancellationToken; - private TcpListener TcpListener; - - public TCPServer(IOptionsMonitor ProxyResponsibilityChainOptions, - IOptionsMonitor ProxyServerOptions, - IMiddlewareResolver MiddlewareResolver, - ILogger Logger) - { - Options = ProxyServerOptions; - - this.MiddlewareResolver = MiddlewareResolver; - - this.ProxyResponsibilityChainOptions = ProxyResponsibilityChainOptions; - - this.Logger = Logger; - - Workers = new List(); - - IncomingQueue = new BufferBlock<(IMessage, Socket)>(); - - OutgoingQueue = new BufferBlock<(IMessage, Socket)>(); - } - public async Task StartAsync(CancellationToken Token) { CancellationToken = Token; - TcpListener = new TcpListener(Options.CurrentValue.IPEndPoint); - - //TcpListener.Server.Bind(Options.CurrentValue.IPEndPoint); - TcpListener.Start(); - for (var I = 0; I < ProxyServerOptions.Threads; I++) + for (var I = 0; I < ProxyServerOptions.CurrentValue.Threads; I++) { Workers.Add(Task.Factory.StartNew(ReceiveAsync, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap()); Workers.Add(Task.Factory.StartNew(ResolveAsync, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap()); Workers.Add(Task.Factory.StartNew(SendAsync, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap()); } - Logger?.Information("TCP Server Started with {@Threads} Threads. Listening On {@IPEndPoint}", ProxyServerOptions.Threads, Options.CurrentValue.IPEndPoint.ToString()); + Logger?.Information("TCP Server Started with {@Threads} Threads. Listening On {@IPEndPoint}", ProxyServerOptions.CurrentValue.Threads, ProxyServerOptions.CurrentValue.IPEndPoint.ToString()); await Task.Yield(); } @@ -135,12 +90,11 @@ private async Task ReceiveAsync() { Thread.CurrentThread.Name = "Receiver"; - while (!CancellationToken.IsCancellationRequested) { try { - var ClientSocket = await TcpListener.AcceptSocketAsync(); + var ClientSocket = await TcpListener.AcceptSocketAsync(CancellationToken); var Prefix = new byte[2]; @@ -183,7 +137,7 @@ private async Task ResolveAsync() { try { - (Query, ClientSocket) = await IncomingQueue.ReceiveAsync(CancellationToken).WithCancellation(CancellationToken); + (Query, ClientSocket) = await IncomingQueue.ReceiveAsync(CancellationToken); var Answer = await ResponsibilityChain.Execute(Query); @@ -220,7 +174,7 @@ private async Task SendAsync() { try { - var (Answer, ClientSocket) = await OutgoingQueue.ReceiveAsync(CancellationToken).WithCancellation(CancellationToken); + var (Answer, ClientSocket) = await OutgoingQueue.ReceiveAsync(CancellationToken); var Bytes = Serialize(Answer); @@ -245,7 +199,7 @@ private async Task SendAsync() } } - private IMessage Handle(Exception Error, ushort ID, string Stage, ResponseCode Response) + private Message Handle(Exception Error, ushort ID, string Stage, ResponseCode Response) { Logger?.Error(Error, $"{@Error} Occurred While {Stage} Message.", Error); @@ -257,7 +211,7 @@ private IMessage Handle(Exception Error, ushort ID, string Stage, ResponseCode R }; } - private IMessage Handle(Exception Error, byte[] Bytes, string Stage, ResponseCode Response) + private Message Handle(Exception Error, byte[] Bytes, string Stage, ResponseCode Response) { Logger?.Error(Error, $"{@Error} Occurred While {Stage} {@Bytes}.", Error, BitConverter.ToString(Bytes).Replace("-", ", 0x")); diff --git a/Texnomic.SecureDNS.Servers/Proxy/TCPServer2.cs b/Texnomic.SecureDNS.Servers/Proxy/TCPServer2.cs index cd52346..5d60942 100644 --- a/Texnomic.SecureDNS.Servers/Proxy/TCPServer2.cs +++ b/Texnomic.SecureDNS.Servers/Proxy/TCPServer2.cs @@ -1,64 +1,27 @@ -using System.Buffers.Binary; -using System.Net.Sockets; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Options; -using PipelineNet.MiddlewareResolver; +namespace Texnomic.SecureDNS.Servers.Proxy; -using Serilog; -using Texnomic.SecureDNS.Abstractions.Enums; -using Texnomic.SecureDNS.Core; -using Texnomic.SecureDNS.Extensions; -using Texnomic.SecureDNS.Serialization; -using Texnomic.SecureDNS.Servers.Proxy.Options; -using Texnomic.SecureDNS.Servers.Proxy.ResponsibilityChain; - - -namespace Texnomic.SecureDNS.Servers.Proxy; - -public sealed class TCPServer2 : IHostedService, IDisposable +public sealed class TCPServer2(IOptionsMonitor ProxyResponsibilityChainOptions, + IOptionsMonitor ProxyServerOptions, + IMiddlewareResolver MiddlewareResolver, + ILogger Logger) : IHostedService, IDisposable { - private readonly ILogger Logger; - private readonly List Threads; - private readonly IOptionsMonitor Options; - private readonly IMiddlewareResolver MiddlewareResolver; - private readonly IOptionsMonitor ProxyResponsibilityChainOptions; - private readonly TcpListener TcpListener; + private readonly List Threads = []; + private readonly TcpListener TcpListener = new(ProxyServerOptions.CurrentValue.IPEndPoint); private CancellationToken CancellationToken; - - public TCPServer2(IOptionsMonitor ProxyResponsibilityChainOptions, - IOptionsMonitor ProxyServerOptions, - IMiddlewareResolver MiddlewareResolver, - ILogger Logger) - { - Options = ProxyServerOptions; - - this.MiddlewareResolver = MiddlewareResolver; - - this.ProxyResponsibilityChainOptions = ProxyResponsibilityChainOptions; - - this.Logger = Logger; - - Threads = new List(); - - TcpListener = new TcpListener(Options.CurrentValue.IPEndPoint); - } - public async Task StartAsync(CancellationToken Token) { CancellationToken = Token; - //TcpListener.Server.Listen(); - TcpListener.Start(); - for (var I = 0; I < ProxyServerOptions.Threads; I++) + for (var I = 0; I < ProxyServerOptions.CurrentValue.Threads; I++) { Threads.Add(Task.Factory.StartNew(ResolveAsync, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap()); } - Logger?.Information("TCP Server Started with {@Threads} Threads. Listening On {@IPEndPoint}", ProxyServerOptions.Threads, Options.CurrentValue.IPEndPoint.ToString()); + Logger?.Information("TCP Server Started with {@Threads} Threads. Listening On {@IPEndPoint}", ProxyServerOptions.CurrentValue.Threads, ProxyServerOptions.CurrentValue.IPEndPoint.ToString()); await Task.Yield(); } @@ -92,7 +55,7 @@ private async Task ResolveAsync() Prefix = new byte[2]; await Client.ReceiveAsync(Prefix, SocketFlags.None); - + var Size = BinaryPrimitives.ReadUInt16BigEndian(Prefix); Buffer = new byte[Size]; @@ -135,7 +98,7 @@ private async Task ResolveAsync() var RawMessage = DnSerializer.Serialize(Message); - BinaryPrimitives.WriteUInt16BigEndian(Prefix, (ushort) RawMessage.Length); + BinaryPrimitives.WriteUInt16BigEndian(Prefix, (ushort)RawMessage.Length); await Client.SendAsync(Prefix, SocketFlags.None); diff --git a/Texnomic.SecureDNS.Servers/Proxy/UDPServer.cs b/Texnomic.SecureDNS.Servers/Proxy/UDPServer.cs index c8fe9ca..3d22de3 100644 --- a/Texnomic.SecureDNS.Servers/Proxy/UDPServer.cs +++ b/Texnomic.SecureDNS.Servers/Proxy/UDPServer.cs @@ -1,79 +1,39 @@ -using System.Net; -using System.Net.Sockets; -using System.Runtime.InteropServices; -using System.Threading.Tasks.Dataflow; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Options; -using Nethereum.Util; -using PipelineNet.MiddlewareResolver; -using Serilog; -using Texnomic.SecureDNS.Abstractions; -using Texnomic.SecureDNS.Abstractions.Enums; -using Texnomic.SecureDNS.Core; -using Texnomic.SecureDNS.Extensions; -using Texnomic.SecureDNS.Serialization; -using Texnomic.SecureDNS.Servers.Proxy.Options; -using Texnomic.SecureDNS.Servers.Proxy.ResponsibilityChain; - -namespace Texnomic.SecureDNS.Servers.Proxy; - -public sealed class UDPServer : IHostedService, IDisposable +namespace Texnomic.SecureDNS.Servers.Proxy; + +public sealed class UDPServer( + IOptionsMonitor ProxyResponsibilityChainOptions, + IOptionsMonitor ProxyServerOptions, + IMiddlewareResolver MiddlewareResolver, + ILogger Logger) + : IHostedService, IDisposable { - private readonly ILogger Logger; - private readonly List Workers; - private readonly IOptionsMonitor Options; - private readonly IMiddlewareResolver MiddlewareResolver; - private readonly IOptionsMonitor ProxyResponsibilityChainOptions; - private readonly BufferBlock<(IMessage, IPEndPoint)> IncomingQueue; - private readonly BufferBlock<(IMessage, IPEndPoint)> OutgoingQueue; - + private readonly List Workers = []; + private readonly BufferBlock<(IMessage, IPEndPoint)> IncomingQueue = new(); + private readonly BufferBlock<(IMessage, IPEndPoint)> OutgoingQueue = new(); + private readonly UdpClient UdpClient = new(); private CancellationToken CancellationToken; - private UdpClient UdpClient; - - public UDPServer(IOptionsMonitor ProxyResponsibilityChainOptions, - IOptionsMonitor ProxyServerOptions, - IMiddlewareResolver MiddlewareResolver, - ILogger Logger) - { - Options = ProxyServerOptions; - - this.MiddlewareResolver = MiddlewareResolver; - - this.ProxyResponsibilityChainOptions = ProxyResponsibilityChainOptions; - - this.Logger = Logger; - - Workers = new List(); - - IncomingQueue = new BufferBlock<(IMessage, IPEndPoint)>(); - - OutgoingQueue = new BufferBlock<(IMessage, IPEndPoint)>(); - } - public async Task StartAsync(CancellationToken Token) { CancellationToken = Token; - UdpClient = new UdpClient(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { //https://stackoverflow.com/questions/5199026/c-sharp-async-udp-listener-socketexception UdpClient.Client.IOControl(-1744830452, new byte[4], null); } - UdpClient.Client.Bind(Options.CurrentValue.IPEndPoint); + UdpClient.Client.Bind(ProxyServerOptions.CurrentValue.IPEndPoint); - for (var I = 0; I < ProxyServerOptions.Threads; I++) + for (var I = 0; I < ProxyServerOptions.CurrentValue.Threads; I++) { Workers.Add(Task.Factory.StartNew(ReceiveAsync, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap()); Workers.Add(Task.Factory.StartNew(ResolveAsync, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap()); Workers.Add(Task.Factory.StartNew(SendAsync, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap()); } - Logger?.Information("UDP Server Started with {@Threads} Threads. Listening On {@IPEndPoint}", ProxyServerOptions.Threads, Options.CurrentValue.IPEndPoint.ToString()); + Logger?.Information("UDP Server Started with {@Threads} Threads. Listening On {@IPEndPoint}", ProxyServerOptions.CurrentValue.Threads, ProxyServerOptions.CurrentValue.IPEndPoint.ToString()); await Task.Yield(); } @@ -175,8 +135,7 @@ private async Task ResolveAsync() { try { - (Query, RemoteEndPoint) = await IncomingQueue.ReceiveAsync(CancellationToken) - .WithCancellation(CancellationToken); + (Query, RemoteEndPoint) = await IncomingQueue.ReceiveAsync(CancellationToken); var Answer = await ResponsibilityChain.Execute(Query); @@ -213,8 +172,7 @@ private async Task SendAsync() { try { - var (Answer, RemoteEndPoint) = await OutgoingQueue.ReceiveAsync(CancellationToken) - .WithCancellation(CancellationToken); + var (Answer, RemoteEndPoint) = await OutgoingQueue.ReceiveAsync(CancellationToken); var Bytes = Serialize(Answer); @@ -234,7 +192,7 @@ private async Task SendAsync() } } - private IMessage Handle(Exception Error, ushort ID, string Stage, ResponseCode Response) + private Message Handle(Exception Error, ushort ID, string Stage, ResponseCode Response) { Logger?.Error(Error, $"{@Error} Occurred While {Stage} Message.", Error); @@ -246,7 +204,7 @@ private IMessage Handle(Exception Error, ushort ID, string Stage, ResponseCode R }; } - private IMessage Handle(Exception Error, byte[] Bytes, string Stage, ResponseCode Response) + private Message Handle(Exception Error, byte[] Bytes, string Stage, ResponseCode Response) { Logger?.Error(Error, $"{@Error} Occurred While {Stage} {@Bytes}.", Error, BitConverter.ToString(Bytes).Replace("-", ", 0x")); diff --git a/Texnomic.SecureDNS.Servers/Proxy/UDPServer2.cs b/Texnomic.SecureDNS.Servers/Proxy/UDPServer2.cs index 5132ce4..fe7cde8 100644 --- a/Texnomic.SecureDNS.Servers/Proxy/UDPServer2.cs +++ b/Texnomic.SecureDNS.Servers/Proxy/UDPServer2.cs @@ -1,50 +1,18 @@ -using System.Buffers.Binary; -using System.Diagnostics; -using System.Net.Sockets; -using System.Runtime.InteropServices; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Options; -using PipelineNet.MiddlewareResolver; - -using Serilog; -using Texnomic.SecureDNS.Abstractions.Enums; -using Texnomic.SecureDNS.Core; -using Texnomic.SecureDNS.Extensions; -using Texnomic.SecureDNS.Serialization; -using Texnomic.SecureDNS.Servers.Proxy.Options; -using Texnomic.SecureDNS.Servers.Proxy.ResponsibilityChain; - -namespace Texnomic.SecureDNS.Servers.Proxy; - -public sealed class UDPServer2 : IHostedService, IDisposable +namespace Texnomic.SecureDNS.Servers.Proxy; + +public sealed class UDPServer2( + IOptionsMonitor ProxyResponsibilityChainOptions, + IOptionsMonitor ProxyServerOptions, + IMiddlewareResolver MiddlewareResolver, + ILogger Logger) + : IHostedService, IDisposable { - private readonly ILogger Logger; - private readonly List Threads; - private readonly IOptionsMonitor Options; - private readonly IMiddlewareResolver MiddlewareResolver; - private readonly IOptionsMonitor ProxyResponsibilityChainOptions; - private readonly UdpClient Server; + private readonly List Threads = []; + private readonly UdpClient Server = new(); private CancellationToken CancellationToken; - private int Affinity; - - public UDPServer2(IOptionsMonitor ProxyResponsibilityChainOptions, IOptionsMonitor ProxyServerOptions, IMiddlewareResolver MiddlewareResolver, ILogger Logger) - { - Options = ProxyServerOptions; - - this.MiddlewareResolver = MiddlewareResolver; - - this.ProxyResponsibilityChainOptions = ProxyResponsibilityChainOptions; - - this.Logger = Logger; - - Threads = new List(); - - Server = new UdpClient(); - - Affinity = 0; - } + private int Affinity = 0; public async Task StartAsync(CancellationToken Token) { @@ -56,14 +24,14 @@ public async Task StartAsync(CancellationToken Token) Server.Client.IOControl(-1744830452, new byte[4], null); } - Server.Client.Bind(Options.CurrentValue.IPEndPoint); + Server.Client.Bind(ProxyServerOptions.CurrentValue.IPEndPoint); - for (var I = 0; I < ProxyServerOptions.Threads; I++) + for (var I = 0; I < ProxyServerOptions.CurrentValue.Threads; I++) { Threads.Add(Task.Factory.StartNew(ResolveAsync, CancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap()); } - Logger?.Information("UDP Server Started with {@Threads} Threads. Listening On {@IPEndPoint}", ProxyServerOptions.Threads, Options.CurrentValue.IPEndPoint.ToString()); + Logger?.Information("UDP Server Started with {@Threads} Threads. Listening On {@IPEndPoint}", ProxyServerOptions.CurrentValue.Threads, ProxyServerOptions.CurrentValue.IPEndPoint.ToString()); await Task.Yield(); } @@ -80,13 +48,13 @@ private async Task ResolveAsync() { var AffinityPointer = (0b00000001 << Affinity++); - var ProcessThread = Process.GetCurrentProcess().Threads[Thread.CurrentThread.ManagedThreadId]; + var ProcessThread = Process.GetCurrentProcess().Threads[Environment.CurrentManagedThreadId]; ProcessThread.ProcessorAffinity = (IntPtr) AffinityPointer; ProcessThread.PriorityLevel = ThreadPriorityLevel.Highest; - Logger?.Debug("UDP Server Started Thread {@Thread} with Affinity {@Affinity} & Priority {@Priority}.", Thread.CurrentThread.ManagedThreadId, AffinityPointer, ThreadPriorityLevel.Highest); + Logger?.Debug("UDP Server Started Thread {@Thread} with Affinity {@Affinity} & Priority {@Priority}.", Environment.CurrentManagedThreadId, AffinityPointer, ThreadPriorityLevel.Highest); var ProxyResponsibilityChain = new ProxyResponsibilityChain(ProxyResponsibilityChainOptions, MiddlewareResolver);