1

In littleProxy, how can I set the remote ip and port? The sample in their website :

HttpProxyServer server =
    DefaultHttpProxyServer.bootstrap()
        .withPort(8080)
        .start();

only sets the local port.

Nvn
  • 23
  • 7
  • What do you mean by "remote IP and port" ? Do you want to configure a chained proxy ? Question seems a bit unclear... – Guillaume Aug 21 '14 at 07:59
  • 1
    @Guillaume, I think he's asking how to define the target server's IP and port – kakoma Sep 26 '14 at 09:05
  • http://stackoverflow.com/questions/27039042/in-littleproxy-how-to-set-proxy-ip-and-port – Kudi Dec 10 '14 at 13:04

2 Answers2

1

The remote IP and port are retrieved from the "Host" field in the request sent to LittleProxy.

e.g. A request with the header below:

POST http://x.x.x.x:1234 HTTP/1.1
Authorization: Basic cjknkcjenkjljvbt==
Host: x.x.x.x:1234
Accept: */*
Proxy-Connection: Keep-Alive
User-Agent: MyNode/test
Content-Type: text/xml
Content-Length: 1079
Expect: 100-continue

would be forwarded by LittleProxy to remote host x.x.x.x, port 1234. Knowing this, one way to ensure that your requests are channeled through the Proxy to the right remote host/port is to modify the requests being sent by your client application

Alternatively, use LittleProxy filters to edit the request header; change it to your desired remote IP/port

HttpProxyServer server =
    DefaultHttpProxyServer.bootstrap()
        .withPort(8080)
        .withFiltersSource(new HttpFiltersSourceAdapter() {
            public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) {
               return new HttpFiltersAdapter(originalRequest) {
                  @Override
                  public HttpResponse clientToProxyRequest(HttpObject httpObject) {
                      // Modify header, replace remote IP/Port
                      return null;
                  }

                  @Override
                  public HttpResponse proxyToServerRequest(HttpObject httpObject) {
                      // TODO: implement your filtering here
                      return null;
                  }

                  @Override
                  public HttpObject serverToProxyResponse(HttpObject httpObject) {
                      // TODO: implement your filtering here
                      return httpObject;
                  }

                  @Override
                  public HttpObject proxyToClientResponse(HttpObject httpObject) {
                      // TODO: implement your filtering here
                      return httpObject;
                  }   
               };
            }
        })
        .start();

I've had success with the first approach (modifying requests from clients)

kakoma
  • 1,179
  • 13
  • 17
1

The request target must be in absolute-form, as specified by RFC 7230, section 5.3.2, to be forwarded by LittleProxy. The following code works in LittleProxy v1.1.0beta1:

Pattern REQUEST_TARGET_ORIGIN_FORM_PREFIX = Pattern.compile("/[^/]");

@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
  if (httpObject instanceof HttpRequest) {
    HttpRequest httpRequest = (HttpRequest) httpObject;
    if (REQUEST_TARGET_ORIGIN_FORM_PREFIX.matcher(httpRequest.getUri()).lookingAt()) {
      String uriRemote = "http://myRemoteHost:myRemotePort" + httpRequest.getUri();
      httpRequest.setUri(uriRemote);
    }
  }
  return null;
}
slobo
  • 761
  • 2
  • 9
  • 16