Added SOCKS proxy support

pull/164/head
M66B 5 years ago
parent fe7649171c
commit 6b2f54a64f

@ -29,6 +29,7 @@ import android.net.NetworkRequest;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -38,6 +39,7 @@ import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
@ -54,12 +56,15 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
private Spinner spDownload;
private SwitchCompat swRoaming;
private SwitchCompat swRlah;
private SwitchCompat swSocks;
private EditText etSocks;
private Button btnSocks;
private Button btnManage;
private TextView tvConnectionType;
private TextView tvConnectionRoaming;
private final static String[] RESET_OPTIONS = new String[]{
"metered", "download", "roaming", "rlah"
"metered", "download", "roaming", "rlah", "socks_enabled", "socks_proxy"
};
@Override
@ -76,6 +81,9 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
spDownload = view.findViewById(R.id.spDownload);
swRoaming = view.findViewById(R.id.swRoaming);
swRlah = view.findViewById(R.id.swRlah);
swSocks = view.findViewById(R.id.swSocks);
etSocks = view.findViewById(R.id.etSocks);
btnSocks = view.findViewById(R.id.btnSocks);
btnManage = view.findViewById(R.id.btnManage);
tvConnectionType = view.findViewById(R.id.tvConnectionType);
@ -124,6 +132,28 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
}
});
swSocks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("socks_enabled", checked).apply();
etSocks.setEnabled(checked);
btnSocks.setEnabled(checked);
ServiceSynchronize.reload(getContext(), "socks=" + checked);
}
});
btnSocks.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String proxy = etSocks.getText().toString();
if (TextUtils.isEmpty(proxy))
prefs.edit().remove("socks_proxy").apply();
else
prefs.edit().putString("socks_proxy", proxy).apply();
ServiceSynchronize.reload(getContext(), "socks=" + proxy);
}
});
final Intent manage = getIntentConnectivity();
btnManage.setVisibility(
manage.resolveActivity(getContext().getPackageManager()) == null
@ -221,6 +251,10 @@ public class FragmentOptionsConnection extends FragmentBase implements SharedPre
swRoaming.setChecked(prefs.getBoolean("roaming", true));
swRlah.setChecked(prefs.getBoolean("rlah", true));
swSocks.setChecked(prefs.getBoolean("socks_enabled", false));
etSocks.setText(prefs.getString("socks_proxy", null));
etSocks.setEnabled(swSocks.isChecked());
btnSocks.setEnabled(swSocks.isChecked());
}
private static Intent getIntentConnectivity() {

@ -3,8 +3,11 @@ package eu.faircode.email;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import androidx.preference.PreferenceManager;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
import com.sun.mail.smtp.SMTPTransport;
@ -66,6 +69,24 @@ public class MailService implements AutoCloseable {
properties = MessageHelper.getSessionProperties();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean socks_enabled = prefs.getBoolean("socks_enabled", false);
String socks_proxy = prefs.getString("socks_proxy", "localhost:9050");
// SOCKS proxy
if (socks_enabled) {
String[] address = socks_proxy.split(":");
String host = (address.length > 0 ? address[0] : null);
String port = (address.length > 1 ? address[1] : null);
if (TextUtils.isEmpty(host))
host = "localhost";
if (TextUtils.isEmpty(port))
port = "9050";
properties.put("mail." + protocol + ".socks.host", host);
properties.put("mail." + protocol + ".socks.port", port);
Log.i("Using SOCKS proxy=" + host + ":" + port);
}
properties.put("mail.event.scope", "folder");
properties.put("mail.event.executor", executor);

@ -119,6 +119,39 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swRlah" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swSocks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:checked="true"
android:text="@string/title_advanced_socks"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvRlahHint"
app:switchPadding="12dp" />
<EditText
android:id="@+id/etSocks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="localhost:9050"
app:layout_constraintEnd_toStartOf="@+id/btnSocks"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swSocks" />
<Button
android:id="@+id/btnSocks"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:minWidth="0dp"
android:minHeight="0dp"
android:text="@string/title_set"
app:layout_constraintBottom_toBottomOf="@id/etSocks"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btnManage"
style="?android:attr/buttonStyleSmall"
@ -129,7 +162,7 @@
android:minHeight="0dp"
android:text="@string/title_advanced_manage_connectivity"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvRlahHint" />
app:layout_constraintTop_toBottomOf="@id/etSocks" />
<TextView
android:id="@+id/tvConnectionType"

@ -240,6 +240,7 @@
<string name="title_advanced_download">Automatically download messages and attachments on a metered connection up to</string>
<string name="title_advanced_roaming">Download messages and attachments while roaming</string>
<string name="title_advanced_rlah">Roam like at home</string>
<string name="title_advanced_socks">Use SOCKS proxy</string>
<string name="title_advanced_manage_connectivity">Manage connectivity</string>
<string name="title_advanced_landscape">Use two columns in landscape mode</string>
@ -898,6 +899,7 @@
<string name="title_dismiss">Dismiss</string>
<string name="title_snooze_now">Snooze</string>
<string name="title_loading">Loading &#8230;</string>
<string name="title_set">Set</string>
<string name="title_icalendar_accept">Accept</string>
<string name="title_icalendar_decline">Decline</string>

Loading…
Cancel
Save