创建一个接受HTTPS的Java代理服务器

我已经有一个工作正常的HTTP代理服务器,可以处理多个HTTP请求。现在我的问题是如何处理https请求?

这是我正在使用的简化代码:

class Daemon

{

public static void main(String[] args)

{

ServerSocket cDaemonSocket = new ServerSocket(3128);

while(true)

{

try

{

Socket ClientSocket = cDaemonSocket.accept();

(new ClientHandler(ClientSocket )).start();

}catch(Exception e) { }

}

}

}

和ClientHandler

class ClientHandler extends Thread

{

private Socket socket = null;

private Socket remoteSocket = null;

private HTTPReqHeader request = null;

ClientHandler(Socket socket)

{

this.socket = socket;

request = new HTTPReqHeader();

request.parse(socket); // I read and parse the HTTP request here

}

public void run()

{

if(!request.isSecure() )

{

remoteSocket = new Socket(request.url,request.port);

}

else

{

// now what should I do to established a secured socket?

}

// start connecting remoteSocket and clientSocket

...........

}

}

}

我确实确实尝试搜索过,但是遇到SSL隧道,证书,握手,SSLSocket,SSLFactory,trustStore等问题,但是仍然无法正常工作。我只需要知道我需要什么,以及建立与启用SSL的Web服务器的连接的步骤。

回答:

我终于明白了。

我只需要使用普通套接字,并向客户端发送一条消息,表明已建立连接。然后继续挖掘。

这是一个工作代码:

private Socket socket = null;

private Socket remoteSocket = null;

private HTTPReqHeader request = null;

ClientHandler(Socket socket)

{

this.socket = socket;

request = new HTTPReqHeader();

request.parse(socket); // I read and parse the HTTP request here

}

public void run()

{

remoteSocket = new Socket(request.url,request.port);

if(request.isSecure() )

{

// send ok message to client

String ConnectResponse = "HTTP/1.0 200 Connection established\n" +

"Proxy-agent: ProxyServer/1.0\n" +

"\r\n";

try

{

DataOutputStream out = new DataOutputStream(socket.getOutputStream());

out.writeByte(ConnectResponse);

out.flush();

} catch(Exception e) {}

}

// start connecting remoteSocket and clientSocket

...........

}

这是有关代理服务器如何处理CONNECT的很好的解释。 http://curl.haxx.se/rfc/draft-luotonen-web-proxy-

tunneling-01.txt

以上是 创建一个接受HTTPS的Java代理服务器 的全部内容, 来源链接: utcz.com/qa/411531.html

回到顶部