Added option to enable TCP keep alive

pull/187/head
M66B 4 years ago
parent da35152457
commit af01f4173d

@ -88,6 +88,14 @@ public class ApplicationEx extends Application implements SharedPreferences.OnSh
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final boolean crash_reports = prefs.getBoolean("crash_reports", false); final boolean crash_reports = prefs.getBoolean("crash_reports", false);
try {
boolean tcp_keep_alive = prefs.getBoolean("tcp_keep_alive", true);
System.setProperty("fairemail.tcp_keep_alive", Boolean.toString(tcp_keep_alive));
} catch (Throwable ex) {
Log.e(ex);
}
prefs.registerOnSharedPreferenceChangeListener(this); prefs.registerOnSharedPreferenceChangeListener(this);
prev = Thread.getDefaultUncaughtExceptionHandler(); prev = Thread.getDefaultUncaughtExceptionHandler();

@ -23,6 +23,7 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.security.KeyChain; import android.security.KeyChain;
import android.system.ErrnoException;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -131,6 +132,8 @@ public class EmailService implements AutoCloseable {
private final static int FETCH_SIZE = 1024 * 1024; // bytes, default 16K private final static int FETCH_SIZE = 1024 * 1024; // bytes, default 16K
private final static int POOL_TIMEOUT = 45 * 1000; // milliseconds, default 45 sec private final static int POOL_TIMEOUT = 45 * 1000; // milliseconds, default 45 sec
private final static int TCP_KEEP_ALIVE_INTERVAL = 9 * 60; // seconds
private static final int APPEND_BUFFER_SIZE = 4 * 1024 * 1024; // bytes private static final int APPEND_BUFFER_SIZE = 4 * 1024 * 1024; // bytes
// https://developer.android.com/reference/javax/net/ssl/SSLSocket.html#protocols // https://developer.android.com/reference/javax/net/ssl/SSLSocket.html#protocols
@ -1047,10 +1050,21 @@ public class EmailService implements AutoCloseable {
socket.setSoLinger(false, -1); socket.setSoLinger(false, -1);
} }
int fd = ParcelFileDescriptor.fromSocket(socket).getFd(); try {
int errno = jni_socket_keep_alive(fd, 9 * 60); boolean tcp_keep_alive = Boolean.parseBoolean(System.getProperty("fairemail.tcp_keep_alive"));
if (errno != 0) if (tcp_keep_alive) {
Log.e("Socket TCP_KEEPIDLE=" + errno); Log.i("Enabling TCP keep alive");
int fd = ParcelFileDescriptor.fromSocket(socket).getFd();
int errno = jni_socket_keep_alive(fd, TCP_KEEP_ALIVE_INTERVAL);
if (errno != 0)
throw new ErrnoException("TCP_KEEPIDLE", errno);
socket.setKeepAlive(true);
}
} catch (Throwable ex) {
Log.e(ex);
}
} }
class UntrustedException extends MessagingException { class UntrustedException extends MessagingException {

@ -61,6 +61,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
private SwitchCompat swRlah; private SwitchCompat swRlah;
private EditText etTimeout; private EditText etTimeout;
private SwitchCompat swPreferIp4; private SwitchCompat swPreferIp4;
private SwitchCompat swTcpKeepAlive;
private SwitchCompat swSslHarden; private SwitchCompat swSslHarden;
private Button btnManage; private Button btnManage;
private TextView tvNetworkMetered; private TextView tvNetworkMetered;
@ -68,7 +69,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
private TextView tvNetworkInfo; private TextView tvNetworkInfo;
private final static String[] RESET_OPTIONS = new String[]{ private final static String[] RESET_OPTIONS = new String[]{
"metered", "download", "roaming", "rlah", "timeout", "prefer_ip4", "ssl_harden" "metered", "download", "roaming", "rlah", "timeout", "prefer_ip4", "tcp_keep_alive", "ssl_harden"
}; };
@Override @Override
@ -87,6 +88,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
swRlah = view.findViewById(R.id.swRlah); swRlah = view.findViewById(R.id.swRlah);
etTimeout = view.findViewById(R.id.etTimeout); etTimeout = view.findViewById(R.id.etTimeout);
swPreferIp4 = view.findViewById(R.id.swPreferIp4); swPreferIp4 = view.findViewById(R.id.swPreferIp4);
swTcpKeepAlive = view.findViewById(R.id.swTcpKeepAlive);
swSslHarden = view.findViewById(R.id.swSslHarden); swSslHarden = view.findViewById(R.id.swSslHarden);
btnManage = view.findViewById(R.id.btnManage); btnManage = view.findViewById(R.id.btnManage);
@ -166,6 +168,18 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
} }
}); });
swTcpKeepAlive.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
try {
System.setProperty("fairemail.tcp_keep_alive", Boolean.toString(checked));
} catch (Throwable ex) {
Log.e(ex);
}
prefs.edit().putBoolean("tcp_keep_alive", checked).apply();
}
});
swSslHarden.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { swSslHarden.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -281,6 +295,7 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
etTimeout.setHint(Integer.toString(EmailService.DEFAULT_CONNECT_TIMEOUT)); etTimeout.setHint(Integer.toString(EmailService.DEFAULT_CONNECT_TIMEOUT));
swPreferIp4.setChecked(prefs.getBoolean("prefer_ip4", true)); swPreferIp4.setChecked(prefs.getBoolean("prefer_ip4", true));
swTcpKeepAlive.setChecked(prefs.getBoolean("tcp_keep_alive", true));
swSslHarden.setChecked(prefs.getBoolean("ssl_harden", false)); swSslHarden.setChecked(prefs.getBoolean("ssl_harden", false));
} }

