2

一段时间以来,我一直在尝试几种不同的方法来让我的自定义代理正常工作,到目前为止,我能够做到的唯一方法是使用 Apache 的 HttpClient。但是,为了了解,我想知道为什么我在下面的自己的代理句柄实现中遇到了问题:


public void processProxyRequest (Socket client, String request) throws Exception {

        if ( !request.equals("") ) {

            String[] requestHeaders = request.split("\\r\\n");
            Pattern p = Pattern.compile("([A-Z]*)\\s*([^:\\/]*):\\/\\/([^\\s]*)\\s*(?:HTTP.*)");
            Matcher m = p.matcher(requestHeaders[0]);

            if ( m.matches() ) {
                String method = m.group(1).toUpperCase();
                String proto = m.group(2).toLowerCase();
                String[] requestInfo = m.group(3).split("\\/", 2);

                String host = requestInfo[0];
                host = ( host.split("\\.").length < 3 ) ? "www." + host : host;
                String page = "/";
                if ( requestInfo.length == 2 && !requestInfo[1].equals("") ) {
                    page += requestInfo[1];
                }

                int remotePort = 80;

                if ( proto.equals("https") ) {
                    remotePort = 443;
                }
                else if ( proto.equals("ftp") ) {
                    remotePort = 21;
                }
                this.sendAndReceive(client, request, host, remotePort);
            }
        }

    }

    public void sendAndReceive (Socket client, String request, String host, int port) throws Exception {
        Socket target = new Socket(host, port);
        System.out.println("Connected to server");

        ByteArrayInputStream inStream = new ByteArrayInputStream(request.getBytes());

        this.inToOut(inStream, target.getOutputStream());
        System.out.println("Sent");
        this.inToOut(target.getInputStream(), client.getOutputStream());
        System.out.println("Received");
        target.close();
    }

    public void inToOut (InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[1024]; // Adjust if you want
        int bytesRead;
        System.out.println("reading");
        while ((bytesRead = input.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
    }

简而言之(并且忽略我的请求标头解析缺陷),上面的代码编译并运行,但是,该inToOut()方法似乎有点挣扎并在 input.read() 期间锁定,我不太清楚为什么。我确实知道我传入的原始套接字是有效的并且打开时没有错误。此外,System.outinToOut() 函数中的 inToOut() 函数会打印“正在阅读”,但永远不会超过该read()部分。

感谢您的任何建议!

4

2 回答 2

1

这不是写代理的方法。在 HTTP 的情况下,您只需要处理第一行,它会告诉您目标主机。其他一切都只是来回复制字节,需要进行一些小的改进,例如正确报告上游连接错误和正确处理关闭。FTP 的情况比较棘手,应该完全分开处理,但是一旦你通过了连接阶段,它只是在复制字节。你在理解协议上付出的努力越少,它就会变得越简单越好。

于 2011-02-01T09:56:56.477 回答
0

在您的 sendAndReceive 函数中,也许尝试使用 DataInputStream 和 DataOutputStream

public void sendAndReceive (Socket client, String request, String host, int port) throws Exception {
    Socket target = new Socket(host, port);
    System.out.println("Connected to server");

    this.inToOut(new DataInputStream(client.getInputStream()), new DataOutputStream(target.getOutputStream()));
    System.out.println("Sent");
    this.inToOut(new DataInputStream(target.getInputStream()), new DataOutputStream(client.getOutputStream()));
    System.out.println("Received");
    target.close();
}

问题似乎不在 inToOut 函数中 - 我尝试使用 inToOut() 并且效果很好(它实际上帮助我解决了我遇到的类似问题 - 谢谢)

于 2011-12-03T16:31:48.170 回答