Java中的域前端代理,不会得到"拉方法"代理工作

时间:2017-05-14 14:30:02

标签: java sockets proxy

我目前正在开发一个使用名为"域前端"的技术的项目。简而言之:它通过加密连接将互联网流量发送到CDN(在本例中为Google),然后CDN将此信息传回给代理。通过这种方式,您可以规避审查(只要CDN没有被阻止),因为观察者不知道真正的目的地。 (要阅读有关域前端的更多信息,这里是原始的paper)许多应用程序(如Signal和Tor)已经使用了这种技术,但是没有一般使用代理只需通过Google代理tcp套接字另一端。我决定继续这项工作并且它已经完成了:HTTP编码部分和Google服务器上的代码正在运行。唯一的问题是真正的代理部分。 这是代理的结构: 来源 - >客户代理 - > [Google] - >服务器代理 - >目的地

与一般代理的区别在于HTTP是基于请求的协议,因此不能执行普通代理所做的异步操作。我的设置是在100ms内向客户端发出请求,向服务器发送客户端收到的字节到服务器。服务器从目标读取字节并将它们发送回客户端。比服务器和客户端都将它们收到的字节写入原始套接字,另一个往返开始于100ms。

这是我的代码(我只粘贴了相关部分,此代码不是域前端): 客户端:

try {
        ServerSocket serverSocket = new ServerSocket(8082);
        System.out.println("client proxy is listening for connections");
        Socket source = serverSocket.accept();
        BufferedReader sourceIn = new BufferedReader(new InputStreamReader(source.getInputStream()));
        PrintWriter sourceOut = new PrintWriter(source.getOutputStream(), false);
        while(true) {
            Socket server = new Socket("localhost", 8081);
            BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
            PrintWriter out = new PrintWriter(server.getOutputStream(), false);

            //read what the source has for the server
            System.out.println("start reading source socket");
            System.out.println("available: " + source.getInputStream().available());
            char[] buffer = new char[15000];
            int bytesRead = 0;
            if(source.getInputStream().available() != 0) {
                bytesRead = sourceIn.read(buffer);
                byte[] bytesToSend = new byte[bytesRead];
                bytesToSend = Arrays.copyOfRange(new String(buffer).getBytes(), 0, bytesRead);
                out.print((byte) 1);
                out.print(bytesToSend);
                out.flush();
                System.out.println("Sent to server: " + new String(bytesToSend) + " bytesRead: " + bytesRead);
            } else {
                out.print((byte) 0);
                out.flush();
                System.out.println("Sent to server: nothing");
            }

            //read what the server has for the source
            buffer = new char[15000];
            bytesRead = 0;
            while((bytesRead = in.read(buffer)) == -1) {
                System.out.println("didn't receive any bytes from server");
                Thread.sleep(20);
            }
            byte[] bytesToSend = Arrays.copyOfRange(new String(buffer).getBytes(), 0, bytesRead);
            if(bytesToSend[0] == '1') {
                sourceOut.print(bytesToSend);
                sourceOut.flush();
                System.out.println("Sent to source: " + new String(bytesToSend));
            } else {
                System.out.println("Server has nothing for source; bytesToSend: " + new String(bytesToSend));
            }

            in.close();
            out.close();
            server.close();
            Thread.sleep(100);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

服务器:

try {
            Socket destination = new Socket("192.168.0.150", 22);
            BufferedReader destinationIn = new BufferedReader(new InputStreamReader(destination.getInputStream()));
            PrintWriter destinationOut = new PrintWriter(destination.getOutputStream(), false);
            ServerSocket server = new ServerSocket(8081);
            while(true) {
                Socket clientSocket = server.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), false);
                System.out.println("received new connection from client");

                //read what client has for destination
                char[] buffer = new char[15000];
                int bytesRead = 0;
                while((bytesRead = in.read(buffer)) == -1) {
                    System.out.println("didn't receive any bytes from client");
                    Thread.sleep(20);
                }
                byte[] bytesToSend = Arrays.copyOfRange(new String(buffer).getBytes(), 0, bytesRead);
                if(bytesToSend[0] == (byte) 1) {
                    destinationOut.print(bytesToSend);
                    destinationOut.flush();
                    System.out.println("Sent to destination: " + new String(bytesToSend));
                } else {
                    System.out.println("Client has nothing for destination");
                }

                //read what distination has for client
                System.out.println("start reading destination socket");
                System.out.println("available: " + destination.getInputStream().available());
                buffer = new char[15000];
                if(destination.getInputStream().available() != 0) {
                    bytesRead = destinationIn.read(buffer);
                    bytesToSend = new byte[bytesRead];
                    bytesToSend = Arrays.copyOfRange(new String(buffer).getBytes(), 0, bytesRead);
                    out.print("1");
                    out.print(new String(bytesToSend));
                    out.flush();
                    System.out.println("Sent to client: " + new String(bytesToSend) + " bytesRead: " + bytesRead);
                } else {
                    out.print((byte) 0);
                    out.flush();
                    System.out.println("Sent to client: nothing");
                }
                out.close();
                in.close();
                clientSocket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

当我运行此代码连接到SSH服务器时,SSH客户端冻结,似乎代理服务器不再响应代理客户端。

我真的希望有人可以帮助我,如果你需要额外的信息,请告诉我! :)

0 个答案:

没有答案