Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

proxy - Jetty ProxyServlet with SSL support

I am using Jetty's ProxyServlet as a HTTP proxy.

After I start the server and add the socks proxy in firefox I can access websites through the proxy without any problems.

The problem is that when I try to access a HTTPs website through the proxy. Firefox displays a "Server not found" error and during debugging I don't see anything happening in my Java code.

Am I missing something here to add SSL support to Jetty?

Here's part of the code:

    Server httpProxy = new Server(8087);

    ServletHandler servletHandler = new ServletHandler();
    servletHandler.addServletWithMapping(new ServletHolder(new TunnelProxyServlet()), "/*");

    httpProxy.setHandler(servletHandler);
    try {
        httpProxy.start();
    } catch (Exception ex) {
        Logger.getLogger(HttpProxy.class.getName()).log(Level.SEVERE, null, ex);
    }

    public class TunnelProxyServlet extends ProxyServlet {
      @Override
      public void init(ServletConfig config) throws ServletException {
        super.init(config);
        System.out.println("init done !");
      }

      @Override
      public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("got a request !");
        super.service(req, res);
      } 
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ZmK's answer is simply a copy of the example from Jetty repositories and does not even work.

Jetty by default does not have an HTTPS Proxy. The AsyncProxyServlet and ProxyServlet classes only do HTTP proxy. In order for you to do an HTTPS proxy, do the following:

  1. Create a class which extends from AsyncProxyServlet class.
  2. Override createHttpClient() method. The key here is that the HttpClient instance which you will create will need an SslContextFactory(). Just set SslContextFactory with appropriate settings on HttpClient object and you will be good to go.

Here is the code example in detail: https://github.com/k2k2e6/jettyHttpsProxy


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...