public static boolean checkIsProxyNet(Context context) { boolean isAboveICS; String proxyAddress; ((WifiManager) context.getSystemService("wifi")).getConnectionInfo(); if (VERSION.SDK_INT >= 14) { isAboveICS = true; } else { isAboveICS = false; } if (isAboveICS) { proxyAddress = System.getProperty("http.proxyHost"); } else { proxyAddress = Proxy.getHost(context); } if (TextUtils.isEmpty(proxyAddress)) { return false; } return true; }
private static HttpURLConnection a(URL url, String str) throws IOException { String replaceFirst; int i = 80; String defaultHost = Proxy.getDefaultHost(); int defaultPort = Proxy.getDefaultPort(); if (defaultPort == -1) { defaultPort = 80; } String host = url.getHost(); int port = url.getPort(); if (port != -1) { i = port; } if (str.indexOf(new StringBuilder(String.valueOf(host)).append(NetworkUtils.DELIMITER_COLON).append(i).toString()) != -1) { replaceFirst = str.replaceFirst(new StringBuilder(String.valueOf(host)).append(NetworkUtils.DELIMITER_COLON).append(i).toString(), new StringBuilder(String.valueOf(defaultHost)).append(NetworkUtils.DELIMITER_COLON).append(defaultPort).toString()); } else { replaceFirst = str.replaceFirst(host, new StringBuilder(String.valueOf(defaultHost)).append(NetworkUtils.DELIMITER_COLON).append(defaultPort).toString()); } try { HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(replaceFirst).openConnection(); httpURLConnection.setRequestProperty("X-Online-Host", new StringBuilder(String.valueOf(host)).append(NetworkUtils.DELIMITER_COLON).append(i).toString()); return httpURLConnection; } catch (MalformedURLException e) { return null; } }
private static int a(Context context) { int i = -1; if (VERSION.SDK_INT >= 11) { Object property = System.getProperty("http.proxyPort"); if (TextUtils.isEmpty(property)) { return i; } try { return Integer.parseInt(property); } catch (NumberFormatException e) { return i; } } else if (context == null) { return Proxy.getDefaultPort(); } else { i = Proxy.getPort(context); if (i < 0) { return Proxy.getDefaultPort(); } return i; } }
private static String b(Context context) { if (android.os.Build.VERSION.SDK_INT < 11) { if (context != null) { String s = Proxy.getHost(context); if (TextUtils.isEmpty(s)) { s = Proxy.getDefaultHost(); } return s; } else { return Proxy.getDefaultHost(); } } else { return System.getProperty("http.proxyHost"); } }
/** * @param context context * @param info current network info * @return the proxy to be used for the given network, or null */ public static String getProxy(Context context, NetworkInfo info) { final String proxy; if (info.getType() == ConnectivityManager.TYPE_MOBILE) { // adjust mobile proxy settings String proxyHost = Proxy.getHost(context); if (proxyHost == null) { proxyHost = Proxy.getDefaultHost(); } int proxyPort = Proxy.getPort(context); if (proxyPort == -1) { proxyPort = Proxy.getDefaultPort(); } if (proxyHost != null && proxyPort > -1) { proxy = new HttpHost(proxyHost, proxyPort).toURI(); } else { proxy = null; } } else { proxy = null; } return proxy; }
private void registerReceiver() { if (mProxyReceiver != null) { return; } IntentFilter filter = new IntentFilter(); filter.addAction(Proxy.PROXY_CHANGE_ACTION); mProxyReceiver = new ProxyReceiver(); mContext.getApplicationContext().registerReceiver(mProxyReceiver, filter); }
private String detectProxy() { String proxyUrl = null; if (!TextUtils.isEmpty(Proxy.getDefaultHost())) { proxyUrl = String.format(FORMAT_PROXY_URL, new Object[]{Proxy.getDefaultHost(), Integer.valueOf(Proxy.getDefaultPort())}); } LogTool.i(TAG, "detectProxy. detected the proxy url(%s)", proxyUrl); return TextUtils.isEmpty(proxyUrl) ? "" : URLEncoder.encode(proxyUrl); }
private static String b(Context context) { if (VERSION.SDK_INT >= 11) { return System.getProperty("http.proxyHost"); } if (context == null) { return Proxy.getDefaultHost(); } String host = Proxy.getHost(context); if (TextUtils.isEmpty(host)) { return Proxy.getDefaultHost(); } return host; }
public static HttpHost v(Context context) { if (context == null) { return null; } try { if (context.getPackageManager().checkPermission("android.permission" + ".ACCESS_NETWORK_STATE", context.getPackageName()) != 0) { return null; } NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService ("connectivity")).getActiveNetworkInfo(); if (activeNetworkInfo == null) { return null; } if (activeNetworkInfo.getTypeName() != null && activeNetworkInfo.getTypeName() .equalsIgnoreCase("WIFI")) { return null; } String extraInfo = activeNetworkInfo.getExtraInfo(); if (extraInfo == null) { return null; } if (extraInfo.equals("cmwap") || extraInfo.equals("3gwap") || extraInfo.equals ("uniwap")) { return new HttpHost(Network.CMWAP_GATEWAY, 80); } if (extraInfo.equals("ctwap")) { return new HttpHost("10.0.0.200", 80); } String defaultHost = Proxy.getDefaultHost(); if (defaultHost != null && defaultHost.trim().length() > 0) { return new HttpHost(defaultHost, Proxy.getDefaultPort()); } return null; } catch (Throwable th) { cT.b(th); } }
private HttpHost c() { String g; if (VERSION.SDK_INT >= 11) { g = g(); if (g != null && !g.contains("wap")) { return null; } URL b = b(); if (b == null) { return null; } b.a.equalsIgnoreCase(b.getProtocol()); Object property = System.getProperty("https.proxyHost"); String property2 = System.getProperty("https.proxyPort"); if (TextUtils.isEmpty(property)) { return null; } return new HttpHost(property, Integer.parseInt(property2)); } NetworkInfo f = f(); if (f == null || !f.isAvailable() || f.getType() != 0) { return null; } g = Proxy.getDefaultHost(); int defaultPort = Proxy.getDefaultPort(); if (g != null) { return new HttpHost(g, defaultPort); } return null; }
private HttpHost d() { NetworkInfo f = f(); if (f == null || !f.isAvailable() || f.getType() != 0) { return null; } String defaultHost = Proxy.getDefaultHost(); int defaultPort = Proxy.getDefaultPort(); if (defaultHost != null) { return new HttpHost(defaultHost, defaultPort); } return null; }
/** * Set Proxy for Android 5.0 and above. */ @SuppressWarnings("all") private static boolean setLollipopWebViewProxy(Context appContext, String host, int port) { System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", port + ""); System.setProperty("https.proxyHost", host); System.setProperty("https.proxyPort", port + ""); try { Class applictionCls = Class.forName("android.app.Application"); Field loadedApkField = applictionCls.getDeclaredField("mLoadedApk"); loadedApkField.setAccessible(true); Object loadedApk = loadedApkField.get(appContext); Class loadedApkCls = Class.forName("android.app.LoadedApk"); Field receiversField = loadedApkCls.getDeclaredField("mReceivers"); receiversField.setAccessible(true); ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk); for (Object receiverMap : receivers.values()) { for (Object rec : ((ArrayMap) receiverMap).keySet()) { Class clazz = rec.getClass(); if (clazz.getName().contains("ProxyChangeListener")) { Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class); Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); /***** In Lollipop, ProxyProperties went public as ProxyInfo *****/ final String CLASS_NAME = "android.net.ProxyInfo"; Class cls = Class.forName(CLASS_NAME); /***** ProxyInfo lacks constructors, use the static buildDirectProxy method instead *****/ Method buildDirectProxyMethod = cls.getMethod("buildDirectProxy", String.class, Integer.TYPE); Object proxyInfo = buildDirectProxyMethod.invoke(cls, host, port); intent.putExtra("proxy", (Parcelable) proxyInfo); onReceiveMethod.invoke(rec, appContext, intent); } } } } catch (Exception e) { Log.e(LOG_TAG, "Setting proxy with >= 5.0 API failed with", e); return false; } Log.d(LOG_TAG, "Setting proxy with >= 5.0 API successful!"); return true; }
public static void setUseProxy (boolean useProxy) { HttpParams params = getHttpClient().getParams(); if (useProxy) { try { params.setParameter(ConnRouteParams.DEFAULT_PROXY, new HttpHost(Proxy.getDefaultHost(), Proxy.getDefaultPort())); } catch (Exception e) { e.printStackTrace(); } } else { params.removeParameter(ConnRouteParams.DEFAULT_PROXY); } }
@Override public void onReceive(Context context, final Intent intent) { if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) { runOnThread(new Runnable() { @Override public void run() { proxySettingsChanged(ProxyReceiver.this, extractNewProxy(intent)); } }); } }
private void registerReceiver() { if (mProxyReceiver != null) { return; } IntentFilter filter = new IntentFilter(); filter.addAction(Proxy.PROXY_CHANGE_ACTION); mProxyReceiver = new ProxyReceiver(); ContextUtils.getApplicationContext().registerReceiver(mProxyReceiver, filter); }
private static HttpURLConnection a(URL url, String s) { int k = 80; String s1 = Proxy.getDefaultHost(); int l = Proxy.getDefaultPort(); if (l == -1) { l = k; } String s2 = url.getHost(); int i1 = url.getPort(); String s3; URL url1; HttpURLConnection httpurlconnection; if (i1 != -1) { k = i1; } if (s.indexOf((new StringBuilder(String.valueOf(s2))).append(":").append(k).toString()) != -1) { s3 = s.replaceFirst((new StringBuilder(String.valueOf(s2))).append(":").append(k).toString(), (new StringBuilder(String.valueOf(s1))).append(":").append(l).toString()); } else { s3 = s.replaceFirst(s2, (new StringBuilder(String.valueOf(s1))).append(":").append(l).toString()); } try { url1 = new URL(s3); } catch (MalformedURLException malformedurlexception) { return null; } httpurlconnection = (HttpURLConnection)url1.openConnection(); httpurlconnection.setRequestProperty("X-Online-Host", (new StringBuilder(String.valueOf(s2))).append(":").append(k).toString()); return httpurlconnection; }
private void setupProxyListener(Context context) { IntentFilter filter = new IntentFilter(); filter.addAction(Proxy.PROXY_CHANGE_ACTION); mProxyReceiver = new ProxyReceiver(); mProxyContext = context; Intent currentProxy = context.getApplicationContext().registerReceiver(mProxyReceiver, filter); if (currentProxy != null) { handleProxyBroadcast(currentProxy); } }
private void handleProxyBroadcast(Intent intent) { ProxyProperties props = (ProxyProperties)intent.getExtra(Proxy.EXTRA_PROXY_INFO); if (props == null || props.getHost() == null) { updateProxyConfig(null); } else { updateProxyConfig(props); } }
@SuppressWarnings("deprecation") public static void setUseProxy (boolean useProxy) { HttpParams params = getHttpClient().getParams(); if (useProxy) { try { params.setParameter(ConnRouteParams.DEFAULT_PROXY, new HttpHost(Proxy.getDefaultHost(), Proxy.getDefaultPort())); } catch (Exception e) { e.printStackTrace(); } } else { params.removeParameter(ConnRouteParams.DEFAULT_PROXY); } }
@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) { proxySettingsChanged(extractNewProxy(intent)); } }
public static boolean isProxy() { return !TextUtils.isEmpty(Proxy.getDefaultHost()); }
public static String[] getProxyHostAndPort(Context context) { return getNetworkType(context) == NetworkType.WIFI ? new String[]{"", "-1"} : new String[]{Proxy.getDefaultHost(), "" + Proxy.getDefaultPort()}; }
/** * Set Proxy for Android 4.4 and above. */ @SuppressWarnings("all") private static boolean setKitKatWebViewProxy(Context appContext, String host, int port) { System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", port + ""); System.setProperty("https.proxyHost", host); System.setProperty("https.proxyPort", port + ""); try { Class applictionCls = Class.forName("android.app.Application"); Field loadedApkField = applictionCls.getDeclaredField("mLoadedApk"); loadedApkField.setAccessible(true); Object loadedApk = loadedApkField.get(appContext); Class loadedApkCls = Class.forName("android.app.LoadedApk"); Field receiversField = loadedApkCls.getDeclaredField("mReceivers"); receiversField.setAccessible(true); ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk); for (Object receiverMap : receivers.values()) { for (Object rec : ((ArrayMap) receiverMap).keySet()) { Class clazz = rec.getClass(); if (clazz.getName().contains("ProxyChangeListener")) { Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class); Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); /*********** optional, may be need in future *************/ final String CLASS_NAME = "android.net.ProxyProperties"; Class cls = Class.forName(CLASS_NAME); Constructor constructor = cls.getConstructor(String.class, Integer.TYPE, String.class); constructor.setAccessible(true); Object proxyProperties = constructor.newInstance(host, port, null); intent.putExtra("proxy", (Parcelable) proxyProperties); /*********** optional, may be need in future *************/ onReceiveMethod.invoke(rec, appContext, intent); } } } } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | IllegalArgumentException | NoSuchMethodException | InvocationTargetException | InstantiationException e) { Log.e(LOG_TAG, "Setting proxy with >= 4.4 API failed with error: ", e); return false; } Log.d(LOG_TAG, "Setting proxy with >= 4.4 API successful!"); return true; }
private void handleIntentLogic(Intent intent) { // LogWrapper.logIntent(TAG, "onHandleIntent: ", intent, Log.VERBOSE); if (intent != null && intent.hasExtra(CALLER_INTENT)) { Intent callerIntent = (Intent) intent.getExtras().get(CALLER_INTENT); if (callerIntent != null) { String callerAction = callerIntent.getAction(); App.getTraceUtils().logIntent(TAG, "onHandleIntent: ", callerIntent, Log.DEBUG); if (callerAction.equals(Intents.PROXY_SETTINGS_STARTED) || callerAction.equals(Intents.PROXY_SETTINGS_MANUAL_REFRESH) || callerAction.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) || callerAction.equals(WifiManager.WIFI_STATE_CHANGED_ACTION) // || callerAction.equals(Intents.WIFI_AP_UPDATED) || callerAction.equals(Proxy.PROXY_CHANGE_ACTION) || callerAction.equals("android.net.wifi.CONFIGURED_NETWORKS_CHANGED_ACTION")) { checkProxySettings(); } else if (callerAction.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { Boolean noConnectivity = callerIntent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); if (noConnectivity) { return; } //TODO : check here //int intentNetworkType = callerIntent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_INFO , -1); NetworkInfo ni = APL.getConnectivityManager().getActiveNetworkInfo(); if (ni != null && ni.isConnected()) { checkProxySettings(); } else { Timber.d("Do not check proxy settings if network is not available!"); } } else { Timber.e("Intent ACTION not handled: " + callerAction); } } else { Timber.e("Received Intent NULL ACTION"); } } return; }
public void setHttpProxy(String host, String port, String exclList, String pacFileUrl) { Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl); }
@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) { handleProxyBroadcast(intent); } }
/** * Set Proxy for KiKkat. */ @SuppressWarnings ("all") private static boolean setProxyKitKat(Context appContext, String host, int port) { System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", port + ""); System.setProperty("https.proxyHost", host); System.setProperty("https.proxyPort", port + ""); try { Class applictionCls = Class.forName("android.app.Application"); Field loadedApkField = applictionCls.getDeclaredField("mLoadedApk"); loadedApkField.setAccessible(true); Object loadedApk = loadedApkField.get(appContext); Class loadedApkCls = Class.forName("android.app.LoadedApk"); Field receiversField = loadedApkCls.getDeclaredField("mReceivers"); receiversField.setAccessible(true); ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk); for (Object receiverMap : receivers.values()) { for (Object rec : ((ArrayMap) receiverMap).keySet()) { Class clazz = rec.getClass(); if (clazz.getName().contains("ProxyChangeListener")) { Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class); Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); /*********** optional, may be need in future *************/ final String CLASS_NAME = "android.net.ProxyProperties"; Class cls = Class.forName(CLASS_NAME); Constructor constructor = cls.getConstructor(String.class, Integer.TYPE, String.class); constructor.setAccessible(true); Object proxyProperties = constructor.newInstance(host, port, null); intent.putExtra("proxy", (Parcelable) proxyProperties); /*********** optional, may be need in future *************/ onReceiveMethod.invoke(rec, appContext, intent); } } } } catch (Exception ex) { Log.e(LOG_TAG, "Setting proxy for KitKat failed with error: " + ex.getMessage()); return false; } Log.d(LOG_TAG, "Setting proxy for KitKat succeeded"); return true; }
@Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) { proxySettingsChanged(); } }
/** * Returns the preferred proxy to be used by clients. This is a wrapper * around {@link android.net.Proxy#getHost()}. Currently no proxy will be * returned for localhost or if the active network is Wi-Fi. * * @param context the context which will be passed to * {@link android.net.Proxy#getHost()} * @param url the target URL for the request * @note Calling this method requires permission * android.permission.ACCESS_NETWORK_STATE * @return The preferred proxy to be used by clients, or null if there is no * proxy. */ public HttpHost getPreferredHttpHost(Context context, String url) { if (!isLocalHost(url) && !mService.isWiFi()) { final String proxyHost = Proxy.getHost(context); if (proxyHost != null) { return new HttpHost(proxyHost, Proxy.getPort(context), "http"); } } return null; }