Exemplo n.º 1
0
  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;
  }
Exemplo n.º 2
0
  public static ProxyStatusItem isProxyValidExclusionAddress(String proxyExclusionAddress) {
    try {
      //            if (TextUtils.isEmpty(proxyExclusionAddress))
      //            {
      //                return new ProxyStatusItem(ProxyStatusProperties.PROXY_VALID_EXCLUSION_ITEM,
      // CheckStatusValues.CHECKED, false,
      // APL.getContext().getString(R.string.status_exclusion_item_empty));
      //            }
      //            else
      //            {
      Matcher match = APLConstants.EXCLUSION_PATTERN.matcher(proxyExclusionAddress);
      if (match.matches()) {
        String msg =
            String.format(
                "%s %s",
                APL.getContext().getString(R.string.status_exclusion_item_valid),
                proxyExclusionAddress);
        return new ProxyStatusItem(
            ProxyStatusProperties.PROXY_VALID_EXCLUSION_ITEM, CheckStatusValues.CHECKED, true, msg);
      }
      //            }
    } catch (Exception e) {
      APL.getEventReport().send(e);
    }

    return new ProxyStatusItem(
        ProxyStatusProperties.PROXY_VALID_EXCLUSION_ITEM,
        CheckStatusValues.CHECKED,
        false,
        APL.getContext().getString(R.string.status_exclusion_item_notvalid));
  }
Exemplo n.º 3
0
  public static ProxyStatusItem isProxyValidExclusionList(String[] proxyExclusionList) {
    try {
      for (int i = 0; i < proxyExclusionList.length; i++) {
        String s = proxyExclusionList[i].trim();
        ProxyStatusItem status = isProxyValidExclusionAddress(s);

        if (!status.result) {
          return new ProxyStatusItem(
              ProxyStatusProperties.PROXY_VALID_EXCLUSION_LIST,
              CheckStatusValues.CHECKED,
              true,
              APL.getContext().getString(R.string.status_exclusion_list_notvalid));
        }
      }

      String msg =
          String.format(
              "%s %s",
              APL.getContext().getString(R.string.status_exclusion_item_valid),
              TextUtils.join(",", proxyExclusionList));
      return new ProxyStatusItem(
          ProxyStatusProperties.PROXY_VALID_EXCLUSION_ITEM, CheckStatusValues.CHECKED, true, msg);
    } catch (Exception e) {
      APL.getEventReport().send(e);
    }

    return new ProxyStatusItem(
        ProxyStatusProperties.PROXY_VALID_EXCLUSION_ITEM,
        CheckStatusValues.CHECKED,
        false,
        APL.getContext().getString(R.string.status_exclusion_item_notvalid));
  }
Exemplo n.º 4
0
  public static ProxyStatusItem isProxyValidHostname(String proxyHost) {
    try {
      if (TextUtils.isEmpty(proxyHost)) {
        return new ProxyStatusItem(
            ProxyStatusProperties.PROXY_VALID_HOSTNAME,
            CheckStatusValues.CHECKED,
            false,
            APL.getContext().getString(R.string.status_hostname_empty));
      } else {
        Matcher match = APLConstants.HOSTNAME_PATTERN.matcher(proxyHost);
        if (match.matches()) {
          String hostnameValidMsg = APL.getContext().getString(R.string.status_hostname_valid);
          String msg = String.format("%s %s", hostnameValidMsg, proxyHost);
          return new ProxyStatusItem(
              ProxyStatusProperties.PROXY_VALID_HOSTNAME, CheckStatusValues.CHECKED, true, msg);
        }
      }
    } catch (Exception e) {
      APL.getEventReport().send(e);
    }

    return new ProxyStatusItem(
        ProxyStatusProperties.PROXY_VALID_HOSTNAME,
        CheckStatusValues.CHECKED,
        false,
        APL.getContext().getString(R.string.status_hostname_notvalid));
  }
Exemplo n.º 5
0
  public static boolean standardAPIPingHost(Proxy proxy) {
    try {
      InetSocketAddress proxySocketAddress = (InetSocketAddress) proxy.address();
      return InetAddress.getByName(proxySocketAddress.toString().split(":")[0]).isReachable(100000);
      //			return proxySocketAddress.getAddress().isReachable(100000);
    } catch (Exception e) {
      APL.getEventReport().send(new Exception("ProxyUtils.standardAPIPingHost() Exception", e));
    }

    return false;
  }
Exemplo n.º 6
0
 private static boolean setProxy(Context ctx, String host, int port) {
   boolean ret = false;
   try {
     Object requestQueueObject = getRequestQueue(ctx);
     if (requestQueueObject != null) {
       // Create Proxy config object and set it into request Q
       HttpHost httpHost = new HttpHost(host, port, "http");
       setDeclaredField(requestQueueObject, "mProxyHost", httpHost);
       // LogWrapper.d("Webkit Setted Proxy to: " + host + ":" + port);
       ret = true;
     }
   } catch (Exception e) {
     APL.getEventReport().send(new Exception("Exception setting WebKit proxy settings", e));
   }
   return ret;
 }
Exemplo n.º 7
0
 /**
  * Try to set the Proxy Settings for active WebViews. This works only for devices with API version
  * < 12.
  */
 public static void setWebViewProxy(Context context, ProxyConfiguration proxyConf) {
   if (Build.VERSION.SDK_INT >= 12) {
     // On newer devices do nothing!
   } else {
     // On older devices try to set or clear the proxy settings, depending on the argument
     // configuration
     try {
       if (proxyConf != null
           && proxyConf.getProxyType() == Type.HTTP
           && APL.getDeviceVersion() < 12) {
         setProxy(context, proxyConf.getProxyIPHost(), proxyConf.getProxyPort());
       } else {
         resetProxy(context);
       }
     } catch (Exception e) {
       APL.getEventReport().send(e);
     }
   }
 }
Exemplo n.º 8
0
  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;
  }