题
我需要建立一个程序,接收HTTP请求,并转发这些请求的网络服务器。
图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内进行。
其他提示
我不认为你可以做到这一点,至少在一个窗户框。Jpcap只是一个包装Winpcap,这一根本机构不可能下降,观察到包:
所以,我不要看你怎么可以建立一个反向代理"的线。" 你不必做到以下几点:
观察来HTTP请求通过拼凑在一起,分组在实的时间放他们从收到预期的主机。
让备用HTTP请求基于任何代理的规则你都实施。
抢回应你的请求,并推动分组在线,其假响应从原来的主人吗?
因为你不能落入包,也不会预期东道尝试处理的请求,并把报其自身上线的反应?有的甚至可能更多,我不知道因为我没有网络的专家。这个问题让我好奇什么将有可能使用这样的"垫".
不隶属于 StackOverflow