1

在 littleProxy 中,如何设置远程 ip 和端口?他们网站上的示例:

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

只设置本地端口。

4

2 回答 2

1

远程 IP 和端口从发送到 LittleProxy 的请求中的“主机”字段中检索。

例如,带有以下标头的请求:

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

将由 LittleProxy 转发到远程主机 xxxx 端口 1234。知道这一点,确保您的请求通过代理引导到正确的远程主机/端口的一种方法是修改客户端应用程序发送的请求

或者,使用 LittleProxy 过滤器来编辑请求头;将其更改为您想要的远程 IP/端口

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();

我在第一种方法上取得了成功(修改来自客户的请求)

于 2014-10-01T13:13:04.693 回答
1

请求目标必须是绝对格式,如 RFC 7230 第 5.3.2 节所指定,由 LittleProxy 转发。以下代码适用于 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;
}
于 2015-12-20T16:30:34.963 回答