private static void acquireProxyStatusSDK1_11( ProxyConfiguration conf, ProxyStatus status, EnumSet<ProxyCheckOptions> checkOptions) { // API version <= 11 (Older devices) status.set(ProxyStatusProperties.WIFI_ENABLED, CheckStatusValues.NOT_CHECKED, false, false); status.set(ProxyStatusProperties.WIFI_SELECTED, CheckStatusValues.NOT_CHECKED, false, false); LogWrapper.d(TAG, "Checking if proxy is enabled ..."); status.set(isProxyEnabled(conf)); broadCastUpdatedStatus(); if (status.getProperty(ProxyStatusProperties.PROXY_ENABLED).result) { LogWrapper.d(TAG, "Checking if proxy is valid hostname ..."); status.set(isProxyValidHostname(conf)); broadCastUpdatedStatus(); LogWrapper.d(TAG, "Checking if proxy is valid port ..."); status.set(isProxyValidPort(conf)); broadCastUpdatedStatus(); if (checkOptions.contains(ProxyCheckOptions.ONLINE_CHECK) && status.getProperty(ProxyStatusProperties.PROXY_VALID_HOSTNAME).result && status.getProperty(ProxyStatusProperties.PROXY_VALID_PORT).result) { LogWrapper.d(TAG, "Checking if proxy is reachable ..."); status.set(isProxyReachable(conf)); broadCastUpdatedStatus(); } else { status.set( ProxyStatusProperties.PROXY_REACHABLE, CheckStatusValues.NOT_CHECKED, false, false); } } else { wifiNotEnabled_DisableChecking(status); } }
public static boolean canGetWebResources(ProxyConfiguration proxyConfiguration, int timeout) { try { // TODO: add better method to check web resources int result = testHTTPConnection(new URI("http://www.un.org/"), proxyConfiguration, timeout); // int rawresult = testHTTPConnection(new URI("http://157.150.34.32"), // proxyConfiguration, timeout); switch (result) { case HttpURLConnection.HTTP_OK: case HttpURLConnection.HTTP_CREATED: case HttpURLConnection.HTTP_NO_CONTENT: case HttpURLConnection.HTTP_NOT_AUTHORITATIVE: case HttpURLConnection.HTTP_ACCEPTED: case HttpURLConnection.HTTP_PARTIAL: case HttpURLConnection.HTTP_RESET: return true; default: return false; } } catch (URISyntaxException e) { LogWrapper.w(TAG, e.toString()); // APL.getEventReport().send(e); } return false; }
public static boolean lowLevelPingHost(Proxy proxy) { int exitValue; Runtime runtime = Runtime.getRuntime(); Process proc; String cmdline = null; String proxyAddress = null; try { InetSocketAddress proxySocketAddress = (InetSocketAddress) proxy.address(); proxyAddress = proxySocketAddress.getAddress().getHostAddress(); } catch (Exception e) { APL.getEventReport() .send( new Exception( "ProxyUtils.lowLevelPingHost() Exception calling getAddress().getHostAddress() on proxySocketAddress : ", e)); } if (proxyAddress == null) { try { InetSocketAddress proxySocketAddress = (InetSocketAddress) proxy.address(); proxyAddress = proxySocketAddress.toString(); } catch (Exception e) { APL.getEventReport() .send( new Exception( "ProxyUtils.lowLevelPingHost() Exception calling toString() on proxySocketAddress", e)); } } if (proxyAddress != null) { cmdline = "ping -c 1 -w 1 " + proxyAddress; try { proc = runtime.exec(cmdline); proc.waitFor(); exitValue = proc.exitValue(); LogWrapper.d(TAG, "Ping exit value: " + exitValue); if (exitValue == 0) { return true; } else { return false; } } catch (IOException e) { APL.getEventReport().send(new Exception("ProxyUtils.lowLevelPingHost() IOException", e)); } catch (InterruptedException e) { APL.getEventReport() .send(new Exception("ProxyUtils.lowLevelPingHost() InterruptedException", e)); } } return false; }
public static int testHTTPConnection( URI uri, ProxyConfiguration proxyConfiguration, int timeout) { int step = 0; while (step < 5) { try { URL url = uri.toURL(); if (proxyConfiguration != null && proxyConfiguration.getProxyType() == Type.HTTP) { System.setProperty("http.proxyHost", proxyConfiguration.getProxyIPHost()); System.setProperty("http.proxyPort", proxyConfiguration.getProxyPort().toString()); } else { System.setProperty("http.proxyHost", ""); System.setProperty("http.proxyPort", ""); } HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setReadTimeout(timeout); httpURLConnection.setConnectTimeout(timeout); int result = httpURLConnection.getResponseCode(); return result; } catch (Exception e) { LogWrapper.w(TAG, e.toString()); } step++; try { Thread.sleep(500); } catch (InterruptedException e) { APL.getEventReport().send(e); return -1; } } return -1; }
/** * Can take a long time to execute this task. - Check if the proxy is enabled - Check if the proxy * address is valid - Check if the proxy is reachable (using a PING) - Check if is possible to * retrieve an URI resource using the proxy */ public static void acquireProxyStatus( ProxyConfiguration conf, ProxyStatus status, EnumSet<ProxyCheckOptions> checkOptions, int timeout) { status.clear(); status.startchecking(); broadCastUpdatedStatus(); if (Build.VERSION.SDK_INT >= 12) { acquireProxyStatusSDK12(conf, status, checkOptions); } else { acquireProxyStatusSDK1_11(conf, status, checkOptions); } if (checkOptions.contains(ProxyCheckOptions.ONLINE_CHECK)) { // Always check if WEB is reachable LogWrapper.d(TAG, "Checking if web is reachable ..."); status.set(isWebReachable(conf, timeout)); broadCastUpdatedStatus(); } else { status.set(ProxyStatusProperties.WEB_REACHABLE, CheckStatusValues.NOT_CHECKED, false, false); } }