SSL Jetty ProxyServlet

I am using Jetty ProxyServlet as an HTTP proxy.

After starting the server and adding the socks proxy server in firefox, I can easily access websites through proxies.

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

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

Here is the piece of 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); } } 
+7
source share
3 answers

You can use "ConnectHandler"

http://grepcode.com/file/repo1.maven.org/maven2/org.eclipse.jetty/example-jetty-embedded/8.1.1.v20120215/org/eclipse/jetty/embedded/ProxyServer.java

 public class ProxyServer { public static void main(String[] args) throws Exception { Server server = new Server(); SelectChannelConnector connector = new SelectChannelConnector(); connector.setPort(8888); server.addConnector(connector); HandlerCollection handlers = new HandlerCollection(); server.setHandler(handlers); // Setup proxy servlet ServletContextHandler context = new ServletContextHandler(handlers, "/", ServletContextHandler.SESSIONS); ServletHolder proxyServlet = new ServletHolder(ProxyServlet.class); proxyServlet.setInitParameter("whiteList", "google.com, www.eclipse.org, localhost"); proxyServlet.setInitParameter("blackList", "google.com/calendar/*, www.eclipse.org/committers/"); context.addServlet(proxyServlet, "/*"); // Setup proxy handler to handle CONNECT methods ConnectHandler proxy = new ConnectHandler(); proxy.setWhite(new String[]{"mail.google.com"}); proxy.addWhite("www.google.com"); handlers.addHandler(proxy); server.start(); } } 
+4
source

ZmK's answer is just a copy of the example from the Jetty repositories and doesn't even work.

Jetty does not have an HTTPS proxy by default. The AsyncProxyServlet and ProxyServlet classes use only HTTP proxies. To run the HTTPS proxy, follow these steps:

  • Create a class that extends from the AsyncProxyServlet class.
  • Override the createHttpClient () method. The key point here is that the HttpClient instance that you create will require SslContextFactory (). Just set the SslContextFactory with the appropriate settings for the HttpClient object, and you will be well off.

Here is a sample code: https://github.com/k2k2e6/jettyHttpsProxy

+10
source

I am using the latest Jetty version currently (9.4.1), and I was able to get the HTTPS proxy by simply adding this to my proxy servlet:

 @Override protected HttpClient newHttpClient() { return new HttpClient(new SslContextFactory()); } 

Read the k2k2e6 example that I started with before realizing that I could just override this simple method, not the whole createHttpClient () method.

+3
source

All Articles