[英]Problem in establishing connection in HTTPS in proxy server.(CONNECT Method)
I am developing a proxy server based on java.我正在开发基于 java 的代理服务器。 For simple http request, proxy server is working.对于简单的 http 请求,代理服务器正在工作。 But for HTTPS Connection, connection gets timed out.但是对于 HTTPS 连接,连接超时。 Here are the steps I did.这是我所做的步骤。 I first read one line from input stream and created a socket connecting Server.我首先从输入 stream 中读取一行并创建了一个套接字连接服务器。 After that I gave 200 Status to client.之后,我给客户 200 状态。 After that I asynchronously read and write between Client Socket and Server socket.之后,我在客户端套接字和服务器套接字之间进行异步读写。 But currently this isn't working and connection gets timedout and I couldn't debug the problem.但目前这不起作用,连接超时,我无法调试问题。
public class ProxyServer extends Thread {
private String host;
private int port;
private ServerSocket serverSocket;
private InputStream proxyToClientIP;
private OutputStream proxyToClientOP;
private InputStream proxyToServerIP;
private OutputStream proxyToServerOP;
private Socket socket;
private Socket socketFromProxyServer;
ProxyServer(ServerSocket serverSocket, Socket socket) {
this.serverSocket = serverSocket;
this.socket = socket;
this.start();
}
public void run() {
processInputRequest();
}
public void processInputRequest() {
try {
proxyToClientIP = socket.getInputStream();
proxyToClientOP = socket.getOutputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(proxyToClientIP));
String hostDetails = reader.readLine();
System.out.println(hostDetails);
boolean isConnect = false;
//Need to parse request and find req type as GET or CONNECT
//As of now we assume it to be Connect request
if (!isConnect) {
processGetRequest();
} else {
processConnectRequest();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void processConnectRequest() {
//Need to get host name from request. Currently Hardcoded for developing purpose
host = "harish-4072";
port = 8383;
try {
socketFromProxyServer = new Socket(host, port);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proxyToClientOP));
writer.write("HTTP/1.1 200 Connection established\r\n" + "\r\n");
writer.flush();
proxyToServerOP = socketFromProxyServer.getOutputStream();
proxyToServerIP = socketFromProxyServer.getInputStream();
proxyRequest();
} catch (IOException ex) {
System.out.println(ex);
}
}
public void proxyRequest() {
try {
new Thread() {
@Override
public void run() {
try {
byte[] read = new byte[1024];
int in;
System.out.println("Reading");
while ((in = proxyToClientIP.read(read)) != -1) {
proxyToServerOP.write(read, 0, in);
proxyToServerOP.flush();
}
} catch (SocketException e) {
System.out.println(e);
} catch (IOException ex) {
}
}
}.start();
byte[] reply = new byte[1024];
int out;
System.out.println("Writing");
while ((out = proxyToServerIP.read(reply)) != -1) {
proxyToClientOP.write(reply, 0, out);
proxyToClientOP.flush();
}
} catch (IOException ex) {
}
public void processGetRequest() {
//
}
}
I first read one line from input stream and created a socket connecting Server.我首先从输入 stream 中读取一行并创建了一个套接字连接服务器。 ... After that I asynchronously read and write between Client Socket and Server socket. ...之后我在客户端套接字和服务器套接字之间异步读写。
The problem is that you are reading only a single line while you would need to read the full HTTP request header from the client, ie everything up to the end of the request header ( \r\n\r\n
).问题是您只读取一行,而您需要从客户端读取完整的 HTTP 请求 header,即到请求结束之前的所有内容 Z099FB995346F31C749F6E40DB0R \r\n\r\n
E)。
Because you fail to do so the unread parts of the HTTP request are forwarded to the server.因为您没有这样做,所以 HTTP 请求的未读部分被转发到服务器。 But the server is expecting the start of the TLS handshake and these data confuse the server.但是服务器正在等待 TLS 握手的开始,这些数据使服务器感到困惑。 This might result in hanging or aborting, depending on the content of the data and one the kind of server.这可能会导致挂起或中止,具体取决于数据的内容和一种服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.