简体   繁体   English

使用套接字写入代理并将响应返回给客户端

[英]Writing to a proxy using a socket and returning response to client

I'm trying to create a proxy server in Java that rotates proxies. 我正在尝试用Java创建可旋转代理的代理服务器。 What I mean is, I am creating a proxy server that passes on the request to another random proxy, gets the response from that random proxy and returns it back to the client. 我的意思是,我正在创建一个代理服务器,该服务器将请求传递给另一个随机代理,从该随机代理获取响应并将其返回给客户端。

Something like this: 像这样:

  • Client Request -> My proxy server 客户端请求->我的代理服务器
  • My proxy server -> random proxy server 我的代理服务器->随机代理服务器
  • random proxy server -> My proxy server 随机代理服务器->我的代理服务器
  • My proxy server -> Client 我的代理服务器->客户端

I have 2 main classes handling this. 我有两个主要的类来处理这个。 The first class is called RunnableRequestLayer and it is responsible for reading the client's request, and sending the response back. 第一类称为RunnableRequestLayer ,它负责读取客户端的请求,并将响应发送回去。 The second class is RequestMaker which connects to a random proxy, and has a send() and receive() method which send/receive from the random proxy. 第二类是RequestMaker ,它连接到随机代理,并具有从随机代理发送/接收的send()receive()方法。

Here is the relevant code from both classes: 这是两个类的相关代码:

Class #1: RunnableRequestRelayer 类别1: RunnableRequestRelayer

public class RunnableRequestRelayer implements Runnable {

    private Socket socket;
    private final int maxTries = 5;

    public RunnableRequestRelayer(Socket socket) {
        this.socket = socket; //This socket is the serverSocket.accept() socket
    }

    @Override
    public void run() {

        System.out.println("Got a request!");

        try(
                PrintWriter out =
                new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
                ){

            RequestMaker rm = new RequestMaker();
            int tries = 0;

            while(tries++ < maxTries){
                try{
                    rm.connect();   
                } catch(IOException e){
                    continue;
                }

                String inputLine;

                //This while loop reads the input HttpRequest fine.
                while((inputLine = in.readLine()) != null){
                    if(inputLine.equals(""))
                        break;

                    rm.send(inputLine + "\r\n");
                    //System.out.println(rm.receive());
                }

                System.out.println("Test"); //This is successfully printing.


                String outputLine;
                //This output loop is never entered... why?
                while((outputLine = rm.receive()) != null){
                    System.out.println("In output loop");
                    if(outputLine.equals(""))
                        break;

                    out.print(outputLine + "\r\n");
                    out.flush();
                    System.out.println(outputLine);
                }

                rm.disconnect();
                tries = maxTries;
            }

        } catch(IOException e) {
            System.out.println("Bad");
        }

    }

Class #2: RequestMaker 第2类: RequestMaker

public class RequestMaker {

    private Socket socket;

    PrintWriter out;
    BufferedReader in;

    public void connect() throws IOException {
        String[] proxy = ProxyGenerator.generate().split(":"); 
        socket = new Socket(proxy[0], Integer.parseInt(proxy[1]));

        System.out.println("Connected to proxy - " + proxy[0] + ":" + proxy[1]);
        out = new PrintWriter(socket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }

    public void send(String s) {
        out.write(s);
        out.flush();
    }

    public String receive() throws IOException {
        return in.readLine();
    }

    public void disconnect() {
        try{
            out.flush();
            out.close();
            in.close();
            socket.close(); 
        } catch(IOException e) {}
    }

}

I tried testing this with fiddler too. 我也尝试用提琴手对此进行测试。 I set the proxy to connect to 127.0.0.1:8888 which is Fiddler's proxy server. 我将代理设置为连接到Fiddler的代理服务器127.0.0.1:8888 Once again, the request was received from the client, but the proxy on Fiddler never received it. 再次,该请求是从客户端收到的,但是Fiddler上的代理从未收到过该请求。

My question is: Why is the while loop that reads from the proxy not entering in the first place? 我的问题是:为什么从代理读取的while循环没有首先进入? I checked if the rm.receive() was returning "" or null using an if, and it wasn't. 我检查了rm.receive()是否使用if返回""null ,但不是。

Turns out I was not following the HTTP protocol properly. 原来我没有正确遵循HTTP协议。 Had to send an empty line after writing the input request to the proxy server. 将输入请求写入代理服务器后,必须发送空行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM