创建一个接受 HTTPS 的 Java 代理服务器 [英] creating a Java Proxy Server that accepts HTTPS

查看:57
本文介绍了创建一个接受 HTTPS 的 Java 代理服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i already have a working HTTP proxy server that can handle multiple HTTP request. now my problem is how do I handle https request?

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

here's a simplified code i am using:

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

and the 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 的网络服务器建立连接的需要和步骤.

I really did try searching how, I have encounter SSL tunneling, certificate,handshaking, SSLSocket, SSLFactory, trustStore and etc. something like that but still could not make it work.. I just need to know what are the things I need and the steps to established a connection to a SSL-enabled web server.

推荐答案

我终于明白了.

我只需要使用普通的套接字并向客户端发送一条连接已建立的消息.然后继续隧道.

I only need to use normal socket and send a message to client that a connection is established. then proceed to tunneling.

这是一个工作代码:

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
" +
                                          "Proxy-agent: ProxyServer/1.0
" +
                                          "
";
                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

here's a good explanation on how proxy server handles CONNECT. http://curl.haxx.se/rfc/draft-luotonen-web-proxy-tunneling-01.txt

这篇关于创建一个接受 HTTPS 的 Java 代理服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