[英]Read Packet Content using pcap4j
我有下面的代碼,它可以使用 pcap4j 從接口捕獲數據包,但我不確定如何打印數據包中存在的請求和響應數據。 例如,如果我從 postman 發出 REST 調用,那么我想跟蹤請求和響應。 這與 Wireshark 相同。 我陷入了能夠捕獲數據包的最后一部分,但不確定如何讀取可以在控制台上打印的數據包內容。
try {
InetAddress addr = InetAddress.getByName("10.227.178.25");
PcapNetworkInterface device = Pcaps.getDevByAddress(addr);
System.out.println("You chose: " + device);
int snapshotLength = 64 * 1024; // in bytes
int readTimeout = 50; // in milliseconds
final PcapHandle handle;
handle = device.openLive(snapshotLength, PromiscuousMode.PROMISCUOUS, readTimeout);
String filter = "tcp port 80";
handle.setFilter(filter, BpfCompileMode.OPTIMIZE);
// Create a listener that defines what to do with the received packets
PacketListener listener = new PacketListener() {
@Override
public void gotPacket(Packet packet) {
// Override the default gotPacket() function and process packet
System.out.println(handle.getTimestamp());
System.out.println(packet);
byte[] b = packet.getRawData();
Packet p = packet.getPayload();
}
};
// Tell the handle to loop using the listener we created
try {
int maxPackets = 50;
handle.loop(maxPackets, listener);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Cleanup when complete
handle.close();
}catch(Exception e) {
e.printStackTrace();
}
所以我有兩個問題:
我確實檢查了 pcap4j 文檔,但不確定如何讀取可以讀取 HTTP 請求和 HTTP 響應的數據包內容。
對於第一個問題:
如果將 [maxPackets] 設置為 -1,它將連續運行。 你可以從官方 Sample 中看到很多這樣的實現。
至於第二個問題:
目前官方庫不支持 Http 包。 您需要自己手動實現。 您可以查看https://github.com/kaitoy/pcap4j/issues/85 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.