我有一個基本的套接字代理服務器,我想在其中添加重定向功能。任何想法我怎麼去。只是更改主機不會有幫助,因爲從客戶端收集的緩衝區包含舊信息。套接字代理服務器重定向url
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleProxyServer {
public static final int portNumber = 5555;
public static void main(String[] args) {
SimpleProxyServer proxyServer = new SimpleProxyServer();
proxyServer.start();
}
public void start() {
System.out.println("Starting the SimpleProxyServer ...");
try {
ServerSocket serverSocket = new ServerSocket(
SimpleProxyServer.portNumber, 1);
byte[] buffer = new byte[10000];
while (true) {
Socket clientSocket = serverSocket.accept();
InputStream bis = clientSocket.getInputStream();
int n = bis.read(buffer);
String browserRequest = new String(buffer, 0, n);
System.out.println(browserRequest);
int start = browserRequest.indexOf("Host: ") + 6;
int end = browserRequest.indexOf('\n', start);
String host = browserRequest.substring(start, end - 1);
System.out.println("Connecting to host " + host);
Socket hostSocket = new Socket(host, 80); //I can change the host over here
OutputStream sos = hostSocket.getOutputStream();
System.out.println("Forwarding request to server");
sos.write(buffer, 0, n);// but then the buffer that is fetched from the client remains same
sos.flush();
InputStream sis = hostSocket.getInputStream();
OutputStream bos = clientSocket.getOutputStream();
System.out.println("Forwarding request from server");
do {
n = sis.read(buffer);
System.out.println("Receiving " + n + " bytes");
if (n > 0) {
bos.write(buffer, 0, n);
}
} while (n > 0);
bos.flush();
hostSocket.close();
clientSocket.close();
System.out.println("End of communication");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
在這裏,我的客戶端是一個瀏覽器,所以緩衝區包含大量的數據。
browerRequest的任何例子
GET http://www.yahoo.com/ HTTP/1.0
Host: www.yahoo.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Proxy-Connection: close
Cookie: B=c6r6cn99536pm&b=3&s=ha; fpc=d=mEdlILxMtEbSZkEHT9nArn_LmO6DBF2HC4aZWnYvepTQpHb86MidH7bPbylrLChwtcznc4rZnUWz5R_xvFr6WJYcYJnpG_aBqekaFtBKsmCUsblQQtM1ivZpRz7m6PYiQtwtcoGVAs55IhfOy0eaqXdfEif8ZkXrgpiYjmy2Qm_wkORpT5tXcZKJ.nKcMcUqh7aoTVA-&v=2; MSC=t=1381079862X; BILLBOARD=nv=1&bn=0&collapsed=0; ypcdb=e5e21a3dacf7b87ec358a08261d310ec
下一次是當while循環迭代再次在browserReqest變,
GET http://in.yahoo.com/?p=us HTTP/1.0
Host: in.yahoo.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: close
Proxy-Connection: close
Cookie: B=c6r6cn99536pm&b=3&s=ha; MSC=t=1381079862X; fpc=d=NVQLQy5MtEYnypFx54.9vMbBB6hMzgZBTkPUzrh5pyN7MdDAbbwV96dq9xhvHLfj_A.DBwRKIwUiufAsj8Gs1EBtj3slZb31KrhNjPe5lEAgMlEeXUijTqpYZnFWROwAFjCqcd_4SV90e1XEu87kCBT7Lb1rzodBKY1ZUHmC57O0QEJWL8rJJcEcaODEc.69elh3CaiKmaxkP5rwWbx0FK15ZCCYbJROMwwFXovaYBfR.Re27s4saqnUCBQSFI3Ro80k_ci5&v=2; fpps=_page=%7B%22wsid%22%3A%2221445690%22%7D; fpt=d=DV0PgAXXeVzrfc2GezJLqQPfzZiyGbbBhDpW.vmTd6yHOqDQACyQiLPWz..0N0jUP35Yo9S9Yw8rMuXlE9gjZ1Ra3MhzyfnyMN67b_8cb7d7NbRceZS4AzDaVqbHB_9Tfyeznojpm3AuzqGUdfG2fnoMVl4OqKM_a7Mt3vV8U4Qr38gB864WFl6NXOyz7n_T1FA89lQfsWVob0oOzuXWNfieD_jikxauj.3nFDkbkFH6qATpBXHLGP7OvE7ibc0IL.66u7kDDaawvEOSSyh1tv3LhSrYshHS5MfslHpkSUJsP4lIBNxcQQRJWBmw27rl9mfg.R7vAOQwFHi6_24D0XtQDhKntlO2sWSCplscG35S353IBzPRZranUwp_fYP3XuePv6E8yAkENSCbmx5..vjQTLOMEEf5mUlNmFvQoxSRzlvoG5vAdRcPmELlbxs6MZxMd822gifvP063z9dF8lNRWZTcK8Mfq0ob4XSUplzWbbY6lb2yJIZ57ddogxHo6phfy59RqLqFPWSb38a9K3UU0TSjPDEg9wjfFmmdew--&v=1; fpc_s=d=2smDyqJMtEb.MYIbCNo48GeCPgKDD8rlRWYK4yILeDPeL8Rmu.M9oX3wGvweGpvAQBcYSlyjemeKTeHpqOmaDGC.eKT3dwzmsyUMo48OZJEkv2z_fkIIMkipQJsEMCFngcwJ8tJL_R2hZBS6Z3MjJwmD28zABdGjrhJ71RmXmfkj6Ofas8R4UkkcXvyGgVsyGKxOeYip45UMltDfVpBa7SNz_GAcrhtpvlV6PRjF4ffKxTpAdx5uGiXok_5S7UmGpD8iQR.SOkZ0uhAr3iBl6wpoVGIHvPvOqPaZ8pAJApGQwmp85Ul3uJYqpXanb0B.WxaiyUXr6DlQBzUVrXHcn0Pm5_pQ6yATqiq0jBzsQqZNDAC8JAqecLmgRQUa7E_7B60IsEwV7TIq1mDdNhDgIk5lbIncWwLYVmoUyPxI0.UYcPJW&v=2; FPCK2=AgBSUZsQABPEEABFMhAARIgQAGMC; BILLBOARD=nv=1&bn=0&collapsed=0; ypcdb=e5e21a3dacf7b87ec358a08261d310ec
我只想重定向www.yahoo.com的請求可以說www.google.com
我該如何做到這一點?因爲從我所看到的是我無法編輯從客戶端/瀏覽器收到的緩衝區。
我想你沒有得到我的問題。我在瀏覽器中輸入www.yahoo.com。因此,對www.yahoo.com的請求將發送給代理,但我希望我的代理向外部服務器發送對www.google.com的請求,並將此響應發送回瀏覽器。希望事情現在更清楚。 – ItachiUchiha
所以你試圖說的是你在瀏覽器中輸入的任何URL,它必須被重定向到你設置的頁面。是嗎 ? 例如,如果您將代理服務器設置爲指向google.com,那麼無論何時用戶輸入任何網址,該網頁都必須重定向到google.com? – jayeshkv
是的,這就是我說的:) – ItachiUchiha