Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 602b232a authored by Jason Monk's avatar Jason Monk
Browse files

Add PAC File support for proxy configuration

PAC (Proxy auto-config) files contain a single javascript function,
FindProxyForURL(url, host).  It gets called to determine what proxy should be
used for a specific request.

This adds PAC support to the system.  The ProxyProperties has been modified
to hold the PAC file when one is present.  The Proxy method
setHttpProxySystemProperty has been modified to insert a PacProxySelector
as the default ProxySelector when it is required.  This new ProxySelector
makes calls to the ConnectivityService to parse the PAC file.

The ConnectivityService and the WifiConfigStore have been modified to support
saving the extra PAC file data.

The ConnectivityService now has a class attached (PacProxyNative) that
interfaces to the native calls for PAC files.  The parsing of the PAC file
is handled by libpac (which is being added to external/) which utilizes
libv8 to parse the javascript.

As a fallback to applications that don't use the java ProxySelector,  the proxy
is setup to point to a local proxy server that will handle the pac parsing.

bug:10182711
Change-Id: I5eb8df893c632fd3e1b732385cb7720ad646f401
parent 55db1e12
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -257,7 +257,8 @@ LOCAL_SRC_FILES += \
	telephony/java/com/android/internal/telephony/ITelephonyRegistry.aidl \
	telephony/java/com/android/internal/telephony/IWapPushManager.aidl \
	wifi/java/android/net/wifi/IWifiManager.aidl \
	wifi/java/android/net/wifi/p2p/IWifiP2pManager.aidl
	wifi/java/android/net/wifi/p2p/IWifiP2pManager.aidl \
	packages/services/Proxy/com/android/net/IProxyService.aidl \

# FRAMEWORKS_BASE_JAVA_SRC_DIRS comes from build/core/pathmap.mk
LOCAL_AIDL_INCLUDES += $(FRAMEWORKS_BASE_JAVA_SRC_DIRS)
+2 −2
Original line number Diff line number Diff line
@@ -793,8 +793,8 @@ public final class ActivityThread {
            InetAddress.clearDnsCache();
        }

        public void setHttpProxy(String host, String port, String exclList) {
            Proxy.setHttpProxySystemProperty(host, port, exclList);
        public void setHttpProxy(String host, String port, String exclList, String pacFileUrl) {
            Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
        }

        public void processInBackground() {
+5 −2
Original line number Diff line number Diff line
@@ -338,7 +338,8 @@ public abstract class ApplicationThreadNative extends Binder
            final String proxy = data.readString();
            final String port = data.readString();
            final String exclList = data.readString();
            setHttpProxy(proxy, port, exclList);
            final String pacFileUrl = data.readString();
            setHttpProxy(proxy, port, exclList, pacFileUrl);
            return true;
        }

@@ -1001,12 +1002,14 @@ class ApplicationThreadProxy implements IApplicationThread {
        data.recycle();
    }

    public void setHttpProxy(String proxy, String port, String exclList) throws RemoteException {
    public void setHttpProxy(String proxy, String port, String exclList,
            String pacFileUrl) throws RemoteException {
        Parcel data = Parcel.obtain();
        data.writeInterfaceToken(IApplicationThread.descriptor);
        data.writeString(proxy);
        data.writeString(port);
        data.writeString(exclList);
        data.writeString(pacFileUrl);
        mRemote.transact(SET_HTTP_PROXY_TRANSACTION, data, null, IBinder.FLAG_ONEWAY);
        data.recycle();
    }
+2 −1
Original line number Diff line number Diff line
@@ -101,7 +101,8 @@ public interface IApplicationThread extends IInterface {
    void scheduleConfigurationChanged(Configuration config) throws RemoteException;
    void updateTimeZone() throws RemoteException;
    void clearDnsCache() throws RemoteException;
    void setHttpProxy(String proxy, String port, String exclList) throws RemoteException;
    void setHttpProxy(String proxy, String port, String exclList,
            String pacFileUrl) throws RemoteException;
    void processInBackground() throws RemoteException;
    void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args)
            throws RemoteException;
+80 −0
Original line number Diff line number Diff line

package android.net;

import android.os.RemoteException;
import android.os.ServiceManager;

import com.android.net.IProxyService;
import com.google.android.collect.Lists;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.List;

/**
 * @hide
 */
public class PacProxySelector extends ProxySelector {
    public static final String PROXY_SERVICE = "com.android.net.IProxyService";
    private IProxyService mProxyService;

    public PacProxySelector() {
        mProxyService = IProxyService.Stub.asInterface(
                ServiceManager.getService(PROXY_SERVICE));
    }

    @Override
    public List<Proxy> select(URI uri) {
        String response = null;
        String urlString;
        try {
            urlString = uri.toURL().toString();
        } catch (MalformedURLException e) {
            urlString = uri.getHost();
        }
        try {
            response = mProxyService.resolvePacFile(uri.getHost(), urlString);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        return parseResponse(response);
    }

    private static List<Proxy> parseResponse(String response) {
        String[] split = response.split(";");
        List<Proxy> ret = Lists.newArrayList();
        for (String s : split) {
            String trimmed = s.trim();
            if (trimmed.equals("DIRECT")) {
                ret.add(java.net.Proxy.NO_PROXY);
            } else if (trimmed.startsWith("PROXY ")) {
                String[] hostPort = trimmed.substring(6).split(":");
                String host = hostPort[0];
                int port;
                try {
                    port = Integer.parseInt(hostPort[1]);
                } catch (Exception e) {
                    port = 8080;
                }
                ret.add(new Proxy(Type.HTTP, new InetSocketAddress(host, port)));
            }
        }
        if (ret.size() == 0) {
            ret.add(java.net.Proxy.NO_PROXY);
        }
        return ret;
    }

    @Override
    public void connectFailed(URI uri, SocketAddress address, IOException failure) {

    }

}
Loading