使用Jpcap创建反向代理

时间:2010-02-14 10:07:39

标签: java reverse-proxy jpcap

我需要创建一个接收HTTP请求的程序,并将这些请求转发给Web服务器。

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);
    }
}

非常感谢任何帮助。谢谢。

2 个答案:

答案 0 :(得分:1)

为什么呢?他的理由是什么?他是否真的想为你已经做过的事情支付十倍的费用?

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

答案 1 :(得分:1)

我认为你不能实现这一点,至少在Windows机器上是这样。 Jpcap只是Winpcap的包装器,这种底层机制不能丢弃观察到的数据包:

http://www.mirrorservice.org/sites/ftp.wiretapped.net/pub/security/packet-capture/winpcap/misc/faq.htm#Q-17

所以,我看不出你如何在线上构建反向代理。你不必做以下事情:

  1. 通过实时拼接数据包并将其从目标主机接收来观察传入的HTTP请求。

  2. 根据您正在实施的任何代理规则发出备用HTTP请求。

  3. 从您的请求中获取响应,然后将数据包中的数据包推出,从而伪造原始主机的响应?

  4. 由于您无法丢弃入站数据包,因此预期的主机是否会尝试处理请求并将自己的数据包抛出到线路上作为响应?可能还有更多我不知道的事情,因为我不是网络专家。这个问题让我很好奇使用这种“垫片”会有什么可能。