JPCAP를 사용하여 리버스 프록시 생성
-
20-09-2019 - |
문제
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);
}
}
모든 도움은 대단히 감사하겠습니다. 고맙습니다.
해결책
왜요? 그의 이유는 무엇입니까? 그는 당신이 이미 한 것과 같은 일에 대해 10 배의 비용을 지불하고 싶습니까?
HTTP 프록시를 구현하기 위해 JPCAP가 필요하지 않습니다. 전적으로 Java.net 또는 Java.nio 내에서 수행 할 수 있습니다.
다른 팁
나는 당신이 적어도 Windows 상자에서 이런 일을 할 수 있다고 생각하지 않습니다. JPCAP는 WinPCAP의 래퍼 일 뿐이며이 기본 메커니즘은 관찰 된 패킷을 떨어 뜨릴 수 없습니다.
그래서 나는 당신이 "와이어에"리버스 프록시를 구축 할 수있는 방법을 알지 못합니다. 다음을 수행 할 필요가 없습니다.
패킷을 실시간으로 묶고 의도 한 호스트가 수신하지 못하도록함으로써 들어오는 HTTP 요청을 관찰하십시오.
구현하는 대리 규칙에 따라 대체 HTTP 요청을 작성하십시오.
귀하의 요청에서 응답을 잡고 원래 호스트의 응답을 가짜 와이어에 패킷을 푸시하십시오.
인바운드 패킷을 떨어 뜨릴 수 없으므로 의도 된 호스트는 요청을 처리하고 응답으로 자체 패킷을 와이어에 던지려고 시도하지 않습니까? 네트워킹 전문가가 아니기 때문에 내가 모르는 것이 훨씬 더 많을 것입니다. 이 질문은 그러한 "심"을 사용하여 가능한 것이 무엇인지 궁금하게 만들었습니다.