首页 > 解决方案 > 代理:如何解析来自套接字连接的 https 请求和响应

问题描述

我正在用java创建一个简单的代理。我正在做的是使用ServerSocket在特定端口上启动它并将我收到的请求路由到外部代理列表。所以,理论上我不需要解析请求和响应,只需接收接收的字节并将它们路由到最终目的地。我的目标是选择具有一些应用程序逻辑的目标代理。为了做到这一点,了解以下内容将非常有用:

对于“正常”的 http 连接,这是可能的。当我读取数据时,我可以解析它并获取我需要的信息。使用 https 连接,所有数据都是加密的,所以我认为这是不可能的。任何人都可以确认这一点并最终提出绕过这个“问题”的方法?

这里以一些代码为例:

//start my proxy
ServerSocket ss = new ServerSocket(portNum);
Socket client= ss.accept();

//connection arrived : get pointer to streams
final InputStream from_client = client.getInputStream();
final OutputStream to_client = client.getOutputStream();

//Try to open socket to destination proxy
Socket server = null;
try {
    server = new Socket([destination proxy host], [destination proxy port]);
} catch (IOException e) {
    PrintWriter out = new PrintWriter(new OutputStreamWriter(to_client));
    out.println("Proxy server cannot connect to " + opt.getProxyHost() + ":" + opt.getProxyPort() + ":\n" + e));
    out.flush();
    client.close();

}

some stuff ...

//get streams from destination 
final InputStream from_server = server.getInputStream();
final OutputStream to_server = server.getOutputStream();

//in a separate Thread :

try {
        //transfer bytes from client to server ( java >= 9 )
        //**NOTE : the https connection is encrypted, is not possibile to parse the request received, just route it**
    from_client.transferTo(to_server);

} catch (IOException e) {

    //Manage exception
}

响应有同样的问题:

int bytes_read;
try {

//**NOTE : the https connection is encrypted, is not possibile to parse the response received, just route it back**

    from_server.transferTo(to_client);

} catch (IOException e) {

  //Manage exception
}

谢谢大卫

标签: javasocketshttpsproxy

解决方案


推荐阅读