订阅
OnPacketArrival
活动不起作用。知道我做错了什么吗?
using System;
using PacketDotNet;
using SharpPcap;
Console.WriteLine("Starting packet capture...");
var devices = CaptureDeviceList.Instance;
if (devices.Count == 0)
{
Console.WriteLine("No network devices found.");
return;
}
var device = devices[0];
Console.WriteLine(device.ToString());
try
{
device.Open();
device.OnPacketArrival += (sender, e) =>
{
try
{
var packet = Packet.ParsePacket(e.GetPacket().LinkLayerType, e.GetPacket().Data);
if (packet is EthernetPacket ethernetPacket)
{
if (ethernetPacket.PayloadPacket is IPPacket ipPacket)
{
if (ipPacket.DestinationAddress.ToString() == "54.220.192.176" &&
ipPacket.PayloadPacket is TcpPacket tcpPacket &&
tcpPacket.DestinationPort == 443) // HTTPS port
{
var sourceIp = ipPacket.SourceAddress;
var destinationIp = ipPacket.DestinationAddress;
var sourcePort = tcpPacket.SourcePort;
var destinationPort = tcpPacket.DestinationPort;
Console.WriteLine($"HTTPS Packet: Source IP: {sourceIp}, Source Port: {sourcePort}, Destination IP: {destinationIp}, Destination Port: {destinationPort}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error processing packet: {ex.Message}");
}
};
device.StartCapture();
Console.WriteLine("Capturing packets. Press ENTER to stop.");
Console.ReadLine();
device.StopCapture();
}
catch (Exception ex)
{
Console.WriteLine($"Error capturing traffic: {ex.Message}");
}
finally
{
device.Close();
}
您可以检查您正在监听的设备是绑定到您的外部 IP 地址的设备还是该设备 0 是否有任何地址。 为此目的有“
Addresses
”列表属性。