我正在编写一个简单的 Java 代理,它打印客户端的请求,然后将其转发到目标服务器。它必须支持 SSL 连接,因为大多数网页都使用 HTTPS,而不是 ...
我正在编写一个简单的 Java 代理,它打印客户端的请求,然后将其转发到目标服务器。它必须支持 SSL 连接,因为大多数网页都使用 HTTPS 而不是纯 HTTP。所以我使用 SSLSockets 而不是普通的 Sockets。
但是,当我将浏览器配置为通过代理连接并发出请求时,连接被拒绝,并且我收到 SSLException:不支持或无法识别的 SSL 消息
我想我忘记了一些有关 SSL 配置的信息。此外,我正在使用 renatoathaydes / rawhttp 库来轻松管理请求。
这是主要方法,无限循环监听新连接:
public static void main(String... args) {
RequestHandler handler = new RequestHandler();
try {
SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket sslServerSocket = (SSLServerSocket) factory.createServerSocket(8080);
while (true) {
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
Runnable action = () -> handler.handleRequest(sslSocket);
Thread thread = new Thread(action);
thread.start();
}
} catch (IOException ex) {
log.error(ex.getMessage(), ex);
}
}
RequestHandler 类负责读取客户端请求、打印到记录器并将其转发到服务器:
@Slf4j
@NoArgsConstructor
public class RequestHandler {
public void handleRequest(SSLSocket clientSocket) {
try {
RawHttpRequest request = readClientRequest(clientSocket);
SSLSocket serverSocket = writeRequestToServer(request);
log.info(request.toString().replace("\\n", ""));
RawHttpRequest serverResponse = readResponseFromServer(serverSocket);
writeResponseToClient(clientSocket, serverResponse);
serverSocket.close();
clientSocket.close();
} catch (IOException ex) {
log.error(ex.getMessage(), ex);
}
}
private RawHttpRequest readClientRequest(SSLSocket clientSocket) throws IOException {
RawHttp http = new RawHttp();
return http.parseRequest(clientSocket.getInputStream());
}
private void writeResponseToClient(SSLSocket clientSocket, RawHttpRequest response) throws IOException {
response.writeTo(clientSocket.getOutputStream());
}
private RawHttpRequest readResponseFromServer(SSLSocket serverSocket) throws IOException {
RawHttp http = new RawHttp();
return http.parseRequest(serverSocket.getInputStream());
}
private SSLSocket writeRequestToServer(RawHttpRequest httpRequest) throws IOException {
String host = httpRequest.getUri().getHost();
int port = httpRequest.getUri().getPort();
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslSocket = (SSLSocket) factory.createSocket(host, port);
httpRequest.writeTo(sslSocket.getOutputStream());
return (sslSocket);
}
}
我尝试了 SSL 和普通 HTTP 连接,将浏览器配置为通过 localhost 和指定端口 (8080) 进行连接。我将端口更改为 443,这是我所看到的 SSL 端口,但没有效果。
也尝试过不使用 RawHttp 库,但问题是一样的。
查看您的代码,似乎存在与 HTTPS 代理工作方式相关的缺陷。当浏览器配置为使用 HTTPS 代理时,它会向代理发送 CONNECT 请求以建立到目标服务器的隧道。这允许浏览器和目标服务器直接处理 SSL/TLS 握手,而无需代理干扰。
在您的实现中,代理尝试处理 SSL / TLS 连接,这不是 HTTPS 代理通常的工作方式,所以在我看来这里的关键问题是:
我尝试使用下面的代码并为我工作,并进行了一些小的更改(与我的特定环境有关)
public static void main(String... args) {
RequestHandler handler = new RequestHandler();
try {
ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
Socket clientSocket = serverSocket.accept();
Runnable action = () -> handler.handleRequest(clientSocket);
Thread thread = new Thread(action);
thread.start();
}
} catch (IOException ex) {
log.error(ex.getMessage(), ex);
}
}
@Slf4j
@NoArgsConstructor
public class RequestHandler {
public void handleRequest(Socket clientSocket) {
try {
InputStream input = clientSocket.getInputStream();
OutputStream output = clientSocket.getOutputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = reader.readLine();
if (line == null || !line.startsWith("CONNECT")) {
log.error("Only CONNECT method is supported by this proxy.");
clientSocket.close();
return;
}
// Extract host and port from the CONNECT request
String[] parts = line.split(" ");
String[] hostPort = parts[1].split(":");
String host = hostPort[0];
int port = Integer.parseInt(hostPort[1]);
// Establish a connection to the target server
try (Socket serverSocket = new Socket(host, port)) {
output.write("HTTP/1.1 200 Connection Established\r\n\r\n".getBytes());
output.flush();
// Tunnel data between the client and the server
tunnelData(clientSocket, serverSocket);
}
clientSocket.close();
} catch (IOException ex) {
log.error(ex.getMessage(), ex);
}
}
private void tunnelData(Socket clientSocket, Socket serverSocket) throws IOException {
Thread clientToServer = new Thread(() -> forwardData(clientSocket, serverSocket));
Thread serverToClient = new Thread(() -> forwardData(serverSocket, clientSocket));
clientToServer.start();
serverToClient.start();
try {
clientToServer.join();
serverToClient.join();
} catch (InterruptedException e) {
log.error(e.getMessage(), e);
}
}
private void forwardData(Socket inputSocket, Socket outputSocket) {
try (InputStream input = inputSocket.getInputStream();
OutputStream output = outputSocket.getOutputStream()) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
output.flush();
}
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
我做了一些修复以使其正常工作: