diff --git a/Texnomic.DNS.Servers/ProxyServer.cs b/Texnomic.DNS.Servers/ProxyServer.cs index 102c0ff1..b0fbc73c 100644 --- a/Texnomic.DNS.Servers/ProxyServer.cs +++ b/Texnomic.DNS.Servers/ProxyServer.cs @@ -4,7 +4,6 @@ using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; -using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; @@ -100,8 +99,6 @@ public List Status() public async Task StopAsync(CancellationToken Token) { - UdpClient.Close(); - await Task.WhenAll(Workers); IncomingQueue.Complete(); @@ -120,8 +117,6 @@ private IMessage Deserialize(byte[] Bytes) } catch (Exception Error) { - //Logger?.Error(Error, "{@Error} Occurred While Deserializing Message.", Error); - Logger?.Error(Error, "{@Error} Occurred While Deserializing {@Bytes}.", Error, BitConverter.ToString(Bytes).Replace("-", ", 0x")); Errored?.Invoke(this, new ErroredEventArgs(Error)); @@ -170,32 +165,16 @@ private async Task ReceiveAsync() var Message = Deserialize(Result.Buffer); - switch (Message.MessageType) - { - case MessageType.Query: - { - await IncomingQueue.SendAsync((Message, Result.RemoteEndPoint), CancellationToken); - - Logger?.Verbose("Received {@Query} From {@RemoteEndPoint}.", Message, - Result.RemoteEndPoint.ToString()); - - Queried?.Invoke(this, new QueriedEventArgs(Message, Result.RemoteEndPoint)); - - break; - } - - case MessageType.Response: - { - await OutgoingQueue.SendAsync((Message, Result.RemoteEndPoint), CancellationToken); + await IncomingQueue.SendAsync((Message, Result.RemoteEndPoint), CancellationToken); - Logger?.Debug("Queueing (Outgoing) Format Error {@Answer} To {@RemoteEndPoint}.", Message, - Result.RemoteEndPoint.ToString()); + Logger?.Verbose("Received {@Query} From {@RemoteEndPoint}.", Message, + Result.RemoteEndPoint.ToString()); - break; - } - default: - throw new ArgumentOutOfRangeException(); - } + Queried?.Invoke(this, new QueriedEventArgs(Message, Result.RemoteEndPoint)); + } + catch (OperationCanceledException) + { + break; } catch (Exception Error) { @@ -232,15 +211,23 @@ private async Task ResolveAsync() Resolved?.Invoke(this, new ResolvedEventArgs(Query, Answer, RemoteEndPoint)); } + catch (OperationCanceledException) + { + break; + } catch (ObjectDisposedException Error) { ResponsibilityChain = new ProxyResponsibilityChain(ProxyResponsibilityChainOptions, MiddlewareResolver); - await Handle(Error, Query, RemoteEndPoint); + var ErrorMessage = Handle(Error, Query.ID, "Resolving", ResponseCode.ServerFailure); + + await OutgoingQueue.SendAsync((ErrorMessage, RemoteEndPoint), CancellationToken); } catch (Exception Error) { - await Handle(Error, Query, RemoteEndPoint); + var ErrorMessage = Handle(Error, Query.ID, "Resolving", ResponseCode.ServerFailure); + + await OutgoingQueue.SendAsync((ErrorMessage, RemoteEndPoint), CancellationToken); } } } @@ -265,6 +252,10 @@ private async Task SendAsync() Answered?.Invoke(this, new AnsweredEventArgs(Answer, RemoteEndPoint)); } + catch (OperationCanceledException) + { + break; + } catch (Exception Error) { Logger?.Error(Error, "{@Error} Occurred While Sending Message.", Error); @@ -274,20 +265,32 @@ private async Task SendAsync() } } - private async Task Handle(Exception Error, IMessage Query, IPEndPoint RemoteEndPoint) + private IMessage Handle(Exception Error, ushort ID, string Stage, ResponseCode Response) { - Logger?.Error(Error, "{@Error} Occurred While Resolving Message.", Error); + Logger?.Error(Error, $"{@Error} Occurred While {Stage} Message.", Error); Errored?.Invoke(this, new ErroredEventArgs(Error)); - var ErrorMessage = new Message() + return new Message() { - ID = Query.ID, + ID = ID, MessageType = MessageType.Response, - ResponseCode = ResponseCode.ServerFailure, + ResponseCode = Response, }; + } - await OutgoingQueue.SendAsync((ErrorMessage, RemoteEndPoint), CancellationToken); + private IMessage Handle(Exception Error, byte[] Bytes, string Stage, ResponseCode Response) + { + Logger?.Error(Error, $"{@Error} Occurred While {Stage} {@Bytes}.", Error, BitConverter.ToString(Bytes).Replace("-", ", 0x")); + + Errored?.Invoke(this, new ErroredEventArgs(Error)); + + return new Message() + { + ID = BitConverter.ToUInt16(Bytes.Slice(2)), + MessageType = MessageType.Response, + ResponseCode = Response + }; } private bool IsDisposed; @@ -314,5 +317,6 @@ private void Dispose(bool Disposing) { Dispose(false); } + } } diff --git a/Texnomic.SecureDNS.Terminal.Installer/Texnomic.SecureDNS.Terminal.Installer.vdproj b/Texnomic.SecureDNS.Terminal.Installer/Texnomic.SecureDNS.Terminal.Installer.vdproj index 10dffd4e..f6ba7000 100644 --- a/Texnomic.SecureDNS.Terminal.Installer/Texnomic.SecureDNS.Terminal.Installer.vdproj +++ b/Texnomic.SecureDNS.Terminal.Installer/Texnomic.SecureDNS.Terminal.Installer.vdproj @@ -13,18 +13,6 @@ "SccProvider" = "8:" "Hierarchy" { - "Entry" - { - "MsmKey" = "8:_1A9A88DF69D64896B04C491D8B7F5852" - "OwnerKey" = "8:_UNDEFINED" - "MsmSig" = "8:_UNDEFINED" - } - "Entry" - { - "MsmKey" = "8:_8E4B95827C654B84BE97F4553AF62386" - "OwnerKey" = "8:_UNDEFINED" - "MsmSig" = "8:_UNDEFINED" - } "Entry" { "MsmKey" = "8:_E10535C6727B4C43AD40319D97B4835D" @@ -39,7 +27,7 @@ "DisplayName" = "8:Debug" "IsDebugOnly" = "11:TRUE" "IsReleaseOnly" = "11:FALSE" - "OutputFilename" = "8:Debug\\Texnomic.SecureDNS.Terminal.Installer.msi" + "OutputFilename" = "8:Publish\\Texnomic.SecureDNS.Terminal.Installer.msi" "PackageFilesAs" = "3:2" "PackageFileSize" = "3:-2147483648" "CabType" = "3:1" @@ -49,6 +37,17 @@ "PrivateKeyFile" = "8:" "TimeStampServer" = "8:" "InstallerBootstrapper" = "3:2" + "BootstrapperCfg:{63ACBE69-63AA-4F98-B2B6-99F9E24495F2}" + { + "Enabled" = "11:FALSE" + "PromptEnabled" = "11:TRUE" + "PrerequisitesLocation" = "2:1" + "Url" = "8:" + "ComponentsUrl" = "8:" + "Items" + { + } + } } "Release" { @@ -65,6 +64,14 @@ "PrivateKeyFile" = "8:" "TimeStampServer" = "8:" "InstallerBootstrapper" = "3:2" + "BootstrapperCfg:{63ACBE69-63AA-4F98-B2B6-99F9E24495F2}" + { + "Enabled" = "11:TRUE" + "PromptEnabled" = "11:TRUE" + "PrerequisitesLocation" = "2:1" + "Url" = "8:" + "ComponentsUrl" = "8:" + } } } "Deployable" @@ -96,46 +103,6 @@ } "File" { - "{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_1A9A88DF69D64896B04C491D8B7F5852" - { - "SourcePath" = "8:..\\Texnomic.SecureDNS.Terminal\\bin\\Publish\\Win-x64\\SecureDNS.Win-x64.exe" - "TargetName" = "8:SecureDNS.Win-x64.exe" - "Tag" = "8:" - "Folder" = "8:_4F58D4D6AEF447EE9A83F32A70203221" - "Condition" = "8:" - "Transitive" = "11:FALSE" - "Vital" = "11:TRUE" - "ReadOnly" = "11:FALSE" - "Hidden" = "11:FALSE" - "System" = "11:FALSE" - "Permanent" = "11:FALSE" - "SharedLegacy" = "11:FALSE" - "PackageAs" = "3:1" - "Register" = "3:1" - "Exclude" = "11:FALSE" - "IsDependency" = "11:FALSE" - "IsolateTo" = "8:" - } - "{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_8E4B95827C654B84BE97F4553AF62386" - { - "SourcePath" = "8:..\\Texnomic.SecureDNS.Terminal\\AppSettings.json" - "TargetName" = "8:AppSettings.json" - "Tag" = "8:" - "Folder" = "8:_4F58D4D6AEF447EE9A83F32A70203221" - "Condition" = "8:" - "Transitive" = "11:FALSE" - "Vital" = "11:TRUE" - "ReadOnly" = "11:FALSE" - "Hidden" = "11:FALSE" - "System" = "11:FALSE" - "Permanent" = "11:FALSE" - "SharedLegacy" = "11:FALSE" - "PackageAs" = "3:1" - "Register" = "3:1" - "Exclude" = "11:FALSE" - "IsDependency" = "11:FALSE" - "IsolateTo" = "8:" - } "{1FB2D0AE-D3B9-43D4-B9DD-F88EC61E35DE}:_E10535C6727B4C43AD40319D97B4835D" { "SourcePath" = "8:..\\Texnomic.SecureDNS.Terminal\\SecureDNS.ico" @@ -223,15 +190,15 @@ { "Name" = "8:Microsoft Visual Studio" "ProductName" = "8:Texnomic SecureDNS Terminal" - "ProductCode" = "8:{FE3E0B25-75D7-4B17-B636-19EC21F3E5F8}" - "PackageCode" = "8:{7551C566-39B4-4EAF-A004-A4FC0458E6A5}" + "ProductCode" = "8:{3564A7C5-F151-4830-A1E7-250DD8D7F713}" + "PackageCode" = "8:{E2B80269-6C9F-4BFC-96A2-BC817CA908FC}" "UpgradeCode" = "8:{D6F79028-861C-4CDB-9C16-31C9F8900733}" "AspNetVersion" = "8:4.0.30319.0" "RestartWWWService" = "11:FALSE" "RemovePreviousVersions" = "11:TRUE" "DetectNewerInstalledVersion" = "11:TRUE" "InstallAllUsers" = "11:FALSE" - "ProductVersion" = "8:0.2" + "ProductVersion" = "8:0.3" "Manufacturer" = "8:Texnomic" "ARPHELPTELEPHONE" = "8:" "ARPHELPLINK" = "8:https://github.com/Texnomic/SecureDNS" @@ -344,30 +311,30 @@ } "Shortcut" { - "{970C0BB2-C7D0-45D7-ABFA-7EC378858BC0}:_3866B2C8FA7B416D894F6477634515B8" + "{970C0BB2-C7D0-45D7-ABFA-7EC378858BC0}:_8CEC9A4F727F47ECAB8111C33C47C3B5" { "Name" = "8:SecureDNS Terminal" "Arguments" = "8:" - "Description" = "8:" + "Description" = "8:SecureDNS Terminal" "ShowCmd" = "3:1" "IconIndex" = "3:0" "Transitive" = "11:FALSE" - "Target" = "8:_1A9A88DF69D64896B04C491D8B7F5852" - "Folder" = "8:_FEF92F2742A348CA8DE82F87E448DC6D" + "Target" = "8:_345CD70979D04FD8AFA153EA39CFC3FE" + "Folder" = "8:_03E385DAF6834CEDB43470C425EA6433" "WorkingFolder" = "8:_4F58D4D6AEF447EE9A83F32A70203221" "Icon" = "8:_E10535C6727B4C43AD40319D97B4835D" "Feature" = "8:" } - "{970C0BB2-C7D0-45D7-ABFA-7EC378858BC0}:_C84AFE765CAC40228A3371B05362DC7F" + "{970C0BB2-C7D0-45D7-ABFA-7EC378858BC0}:_AFFD3374038C4B3BB38BB4C0B28C2116" { "Name" = "8:SecureDNS Terminal" "Arguments" = "8:" - "Description" = "8:" + "Description" = "8:SecureDNS Terminal" "ShowCmd" = "3:1" "IconIndex" = "3:0" "Transitive" = "11:FALSE" - "Target" = "8:_1A9A88DF69D64896B04C491D8B7F5852" - "Folder" = "8:_03E385DAF6834CEDB43470C425EA6433" + "Target" = "8:_345CD70979D04FD8AFA153EA39CFC3FE" + "Folder" = "8:_FEF92F2742A348CA8DE82F87E448DC6D" "WorkingFolder" = "8:_4F58D4D6AEF447EE9A83F32A70203221" "Icon" = "8:_E10535C6727B4C43AD40319D97B4835D" "Feature" = "8:" @@ -771,6 +738,35 @@ } "ProjectOutput" { + "{5259A561-127C-4D43-A0A1-72F10C7B3BF8}:_345CD70979D04FD8AFA153EA39CFC3FE" + { + "SourcePath" = "8:..\\Texnomic.SecureDNS.Terminal\\obj\\Debug\\netcoreapp3.1\\win-x64\\Texnomic.SecureDNS.Terminal.exe" + "TargetName" = "8:" + "Tag" = "8:" + "Folder" = "8:_4F58D4D6AEF447EE9A83F32A70203221" + "Condition" = "8:" + "Transitive" = "11:FALSE" + "Vital" = "11:TRUE" + "ReadOnly" = "11:FALSE" + "Hidden" = "11:FALSE" + "System" = "11:FALSE" + "Permanent" = "11:FALSE" + "SharedLegacy" = "11:FALSE" + "PackageAs" = "3:1" + "Register" = "3:1" + "Exclude" = "11:FALSE" + "IsDependency" = "11:FALSE" + "IsolateTo" = "8:" + "ProjectOutputGroupRegister" = "3:1" + "OutputConfiguration" = "8:Release|Any CPU" + "OutputGroupCanonicalName" = "8:PublishItems" + "OutputProjectGuid" = "8:{66D712B8-4952-4BA9-980A-EB6618BD228C}" + "ShowKeyOutput" = "11:TRUE" + "PublishProfilePath" = "8:Properties\\PublishProfiles\\Win-x64-Installer.pubxml" + "ExcludeFilters" + { + } + } } } } diff --git a/Texnomic.SecureDNS.Terminal/AppSettings.json b/Texnomic.SecureDNS.Terminal/AppSettings.json index a1bf9da6..b628452f 100644 --- a/Texnomic.SecureDNS.Terminal/AppSettings.json +++ b/Texnomic.SecureDNS.Terminal/AppSettings.json @@ -1,25 +1,25 @@ { "Serilog": { "WriteTo": [ - //{ - // "Name": "Console" - //} { - "Name": "Seq", - "Args": { - "serverUrl": "http://127.0.0.1:5341", - "compact": true - } + "Name": "Console" } + //{ + // "Name": "Seq", + // "Args": { + // "serverUrl": "http://127.0.0.1:5341", + // "compact": true + // } + //} ], - "Properties": { "Application": "SecureDNS" } }, "DNSCrypt Protocol": { - "Stamp": "sdns://AQcAAAAAAAAAEzE3Mi4xNy4xNzguMTQ1OjEyMzQgwOMn03j07Mp5UElpNZ2xR_86q9fVEHmIDVm73vxlTN4cMi5kbnNjcnlwdC1jZXJ0LnRleG5vbWljLmNvbQ", + "Stamp": + "sdns://AQYAAAAAAAAADTkuOS45LjEwOjg0NDMgZ8hHuMh1jNEgJFVDvnVnRt803x2EwAuMRwNo34Idhj4ZMi5kbnNjcnlwdC1jZXJ0LnF1YWQ5Lm5ldA", "Timeout": 5000 }, @@ -51,14 +51,14 @@ "UDP Protocol": { "Host": "1.1.1.1", "Port": 53, - "Timeout": 1000 + "Timeout": 2000 }, "Proxy Responsibility Chain": { "Middlewares": [ - //"HostTableMiddleware", - //"FilterListsMiddleware", - //"ENSMiddleware", + "HostTableMiddleware", + "FilterListsMiddleware", + "ENSMiddleware", "ResolverMiddleware" ] }, @@ -70,8 +70,8 @@ "Proxy Server": { "Address": "127.0.0.1", - "Port": 53, - "Threads": 1 + "Port": 53 + //"Threads": 1 }, "HostTable Middleware": { diff --git a/Texnomic.SecureDNS.Terminal/Texnomic.SecureDNS.Terminal.csproj b/Texnomic.SecureDNS.Terminal/Texnomic.SecureDNS.Terminal.csproj index bc72652c..a06dd402 100644 --- a/Texnomic.SecureDNS.Terminal/Texnomic.SecureDNS.Terminal.csproj +++ b/Texnomic.SecureDNS.Terminal/Texnomic.SecureDNS.Terminal.csproj @@ -9,12 +9,12 @@ Texnomic SecureDNS Terminal Texnomic Ltd, Copyrights 2020 0.3.0.0 - 0.2.0.0 + 0.3.0.0 https://github.com/Texnomic/SecureDNS Github SecureDNS.ico https://github.com/Texnomic/SecureDNS - 0.2 + 0.3 SecureDNS.ico Alpha diff --git a/Texnomic.SecureDNS.Tests/Core/Serialization.cs b/Texnomic.SecureDNS.Tests/Core/Serialization.cs index 4e358380..9154dc1b 100644 --- a/Texnomic.SecureDNS.Tests/Core/Serialization.cs +++ b/Texnomic.SecureDNS.Tests/Core/Serialization.cs @@ -64,9 +64,8 @@ public void DeserializeMalformed() { var ResponseBytes = new byte[] { - 0x02, 0x69, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x75, 0x67, 0x63, 0x06, - 0x64, 0x75, 0x63, 0x61, 0x74, 0x69, 0x07, 0x6B, 0x6F, 0x6E, 0x74, 0x61, 0x69, 0x6E, 0x03, 0x63, 0x6F, - 0x6D, 0x00, 0x00, 0x01, 0x00 + 0x02, 0xA6, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x73, 0x65, 0x61, 0x08, + 0x63, 0x6F, 0x6D, 0x63, 0x6F, 0x6E, 0x64, 0x75, 0x69, 0x74, 0x03, 0x63, 0x6F, 0x6D, 0x00, 0x00, 0x01 }; var ResponseMessage = DnSerializer.Deserialize(in ResponseBytes);