Search in sources :

Example 36 with ProxyInfo

use of android.net.ProxyInfo in project platform_frameworks_base by android.

the class ConnectivityService method sendProxyBroadcast.

private void sendProxyBroadcast(ProxyInfo proxy) {
    if (proxy == null)
        proxy = new ProxyInfo("", 0, "");
    if (mPacManager.setCurrentProxyScriptUrl(proxy))
        return;
    if (DBG)
        log("sending Proxy Broadcast for " + proxy);
    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
    intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
    intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy);
    final long ident = Binder.clearCallingIdentity();
    try {
        mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
Also used : ProxyInfo(android.net.ProxyInfo) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent)

Example 37 with ProxyInfo

use of android.net.ProxyInfo in project platform_frameworks_base by android.

the class ConnectivityService method loadGlobalProxy.

private void loadGlobalProxy() {
    ContentResolver res = mContext.getContentResolver();
    String host = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST);
    int port = Settings.Global.getInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, 0);
    String exclList = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST);
    String pacFileUrl = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC);
    if (!TextUtils.isEmpty(host) || !TextUtils.isEmpty(pacFileUrl)) {
        ProxyInfo proxyProperties;
        if (!TextUtils.isEmpty(pacFileUrl)) {
            proxyProperties = new ProxyInfo(pacFileUrl);
        } else {
            proxyProperties = new ProxyInfo(host, port, exclList);
        }
        if (!proxyProperties.isValid()) {
            if (DBG)
                log("Invalid proxy properties, ignoring: " + proxyProperties.toString());
            return;
        }
        synchronized (mProxyLock) {
            mGlobalProxy = proxyProperties;
        }
    }
}
Also used : ProxyInfo(android.net.ProxyInfo) NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString) ContentResolver(android.content.ContentResolver)

Example 38 with ProxyInfo

use of android.net.ProxyInfo in project platform_frameworks_base by android.

the class ConnectivityService method setGlobalProxy.

public void setGlobalProxy(ProxyInfo proxyProperties) {
    enforceConnectivityInternalPermission();
    synchronized (mProxyLock) {
        if (proxyProperties == mGlobalProxy)
            return;
        if (proxyProperties != null && proxyProperties.equals(mGlobalProxy))
            return;
        if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties))
            return;
        String host = "";
        int port = 0;
        String exclList = "";
        String pacFileUrl = "";
        if (proxyProperties != null && (!TextUtils.isEmpty(proxyProperties.getHost()) || !Uri.EMPTY.equals(proxyProperties.getPacFileUrl()))) {
            if (!proxyProperties.isValid()) {
                if (DBG)
                    log("Invalid proxy properties, ignoring: " + proxyProperties.toString());
                return;
            }
            mGlobalProxy = new ProxyInfo(proxyProperties);
            host = mGlobalProxy.getHost();
            port = mGlobalProxy.getPort();
            exclList = mGlobalProxy.getExclusionListAsString();
            if (!Uri.EMPTY.equals(proxyProperties.getPacFileUrl())) {
                pacFileUrl = proxyProperties.getPacFileUrl().toString();
            }
        } else {
            mGlobalProxy = null;
        }
        ContentResolver res = mContext.getContentResolver();
        final long token = Binder.clearCallingIdentity();
        try {
            Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, host);
            Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, port);
            Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, exclList);
            Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC, pacFileUrl);
        } finally {
            Binder.restoreCallingIdentity(token);
        }
        if (mGlobalProxy == null) {
            proxyProperties = mDefaultProxy;
        }
        sendProxyBroadcast(proxyProperties);
    }
}
Also used : ProxyInfo(android.net.ProxyInfo) NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString) ContentResolver(android.content.ContentResolver)

Example 39 with ProxyInfo

use of android.net.ProxyInfo in project platform_frameworks_base by android.

the class IpConfigStore method writeConfig.

@VisibleForTesting
public static boolean writeConfig(DataOutputStream out, int configKey, IpConfiguration config) throws IOException {
    boolean written = false;
    try {
        switch(config.ipAssignment) {
            case STATIC:
                out.writeUTF(IP_ASSIGNMENT_KEY);
                out.writeUTF(config.ipAssignment.toString());
                StaticIpConfiguration staticIpConfiguration = config.staticIpConfiguration;
                if (staticIpConfiguration != null) {
                    if (staticIpConfiguration.ipAddress != null) {
                        LinkAddress ipAddress = staticIpConfiguration.ipAddress;
                        out.writeUTF(LINK_ADDRESS_KEY);
                        out.writeUTF(ipAddress.getAddress().getHostAddress());
                        out.writeInt(ipAddress.getPrefixLength());
                    }
                    if (staticIpConfiguration.gateway != null) {
                        out.writeUTF(GATEWAY_KEY);
                        // Default route.
                        out.writeInt(0);
                        // Have a gateway.
                        out.writeInt(1);
                        out.writeUTF(staticIpConfiguration.gateway.getHostAddress());
                    }
                    for (InetAddress inetAddr : staticIpConfiguration.dnsServers) {
                        out.writeUTF(DNS_KEY);
                        out.writeUTF(inetAddr.getHostAddress());
                    }
                }
                written = true;
                break;
            case DHCP:
                out.writeUTF(IP_ASSIGNMENT_KEY);
                out.writeUTF(config.ipAssignment.toString());
                written = true;
                break;
            case UNASSIGNED:
                /* Ignore */
                break;
            default:
                loge("Ignore invalid ip assignment while writing");
                break;
        }
        switch(config.proxySettings) {
            case STATIC:
                ProxyInfo proxyProperties = config.httpProxy;
                String exclusionList = proxyProperties.getExclusionListAsString();
                out.writeUTF(PROXY_SETTINGS_KEY);
                out.writeUTF(config.proxySettings.toString());
                out.writeUTF(PROXY_HOST_KEY);
                out.writeUTF(proxyProperties.getHost());
                out.writeUTF(PROXY_PORT_KEY);
                out.writeInt(proxyProperties.getPort());
                if (exclusionList != null) {
                    out.writeUTF(EXCLUSION_LIST_KEY);
                    out.writeUTF(exclusionList);
                }
                written = true;
                break;
            case PAC:
                ProxyInfo proxyPacProperties = config.httpProxy;
                out.writeUTF(PROXY_SETTINGS_KEY);
                out.writeUTF(config.proxySettings.toString());
                out.writeUTF(PROXY_PAC_FILE);
                out.writeUTF(proxyPacProperties.getPacFileUrl().toString());
                written = true;
                break;
            case NONE:
                out.writeUTF(PROXY_SETTINGS_KEY);
                out.writeUTF(config.proxySettings.toString());
                written = true;
                break;
            case UNASSIGNED:
                /* Ignore */
                break;
            default:
                loge("Ignore invalid proxy settings while writing");
                break;
        }
        if (written) {
            out.writeUTF(ID_KEY);
            out.writeInt(configKey);
        }
    } catch (NullPointerException e) {
        loge("Failure in writing " + config + e);
    }
    out.writeUTF(EOS);
    return written;
}
Also used : LinkAddress(android.net.LinkAddress) ProxyInfo(android.net.ProxyInfo) StaticIpConfiguration(android.net.StaticIpConfiguration) InetAddress(java.net.InetAddress) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 40 with ProxyInfo

use of android.net.ProxyInfo in project platform_frameworks_base by android.

the class IpConfigStore method readIpAndProxyConfigurations.

