套接字代理服务器重定向URL

时间:2013-10-06 19:13:43

标签: java sockets proxy

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

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

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

2 个答案:

答案 0 :(得分:0)

您无需对内容采取任何措施。客户端将以与路由您当前代理的URL的请求相同的方式路由对嵌入式URL的请求:向您发送。

答案 1 :(得分:0)

我有同样的问题,但试图解决这个问题,通过servlet捕获请求/响应。试试这个http://www.tidytutorials.com/2009/12/simple-java-proxy-servlet-using.html并将网址更改为重定向。响应不是最好的(图像,css和js不会被渲染),但通过更改Content-type,它可以被解析。