@ -134,7 +134,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
"sync_kept", "sync_kept",
"sync_folders", "sync_folders",
"sync_shared_folders", "sync_shared_folders",
"prefer_ip4", "ssl_harden", // force reconnect "prefer_ip4", "tcp_keep_alive", "ssl_harden", // force reconnect
"badge", "unseen_ignored", // force update badge/widget "badge", "unseen_ignored", // force update badge/widget
"protocol", "debug", // force reconnect "protocol", "debug", // force reconnect
"auth_plain", "auth_plain",

@ -196,6 +196,18 @@
app:layout_constraintTop_toBottomOf="@id/etTimeout" app:layout_constraintTop_toBottomOf="@id/etTimeout"
app:switchPadding="12dp" /> app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swTcpKeepAlive"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:checked="true"
android:text="@string/title_advanced_tcp_keep_alive"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swPreferIp4"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat <androidx.appcompat.widget.SwitchCompat
android:id="@+id/swSslHarden" android:id="@+id/swSslHarden"
android:layout_width="0dp" android:layout_width="0dp"
@ -204,7 +216,7 @@
android:text="@string/title_advanced_ssl_harden" android:text="@string/title_advanced_ssl_harden"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swPreferIp4" app:layout_constraintTop_toBottomOf="@id/swTcpKeepAlive"
app:switchPadding="12dp" /> app:switchPadding="12dp" />
<eu.faircode.email.FixedTextView <eu.faircode.email.FixedTextView

@ -330,6 +330,7 @@
<string name="title_advanced_rlah">Roam like at home</string> <string name="title_advanced_rlah">Roam like at home</string>
<string name="title_advanced_timeout">Connection timeout (seconds)</string> <string name="title_advanced_timeout">Connection timeout (seconds)</string>
<string name="title_advanced_prefer_ip4">Prefer IPv4 over IPv6</string> <string name="title_advanced_prefer_ip4">Prefer IPv4 over IPv6</string>
<string name="title_advanced_tcp_keep_alive" translatable="false">TCP keep alive</string>
<string name="title_advanced_ssl_harden">Harden SSL connections</string> <string name="title_advanced_ssl_harden">Harden SSL connections</string>
<string name="title_advanced_manage_connectivity">Manage connectivity</string> <string name="title_advanced_manage_connectivity">Manage connectivity</string>

Loading…
Cancel
Save