summaryrefslogtreecommitdiff
path: root/core/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main')
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java43
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/download/AntennapodHttpClient.java32
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/service/download/ProxyConfig.java32
-rw-r--r--core/src/main/res/values/strings.xml15
4 files changed, 118 insertions, 4 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java
index ac042de6f..3631f881b 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java
@@ -17,6 +17,7 @@ import org.json.JSONException;
import java.io.File;
import java.io.IOException;
+import java.net.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
@@ -25,6 +26,7 @@ import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.receiver.FeedUpdateReceiver;
+import de.danoeh.antennapod.core.service.download.ProxyConfig;
import de.danoeh.antennapod.core.storage.APCleanupAlgorithm;
import de.danoeh.antennapod.core.storage.APNullCleanupAlgorithm;
import de.danoeh.antennapod.core.storage.APQueueCleanupAlgorithm;
@@ -78,6 +80,11 @@ public class UserPreferences {
public static final String PREF_ENABLE_AUTODL_ON_BATTERY = "prefEnableAutoDownloadOnBattery";
public static final String PREF_ENABLE_AUTODL_WIFI_FILTER = "prefEnableAutoDownloadWifiFilter";
public static final String PREF_AUTODL_SELECTED_NETWORKS = "prefAutodownloadSelectedNetworks";
+ public static final String PREF_PROXY_TYPE = "prefProxyType";
+ public static final String PREF_PROXY_HOST = "prefProxyHost";
+ public static final String PREF_PROXY_PORT = "prefProxyPort";
+ public static final String PREF_PROXY_USER = "prefProxyUser";
+ public static final String PREF_PROXY_PASSWORD = "prefProxyPassword";
// Services
public static final String PREF_AUTO_FLATTR = "pref_auto_flattr";
@@ -371,6 +378,42 @@ public class UserPreferences {
return TextUtils.split(selectedNetWorks, ",");
}
+ public static void setProxyConfig(ProxyConfig config) {
+ SharedPreferences.Editor editor = prefs.edit();
+ editor.putString(PREF_PROXY_TYPE, config.type.name());
+ if(TextUtils.isEmpty(config.host)) {
+ editor.remove(PREF_PROXY_HOST);
+ } else {
+ editor.putString(PREF_PROXY_HOST, config.host);
+ }
+ if(config.port <= 0 || config.port > 65535) {
+ editor.remove(PREF_PROXY_PORT);
+ } else {
+ editor.putInt(PREF_PROXY_PORT, config.port);
+ }
+ if(TextUtils.isEmpty(config.username)) {
+ editor.remove(PREF_PROXY_USER);
+ } else {
+ editor.putString(PREF_PROXY_USER, config.username);
+ }
+ if(TextUtils.isEmpty(config.password)) {
+ editor.remove(PREF_PROXY_PASSWORD);
+ } else {
+ editor.putString(PREF_PROXY_PASSWORD, config.password);
+ }
+ editor.apply();
+ }
+
+ public static ProxyConfig getProxyConfig() {
+ Proxy.Type type = Proxy.Type.valueOf(prefs.getString(PREF_PROXY_TYPE, Proxy.Type.DIRECT.name()));
+ String host = prefs.getString(PREF_PROXY_HOST, null);
+ int port = prefs.getInt(PREF_PROXY_PORT, 0);
+ String username = prefs.getString(PREF_PROXY_USER, null);
+ String password = prefs.getString(PREF_PROXY_PASSWORD, null);
+ ProxyConfig config = new ProxyConfig(type, host, port, username, password);
+ return config;
+ }
+
public static boolean shouldResumeAfterCall() {
return prefs.getBoolean(PREF_RESUME_AFTER_CALL, true);
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/AntennapodHttpClient.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/AntennapodHttpClient.java
index b23819ef7..5dd1e2dfa 100644
--- a/core/src/main/java/de/danoeh/antennapod/core/service/download/AntennapodHttpClient.java
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/AntennapodHttpClient.java
@@ -2,8 +2,10 @@ package de.danoeh.antennapod.core.service.download;
import android.os.Build;
import android.support.annotation.NonNull;
+import android.text.TextUtils;
import android.util.Log;
+import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
@@ -14,7 +16,10 @@ import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Proxy;
import java.net.Socket;
+import java.net.SocketAddress;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.concurrent.TimeUnit;
@@ -23,6 +28,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
+import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBWriter;
/**
@@ -44,12 +50,15 @@ public class AntennapodHttpClient {
*/
public static synchronized OkHttpClient getHttpClient() {
if (httpClient == null) {
-
httpClient = newHttpClient();
}
return httpClient;
}
+ public static synchronized void reinit() {
+ httpClient = newHttpClient();
+ }
+
/**
* Creates a new HTTP client. Most users should just use
* getHttpClient() to get the standard AntennaPod client,
@@ -69,13 +78,13 @@ public class AntennapodHttpClient {
client.networkInterceptors().add(chain -> {
Request request = chain.request();
Response response = chain.proceed(request);
- if(response.code() == HttpURLConnection.HTTP_MOVED_PERM ||
+ if (response.code() == HttpURLConnection.HTTP_MOVED_PERM ||
response.code() == StatusLine.HTTP_PERM_REDIRECT) {
String location = response.header("Location");
- if(location.startsWith("/")) { // URL is not absolute, but relative
+ if (location.startsWith("/")) { // URL is not absolute, but relative
URL url = request.url();
location = url.getProtocol() + "://" + url.getHost() + location;
- } else if(!location.toLowerCase().startsWith("http://") &&
+ } else if (!location.toLowerCase().startsWith("http://") &&
!location.toLowerCase().startsWith("https://")) {
// Reference is relative to current path
URL url = request.url();
@@ -106,6 +115,21 @@ public class AntennapodHttpClient {
client.setFollowRedirects(true);
client.setFollowSslRedirects(true);
+ ProxyConfig config = UserPreferences.getProxyConfig();
+ if (config.type != Proxy.Type.DIRECT) {
+ int port = config.port > 0 ? config.port : ProxyConfig.DEFAULT_PORT;
+ SocketAddress address = InetSocketAddress.createUnresolved(config.host, port);
+ Proxy proxy = new Proxy(config.type, address);
+ client.setProxy(proxy);
+ if (!TextUtils.isEmpty(config.username)) {
+ String credentials = Credentials.basic(config.username, config.password);
+ client.interceptors().add(chain -> {
+ Request request = chain.request().newBuilder()
+ .header("Proxy-Authorization", credentials).build();
+ return chain.proceed(request);
+ });
+ }
+ }
if(16 <= Build.VERSION.SDK_INT && Build.VERSION.SDK_INT < 21) {
client.setSslSocketFactory(new CustomSslSocketFactory());
}
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/ProxyConfig.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/ProxyConfig.java
new file mode 100644
index 000000000..e886932f2
--- /dev/null
+++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/ProxyConfig.java
@@ -0,0 +1,32 @@
+package de.danoeh.antennapod.core.service.download;
+
+import android.support.annotation.Nullable;
+
+import java.net.Proxy;
+
+public class ProxyConfig {
+
+ public final Proxy.Type type;
+ @Nullable public final String host;
+ @Nullable public final int port;
+ @Nullable public final String username;
+ @Nullable public final String password;
+
+ public final static int DEFAULT_PORT = 8080;
+
+ public static ProxyConfig direct() {
+ return new ProxyConfig(Proxy.Type.DIRECT, null, 0, null, null);
+ }
+
+ public static ProxyConfig http(String host, int port, String username, String password) {
+ return new ProxyConfig(Proxy.Type.HTTP, host, port, username, password);
+ }
+
+ public ProxyConfig(Proxy.Type type, String host, int port, String username, String password) {
+ this.type = type;
+ this.host = host;
+ this.port = port;
+ this.username = username;
+ this.password = password;
+ }
+}
diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml
index 0d4518a15..f9af78d63 100644
--- a/core/src/main/res/values/strings.xml
+++ b/core/src/main/res/values/strings.xml
@@ -387,6 +387,8 @@
<string name="pref_sonic_title">Sonic media player</string>
<string name="pref_sonic_message">Use built-in sonic media player as a replacement for Android\'s native mediaplayer and Prestissimo</string>
<string name="pref_current_value">Current value: %1$s</string>
+ <string name="pref_proxy_title">Proxy</string>
+ <string name="pref_proxy_sum">Set a network proxy</string>
<!-- Auto-Flattr dialog -->
<string name="auto_flattr_enable">Enable automatic flattring</string>
@@ -593,4 +595,17 @@
<string name="stereo_to_mono">Downmix: Stereo to mono</string>
<string name="sonic_only">Sonic only</string>
+ <!-- proxy settings -->
+ <string name="proxy_type_label">Type</string>
+ <string name="host_label">Host</string>
+ <string name="port_label">Port</string>
+ <string name="optional_hint">(Optional)</string>
+ <string name="proxy_test_label">Test</string>
+ <string name="proxy_checking">Checking&#8230;</string>
+ <string name="proxy_test_successful">Test successful</string>
+ <string name="proxy_test_failed">Test failed</string>
+ <string name="proxy_host_empty_error">Host must not be empty</string>
+ <string name="proxy_host_invalid_error">Host not valid UP or domain</string>
+ <string name="proxy_port_invalid_error">Port not valid</string>
+
</resources>