Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetty ProxyServlet with SSL support

Tags:

proxy

ssl

jetty

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);
      } 
   }
like image 622
Jochen Avatar asked Mar 24 '12 13:03

Jochen


People also ask

How do I configure jetty to use HTTPS between servers?

If you are using a central-to-remote configuration, configure your system to use HTTPS between servers in Central Server mode. JAVA_OPTIONS="-Djetty.home=$JETTY_HOME -Djetty.log=$JETTY_LOG -Djava.protocol.handler.pkgs=com.ibm.net.ssl.www2.protocol -Xms128m -Xmx512m -server $JAVA_OPTIONS"

How do I create a sslcontextfactory in jetty?

In the jetty-ssl.xml file, locate the lines that are similar to these lines, and then paste these lines into the jetty.xml file. <New id="sslContextFactory" class="org.eclipse.jetty.http.ssl.SslContextFactory"> <Set name="KeyStore"><Property name="jetty.home" default="."

Is it possible to replicate the entire environment locally using Jetty?

Further Jetty is the best choice for local development either embedded or using jetty-maven-plugin. In this case it does not make sense to replicate the entire environment locally, instead those urls can be proxied in local environment such that /someotherapp/style.css would be proxied to http://devhost:3000/someotherapp/style.css

What is the HTTPS protocol used for?

We use https for secure communication over the computer network. Technically, https is not a protocol in and of itself; rather, it is the result of simply layering the Hypertext Transfer Protocol (HTTP) on top of the SSL/TLS protocol, thus adding the security capabilities of SSL/TLS to standard HTTP communications.


1 Answers

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

like image 125
k2k2e6 Avatar answered Oct 06 '22 23:10

k2k2e6