[英]How to get Client Ip address at Proxy server in HttpResponse in java?
我正在嘗試在 Java 中實現一個代理服務器,它將從客戶端瀏覽器獲取 HttpRequest,並將其轉發到服務器。 從服務器接收到 HttpResponse 后,它提取 cookie 並修改 cookie,然后將帶有修改后的 cookie 的 HttpResponse 轉發到客戶端瀏覽器。 我這樣做是為了防止濫用 cookie 進行會話劫持。
下次,當客戶端嘗試連接同一台服務器時,我的代理服務器將使用此修改后的 cookie 獲取 HttpRequest。 在將該 HttpRequest 轉發到服務器之前,我的代理服務器將用原始 cookie 替換此修改后的 cookie,服務器將響應,因為它將檢測到正確的 cookie。
這種方法的一個缺點是,如果同一局域網的其他客戶端成為攻擊者並竊取客戶端的修改后的 cookie 並將 HttpRequest 發送到代理服務器,代理服務器會將修改后的 cookie 替換為原始 cookie 並轉發到服務器,攻擊者將能夠執行會話劫持。
為了解決這個問題,我試圖通過以下方式實現這一點。
而不是 (cookie original value= modified value),我將存儲 (Ip, cookie original value)= modified value。 因此,每當 HttpReqest 來自客戶端瀏覽器時,我都會檢查發件人的 Ip 地址以及與此修改后的 cookie 的原始 cookie 一起存儲的 Ip 地址。
我嘗試在一台機器上運行此代碼,在那里我已將所有瀏覽器請求重定向到本地端口 1111,並且我能夠獲取客戶端 Ip 地址。
public static void runServer(String host, int remoteport, int localport)
throws IOException {
// Create a ServerSocket to listen for connections with
ServerSocket ss = new ServerSocket(1111);
final byte[] request = new byte[1024];
byte[] reply = new byte[4096];
while (true) {
Socket client = null, server = null;
try {
// Wait for a connection on the local port
client = ss.accept();
final String SenderIp=client.getInetAddress().getHostAddress();
System.out.println("Client Ip address "+ SenderIp);
.....
// cookie convertion moodified to original is done here
(modified cookie ---> original cookie value)
.....
// forward HttpRequest to server
byte[] requestBytes = requestString.getBytes("ISO-8859-1");
streamToServer.write(requestBytes,0,requestBytes.length);
streamToServer.flush();
}
// Read the server's responses
// and pass them back to the client.
int bytesRead;
try {
while ((bytesRead = streamFromServer.read(reply)) != -1) {
String responseString = new String(reply, 0, bytesRead, "ISO-8859-1");
System.out.println("Reply string "+responseString);
// cookie modification done here
System.out.println("Forwarded to IP "+SenderIp);
byte[] responseBytes = responseString.getBytes("ISO-8859-1");
streamToClient.write(responseBytes, 0, responseBytes.length);
streamToClient.flush();
HashMap(original_cookie, modified cookie)
}
}
我怎么知道這個 HttpResponse 將發送到的IP 地址? 我的意思是我將發送修改 cookie 的客戶端 IP 地址
我想在從外部服務器獲取 HttpResponse 后使用 HashMap(original_cookie,IP address, modified cookie)。
先感謝您。
我得到了解決方案。 我正在新客戶端和服務器之間創建一個單獨的套接字。 因此,我們可以輕松地從特定客戶端和服務器之間的套接字中檢索單個客戶端的 IP 地址。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.