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.
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.
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)
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;
}