gpt4 book ai didi

java - 套接字代理服务器重定向 url

转载 作者:行者123 更新时间:2023-12-01 04:18:30 28 4
gpt4 key购买 nike

我有一个基本的套接字代理服务器,我想在其中添加重定向功能。知道我该怎么走吗?仅更改主机没有帮助,因为从客户端收集的缓冲区包含旧信息。

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

我该如何实现这一目标?因为从我所看到的是我无法编辑从客户端/浏览器接收到的缓冲区。

最佳答案

您无需对内容执行任何操作。客户端将以与您当前代理的 URL 请求相同的方式路由对嵌入 URL 的请求:给您。

关于java - 套接字代理服务器重定向 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212830/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com