Using Jetty transparent http proxy to forward to public website

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using Jetty transparent http proxy to forward to public website

Martin Brentnall
Hi everyone,

I'm trying to use Jetty transparent proxy to forward to a public web server, but currently unable to get a simple example working as I would expect.  This is my code:


--- CODE BEGIN ---


package test.myproxy;

import org.eclipse.jetty.proxy.ProxyServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class MyHttpProxy {
  public static void main(String[] args) {
    try {
      Server mServer = new Server(7070);
      ServletContextHandler mContext = new ServletContextHandler(mServer, "/");
      ServletHolder mHolder = mContext.addServlet(ProxyServlet.Transparent.class, "/myproxy/*");
      mHolder.setInitOrder(1);
      mHolder.setInitParameter("proxyTo", "http://www.htmlhelp.com");
      mHolder.setInitParameter("prefix", "/myproxy");
      mServer.start();
      mServer.join();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}


--- CODE END ---


When I run this, I can access the website htmlhelp.com in Chrome via the URL http://127.0.0.1:7070/myproxy/ as expected.  However, the links and images on the page do not work, since they all refer to "127.0.0.1:7070/" (rather than "127.0.0.1:7070/myproxy/").  So for example, "http://127.0.0.1:7070/about/", "http://127.0.0.1:7070/icon/wdglogo.gif", etc. which of course are not recognised URL's on the Jetty server.

On further investigation, I learned that the HTTP request includes headers, one of which is "host".  It seems that a website uses this header to construct the link and image URL's.  Indeed, if I change the "host" header in the debugger to "htmlhelp.com" immediately before the request is sent, then everything loads as expected.

Now as I understand, the "host" header is an "end to end" header, meaning that the proxy should NOT modify it, so it seems that isn't an option.  Besides, doing this would cause image loading and links to bypass the proxy anyway.

Is it possible to accomplish what I'm trying to do?  Apologies for the newbie question; this HTTP and web stuff is still quite new to me.

In case it makes any difference, I'm using Jetty 9.0.7.

--
With Kind Regards,
Martin Brentnall


_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/jetty-users
Reply | Threaded
Open this post in threaded view
|

Re: Using Jetty transparent http proxy to forward to public website

Greg Wilkins
Martin,

With a transparent proxy as you are using, you need to proxy all the links.  The issue is that you are injecting a /myproxy servlet path into the "transparent" proxy, so you are transforming request for  http://127.0.0.1:7070/myproxy/ to requests for  http://htmlhelp.com/.
Plus you are not translating any of the image and link requests that will be going to /* rather than /myproxy/*

If you registered the transparent proxy servlet at /* it would probably work fine.

If you really wanted to, you could rewrite the host header, but then all images and links would bypass your proxy and you would lose capture of the client.

cheers




On Mon, 4 Nov 2019 at 20:00, Martin Brentnall <[hidden email]> wrote:
Hi everyone,

I'm trying to use Jetty transparent proxy to forward to a public web server, but currently unable to get a simple example working as I would expect.  This is my code:


--- CODE BEGIN ---


package test.myproxy;

import org.eclipse.jetty.proxy.ProxyServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class MyHttpProxy {
  public static void main(String[] args) {
    try {
      Server mServer = new Server(7070);
      ServletContextHandler mContext = new ServletContextHandler(mServer, "/");
      ServletHolder mHolder = mContext.addServlet(ProxyServlet.Transparent.class, "/myproxy/*");
      mHolder.setInitOrder(1);
      mHolder.setInitParameter("proxyTo", "http://www.htmlhelp.com");
      mHolder.setInitParameter("prefix", "/myproxy");
      mServer.start();
      mServer.join();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}


--- CODE END ---


When I run this, I can access the website htmlhelp.com in Chrome via the URL http://127.0.0.1:7070/myproxy/ as expected.  However, the links and images on the page do not work, since they all refer to "127.0.0.1:7070/" (rather than "127.0.0.1:7070/myproxy/").  So for example, "http://127.0.0.1:7070/about/", "http://127.0.0.1:7070/icon/wdglogo.gif", etc. which of course are not recognised URL's on the Jetty server.

On further investigation, I learned that the HTTP request includes headers, one of which is "host".  It seems that a website uses this header to construct the link and image URL's.  Indeed, if I change the "host" header in the debugger to "htmlhelp.com" immediately before the request is sent, then everything loads as expected.

Now as I understand, the "host" header is an "end to end" header, meaning that the proxy should NOT modify it, so it seems that isn't an option.  Besides, doing this would cause image loading and links to bypass the proxy anyway.

Is it possible to accomplish what I'm trying to do?  Apologies for the newbie question; this HTTP and web stuff is still quite new to me.

In case it makes any difference, I'm using Jetty 9.0.7.

--
With Kind Regards,
Martin Brentnall

_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/jetty-users


--

_______________________________________________
jetty-users mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/jetty-users