Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get littleproxy to work on Android #41

Open
adonnini opened this issue Mar 21, 2022 · 0 comments
Open

How to get littleproxy to work on Android #41

adonnini opened this issue Mar 21, 2022 · 0 comments

Comments

@adonnini
Copy link

Hi,

I have been trying to run littleproxy from my Android app. Below, you will find the code.

Before publishing this post, I searched through StackOverflow and the web for information on running littleproxy on Android devices. I could not find answers to my questions below. There is very little information about littleproxy on Android.

On my test Android phone, a Huawei Mate 20 Pro running Androind 9, I enabled proxy manually on the Wifi network I am using as follows:

Proxy hostname - 127.0.0.1
Proxy port - 8100

The logs posted by littleproxy are as follows:

03-20 18:46:48.345: I/DefaultHttpProxyServer(27269): - withAddress - /127.0.0.1:8100
03-20 18:46:48.547: I/DefaultHttpProxyServer(27269): Starting proxy at address: {} - /127.0.0.1:8100
03-20 18:46:48.549: D/ServerGroup(27269): Initializing thread pools for {} with {} acceptor threads, {} incoming worker threads, and {} outgoing worker threads - TCP - 2 - 8 - 8
03-20 18:46:48.558: I/DefaultHttpProxyServer(27269): Proxy listening with TCP transport
03-20 18:46:48.589: I/DefaultHttpProxyServer(27269): Proxy started at address: {} - /127.0.0.1:8100

After proxy starts as the above logs seem to indicate, I try to access a (any) site on the web and I am unable to do so.

Based on the code (below), the only sites I should not be able to access are ones like
http://httpbin.org/image/png

I have four questions/issues I hope someone can help me answer:

  1. Is setting the address of the proxy server on an Android device to 127.0.0.1 allowed? If not what can it be set to? Can I use port 8100, or is there a preset port I should be using (e.g. 8080) (I don't think so, just checking to make sure)?

  2. How can I check whether the littleproxy proxy server is running after I start it (see code below)? Do I need to add code to ensure that it keep running?

  3. Is the littleproxy start call (see code below) correct?

  4. Does anyone have a successful experience of running a local proxy on an Android device without rooting the device? If yes, what's the proxy application used?

Thanks for any help in addressing these issues.

Code which calls littleproxy from main activity

    Log.i(TAG, " - onCreate - About to run BlockingFilterProxy.main(args) --- ");

        String[] args = new String[1];

        BlockingFilterProxy.main(args);

    Log.i(TAG, " - onCreate - Returned from BlockingFilterProxy.main(args) --- ");

BlockingFilterProxy code

package com.android.contextq.gatekeepertolltaker.proxy.filters;

import android.util.Log;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Date;

import org.littleshoot.proxy.HttpFilters;
import org.littleshoot.proxy.HttpFiltersAdapter;
import org.littleshoot.proxy.HttpFiltersSource;
import org.littleshoot.proxy.HttpFiltersSourceAdapter;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
import org.littleshoot.proxy.impl.ProxyUtils;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

/**

  • Blocks access to URLs ending in "png" or "jpeg" and returns a 502 response.
  • Test URLs - HTTP : http://httpbin.org/image/png
  •         HTTPS : https://httpbin.org/image/png
    

*/
public class BlockingFilterProxy {

private static String TAG = "BlockingFilterProxy";
private static final int PORT = 8100;

public static void main(String[] args) {

    HttpFiltersSource filtersSource = getFiltersSource();

    DefaultHttpProxyServer.bootstrap()
            .withAddress(new InetSocketAddress("127.0.0.1",8100))

// .withAddress(new InetSocketAddress("10.125.24.28",8100))
// .withPort(PORT)
.withAllowLocalOnly(false)
.withFiltersSource(filtersSource)
.withName("BlockingFilterProxy")
.start();
}

private static HttpFiltersSource getFiltersSource() {
    return new HttpFiltersSourceAdapter(){

        @Override
        public HttpFilters filterRequest(HttpRequest originalRequest) {

            return new HttpFiltersAdapter(originalRequest){

                @Override
                public HttpResponse clientToProxyRequest(HttpObject httpObject) {

                    if(httpObject instanceof HttpRequest){
                        HttpRequest request = (HttpRequest) httpObject;

                        Log.i(TAG, "Method URI : " + request.getMethod() + " " + request.getUri());

                        if(request.getUri().endsWith("png") || request.getUri().endsWith("jpeg")){
                            //For URLs ending in 'png' and 'jpeg', return a 502 response.
                            return getBadGatewayResponse();
                        }
                    }

                    return null;
                }

                private HttpResponse getBadGatewayResponse() {
                    String body = "<!DOCTYPE HTML \"-//IETF//DTD HTML 2.0//EN\">\n"
                            + "<html><head>\n"
                            + "<title>"+"Bad Gateway"+"</title>\n"
                            + "</head><body>\n"
                            + "An error occurred"
                            + "</body></html>\n";
                    byte[] bytes = body.getBytes(Charset.forName("UTF-8"));
                    ByteBuf content = Unpooled.copiedBuffer(bytes);
                    HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_GATEWAY, content);
                    response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, bytes.length);
                    response.headers().set("Content-Type", "text/html; charset=UTF-8");
                    response.headers().set("Date", ProxyUtils.formatDate(new Date()));
                    response.headers().set(HttpHeaders.Names.CONNECTION, "close");
                    return response;
                }					
            };
        }			
    };
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant