From 2da14589996b0808453f49831a82661c803be964 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 20 Jan 2015 15:12:04 +0100 Subject: [PATCH] add a preference to allow user to set a network proxy Some networks require a proxy to get internet beyond the firewall, and proxies are also useful for channeling all traffic through circumvention and privacy proxies like Tor, I2P, Psiphon, etc. In places where the connection to download tiles might be blocked, using a proxy can give the user access. --- .../src/net/osmand/osm/io/NetworkUtils.java | 28 +++++++++- OsmAnd/res/values/strings.xml | 7 +++ OsmAnd/res/xml/general_settings.xml | 10 ++++ .../src/net/osmand/plus/OsmandSettings.java | 3 ++ .../activities/SettingsGeneralActivity.java | 51 +++++++++++++++++++ .../plus/download/DownloadFileHelper.java | 3 +- .../download/DownloadOsmandIndexesHelper.java | 6 ++- .../osmand/plus/download/DownloadTracker.java | 4 +- .../plus/osmedit/OpenstreetmapRemoteUtil.java | 2 +- .../plus/osmedit/OsmBugsRemoteUtil.java | 3 +- .../osmand/plus/poi/NameFinderPoiFilter.java | 6 ++- .../osmand/plus/routing/RouteProvider.java | 4 +- .../plus/views/YandexTrafficAdapter.java | 5 +- 13 files changed, 119 insertions(+), 13 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/osm/io/NetworkUtils.java b/OsmAnd-java/src/net/osmand/osm/io/NetworkUtils.java index f74da15963..4328edfec4 100644 --- a/OsmAnd-java/src/net/osmand/osm/io/NetworkUtils.java +++ b/OsmAnd-java/src/net/osmand/osm/io/NetworkUtils.java @@ -9,6 +9,9 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; +import java.net.InetSocketAddress; +import java.net.MalformedURLException; +import java.net.Proxy; import java.net.URL; import java.net.URLEncoder; import java.util.Map; @@ -21,12 +24,15 @@ import org.apache.commons.logging.Log; public class NetworkUtils { private static final Log log = PlatformUtil.getLog(NetworkUtils.class); + + private static Proxy proxy = null; + public static String sendGetRequest(String urlText, String userNamePassword){ URL url; try { log.info("GET : " + urlText); url = new URL(urlText); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + HttpURLConnection conn = getHttpURLConnection(urlText); conn.setDoInput(true); conn.setDoOutput(false); conn.setRequestMethod("GET"); @@ -151,4 +157,24 @@ public class NetworkUtils { } } + public static void setProxy(String host, int port) { + if(host != null && port > 0) { + InetSocketAddress isa = new InetSocketAddress(host, port); + proxy = new Proxy(Proxy.Type.HTTP, isa); + } else { + proxy = null; + } + } + + public static HttpURLConnection getHttpURLConnection(String urlString) throws MalformedURLException, IOException { + return getHttpURLConnection(new URL(urlString)); + } + + public static HttpURLConnection getHttpURLConnection(URL url) throws IOException { + if (proxy != null) { + return (HttpURLConnection) url.openConnection(proxy); + } else { + return (HttpURLConnection) url.openConnection(); + } + } } diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index d7ae79244d..71b7f721a6 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -155,6 +155,13 @@ Choose logging interval for track recording during navigation Select voice guidance for navigation Voice guidance + Proxy + Enable HTTP Proxy + Configure HTTP Proxy for all network requests + Proxy Host + Configure your proxy\'s hostname (e.g. 127.0.0.1) + Proxy Port + Configure your proxy\'s port number (e.g. 8118) Help Help OsmAnd is a navigation application with many features. diff --git a/OsmAnd/res/xml/general_settings.xml b/OsmAnd/res/xml/general_settings.xml index 9d8cf09906..d6f60f4217 100644 --- a/OsmAnd/res/xml/general_settings.xml +++ b/OsmAnd/res/xml/general_settings.xml @@ -13,6 +13,16 @@ + + + + + diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index d5e369f781..4eb256d465 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -796,6 +796,9 @@ public class OsmandSettings { public final CommonPreference INTERRUPT_MUSIC = new BooleanPreference("interrupt_music", false).makeGlobal(); + public final CommonPreference ENABLE_PROXY = new BooleanPreference("enable_proxy", false).makeGlobal(); + public final CommonPreference PROXY_HOST = new StringPreference("proxy_host", "127.0.0.1").makeGlobal(); + public final CommonPreference PROXY_PORT = new IntPreference("proxy_port", 8118).makeGlobal(); // this value string is synchronized with settings_pref.xml preference name public static final String SAVE_CURRENT_TRACK = "save_current_track"; //$NON-NLS-1$ diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java index f15838da6b..0056414517 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java @@ -14,6 +14,7 @@ import net.osmand.CallbackWithObject; import net.osmand.IProgress; import net.osmand.IndexConstants; import net.osmand.access.AccessibleToast; +import net.osmand.osm.io.NetworkUtils; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; @@ -43,6 +44,7 @@ import android.media.AudioManager; import android.os.AsyncTask; import android.os.Bundle; import android.preference.CheckBoxPreference; +import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; @@ -94,6 +96,7 @@ public class SettingsGeneralActivity extends SettingsBaseActivity { addLocalPrefs((PreferenceGroup) screen.findPreference("localization")); addVoicePrefs((PreferenceGroup) screen.findPreference("voice")); + addProxyPrefs((PreferenceGroup) screen.findPreference("proxy")); addMiscPreferences((PreferenceGroup) screen.findPreference("misc")); @@ -264,6 +267,54 @@ public class SettingsGeneralActivity extends SettingsBaseActivity { + + private void addProxyPrefs(PreferenceGroup proxy) { + CheckBoxPreference enableProxyPref = (CheckBoxPreference) proxy.findPreference(settings.ENABLE_PROXY.getId()); + enableProxyPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if ((Boolean) newValue) + NetworkUtils.setProxy(settings.PROXY_HOST.get(), settings.PROXY_PORT.get()); + else + NetworkUtils.setProxy(null, 0); + return true; + } + }); + + EditTextPreference hostPref = (EditTextPreference) proxy.findPreference(settings.PROXY_HOST.getId()); + hostPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + System.out.println("PROXY newValue: " + newValue); + settings.PROXY_HOST.set((String) newValue); + NetworkUtils.setProxy((String) newValue, settings.PROXY_PORT.get()); + return true; + } + }); + + EditTextPreference portPref = (EditTextPreference) proxy.findPreference(settings.PROXY_PORT.getId()); + portPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + System.out.println("PROXY newValue: " + newValue); + int port = -1; + String portString = (String) newValue; + try { + port = Integer.valueOf(portString.replaceAll("[^0-9]", "")); + } catch (NumberFormatException e1) { + } + settings.PROXY_PORT.set(port); + NetworkUtils.setProxy(settings.PROXY_HOST.get(), port); + return true; + } + }); + } + + + private void addMiscPreferences(PreferenceGroup misc) { if (!Version.isBlackberry(getMyApplication())) { applicationDir = new Preference(this); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java index b4e7cfb6a1..23d8127333 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java @@ -14,6 +14,7 @@ import java.util.zip.ZipInputStream; import net.osmand.IProgress; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; +import net.osmand.osm.io.NetworkUtils; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.Version; @@ -66,7 +67,7 @@ public class DownloadFileHelper { } catch (InterruptedException e) { } } - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + HttpURLConnection conn = NetworkUtils.getHttpURLConnection(url); conn.setRequestProperty("User-Agent", Version.getFullVersion(ctx)); //$NON-NLS-1$ conn.setReadTimeout(30000); if (fileread > 0) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java index 650660de05..8c86372812 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.net.URLConnection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -12,6 +13,7 @@ import java.util.zip.GZIPInputStream; import net.osmand.AndroidUtils; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; +import net.osmand.osm.io.NetworkUtils; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; @@ -104,9 +106,9 @@ public class DownloadOsmandIndexesHelper { String strUrl = ctx.getAppCustomization().getIndexesUrl(); log.info(strUrl); - URL url = new URL(strUrl ); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); - parser.setInput(new GZIPInputStream(url.openStream()), "UTF-8"); //$NON-NLS-1$ + URLConnection connection = NetworkUtils.getHttpURLConnection(strUrl); + parser.setInput(new GZIPInputStream(connection.getInputStream()), "UTF-8"); //$NON-NLS-1$ int next; while((next = parser.next()) != XmlPullParser.END_DOCUMENT) { if (next == XmlPullParser.START_TAG) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadTracker.java b/OsmAnd/src/net/osmand/plus/download/DownloadTracker.java index 3df7b14917..20aa692dad 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadTracker.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadTracker.java @@ -13,6 +13,7 @@ import java.util.Map.Entry; import java.util.Random; import net.osmand.PlatformUtil; +import net.osmand.osm.io.NetworkUtils; import net.osmand.plus.OsmandApplication; import net.osmand.plus.Version; @@ -102,8 +103,7 @@ public class DownloadTracker { } log.debug(urlString); - URL url = new URL(urlString.toString()); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + HttpURLConnection conn = NetworkUtils.getHttpURLConnection(urlString.toString()); conn.setConnectTimeout(5000); conn.setDoInput(false); conn.setDoOutput(false); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java index d1bfabf1d5..5ed3c88d05 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OpenstreetmapRemoteUtil.java @@ -103,7 +103,7 @@ public class OpenstreetmapRemoteUtil implements OpenstreetmapUtil { private String sendRequest(String url, String requestMethod, String requestBody, String userOperation, boolean doAuthenticate) { log.info("Sending request " + url); //$NON-NLS-1$ try { - HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + HttpURLConnection connection = NetworkUtils.getHttpURLConnection(url); connection.setConnectTimeout(15000); connection.setRequestMethod(requestMethod); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java index 3d475e3639..38081cd30c 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsRemoteUtil.java @@ -11,6 +11,7 @@ import java.net.URLEncoder; import net.osmand.PlatformUtil; import net.osmand.osm.io.Base64; +import net.osmand.osm.io.NetworkUtils; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; @@ -76,7 +77,7 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil { private String editingPOI(String url, String requestMethod, String userOperation) { try { - HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + HttpURLConnection connection = NetworkUtils.getHttpURLConnection(url); log.info("Editing poi " + url); connection.setConnectTimeout(15000); connection.setRequestMethod(requestMethod); diff --git a/OsmAnd/src/net/osmand/plus/poi/NameFinderPoiFilter.java b/OsmAnd/src/net/osmand/plus/poi/NameFinderPoiFilter.java index b04c877fba..f79f0ecb45 100644 --- a/OsmAnd/src/net/osmand/plus/poi/NameFinderPoiFilter.java +++ b/OsmAnd/src/net/osmand/plus/poi/NameFinderPoiFilter.java @@ -3,6 +3,7 @@ package net.osmand.plus.poi; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.net.URLConnection; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; @@ -11,6 +12,7 @@ import net.osmand.PlatformUtil; import net.osmand.ResultMatcher; import net.osmand.data.Amenity; import net.osmand.data.AmenityType; +import net.osmand.osm.io.NetworkUtils; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.R.string; @@ -76,8 +78,8 @@ public class NameFinderPoiFilter extends PoiLegacyFilter { lastError = ""; String urlq = NOMINATIM_API + URLEncoder.encode(query)+ "?format=xml&addressdetails=1&limit="+LIMIT+"&bounded=1&"+viewbox; log.info(urlq); - URL url = new URL(urlq); //$NON-NLS-1$ - InputStream stream = url.openStream(); + URLConnection connection = NetworkUtils.getHttpURLConnection(urlq); //$NON-NLS-1$ + InputStream stream = connection.getInputStream(); XmlPullParser parser = PlatformUtil.newXMLPullParser(); parser.setInput(stream, "UTF-8"); //$NON-NLS-1$ int eventType; diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java index e228cb0da5..e46c634809 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java @@ -27,6 +27,7 @@ import net.osmand.PlatformUtil; import net.osmand.binary.BinaryMapIndexReader; import net.osmand.data.LatLon; import net.osmand.data.LocationPoint; +import net.osmand.osm.io.NetworkUtils; import net.osmand.plus.ApplicationMode; import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities.GPXFile; @@ -1103,8 +1104,7 @@ public class RouteProvider { log.info("URL route " + uri); - URL url = new URL(uri.toString()); - URLConnection connection = url.openConnection(); + URLConnection connection = NetworkUtils.getHttpURLConnection(uri.toString()); connection.setRequestProperty("User-Agent", Version.getFullVersion(params.ctx)); // StringBuilder content = new StringBuilder(); // BufferedReader rs = new BufferedReader(new InputStreamReader(connection.getInputStream())); diff --git a/OsmAnd/src/net/osmand/plus/views/YandexTrafficAdapter.java b/OsmAnd/src/net/osmand/plus/views/YandexTrafficAdapter.java index 2982398549..3b7d0460bf 100644 --- a/OsmAnd/src/net/osmand/plus/views/YandexTrafficAdapter.java +++ b/OsmAnd/src/net/osmand/plus/views/YandexTrafficAdapter.java @@ -6,10 +6,12 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.net.URL; +import java.net.URLConnection; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.data.RotatedTileBox; +import net.osmand.osm.io.NetworkUtils; import net.osmand.map.TileSourceManager.TileSourceTemplate; import net.osmand.util.Algorithms; @@ -65,7 +67,8 @@ public class YandexTrafficAdapter extends MapTileAdapter { if (mTimestamp == null || (System.currentTimeMillis() - lastTimestampUpdated) > DELTA) { log.info("Updating timestamp"); //$NON-NLS-1$ try { - BufferedInputStream in = new BufferedInputStream(new URL(YANDEX_BASE_URL + "/trf/stat.js").openStream(), 1024); //$NON-NLS-1$ + URLConnection connection = NetworkUtils.getHttpURLConnection(YANDEX_BASE_URL + "/trf/stat.js"); + BufferedInputStream in = new BufferedInputStream(connection.getInputStream(), 1024); //$NON-NLS-1$ ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); BufferedOutputStream out = new BufferedOutputStream(dataStream, 1024); Algorithms.streamCopy(in, out);