network - Authenticate in an EAP network with proxy

0
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void helperConnect(final ScanResult scanResult) {
    try {

        Log.v("rht", "Item clicked, SSID " + scanResult.SSID + " Security : " + scanResult.capabilities);

        conf = new WifiConfiguration();

        networkSSID = scanResult.SSID;

        wifiEnterpriseConfig = new WifiEnterpriseConfig();
        conf.SSID = "\"" + networkSSID + "\"";   // Please note the quotes. String should contain ssid in quotes
        conf.status = WifiConfiguration.Status.ENABLED;
        conf.priority = 40;

        if (scanResult.capabilities.toUpperCase().contains("EAP")) {
            Log.v("EAP", "Configurando EAP");

            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);

            wifiEnterpriseConfig.setIdentity(networkIdentity);
            wifiEnterpriseConfig.setPassword(networkPass);
            wifiEnterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
            wifiEnterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.MSCHAPV2);

            conf.enterpriseConfig = wifiEnterpriseConfig;

        }

        int networkId = wifiManager.addNetwork(conf);

        //setWifiProxySettings(); ESTA SERIA LA FUNCION DE CONFIGURACION DEL PROXY

        Log.v("rht", "Add result " + networkId);

        boolean isDisconnected = wifiManager.disconnect();
        Log.v("rht", "isDisconnected : " + isDisconnected);

        boolean isEnabled = wifiManager.enableNetwork(networkId, true);
        Log.v("rht", "isEnabled : " + isEnabled);

        boolean isReconnected = wifiManager.reconnect();
        Log.v("rht", "isReconnected : " + isReconnected);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

I want to know how it is that the proxy is configured in order to connect. Thanks

    
asked by Alex Rivas 15.05.2018 в 16:21
source

1 answer

0

To whom it may interest.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressWarnings("unchecked")
private static WifiConfiguration setProxy(WifiConfiguration conf, String hostname, int port, List<String> bypass, PROXY_TYPE type) {
    try {
        //linkProperties is no longer in WifiConfiguration
        Class proxyInfoClass = Class.forName("android.net.ProxyInfo");
        Class[] setHttpProxyParams = new Class[1];
        setHttpProxyParams[0] = proxyInfoClass;
        Class wifiConfigClass = Class.forName("android.net.wifi.WifiConfiguration");
        Method setHttpProxy = wifiConfigClass.getDeclaredMethod("setHttpProxy", setHttpProxyParams);
        setHttpProxy.setAccessible(true);

        //Get the ENUM ProxySettings in IpConfiguration
        Class ipConfigClass = Class.forName("android.net.IpConfiguration");
        Field f = ipConfigClass.getField("proxySettings");
        Class proxySettingsClass = f.getType();

        Class[] setProxySettingsParams = new Class[1];
        setProxySettingsParams[0] = proxySettingsClass;
        Method setProxySettings = wifiConfigClass.getDeclaredMethod("setProxySettings", setProxySettingsParams);
        setProxySettings.setAccessible(true);


        ProxyInfo pi = null;
        String Type = null;
        switch (type) {
            case MANUAL:
                Type = "STATIC";
                pi = ProxyInfo.buildDirectProxy(hostname, port, bypass);
                break;

            case PAC_URL:
                Type = "PAC";
                pi = ProxyInfo.buildPacProxy(Uri.parse(hostname));
        }

        //pass the new object to setHttpProxy
        Object[] params_SetHttpProxy = new Object[1];
        params_SetHttpProxy[0] = pi;
        setHttpProxy.invoke(conf, params_SetHttpProxy);

        //pass the enum to setProxySettings
        Object[] params_setProxySettings = new Object[1];
        params_setProxySettings[0] = Enum.valueOf((Class<Enum>) proxySettingsClass, Type);
        setProxySettings.invoke(conf, params_setProxySettings);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return conf;
}

With this code I was able to modify the proxy of a Wi-Fi connection.

    
answered by 16.05.2018 в 18:17