High-Performance HTTP proxy littleproxy

Source: Internet
Author: User

Reference:

Https://github.com/adamfisk/LittleProxy

Intercept and manipulate HTTPS traffic, littleproxy use the man-in-the-middle (MITM) Manager. The default implementation of Littleproxy (Selfsignedmitmmanager) has a fairly limited set of features. For better control of certificate impersonation, browser trusts, TLS handshake, etc., use Littleproxy compatible MITM extensions:

A littleproxy MITM extension designed to support all Java platforms, including Android
Littleproxy MITM extensions that support elliptic curve encryption and custom trust storage

To filter HTTP traffic, you can use Httpfilterssource (adapter) to add request and response filters, for example:

See Org.littleshoot.proxy.HttpFilters's Javadoc to see the methods you can use.

To enable aggregator and Inflater, you must return a value greater than 0 in the Httpfilterssource#get (request/response) buffersizeinbytes () method. This provides you with a "fullhttp (Request/Response)", which does not compress the full contents of the filter. Otherwise, you have to deal with chunks yourself.

This size limit applies to each connection. For example, to disallow aggregation through URLs in *. iso or * DMG files, you can return filters in the filter source code such as the following:


This can be done in a large number of downloads in the application, which regularly process fullhttpresponses of limited size to modify its contents, such as HTML.

A proxy server such as Littleproxy always contains a Web server. If you get the URI of the host and port in the original request without a schema, then this is a direct request to your proxy. You can return a httpfilters implementation that responds to the HTML content, or redirects in clienttoproxyrequest like this:

When replying to a redirect, you should add a connection:close header to avoid blocking the behavior:

<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 = Defaulthttppro        Xyserver.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, "Te                            St "). Clienttoproxyrequest (Httpobject); } @Override Public Httpobject servertoproxyresponse (Httpobject httpo Bject) {//todo:implement your filtering here return Httpo                            Bject; } @Override public void proxytoserverconnectionsucceeded (Channelhand                                Lercontext 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, CT    x);        } @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; }}

  

High-Performance HTTP proxy littleproxy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.