3

HTTP リクエストを受信し、それらのリクエストを Web サーバーに転送するプログラムを作成する必要があります。

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

どんな助けでも大歓迎です。ありがとうございました。

4

2 に答える 2

1

なんで?彼の理由は何ですか?彼は本当にあなたがすでに行ったのと同じことに対して10倍の費用を払いたいですか?

HTTPプロキシを実装するためにJpcapは必要ありません。これは、完全にjava.netまたはjava.nio内で実行できます。

于 2010-02-15T00:12:59.510 に答える
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. リクエストからのレスポンスを取得し、元のホストからのレスポンスを偽装するネットワーク上にパケットをプッシュしますか?

インバウンド パケットをドロップすることはできないため、目的のホストは要求を処理して、応答としてネットワーク上に独自のパケットをスローしようとしないでしょうか? 私はネットワークの専門家ではないので、知らないことがもっとあるでしょう。この質問は、そのような「シム」を使用して何が可能になるかについて興味をそそられました。

于 2010-03-22T01:58:27.033 に答える