Added debug option to bind sockets to active network

This commit is contained in:
M66B
2021-09-02 21:31:06 +02:00
parent 0c25724063
commit a2c3f642e0
6 changed files with 51 additions and 5 deletions

View File

@@ -24,6 +24,10 @@ import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_OAUTH;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.security.KeyChain;
import android.system.ErrnoException;
@@ -353,6 +357,19 @@ public class EmailService implements AutoCloseable {
String certificate, String fingerprint) throws MessagingException {
properties.put("fairemail.server", host);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean bind_socket = prefs.getBoolean("bind_socket", false);
if (bind_socket &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
try {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network active = cm.getActiveNetwork();
if (active != null)
properties.put("fairemail.factory", active.getSocketFactory());
} catch (Throwable ex) {
Log.e(ex);
}
SSLSocketFactoryService factory = null;
try {
PrivateKey key = null;

View File

@@ -65,6 +65,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
private SwitchCompat swValidated;
private EditText etTimeout;
private SwitchCompat swPreferIp4;
private SwitchCompat swBindSocket;
private SwitchCompat swStandaloneVpn;
private SwitchCompat swTcpKeepAlive;
private TextView tvTcpKeepAliveHint;
@@ -80,7 +81,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
private final static String[] RESET_OPTIONS = new String[]{
"metered", "download", "roaming", "rlah",
"download_headers", "download_eml",
"require_validated", "timeout", "prefer_ip4", "standalone_vpn", "tcp_keep_alive", "ssl_harden"
"require_validated", "timeout", "prefer_ip4", "bind_socket", "standalone_vpn", "tcp_keep_alive", "ssl_harden"
};
@Override
@@ -102,6 +103,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
swValidated = view.findViewById(R.id.swValidated);
etTimeout = view.findViewById(R.id.etTimeout);
swPreferIp4 = view.findViewById(R.id.swPreferIp4);
swBindSocket = view.findViewById(R.id.swBindSocket);
swStandaloneVpn = view.findViewById(R.id.swStandaloneVpn);
swTcpKeepAlive = view.findViewById(R.id.swTcpKeepAlive);
tvTcpKeepAliveHint = view.findViewById(R.id.tvTcpKeepAliveHint);
@@ -211,6 +213,15 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
}
});
swBindSocket.setVisibility(debug || BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
swBindSocket.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("bind_socket", checked).apply();
}
});
swStandaloneVpn.setVisibility(debug || BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
swStandaloneVpn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@@ -348,6 +359,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
etTimeout.setHint(Integer.toString(EmailService.DEFAULT_CONNECT_TIMEOUT));
swPreferIp4.setChecked(prefs.getBoolean("prefer_ip4", true));
swBindSocket.setChecked(prefs.getBoolean("bind_socket", false));
swStandaloneVpn.setChecked(prefs.getBoolean("standalone_vpn", false));
swTcpKeepAlive.setChecked(prefs.getBoolean("tcp_keep_alive", false));
swSslHarden.setChecked(prefs.getBoolean("ssl_harden", false));

View File

@@ -148,7 +148,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
"sync_folders",
"sync_shared_folders",
"download_headers", "download_eml",
"prefer_ip4", "standalone_vpn", "tcp_keep_alive", "ssl_harden", // force reconnect
"prefer_ip4", "bind_socket", "standalone_vpn", "tcp_keep_alive", "ssl_harden", // force reconnect
"experiments", "debug", "protocol", // force reconnect
"auth_plain", "auth_login", "auth_ntlm", "auth_sasl", // force reconnect
"exact_alarms" // force schedule