在littleproxy中设置远程ip和端口

时间:2014-06-15 23:45:05

标签: java proxy little-proxy

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

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

仅设置本地端口。

1 个答案:

答案 0 :(得分:1)

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

e.g。带有以下标题的请求:

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.了解这一点,确保您的请求通过代理传递到正确的远程主机/端口的一种方法是修改客户端应用程序发送的请求< / p>

或者,使用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();

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