A: 添加网络相关工具类 (NetUtils, SysInfoUtils) 及模型 (NetInfo)

A: 新增网络事件捕获及诊断功能支持 (TCP/UDP 连接、进程信息获取)
U: 更新项目引用,Shell 添加对 Infrastructure 的依赖
U: 优化 WebPanel 消息处理逻辑,支持多种新指令 (GetNetInfo 等)
U: 调整 ClientApp 中 WinDivertCommand.vue 的接口调用逻辑
D: 移除 Core 项目及相关配置
This commit is contained in:
jimmy committed 2025-05-10 22:46:59 +08:00
1 parent b14594ffb3
commit 0feeb00fae
11 files changed
+556 -29

No files matched your search

+202
View File
@@ -0,0 +1,202 @@
using System.Net;
using System.Runtime.InteropServices;
namespace Infrastructure.utils;
using Microsoft.Diagnostics.Tracing.Parsers;
using System;
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Parsers.Kernel;
using Microsoft.Diagnostics.Tracing.Session;
public class NetUtils
{
/// <summary>
/// 联网内容监视
/// </summary>
public static void GetNetInfo()
{
if (TraceEventSession.IsElevated() == false)
{
Console.WriteLine("请以管理员身份运行此程序。");
return;
}
using (var session = new TraceEventSession("NetTraceSession"))
{
// 启用 Kernel 网络事件
session.EnableKernelProvider(
KernelTraceEventParser.Keywords.NetworkTCPIP);
Console.WriteLine("监听网络事件中(按 Ctrl+C 停止)...");
session.Source.Kernel.TcpIpConnect += data =>
{
Console.WriteLine(
$"[TCP Connect] PID={data.ProcessID} {data.saddr}:{data.sport} -> {data.daddr}:{data.dport}");
};
session.Source.Kernel.TcpIpDisconnect += data =>
{
Console.WriteLine(
$"[TCP Disconnect] PID={data.ProcessID} {data.saddr}:{data.sport} -> {data.daddr}:{data.dport}");
};
session.Source.Kernel.TcpIpSend += data =>
{
Console.WriteLine(
$"[TCP Send] PID={data.ProcessID} {data.saddr}:{data.sport} -> {data.daddr}:{data.dport}, Size={data.size} bytes");
};
session.Source.Kernel.TcpIpRecv += data =>
{
Console.WriteLine(
$"[TCP Recv] PID={data.ProcessID} {data.saddr}:{data.sport} <- {data.daddr}:{data.dport}, Size={data.size} bytes");
};
session.Source.Kernel.UdpIpSend += data =>
{
Console.WriteLine(
$"[UDP Send] PID={data.ProcessID} {data.saddr}:{data.sport} -> {data.daddr}:{data.dport}, Size={data.size} bytes");
};
session.Source.Kernel.UdpIpRecv += data =>
{
Console.WriteLine(
$"[UDP Recv] PID={data.ProcessID} {data.saddr}:{data.sport} <- {data.daddr}:{data.dport}, Size={data.size} bytes");
};
session.Source.Process(); // 开始监听
}
}
private enum TCP_TABLE_CLASS
{
TCP_TABLE_BASIC_LISTENER,
TCP_TABLE_BASIC_CONNECTIONS,
TCP_TABLE_BASIC_ALL,
TCP_TABLE_OWNER_PID_LISTENER,
TCP_TABLE_OWNER_PID_CONNECTIONS,
TCP_TABLE_OWNER_PID_ALL, // 我们使用这个
TCP_TABLE_OWNER_MODULE_LISTENER,
TCP_TABLE_OWNER_MODULE_CONNECTIONS,
TCP_TABLE_OWNER_MODULE_ALL
}
/// <summary>
/// 获取所有连接
/// </summary>
public static void GetAllTcpConnections()
{
int buffSize = 0;
GetExtendedTcpTable(IntPtr.Zero, ref buffSize, true, 2, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);
IntPtr tcpTablePtr = Marshal.AllocHGlobal(buffSize);
try
{
if (GetExtendedTcpTable(tcpTablePtr, ref buffSize, true, 2, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0) ==
0)
{
int rowCount = Marshal.ReadInt32(tcpTablePtr);
IntPtr rowPtr = IntPtr.Add(tcpTablePtr, 4);
for (int i = 0; i < rowCount; i++)
{
MIB_TCPROW_OWNER_PID row = Marshal.PtrToStructure<MIB_TCPROW_OWNER_PID>(rowPtr);
IPAddress localIp = new IPAddress(row.localAddr);
IPAddress remoteIp = new IPAddress(row.remoteAddr);
int localPort = ntohs((ushort)row.localPort);
int remotePort = ntohs((ushort)row.remotePort);
Console.WriteLine(
$"PID: {row.owningPid}, {localIp}:{localPort} -> {remoteIp}:{remotePort}, State: {row.state}");
rowPtr = IntPtr.Add(rowPtr, Marshal.SizeOf<MIB_TCPROW_OWNER_PID>());
}
}
}
finally
{
Marshal.FreeHGlobal(tcpTablePtr);
}
}
// 网络字节序转主机字节序
private static ushort ntohs(ushort netshort) => (ushort)((netshort >> 8) | (netshort << 8));
[DllImport("iphlpapi.dll", SetLastError = true)]
private static extern uint GetExtendedTcpTable(
IntPtr pTcpTable,
ref int dwOutBufLen,
bool sort,
int ipVersion,
TCP_TABLE_CLASS tableClass,
uint reserved = 0);
[StructLayout(LayoutKind.Sequential)]
public struct MIB_TCPROW_OWNER_PID
{
public uint state; // 状态:如 ESTABLISHED, LISTENING
public uint localAddr; // 本地 IP
public uint localPort; // 本地端口(需要字节序转换)
public uint remoteAddr; // 远程 IP
public uint remotePort; // 远程端口
public int owningPid; // 对应的进程 PID
}
[StructLayout(LayoutKind.Sequential)]
public struct MIB_TCPTABLE_OWNER_PID
{
public uint dwNumEntries; // 总连接数
public MIB_TCPROW_OWNER_PID table; // 实际连接(数组)
}
[DllImport("iphlpapi.dll", SetLastError = true)]
private static extern uint GetExtendedUdpTable(
IntPtr pUdpTable,
ref int dwOutBufLen,
bool sort,
int ipVersion,
UDP_TABLE_CLASS tableClass,
uint reserved = 0);
public enum UDP_TABLE_CLASS
{
UDP_TABLE_BASIC,
UDP_TABLE_OWNER_PID,
UDP_TABLE_OWNER_MODULE
}
[StructLayout(LayoutKind.Sequential)]
public struct MIB_UDPROW_OWNER_PID
{
public uint localAddr; // 本地 IP
public uint localPort; // 本地端口(需要字节序转换)
public int owningPid; // 进程 PID
}
public static void GetAllUdpListeners()
{
int buffSize = 0;
GetExtendedUdpTable(IntPtr.Zero, ref buffSize, true, 2, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0);
IntPtr udpTablePtr = Marshal.AllocHGlobal(buffSize);
try
{
if (GetExtendedUdpTable(udpTablePtr, ref buffSize, true, 2, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0) == 0)
{
int rowCount = Marshal.ReadInt32(udpTablePtr);
IntPtr rowPtr = IntPtr.Add(udpTablePtr, 4);
for (int i = 0; i < rowCount; i++)
{
MIB_UDPROW_OWNER_PID row = Marshal.PtrToStructure<MIB_UDPROW_OWNER_PID>(rowPtr);
IPAddress localIp = new IPAddress(row.localAddr);
int localPort = ntohs1((ushort)row.localPort);
Console.WriteLine($"PID: {row.owningPid}, UDP {localIp}:{localPort}");
rowPtr = IntPtr.Add(rowPtr, Marshal.SizeOf<MIB_UDPROW_OWNER_PID>());
}
}
}
finally
{
Marshal.FreeHGlobal(udpTablePtr);
}
}
private static ushort ntohs1(ushort netshort) => (ushort)((netshort >> 8) | (netshort << 8));
}
+108
View File
@@ -0,0 +1,108 @@
using System.Text.Json;
namespace Infrastructure.utils;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
public static class SysInfoUtils
{
public static string GetProgramDiagnostics()
{
var sb = new StringBuilder();
// === 程序自身信息 ===
var asm = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
sb.AppendLine("=== 程序信息 ===");
sb.AppendLine($"程序集名称:{asm.GetName().Name}");
sb.AppendLine($"版本号:{asm.GetName().Version}");
sb.AppendLine($"路径:{asm.Location}");
sb.AppendLine($"启动目录:{AppDomain.CurrentDomain.BaseDirectory}");
sb.AppendLine($"命令行参数:{string.Join(" ", Environment.GetCommandLineArgs())}");
sb.AppendLine();
// === 进程运行信息 ===
var proc = Process.GetCurrentProcess();
sb.AppendLine("=== 进程状态 ===");
sb.AppendLine($"进程 ID:{proc.Id}");
sb.AppendLine($"进程名称:{proc.ProcessName}");
sb.AppendLine($"启动时间:{proc.StartTime}");
sb.AppendLine($"运行时长:{DateTime.Now - proc.StartTime}");
sb.AppendLine($"线程数:{proc.Threads.Count}");
sb.AppendLine($"内存占用:{FormatBytes(proc.WorkingSet64)}");
sb.AppendLine();
// === 系统环境信息 ===
sb.AppendLine("=== 系统环境 ===");
sb.AppendLine($"操作系统:{RuntimeInformation.OSDescription}");
sb.AppendLine($"架构:{(Environment.Is64BitOperatingSystem ? "64位" : "32位")}");
sb.AppendLine($"CPU 核心数:{Environment.ProcessorCount}");
sb.AppendLine($"机器名:{Environment.MachineName}");
sb.AppendLine($"当前用户:{Environment.UserName}");
sb.AppendLine();
return sb.ToString();
}
public static void InspectProcess(int pid)
{
try
{
var proc = Process.GetProcessById(pid);
Console.WriteLine($"进程名称:{proc.ProcessName}");
Console.WriteLine($"进程ID:{proc.Id}");
Console.WriteLine($"启动时间:{proc.StartTime}");
Console.WriteLine($"是否退出:{proc.HasExited}");
Console.WriteLine($"退出时间:{proc.ExitTime}");
Console.WriteLine($"退出代码:{proc.ExitCode}");
Console.WriteLine($"占用内存:{FormatBytes(proc.WorkingSet64)}");
Console.WriteLine($"线程数:{proc.Threads.Count}");
Console.WriteLine($"主模块路径:{proc.MainModule?.FileName}");
Console.WriteLine($"启动文件名:{proc.MainModule?.ModuleName}");
var serialize = JsonSerializer.Serialize(proc.MainModule);
Console.WriteLine($"YYY:{serialize}");
var filePath = proc.MainModule?.FileName;
if (filePath != null)
{
var versionInfo = FileVersionInfo.GetVersionInfo(filePath);
Console.WriteLine($"文件描述:{versionInfo.FileDescription}");
Console.WriteLine($"产品名称:{versionInfo.ProductName}");
Console.WriteLine($"公司名称:{versionInfo.CompanyName}");
Console.WriteLine($"版本号:{versionInfo.FileVersion}");
Console.WriteLine($"版权:{versionInfo.LegalCopyright}");
var serialize1 = JsonSerializer.Serialize(versionInfo);
Console.WriteLine($"XXX:{serialize1}");
}
}
catch (Exception ex)
{
Console.WriteLine($"获取进程信息失败:{ex.Message}");
}
}
private static string FormatBytes(long bytes)
{
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
double len = bytes;
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order++;
len /= 1024;
}
return $"{len:0.##} {sizes[order]}";
}
}