java - Java 中的代理输入/输出流问题

标签 java proxy inputstream outputstream

一段时间以来,我一直在尝试一些不同的方法来让我的自定义代理正常工作,到目前为止我能够做到的唯一方法是使用 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);
        }
    }

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

谢谢您的建议!

最佳答案

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

关于java - Java 中的代理输入/输出流问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4860044/

相关文章:

java - 尝试将对象添加到数组列表时出现空错误(简单的代码段)

java - 在java中实时将IO Process的值传递给另一个类

java - 使用关于持久性和 XML 的 InputStream 的最佳方式

Angular 代理 HTTP

java - 如何使用 URLConnection 超时

c++ 无法清除 inputStream

java - JTable 由 ArrayList 填充,结果不好

java - CertPathBuilderException : unable to find valid certification path was 8. 5 网络部署

java - JBoss如何集成OpenID登录认证实现(Java)

node.js - NodeJS - 安全连接到外部 redis 服务器