From f33ceae6764f1a6b4d0abbd668e9e3016d3eb5fd Mon Sep 17 00:00:00 2001 From: DJ Daemonix Date: Sat, 29 Jun 2019 00:45:24 +0200 Subject: [PATCH 1/2] Replace ICMP Ping by TCP Ping Fixing Issue #9 + Added Debug Flag on Status Page --- .../Network/PingModule.cs | 65 +++++++++++++++---- src/Nodsoft.YumeChan.Modules/Status/Status.cs | 3 + .../Nodsoft.YumeChan.NetRunner.csproj | 2 +- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/src/Nodsoft.YumeChan.Modules/Network/PingModule.cs b/src/Nodsoft.YumeChan.Modules/Network/PingModule.cs index 0409862..3c6c56f 100644 --- a/src/Nodsoft.YumeChan.Modules/Network/PingModule.cs +++ b/src/Nodsoft.YumeChan.Modules/Network/PingModule.cs @@ -2,9 +2,11 @@ using Discord.Commands; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Net; using System.Net.NetworkInformation; +using System.Net.Sockets; using System.Text; using System.Threading.Tasks; @@ -13,12 +15,6 @@ namespace Nodsoft.YumeChan.Modules.Network [Group("ping")] public class PingModule : ModuleBase { - //[Command] - public async Task SimplePingCommand() - { - await ReplyAsync("Pong !"); - } - [Command("")] public async Task NetworkPingCommand(string host) { @@ -36,7 +32,7 @@ public async Task NetworkPingCommand(string host) // 2. Ping the IP int pingCount = 4; - PingReply[] pingReplies = ComplexPing(hostResolved, pingCount).Result; + PingModuleReply[] pingReplies = TcpPing(hostResolved, 80, pingCount).Result; // 3. Retrieve statistics // 4. Return results to user with ReplyAsync(); (Perhaps Embed ?) List roundTripTimings = new List(); @@ -67,10 +63,10 @@ public async Task NetworkPingCommand(string host) await ReplyAsync(message: contextUser, embed: embedBuilder.Build()); //Quote user in main message, and attach Embed. } - internal static async Task ComplexPing(IPAddress host, int count) => await ComplexPing(host, count, 2000, new PingOptions(64, true)).ConfigureAwait(false); - internal static async Task ComplexPing(IPAddress host, int count, int timeout, PingOptions options) + internal static async Task ComplexPing(IPAddress host, int count) => await ComplexPing(host, count, 2000, new PingOptions(64, true)).ConfigureAwait(false); + internal static async Task ComplexPing(IPAddress host, int count, int timeout, PingOptions options) { - PingReply[] pingReplies = new PingReply[count]; + PingModuleReply[] pingReplies = new PingModuleReply[count]; // Create a buffer of 32 bytes of data to be transmitted. byte[] buffer = Encoding.ASCII.GetBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); @@ -79,12 +75,59 @@ internal static async Task ComplexPing(IPAddress host, int count, i for (int i = 0; i < count; i++) { Ping ping = new Ping(); - pingReplies[i] = await ping.SendPingAsync(host, timeout, buffer, options).ConfigureAwait(false); + pingReplies[i] = new PingModuleReply(await ping.SendPingAsync(host, timeout, buffer, options).ConfigureAwait(false)); ping.Dispose(); } return pingReplies; } + // See : https://stackoverflow.com/questions/26067342/how-to-implement-psping-tcp-ping-in-c-sharp + internal static Task TcpPing(IPAddress host, int port, int count) + { + PingModuleReply[] pingReplies = new PingModuleReply[count]; + for (int i = 0; i < count; i++) + { + Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + socket.Blocking = true; + + Stopwatch latencyMeasurement = new Stopwatch(); + IPStatus? status = null; + try + { + latencyMeasurement.Start(); + socket.Connect(host, port); + latencyMeasurement.Stop(); + } + catch (Exception) + { + status = IPStatus.TimedOut; + } + + pingReplies[i] = new PingModuleReply(host, latencyMeasurement.ElapsedMilliseconds, status ?? IPStatus.Success ); + } + + return Task.FromResult(pingReplies); + } + + internal struct PingModuleReply + { + internal IPAddress Host { get; } + internal long RoundtripTime { get; } + internal IPStatus Status { get; } + + public PingModuleReply(IPAddress host, long roundtripTime, IPStatus status) + { + Host = host; + RoundtripTime = roundtripTime; + Status = status; + } + public PingModuleReply(PingReply reply) + { + Host = reply.Address; + RoundtripTime = reply.RoundtripTime; + Status = reply.Status; + } + } } } diff --git a/src/Nodsoft.YumeChan.Modules/Status/Status.cs b/src/Nodsoft.YumeChan.Modules/Status/Status.cs index 8fcd607..b9d1aae 100644 --- a/src/Nodsoft.YumeChan.Modules/Status/Status.cs +++ b/src/Nodsoft.YumeChan.Modules/Status/Status.cs @@ -16,6 +16,9 @@ public async Task DefaultAsync() .WithDescription("Status : Online") .AddField("Core", $"Version : {(CoreVersion != null ? CoreVersion.ToString() : MissingVersionSubstitute)}", true) .AddField("Modules", $"Version : {ModulesVersion}", true); +#if DEBUG + embed.AddField("Debug", "Debug Build Active."); +#endif await ReplyAsync(embed: embed.Build()); } diff --git a/src/Nodsoft.YumeChan.NetRunner/Nodsoft.YumeChan.NetRunner.csproj b/src/Nodsoft.YumeChan.NetRunner/Nodsoft.YumeChan.NetRunner.csproj index 1686097..5a7e384 100644 --- a/src/Nodsoft.YumeChan.NetRunner/Nodsoft.YumeChan.NetRunner.csproj +++ b/src/Nodsoft.YumeChan.NetRunner/Nodsoft.YumeChan.NetRunner.csproj @@ -2,7 +2,7 @@ netcoreapp3.0 - 8.0 + preview 0.1.2 DJ Daemonix Nodsoft ES From 4fbaf24e7badd08ba4989d94c76fd671b030173d Mon Sep 17 00:00:00 2001 From: DJ Daemonix Date: Sat, 29 Jun 2019 01:20:21 +0200 Subject: [PATCH 2/2] Versioning to 0.1.3 --- .../Nodsoft.YumeChan.ConsoleRunner.csproj | 2 +- src/Nodsoft.YumeChan.Core/Nodsoft.YumeChan.Core.csproj | 2 +- src/Nodsoft.YumeChan.Modules/Nodsoft.YumeChan.Modules.csproj | 2 +- .../Nodsoft.YumeChan.NetRunner.csproj | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Nodsoft.YumeChan.ConsoleRunner/Nodsoft.YumeChan.ConsoleRunner.csproj b/src/Nodsoft.YumeChan.ConsoleRunner/Nodsoft.YumeChan.ConsoleRunner.csproj index 045367e..3fb580b 100644 --- a/src/Nodsoft.YumeChan.ConsoleRunner/Nodsoft.YumeChan.ConsoleRunner.csproj +++ b/src/Nodsoft.YumeChan.ConsoleRunner/Nodsoft.YumeChan.ConsoleRunner.csproj @@ -4,7 +4,7 @@ Exe netcoreapp3.0 8.0 - 0.1.2 + 0.1.3 DJ Daemonix Nodsoft ES YumeChan diff --git a/src/Nodsoft.YumeChan.Core/Nodsoft.YumeChan.Core.csproj b/src/Nodsoft.YumeChan.Core/Nodsoft.YumeChan.Core.csproj index 36c6f64..bb6929f 100644 --- a/src/Nodsoft.YumeChan.Core/Nodsoft.YumeChan.Core.csproj +++ b/src/Nodsoft.YumeChan.Core/Nodsoft.YumeChan.Core.csproj @@ -2,7 +2,7 @@ netstandard2.1 - 0.1.2 + 0.1.3 DJ Daemonix Nodsoft ES YumeChan diff --git a/src/Nodsoft.YumeChan.Modules/Nodsoft.YumeChan.Modules.csproj b/src/Nodsoft.YumeChan.Modules/Nodsoft.YumeChan.Modules.csproj index 6421621..6d3983d 100644 --- a/src/Nodsoft.YumeChan.Modules/Nodsoft.YumeChan.Modules.csproj +++ b/src/Nodsoft.YumeChan.Modules/Nodsoft.YumeChan.Modules.csproj @@ -2,7 +2,7 @@ netstandard2.1 - 0.1.2 + 0.1.3 DJ Daemonix Nodsoft ES YumeChan Modules diff --git a/src/Nodsoft.YumeChan.NetRunner/Nodsoft.YumeChan.NetRunner.csproj b/src/Nodsoft.YumeChan.NetRunner/Nodsoft.YumeChan.NetRunner.csproj index 5a7e384..9743307 100644 --- a/src/Nodsoft.YumeChan.NetRunner/Nodsoft.YumeChan.NetRunner.csproj +++ b/src/Nodsoft.YumeChan.NetRunner/Nodsoft.YumeChan.NetRunner.csproj @@ -3,7 +3,7 @@ netcoreapp3.0 preview - 0.1.2 + 0.1.3 DJ Daemonix Nodsoft ES YumeChan