Domanda

Ho bisogno di creare un programma che riceve richiesta HTTP e inoltra le richieste ai server web.

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

Ho fatto con successo questo utilizzando solo Sockets Java ma il client necessario il programma da attuare in Jpcap. Mi piacerebbe sapere se questo è possibile e ciò che la letteratura io dovrei leggere per questo progetto.

Questo è quello che ho adesso cucendo insieme pezzi dal tutorial 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);
    }
}

Qualsiasi aiuto sarebbe molto apprezzato. Grazie.

È stato utile?

Soluzione

Perché? Quali sono le sue ragioni? Vuole davvero a pagare dieci volte il costo per la stessa cosa che hai già fatto?

Non è necessario implementare Jpcap proxy HTTP. Esso può essere fatto interamente all'interno java.net o java.nio.

Altri suggerimenti

Non credo che si può fare questo accadere, almeno su una macchina Windows. Jpcap è solo un involucro per Winpcap, e questo meccanismo sottostante non può elaborare i pacchetti rilevate:

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

Quindi, non vedo come si può costruire un proxy inverso "sul filo". Non ti hanno a che fare il seguente:

  1. Osservare una richiesta HTTP in entrata mettendo insieme i pacchetti in tempo reale e rilasciandoli da essere ricevuto da l'host di destinazione.

  2. Fare la richiesta HTTP si alternano in base a ciò che le regole proxy che si sta implementando.

  3. Afferra la risposta dalla richiesta e spingere fuori i pacchetti sul filo, che simulare una risposta dall'host originale?

Dal momento che non è possibile eliminare i pacchetti in entrata, non sarà il tentativo destinato host per gestire la richiesta e gettare i pacchetti del proprio sul filo, in risposta? Probabilmente c'è ancora di più che io non so come io non sono un esperto di networking. Questa domanda mi ha fatto solo curioso di sapere quello che sarebbe possibile con un tale "spessore".

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top