X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fhelpers%2FProxyHelper.java;h=04e40e9b4dad4ef85fa5625b38079bc4001812c0;hp=dce61690937c639367b46aea0d267edf3f0b5180;hb=5c9367467e671483924626fa69eb8a4e61d24352;hpb=01d647c09b892384e0b42e86b000b5cb858f7a2b diff --git a/app/src/main/java/com/stoutner/privacybrowser/helpers/ProxyHelper.java b/app/src/main/java/com/stoutner/privacybrowser/helpers/ProxyHelper.java index dce61690..04e40e9b 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/helpers/ProxyHelper.java +++ b/app/src/main/java/com/stoutner/privacybrowser/helpers/ProxyHelper.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2019 Soren Stoutner . + * Copyright © 2016-2020 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -23,12 +23,10 @@ import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.net.Proxy; import android.net.Uri; import android.os.Build; import android.os.Parcelable; import android.util.ArrayMap; -import android.util.Log; import android.view.View; import androidx.preference.PreferenceManager; @@ -44,6 +42,9 @@ import com.stoutner.privacybrowser.activities.MainWebViewActivity; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.SocketAddress; import java.util.concurrent.Executor; public class ProxyHelper { @@ -210,7 +211,7 @@ public class ProxyHelper { Method onReceiveMethod = receiverClass.getDeclaredMethod("onReceive", Context.class, Intent.class); // Create a proxy change intent. - Intent proxyChangeIntent = new Intent(Proxy.PROXY_CHANGE_ACTION); + Intent proxyChangeIntent = new Intent(android.net.Proxy.PROXY_CHANGE_ACTION); if (Build.VERSION.SDK_INT >= 21) { // Get a proxy info class. @@ -234,9 +235,81 @@ public class ProxyHelper { } } } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException exception) { - Log.d("enableProxyThroughOrbot", "Exception: " + exception); + // Do nothing. } } } } + + public Proxy getCurrentProxy(Context context) { + // Define a proxy variable. + Proxy proxy; + + // Get the proxy according to the current proxy mode. + switch (MainWebViewActivity.proxyMode) { + case (ProxyHelper.TOR): + if (Build.VERSION.SDK_INT >= 21) { + // Use localhost port 9050 as the socket address. + SocketAddress torSocketAddress = InetSocketAddress.createUnresolved("localhost", 9050); + + // Create a SOCKS proxy. + proxy = new Proxy(Proxy.Type.SOCKS, torSocketAddress); + } else { + // Use localhost port 8118 as the socket address. + SocketAddress oldTorSocketAddress = InetSocketAddress.createUnresolved("localhost", 8118); + + // Create an HTTP proxy. + proxy = new Proxy(Proxy.Type.HTTP, oldTorSocketAddress); + } + break; + + case (ProxyHelper.I2P): + // Use localhost port 4444 as the socket address. + SocketAddress i2pSocketAddress = InetSocketAddress.createUnresolved("localhost", 4444); + + // Create an HTTP proxy. + proxy = new Proxy(Proxy.Type.HTTP, i2pSocketAddress); + break; + + case (ProxyHelper.CUSTOM): + // Get the shared preferences. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + + // Get the custom proxy URL string. + String customProxyUrlString = sharedPreferences.getString("proxy_custom_url", context.getString(R.string.proxy_custom_url_default_value)); + + // Parse the custom proxy URL. + try { + // Convert the custom proxy URL string to a URI. + Uri customProxyUri = Uri.parse(customProxyUrlString); + + // Get the custom socket address. + SocketAddress customSocketAddress = InetSocketAddress.createUnresolved(customProxyUri.getHost(), customProxyUri.getPort()); + + // Get the custom proxy scheme. + String customProxyScheme = customProxyUri.getScheme(); + + // Create a proxy according to the scheme. + if ((customProxyScheme != null) && customProxyScheme.startsWith("socks")) { // A SOCKS proxy is specified. + // Create a SOCKS proxy. + proxy = new Proxy(Proxy.Type.SOCKS, customSocketAddress); + } else { // A SOCKS proxy is not specified. + // Create an HTTP proxy. + proxy = new Proxy(Proxy.Type.HTTP, customSocketAddress); + } + } catch (Exception exception) { // The custom proxy cannot be parsed. + // Disable the proxy. + proxy = Proxy.NO_PROXY; + } + break; + + default: // No proxy is in use. + // Create a direct proxy. + proxy = Proxy.NO_PROXY; + break; + } + + // Return the proxy. + return proxy; + } } \ No newline at end of file