Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement proxy support #1754

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import ml.docilealligator.infinityforreddit.settings.NotificationPreferenceFragment;
import ml.docilealligator.infinityforreddit.settings.NsfwAndSpoilerFragment;
import ml.docilealligator.infinityforreddit.settings.PostHistoryFragment;
import ml.docilealligator.infinityforreddit.settings.ProxyPreferenceFragment;
import ml.docilealligator.infinityforreddit.settings.SecurityPreferenceFragment;
import ml.docilealligator.infinityforreddit.settings.ThemePreferenceFragment;
import ml.docilealligator.infinityforreddit.settings.TranslationFragment;
Expand Down Expand Up @@ -315,6 +316,8 @@ public interface AppComponent {

void inject(PostOptionsBottomSheetFragment postOptionsBottomSheetFragment);

void inject(ProxyPreferenceFragment proxyPreferenceFragment);

@Component.Factory
interface Factory {
AppComponent create(@BindsInstance Application application);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.content.SharedPreferences;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

import javax.inject.Named;
Expand All @@ -15,6 +17,7 @@
import ml.docilealligator.infinityforreddit.network.ServerAccessTokenAuthenticator;
import ml.docilealligator.infinityforreddit.network.SortTypeConverterFactory;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import okhttp3.ConnectionPool;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
Expand All @@ -28,12 +31,24 @@ abstract class NetworkModule {
@Provides
@Named("base")
@Singleton
static OkHttpClient provideBaseOkhttp() {
return new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.build();
static OkHttpClient provideBaseOkhttp(@Named("default") SharedPreferences mSharedPreferences) {
boolean proxyEnabled = mSharedPreferences.getBoolean(SharedPreferencesUtils.PROXY_ENABLED, false);
Proxy.Type proxyType = Proxy.Type.valueOf(mSharedPreferences.getString(SharedPreferencesUtils.PROXY_TYPE, "HTTP"));
String proxyHost = mSharedPreferences.getString(SharedPreferencesUtils.PROXY_HOSTNAME, "127.0.0.1");
int proxyPort = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.PROXY_PORT, "1080"));

var builder = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS);

if (proxyEnabled) {
InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(proxyType, proxyAddr);
builder.proxy(proxy);
}

return builder.build();
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ml.docilealligator.infinityforreddit.settings;

import static ml.docilealligator.infinityforreddit.utils.Utils.HOSTNAME_REGEX;

import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.preference.EditTextPreference;

import com.google.common.net.InetAddresses;

import java.util.regex.Pattern;

import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.customviews.CustomFontPreferenceFragmentCompat;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;

public class ProxyPreferenceFragment extends CustomFontPreferenceFragmentCompat {
public ProxyPreferenceFragment() {}

@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
setPreferencesFromResource(R.xml.proxy_preferences, rootKey);
((Infinity) activity.getApplication()).getAppComponent().inject(this);

EditTextPreference proxyHostnamePref = findPreference(SharedPreferencesUtils.PROXY_HOSTNAME);
EditTextPreference proxyPortPref = findPreference(SharedPreferencesUtils.PROXY_PORT);

if (proxyHostnamePref != null) {
proxyHostnamePref.setOnPreferenceChangeListener(((preference, newValue) -> {
boolean isHostname = Pattern.matches(HOSTNAME_REGEX, (String) newValue);
boolean isInetAddress = InetAddresses.isInetAddress((String) newValue);

if (!isInetAddress && !isHostname) {
Toast.makeText(activity, R.string.not_a_valid_ip_or_hostname, Toast.LENGTH_SHORT).show();
return false;
}
return true;
}));
}

if (proxyPortPref != null) {
proxyPortPref.setOnPreferenceChangeListener(((preference, newValue) -> {
try {
int port = Integer.parseInt((String) newValue);
if (port < 0 || port > 65535) {
Toast.makeText(activity, R.string.not_a_valid_port, Toast.LENGTH_SHORT).show();
return false;
}
} catch (NumberFormatException e) {
Toast.makeText(activity, R.string.not_a_valid_number, Toast.LENGTH_SHORT).show();
return false;
}
return true;
}));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ public static boolean canShowEmote(int embeddedMediaType) {
public static final String HAS_REQUESTED_NOTIFICATION_PERMISSION = "has_requested_notification_permission";
public static final String DO_NOT_SHOW_REDDIT_API_INFO_V2_AGAIN = "do_not_show_reddit_api_info_v2_again";

public static final String PROXY_ENABLED = "proxy_enabled";
public static final String PROXY_TYPE = "proxy_type";
public static final String PROXY_HOSTNAME = "proxy_hostname";
public static final String PROXY_PORT = "proxy_port";

//Legacy Settings
public static final String MAIN_PAGE_TAB_1_TITLE_LEGACY = "main_page_tab_1_title";
public static final String MAIN_PAGE_TAB_2_TITLE_LEGACY = "main_page_tab_2_title";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public final class Utils {
private static final long DAY_MILLIS = 24 * HOUR_MILLIS;
private static final long MONTH_MILLIS = 30 * DAY_MILLIS;
private static final long YEAR_MILLIS = 12 * MONTH_MILLIS;

public static String HOSTNAME_REGEX = "^(?=^.{1,253}$)(([a-z\\d]([a-z\\d-]{0,62}[a-z\\d])*[\\.]){1,3}[a-z]{1,61})$";
private static final Pattern[] REGEX_PATTERNS = {
Pattern.compile("((?<=[\\s])|^)/[rRuU]/[\\w-]+/{0,1}"),
Pattern.compile("((?<=[\\s])|^)[rRuU]/[\\w-]+/{0,1}"),
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_call_split_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M14,4l2.29,2.29 -2.88,2.88 1.42,1.42 2.88,-2.88L20,10L20,4zM10,4L4,4v6l2.29,-2.29 4.71,4.7L11,20h2v-8.41l-5.29,-5.3z"/>

</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ Tippen für Neuversuch."</string>
<string name="settings_tab_post_type">"Typ"</string>
<string name="settings_tab_subreddit_name">"Subreddit-Name (ohne r/ Präfix)"</string>

<!-- Proxy -->
<string name="settings_proxy_enabled">Proxy verwenden</string>
<string name="settings_proxy_type">Proxy-Typ</string>

<!-- Fuzzy -->
<string name="settings_tab_multi_reddit_name">"Multireddit-Name (/user/DeinBenutzername/m/DeinMultiredditName) (nur Kleinbuchstaben)"</string>
<string name="settings_tab_username">"Benutzername (ohne u/ Präfix)"</string>
Expand Down Expand Up @@ -1233,6 +1237,8 @@ Antippen zum Wiederholen."</string>
<string name="option_5_hint">"Option 5"</string>
<string name="option_6_hint">"Option 6"</string>
<string name="not_a_valid_number">"Keine valide Nummer"</string>
<string name="not_a_valid_ip_or_hostname">Ungültige IP oder Hostname</string>
<string name="not_a_valid_port">Ungültiger Port (0 - 65535)</string>
<string name="post_karma">"Beitragskarma:"</string>
<string name="comment_karma">"Kommentarkarma:"</string>
<string name="awarder_karma">"Auszeichnungskarma:"</string>
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -733,4 +733,16 @@
<item>@string/comment_filter_display_mode_fully_collapse_comments</item>
</string-array>

<string-array name="settings_proxy_type">
<item>@string/proxy_http</item>
<item>@string/proxy_socks</item>
<item>@string/proxy_direct</item>
</string-array>

<string-array name="settings_proxy_type_values">
<item>HTTP</item>
<item>SOCKS</item>
<item>DIRECT</item>
</string-array>

</resources>
10 changes: 10 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,11 @@
<string name="settings_comment_divider_type">Comment Divider Type</string>
<string name="settings_comment_filter_title">Comment Filter</string>
<string name="settings_embedded_media_type_title">Embedded Media Type</string>
<string name="settings_proxy_title">Proxy</string>
<string name="settings_proxy_enabled">Proxy Enabled</string>
<string name="settings_proxy_type">Proxy Type</string>
<string name="settings_proxy_hostname">Hostname</string>
<string name="settings_proxy_port">Port</string>

<string name="no_link_available">Cannot get the link</string>

Expand Down Expand Up @@ -1363,6 +1368,8 @@
<string name="option_6_hint">Option 6</string>

<string name="not_a_valid_number">Not a valid number</string>
<string name="not_a_valid_ip_or_hostname">Not a valid IP or host name</string>
<string name="not_a_valid_port">Not a valid port (0 - 65535)</string>

<string name="post_karma">Post Karma:</string>
<string name="comment_karma">Comment Karma:</string>
Expand Down Expand Up @@ -1523,4 +1530,7 @@
<string name="download_video">Download Video</string>
<string name="download_all_gallery_images">Download All Gallery Images</string>

<string name="proxy_http">HTTP</string>
<string name="proxy_socks">SOCKS</string>
<string name="proxy_direct">Direct</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/main_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
app:icon="@drawable/ic_data_saving_mode_day_night_24dp"
app:fragment="ml.docilealligator.infinityforreddit.settings.DataSavingModePreferenceFragment" />

<ml.docilealligator.infinityforreddit.customviews.CustomFontPreference
app:icon="@drawable/ic_call_split_24"
app:title="@string/settings_proxy_title"
app:fragment="ml.docilealligator.infinityforreddit.settings.ProxyPreferenceFragment" />

<ml.docilealligator.infinityforreddit.customviews.CustomFontPreference
app:icon="@drawable/ic_nsfw_on_day_night_24dp"
app:title="@string/settings_over_18_and_spoiler_title"
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/res/xml/proxy_preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

<ml.docilealligator.infinityforreddit.customviews.CustomFontSwitchPreference
app:defaultValue="false"
app:key="proxy_enabled"
app:title="@string/settings_proxy_enabled"
app:summary="@string/restart_app_see_changes" />

<ml.docilealligator.infinityforreddit.customviews.CustomFontListPreference
app:defaultValue="HTTP"
app:entries="@array/settings_proxy_type"
app:entryValues="@array/settings_proxy_type_values"
app:key="proxy_type"
app:title="@string/settings_proxy_type"
app:useSimpleSummaryProvider="true" />

<ml.docilealligator.infinityforreddit.customviews.CustomFontEditTextPreference
app:defaultValue="127.0.0.1"
app:key="proxy_hostname"
app:title="@string/settings_proxy_hostname"
app:useSimpleSummaryProvider="true" />

<ml.docilealligator.infinityforreddit.customviews.CustomFontEditTextPreference
app:defaultValue="1080"
app:key="proxy_port"
app:title="@string/settings_proxy_port"
app:useSimpleSummaryProvider="true" />

</PreferenceScreen>