java - 代理:如何解析来自套接字连接的 https 请求和响应
问题描述
我正在用java创建一个简单的代理。我正在做的是使用ServerSocket在特定端口上启动它并将我收到的请求路由到外部代理列表。所以,理论上我不需要解析请求和响应,只需接收接收的字节并将它们路由到最终目的地。我的目标是选择具有一些应用程序逻辑的目标代理。为了做到这一点,了解以下内容将非常有用:
- 连接到我的代理的客户端的 IP
- 我从最终代理收到的 Http 响应代码
对于“正常”的 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
}
谢谢大卫
解决方案
推荐阅读
- verilog - 如何修复我当前的 MIPS 32 SCP 寄存器文件的时钟/数据错误
- machine-learning - 如何对具有多个目标变量的回归分析进行预测
- javascript - 如何使用Javascript动态添加复选框
- java - 如何用 jooq 编写 join fetch 查询?
- database - 如何从数据库中获取现场时间
- angular - 如何在材料日期选择器中显示UTC时间
- html - 如何格式化html表格
- julia - 在 for 循环中填充类型为 any 的多维数组
- angular - Angular 功能、共享和核心模块依赖项
- python-3.x - 字典条目的顺序问题