引用:
https://github.com/adamfisk/LittleProxy
拦截和操纵HTTPS流量,LittleProxy使用中间人(MITM)管理器。 LittleProxy的默认实现(SelfSignedMitmManager)具有相当有限的功能集。 为了更好地控制证书模拟,浏览器信任,TLS握手等,请使用LittleProxy兼容的MITM扩展:
一个LittleProxy MITM扩展,旨在支持包括android在内的所有Java平台
支持椭圆曲线加密和自定义信任存储的LittleProxy MITM扩展
要过滤HTTP流量,可以使用HttpFiltersSource(适配器)添加请求和响应过滤器,例如:
请参阅org.littleshoot.proxy.HttpFilters的Javadoc来查看您可以使用的方法。
要启用aggregator和inflater,必须在HttpFiltersSource#get(Request / Response)BufferSizeInBytes()方法中返回一个大于0的值。 这为您提供了一个“FullHttp(请求/响应)”,未压缩过滤器中的完整内容。 否则,你必须自己处理大块。
这个大小限制适用于每个连接。 例如,要禁止在* .iso或* dmg文件中通过URL进行聚合,您可以在过滤器源代码中返回如下所示的过滤器:
这可以在应用程序中进行大量的下载,这些应用程序定期处理大小有限的FullHttpResponses来修改其内容,例如html。
像LittleProxy这样的代理服务器总是包含一个Web服务器。 如果您在原始请求中获得没有方案,主机和端口的URI,那么这是对您的代理的直接请求。 您可以返回一个HttpFilters实现,它可以回答HTML内容的响应,或者像这样在clientToProxyRequest中重定向:
在回答重定向时,您应该添加一个Connection:close标头,以避免阻止行为:
<dependency> <groupId>org.littleshoot</groupId> <artifactId>littleproxy</artifactId> <version>1.1.2</version> <exclusions> <exclusion> <artifactId>netty-all</artifactId> <groupId>io.netty</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.19.Final</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.6-jre</version> </dependency>
package com.pc.proxy; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.http.HttpObject; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; import org.littleshoot.proxy.HttpFilters; import org.littleshoot.proxy.HttpFiltersAdapter; import org.littleshoot.proxy.HttpFiltersSourceAdapter; import org.littleshoot.proxy.HttpProxyServerBootstrap; import org.littleshoot.proxy.impl.DefaultHttpProxyServer; public class ProxyServer { public static void main(String[] args) { HttpProxyServerBootstrap bootstrap = DefaultHttpProxyServer.bootstrap(); bootstrap.withPort(8080) .withFiltersSource(new HttpFiltersSourceAdapter() { @Override public HttpFilters filterRequest(HttpRequest originalRequest, ChannelHandlerContext ctx) { return new HttpFiltersAdapter(originalRequest) { @Override public HttpResponse clientToProxyRequest(HttpObject httpObject) { // TODO: implement your filtering here return new AnswerRequestFilter(originalRequest,"test").clientToProxyRequest(httpObject); } @Override public HttpObject serverToProxyResponse(HttpObject httpObject) { // TODO: implement your filtering here return httpObject; } @Override public void proxyToServerConnectionSucceeded(ChannelHandlerContext serverCtx) { ChannelPipeline pipeline = serverCtx.pipeline(); if (pipeline.get("inflater") != null) { pipeline.remove("inflater"); } if (pipeline.get("aggregator") != null) { pipeline.remove("aggregator"); } super.proxyToServerConnectionSucceeded(serverCtx); } }; } @Override public int getMaximumResponseBufferSizeInBytes() { return 10 * 1024 * 1024; } }); bootstrap.start(); } }
package com.pc.proxy; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http.*; import org.littleshoot.proxy.HttpFiltersAdapter; import java.io.UnsupportedEncodingException; public class AnswerRequestFilter extends HttpFiltersAdapter { private String answer; public AnswerRequestFilter(HttpRequest originalRequest, String answer) { super(originalRequest, null); this.answer = answer; } public AnswerRequestFilter(HttpRequest originalRequest, ChannelHandlerContext ctx) { super(originalRequest, ctx); } @Override public HttpResponse clientToProxyRequest(HttpObject httpObject){ ByteBuf buffer = null; try { buffer = Unpooled.wrappedBuffer(answer.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buffer); HttpUtil.setContentLength(response, buffer.readableBytes()); // HttpHeaders.setHeader(response, HttpHeaders.Names.CONTENT_TYPE, "text/html"); response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/html"); return response; } }