Browse Source

Release: ConnectionStat

HOME 1 year ago
parent
commit
3d8aea61cc
3 changed files with 55 additions and 13 deletions
  1. 20 0
      ConnectionStat/ConnectionStat.csproj
  2. 31 13
      ConnectionStat/Program.cs
  3. 4 0
      ConnectionStat/packages.config

+ 20 - 0
ConnectionStat/ConnectionStat.csproj

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props" Condition="Exists('C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props')" />
   <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -11,6 +12,8 @@
     <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
     <Deterministic>true</Deterministic>
+    <NuGetPackageImportStamp>
+    </NuGetPackageImportStamp>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <PlatformTarget>AnyCPU</PlatformTarget>
@@ -31,6 +34,9 @@
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
+  <PropertyGroup>
+    <RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
+  </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
   </ItemGroup>
@@ -40,5 +46,19 @@
   <ItemGroup>
     <Folder Include="Properties\" />
   </ItemGroup>
+  <ItemGroup>
+    <None Include="packages.config" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
+    <PropertyGroup>
+      <ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
+    </PropertyGroup>
+    <Error Condition="!Exists('C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props')" Text="$([System.String]::Format('$(ErrorText)', 'C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props'))" />
+  </Target>
+  <PropertyGroup>
+    <PostBuildEvent>if $(ConfigurationName) == Release if not exist "$(TargetDir)Packed" md "$(TargetDir)Packed"
+if $(ConfigurationName) == Release $(ILRepack) /ndebug "/out:$(TargetDir)Packed\$(TargetFileName)" "$(TargetPath)"
+if $(ConfigurationName) == Release if exist "$(TargetDir)Packed\$(TargetFileName).config" del "$(TargetDir)Packed\$(TargetFileName).config"</PostBuildEvent>
+  </PropertyGroup>
 </Project>

+ 31 - 13
ConnectionStat/Program.cs

@@ -17,26 +17,35 @@ using System.Reflection;
 
 namespace ConnectionStat
 {
-    class Program
+    internal class Program
     {
-        static void Main(string[] args)
+        private static void Main(string[] args)
         {
             var all = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections();
 
             if (all.Length == 0)
             {
-                Console.WriteLine("GetActiveTcpConnections return 0 items.");
+                Console.WriteLine("Unexpected: GetActiveTcpConnections return 0 items.");
                 return;
             }
 
-            var allDic = all.GroupBy(p => p.State).Select(p => new { State = p.Key, Count = p.Count() }).Where(p => p.Count != 0).ToDictionary(p => p.State, p => p.Count);
+            var allDic = all.GroupBy(p => p.State)
+                .Select(p => new { State = p.Key, Count = p.Count() })
+                .ToDictionary(p => p.State, p => p.Count);
 
-            if (args.Length == 0)
-            {
-                PrintConnectionStat(allDic, "All");
-            }
-            else
+            PrintConnectionStat(allDic, "<ALL IN THIS HOST>");
+
+            var topRemote = all.GroupBy(p => p.RemoteEndPoint.Address)
+               .Select(p => new { Address = p.Key, Count = p.Count() })
+               .OrderByDescending(p => p.Count)
+               .FirstOrDefault();
+
+            if(topRemote!=null) Console.WriteLine($"   Top Remote Address: {topRemote.Address}, Connections: {topRemote.Count}");
+
+            if (args.Length != 0)
             {
+                var dicTotal = new Dictionary<TcpState, int>();
+
                 foreach (var argString in args)
                 {
                     if (!int.TryParse(argString, out var port)) continue;
@@ -45,19 +54,28 @@ namespace ConnectionStat
                     var portDic = filterByPort.GroupBy(p => p.State).Select(p => new { State = p.Key, Count = p.Count() }).Where(p => p.Count != 0).ToDictionary(p => p.State, p => p.Count);
 
                     PrintConnectionStat(portDic, "Port " + port);
+
+                    foreach (var kvp in portDic)
+                    {
+                        if (dicTotal.ContainsKey(kvp.Key)) dicTotal[kvp.Key] += kvp.Value;
+                        else dicTotal[kvp.Key] = kvp.Value;
+                    }
                 }
+
+                if (dicTotal.Count > 1) PrintConnectionStat(dicTotal, "<SUM OF LISTED PORTS>");
             }
         }
 
         private static void PrintConnectionStat(Dictionary<TcpState, int> allDic, string label)
         {
-            Console.WriteLine($" * Connections of {label}");
+            Console.Write($" * Connections of {label}");
+            Console.Write($"  TOTAL={allDic.Sum(p => p.Value)}");
             foreach (var kvp in allDic)
             {
-                Console.Write($"    {kvp.Key}={kvp.Value}");
+                Console.Write($"  {kvp.Key}={kvp.Value}");
             }
-            Console.WriteLine($"   TOTAL={allDic.Sum(p => p.Value)}");
+
             Console.WriteLine();
         }
     }
-}
+}

+ 4 - 0
ConnectionStat/packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="ILRepack" version="2.0.18" targetFramework="net35" />
+</packages>