gpt4 book ai didi

java - 使用 Jpcap 创建反向代理

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:41:56 28 4
gpt4 key购买 nike

我需要创建一个程序来接收 HTTP 请求并将这些请求转发到网络服务器。

Diagram http://img269.imageshack.us/img269/1862/h98trsly.jpg

我仅使用 Java 套接字就成功地实现了这一点,但客户需要在 Jpcap 中实现该程序。我想知道这是否可能以及我应该为这个项目阅读哪些文献。

这就是我现在通过将 Jpcap 教程中的片段拼接在一起而得到的:

import java.net.InetAddress;
import java.io.*;
import jpcap.*;
import jpcap.packet.*;


public class Router {
public static void main(String args[]) {
//Obtain the list of network interfaces
NetworkInterface[] devices = JpcapCaptor.getDeviceList();

//for each network interface
for (int i = 0; i < devices.length; i++) {
//print out its name and description
System.out.println(i+": "+devices[i].name + "(" + devices[i].description+")");

//print out its datalink name and description
System.out.println(" datalink: "+devices[i].datalink_name + "(" + devices[i].datalink_description+")");

//print out its MAC address
System.out.print(" MAC address:");
for (byte b : devices[i].mac_address)
System.out.print(Integer.toHexString(b&0xff) + ":");
System.out.println();

//print out its IP address, subnet mask and broadcast address
for (NetworkInterfaceAddress a : devices[i].addresses)
System.out.println(" address:"+a.address + " " + a.subnet + " "+ a.broadcast);
}

int index = 1; // set index of the interface that you want to open.

//Open an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms)
JpcapCaptor captor = null;
try {
captor = JpcapCaptor.openDevice(devices[index], 65535, false, 20);
captor.setFilter("port 80 and host 192.168.56.1", true);
} catch(java.io.IOException e) {
System.err.println(e);
}

//call processPacket() to let Jpcap call PacketPrinter.receivePacket() for every packet capture.
captor.loopPacket(-1,new PacketPrinter());

captor.close();
}
}

class PacketPrinter implements PacketReceiver {
//this method is called every time Jpcap captures a packet
public void receivePacket(Packet p) {
JpcapSender sender = null;
try {
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
sender = JpcapSender.openDevice(devices[1]);
} catch(IOException e) {
System.err.println(e);
}


IPPacket packet = (IPPacket)p;

try {
// IP Address of machine sending HTTP requests (the client)
// It's still on the same LAN as the servers for testing purposes.
packet.dst_ip = InetAddress.getByName("192.168.56.2");
} catch(java.net.UnknownHostException e) {
System.err.println(e);
}

//create an Ethernet packet (frame)
EthernetPacket ether=new EthernetPacket();
//set frame type as IP
ether.frametype=EthernetPacket.ETHERTYPE_IP;
//set source and destination MAC addresses

// MAC Address of machine running reverse proxy server
ether.src_mac = new MacAddress("08:00:27:00:9C:80").getAddress();
// MAC Address of machine running web server
ether.dst_mac = new MacAddress("08:00:27:C7:D2:4C").getAddress();

//set the datalink frame of the packet as ether
packet.datalink=ether;

//send the packet
sender.sendPacket(packet);

sender.close();

//just print out a captured packet
System.out.println(packet);
}
}

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

为什么?他的理由是什么?他真的愿意为你已经做过的同样事情付出十倍的代价吗?

您不需要 Jpcap 来实现 HTTP 代理。它可以完全在 java.net 或 java.nio 中完成。

关于java - 使用 Jpcap 创建反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2260710/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com