public static SparseArray<IpConfiguration> readIpAndProxyConfigurations(InputStream inputStream) {
    SparseArray<IpConfiguration> networks = new SparseArray<IpConfiguration>();
    DataInputStream in = null;
    try {
        in = new DataInputStream(inputStream);
        int version = in.readInt();
        if (version != 2 && version != 1) {
            loge("Bad version on IP configuration file, ignore read");
            return null;
        }
        while (true) {
            int id = -1;
            // Default is DHCP with no proxy
            IpAssignment ipAssignment = IpAssignment.DHCP;
            ProxySettings proxySettings = ProxySettings.NONE;
            StaticIpConfiguration staticIpConfiguration = new StaticIpConfiguration();
            String proxyHost = null;
            String pacFileUrl = null;
            int proxyPort = -1;
            String exclusionList = null;
            String key;
            do {
                key = in.readUTF();
                try {
                    if (key.equals(ID_KEY)) {
                        id = in.readInt();
                    } else if (key.equals(IP_ASSIGNMENT_KEY)) {
                        ipAssignment = IpAssignment.valueOf(in.readUTF());
                    } else if (key.equals(LINK_ADDRESS_KEY)) {
                        LinkAddress linkAddr = new LinkAddress(NetworkUtils.numericToInetAddress(in.readUTF()), in.readInt());
                        if (linkAddr.getAddress() instanceof Inet4Address && staticIpConfiguration.ipAddress == null) {
                            staticIpConfiguration.ipAddress = linkAddr;
                        } else {
                            loge("Non-IPv4 or duplicate address: " + linkAddr);
                        }
                    } else if (key.equals(GATEWAY_KEY)) {
                        LinkAddress dest = null;
                        InetAddress gateway = null;
                        if (version == 1) {
                            // only supported default gateways - leave the dest/prefix empty
                            gateway = NetworkUtils.numericToInetAddress(in.readUTF());
                            if (staticIpConfiguration.gateway == null) {
                                staticIpConfiguration.gateway = gateway;
                            } else {
                                loge("Duplicate gateway: " + gateway.getHostAddress());
                            }
                        } else {
                            if (in.readInt() == 1) {
                                dest = new LinkAddress(NetworkUtils.numericToInetAddress(in.readUTF()), in.readInt());
                            }
                            if (in.readInt() == 1) {
                                gateway = NetworkUtils.numericToInetAddress(in.readUTF());
                            }
                            RouteInfo route = new RouteInfo(dest, gateway);
                            if (route.isIPv4Default() && staticIpConfiguration.gateway == null) {
                                staticIpConfiguration.gateway = gateway;
                            } else {
                                loge("Non-IPv4 default or duplicate route: " + route);
                            }
                        }
                    } else if (key.equals(DNS_KEY)) {
                        staticIpConfiguration.dnsServers.add(NetworkUtils.numericToInetAddress(in.readUTF()));
                    } else if (key.equals(PROXY_SETTINGS_KEY)) {
                        proxySettings = ProxySettings.valueOf(in.readUTF());
                    } else if (key.equals(PROXY_HOST_KEY)) {
                        proxyHost = in.readUTF();
                    } else if (key.equals(PROXY_PORT_KEY)) {
                        proxyPort = in.readInt();
                    } else if (key.equals(PROXY_PAC_FILE)) {
                        pacFileUrl = in.readUTF();
                    } else if (key.equals(EXCLUSION_LIST_KEY)) {
                        exclusionList = in.readUTF();
                    } else if (key.equals(EOS)) {
                        break;
                    } else {
                        loge("Ignore unknown key " + key + "while reading");
                    }
                } catch (IllegalArgumentException e) {
                    loge("Ignore invalid address while reading" + e);
                }
            } while (true);
            if (id != -1) {
                IpConfiguration config = new IpConfiguration();
                networks.put(id, config);
                switch(ipAssignment) {
                    case STATIC:
                        config.staticIpConfiguration = staticIpConfiguration;
                        config.ipAssignment = ipAssignment;
                        break;
                    case DHCP:
                        config.ipAssignment = ipAssignment;
                        break;
                    case UNASSIGNED:
                        loge("BUG: Found UNASSIGNED IP on file, use DHCP");
                        config.ipAssignment = IpAssignment.DHCP;
                        break;
                    default:
                        loge("Ignore invalid ip assignment while reading.");
                        config.ipAssignment = IpAssignment.UNASSIGNED;
                        break;
                }
                switch(proxySettings) {
                    case STATIC:
                        ProxyInfo proxyInfo = new ProxyInfo(proxyHost, proxyPort, exclusionList);
                        config.proxySettings = proxySettings;
                        config.httpProxy = proxyInfo;
                        break;
                    case PAC:
                        ProxyInfo proxyPacProperties = new ProxyInfo(pacFileUrl);
                        config.proxySettings = proxySettings;
                        config.httpProxy = proxyPacProperties;
                        break;
                    case NONE:
                        config.proxySettings = proxySettings;
                        break;
                    case UNASSIGNED:
                        loge("BUG: Found UNASSIGNED proxy on file, use NONE");
                        config.proxySettings = ProxySettings.NONE;
                        break;
                    default:
                        loge("Ignore invalid proxy settings while reading");
                        config.proxySettings = ProxySettings.UNASSIGNED;
                        break;
                }
            } else {
                if (DBG)
                    log("Missing id while parsing configuration");
            }
        }
    } catch (EOFException ignore) {
    } catch (IOException e) {
        loge("Error parsing configuration: " + e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
            }
        }
    }
    return networks;
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) IpConfiguration(android.net.IpConfiguration) StaticIpConfiguration(android.net.StaticIpConfiguration) IpAssignment(android.net.IpConfiguration.IpAssignment) ProxySettings(android.net.IpConfiguration.ProxySettings) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) ProxyInfo(android.net.ProxyInfo) SparseArray(android.util.SparseArray) StaticIpConfiguration(android.net.StaticIpConfiguration) EOFException(java.io.EOFException) RouteInfo(android.net.RouteInfo) InetAddress(java.net.InetAddress)

Aggregations

ProxyInfo (android.net.ProxyInfo)66 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)15 StaticIpConfiguration (android.net.StaticIpConfiguration)11 ContentResolver (android.content.ContentResolver)10 LinkAddress (android.net.LinkAddress)10 IOException (java.io.IOException)10 InetAddress (java.net.InetAddress)10 Intent (android.content.Intent)8 PendingIntent (android.app.PendingIntent)7 ApplicationInfo (android.content.pm.ApplicationInfo)7 RemoteException (android.os.RemoteException)7 ProxySettings (android.net.IpConfiguration.ProxySettings)6 ComponentName (android.content.ComponentName)5 Context (android.content.Context)5 IPackageManager (android.content.pm.IPackageManager)5 InstrumentationInfo (android.content.pm.InstrumentationInfo)5 IpConfiguration (android.net.IpConfiguration)5 IpAssignment (android.net.IpConfiguration.IpAssignment)5 RouteInfo (android.net.RouteInfo)5 SparseArray (android.util.SparseArray)5