我正在用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
}
谢谢 大卫(Davide)