diff options
author | cyBerta <cyberta@riseup.net> | 2021-11-25 12:23:04 +0100 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2021-11-25 12:23:04 +0100 |
commit | 0ccec2d3bd7c7f890f7b651110ea10a8933eab29 (patch) | |
tree | 6809b31be22162523a187ad65b2d18048b7e2e0f /app/src/main | |
parent | dcee5f2fa1dc67e5b08223db3441da8bd38e8b20 (diff) |
implement UI, logic and test for prefer UDP preferences
Diffstat (limited to 'app/src/main')
8 files changed, 78 insertions, 16 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/base/fragments/SettingsFragment.java b/app/src/main/java/se/leap/bitmaskclient/base/fragments/SettingsFragment.java index ed1e8b6d..87c64a89 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/fragments/SettingsFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/fragments/SettingsFragment.java @@ -34,10 +34,12 @@ import static se.leap.bitmaskclient.base.MainActivity.ACTION_SHOW_VPN_FRAGMENT; import static se.leap.bitmaskclient.base.models.Constants.SHARED_PREFERENCES; import static se.leap.bitmaskclient.base.models.Constants.USE_BRIDGES; import static se.leap.bitmaskclient.base.models.Constants.USE_IPv6_FIREWALL; +import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getPreferUDP; import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getShowAlwaysOnDialog; import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getUseBridges; import static se.leap.bitmaskclient.base.utils.PreferenceHelper.getUseSnowflake; import static se.leap.bitmaskclient.base.utils.PreferenceHelper.hasSnowflakePrefs; +import static se.leap.bitmaskclient.base.utils.PreferenceHelper.preferUDP; import static se.leap.bitmaskclient.base.utils.PreferenceHelper.useBridges; import static se.leap.bitmaskclient.base.utils.PreferenceHelper.useSnowflake; @@ -63,10 +65,11 @@ public class SettingsFragment extends Fragment implements SharedPreferences.OnSh View view = inflater.inflate(R.layout.f_settings, container, false); initAlwaysOnVpnEntry(view); initExcludeAppsEntry(view); - initFirewallEntry(view); - initTetheringEntry(view); + initPreferUDPEntry(view); initUseBridgesEntry(view); initUseSnowflakeEntry(view); + initFirewallEntry(view); + initTetheringEntry(view); return view; } @@ -126,6 +129,18 @@ public class SettingsFragment extends Fragment implements SharedPreferences.OnSh } } + private void initPreferUDPEntry(View rootView) { + IconSwitchEntry useSnowflake = rootView.findViewById(R.id.prefer_udp); + useSnowflake.setVisibility(VISIBLE); + useSnowflake.setChecked(getPreferUDP(getContext())); + useSnowflake.setOnCheckedChangeListener((buttonView, isChecked) -> { + if (!buttonView.isPressed()) { + return; + } + preferUDP(getContext(), isChecked); + }); + } + private void initExcludeAppsEntry(View rootView) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { IconTextEntry excludeApps = rootView.findViewById(R.id.exclude_apps); diff --git a/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java b/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java index 016a8563..468b1b01 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/models/Constants.java @@ -42,6 +42,7 @@ public interface Constants { String LAST_UPDATE_CHECK = "last_update_check"; String PREFERRED_CITY = "preferred_city"; String USE_SNOWFLAKE = "use_snowflake"; + String PREFER_UDP = "prefer_UDP"; ////////////////////////////////////////////// @@ -163,6 +164,7 @@ public interface Constants { String REMOTE = "remote"; String PORTS = "ports"; String PROTOCOLS = "protocols"; + String UDP = "udp"; String CAPABILITIES = "capabilities"; String TRANSPORT = "transport"; String TYPE = "type"; diff --git a/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java b/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java index 515ec282..fe9100cb 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/utils/PreferenceHelper.java @@ -29,6 +29,7 @@ import static se.leap.bitmaskclient.base.models.Constants.EXCLUDED_APPS; import static se.leap.bitmaskclient.base.models.Constants.LAST_UPDATE_CHECK; import static se.leap.bitmaskclient.base.models.Constants.LAST_USED_PROFILE; import static se.leap.bitmaskclient.base.models.Constants.PREFERRED_CITY; +import static se.leap.bitmaskclient.base.models.Constants.PREFER_UDP; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_CONFIGURED; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_EIP_DEFINITION; import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_PRIVATE_KEY; @@ -145,6 +146,13 @@ public class PreferenceHelper { return getBoolean(context, RESTART_ON_UPDATE, false); } + public static boolean getPreferUDP(Context context) { + return getBoolean(context, PREFER_UDP, false); + } + + public static void preferUDP(Context context, boolean prefer) { + putBoolean(context, PREFER_UDP, prefer); + } public static boolean getUseBridges(SharedPreferences preferences) { return preferences.getBoolean(USE_BRIDGES, false); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java index a3d3abbc..60507363 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java @@ -174,7 +174,8 @@ public class Gateway { */ private @NonNull HashMap<Connection.TransportType, VpnProfile> createVPNProfiles(Context context) throws ConfigParser.ConfigParseError, IOException, JSONException { - VpnConfigGenerator vpnConfigurationGenerator = new VpnConfigGenerator(generalConfiguration, secrets, gateway, apiVersion); + boolean preferUDP = PreferenceHelper.getPreferUDP(context); + VpnConfigGenerator vpnConfigurationGenerator = new VpnConfigGenerator(generalConfiguration, secrets, gateway, apiVersion, preferUDP); HashMap<Connection.TransportType, VpnProfile> profiles = vpnConfigurationGenerator.generateVpnProfiles(); addProfileInfos(context, profiles); return profiles; diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index d72f0936..303959d0 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -48,6 +48,7 @@ import static se.leap.bitmaskclient.base.models.Constants.PROVIDER_VPN_CERTIFICA import static se.leap.bitmaskclient.base.models.Constants.REMOTE; import static se.leap.bitmaskclient.base.models.Constants.TRANSPORT; import static se.leap.bitmaskclient.base.models.Constants.TYPE; +import static se.leap.bitmaskclient.base.models.Constants.UDP; import static se.leap.bitmaskclient.pluggableTransports.Dispatcher.DISPATCHER_IP; import static se.leap.bitmaskclient.pluggableTransports.Dispatcher.DISPATCHER_PORT; @@ -57,16 +58,18 @@ public class VpnConfigGenerator { private JSONObject secrets; private JSONObject obfs4Transport; private int apiVersion; + private boolean preferUDP; public final static String TAG = VpnConfigGenerator.class.getSimpleName(); private final String newLine = System.getProperty("line.separator"); // Platform new line - public VpnConfigGenerator(JSONObject generalConfiguration, JSONObject secrets, JSONObject gateway, int apiVersion) throws ConfigParser.ConfigParseError { + public VpnConfigGenerator(JSONObject generalConfiguration, JSONObject secrets, JSONObject gateway, int apiVersion, boolean preferUDP) throws ConfigParser.ConfigParseError { this.generalConfiguration = generalConfiguration; this.gateway = gateway; this.secrets = secrets; this.apiVersion = apiVersion; + this.preferUDP = preferUDP; checkCapabilities(); } @@ -174,7 +177,7 @@ public class VpnConfigGenerator { default: case 1: case 2: - ipAddress = gateway.getString(IP_ADDRESS); + ipAddress = gateway.getString(IP_ADDRESS); gatewayConfigApiv1(stringBuilder, ipAddress, capabilities); break; case 3: @@ -213,9 +216,9 @@ public class VpnConfigGenerator { int port; String protocol; JSONArray ports = capabilities.getJSONArray(PORTS); + JSONArray protocols = capabilities.getJSONArray(PROTOCOLS); for (int i = 0; i < ports.length(); i++) { port = ports.getInt(i); - JSONArray protocols = capabilities.getJSONArray(PROTOCOLS); for (int j = 0; j < protocols.length(); j++) { protocol = protocols.optString(j); String newRemote = REMOTE + " " + ipAddress + " " + port + " " + protocol + newLine; @@ -229,14 +232,35 @@ public class VpnConfigGenerator { String protocol; JSONObject openvpnTransport = getTransport(transports, OPENVPN); JSONArray ports = openvpnTransport.getJSONArray(PORTS); - for (int j = 0; j < ports.length(); j++) { - port = ports.getString(j); - JSONArray protocols = openvpnTransport.getJSONArray(PROTOCOLS); - for (int k = 0; k < protocols.length(); k++) { - protocol = protocols.optString(k); - for (String ipAddress : ipAddresses) { - String newRemote = REMOTE + " " + ipAddress + " " + port + " " + protocol + newLine; - stringBuilder.append(newRemote); + JSONArray protocols = openvpnTransport.getJSONArray(PROTOCOLS); + if (preferUDP) { + StringBuilder udpRemotes = new StringBuilder(); + StringBuilder tcpRemotes = new StringBuilder(); + for (int i = 0; i < protocols.length(); i++) { + protocol = protocols.optString(i); + for (int j = 0; j < ports.length(); j++) { + port = ports.optString(j); + for (String ipAddress : ipAddresses) { + String newRemote = REMOTE + " " + ipAddress + " " + port + " " + protocol + newLine; + if (UDP.equals(protocol)) { + udpRemotes.append(newRemote); + } else { + tcpRemotes.append(newRemote); + } + } + } + } + stringBuilder.append(udpRemotes.toString()); + stringBuilder.append(tcpRemotes.toString()); + } else { + for (int j = 0; j < ports.length(); j++) { + port = ports.getString(j); + for (int k = 0; k < protocols.length(); k++) { + protocol = protocols.optString(k); + for (String ipAddress : ipAddresses) { + String newRemote = REMOTE + " " + ipAddress + " " + port + " " + protocol + newLine; + stringBuilder.append(newRemote); + } } } } diff --git a/app/src/main/res/drawable/ic_multiple_stop.xml b/app/src/main/res/drawable/ic_multiple_stop.xml new file mode 100644 index 00000000..c43c84f3 --- /dev/null +++ b/app/src/main/res/drawable/ic_multiple_stop.xml @@ -0,0 +1,10 @@ +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="24dp" + android:height="24dp" + android:viewportWidth="24" + android:viewportHeight="24" + android:tint="?attr/colorControlNormal"> + <path + android:fillColor="@android:color/white" + android:pathData="M17,4l4,4l-4,4V9h-4V7h4V4zM10,7C9.45,7 9,7.45 9,8s0.45,1 1,1s1,-0.45 1,-1S10.55,7 10,7zM6,7C5.45,7 5,7.45 5,8s0.45,1 1,1s1,-0.45 1,-1S6.55,7 6,7zM7,17h4v-2H7v-3l-4,4l4,4V17zM14,17c0.55,0 1,-0.45 1,-1c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1C13,16.55 13.45,17 14,17zM18,17c0.55,0 1,-0.45 1,-1c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1C17,16.55 17.45,17 18,17z"/> +</vector> diff --git a/app/src/main/res/layout/f_settings.xml b/app/src/main/res/layout/f_settings.xml index b625d548..1851fb54 100644 --- a/app/src/main/res/layout/f_settings.xml +++ b/app/src/main/res/layout/f_settings.xml @@ -35,7 +35,7 @@ android:visibility="visible" /> - <!-- <se.leap.bitmaskclient.base.views.IconSwitchEntry + <se.leap.bitmaskclient.base.views.IconSwitchEntry android:id="@+id/prefer_udp" android:layout_width="match_parent" android:layout_height="wrap_content" @@ -44,7 +44,6 @@ app:icon="@drawable/ic_multiple_stop" app:singleLine="false" /> - --> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/circumvention_header" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c49a456e..9ef0b9a5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -188,5 +188,8 @@ <string name="use_snowflake">Use Snowflake</string> <string name="snowflake_description">Circumvent blocking of the provider\'s configuration server.</string> <string name="vpn_settings">VPN settings</string> + <string name="prefer_udp">Use UDP if available</string> + <string name="prefer_udp_subtitle">UDP can be faster and better for streaming, but does not work for all networks.</string> + </resources> |