diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java b/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java index d566d90..55db442 100644 --- a/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java +++ b/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java @@ -320,7 +320,7 @@ public class TriviaGameActivity extends BaseActivity String json; try { json = ApiUtil.GET(query[0]); - } catch (IOException e) { + } catch (Exception e) { return null; } return json; diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/exceptions/MalformedProxyAddressException.java b/app/src/main/java/com/luca0n/joguitos/pluck/exceptions/MalformedProxyAddressException.java new file mode 100644 index 0000000..3a951b2 --- /dev/null +++ b/app/src/main/java/com/luca0n/joguitos/pluck/exceptions/MalformedProxyAddressException.java @@ -0,0 +1,40 @@ +/* +Pluck: an open source trivia game for Android + +Copyright (C) 2021 Joguitos do luca0N! + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . + +This game is a fork of LibreTrivia, and its source code is available at +. + +Contact us at . +*/ + +package com.luca0n.joguitos.pluck.exceptions; + +import java.lang.Throwable; + +import com.luca0n.joguitos.pluck.exceptions.ProxyException; + +public class MalformedProxyAddressException extends ProxyException { + public MalformedProxyAddressException(){ + super(); + } + public MalformedProxyAddressException(String message){ + super(message); + } + public MalformedProxyAddressException(Throwable cause){ + super(cause); + } +} diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/exceptions/ProxyException.java b/app/src/main/java/com/luca0n/joguitos/pluck/exceptions/ProxyException.java new file mode 100644 index 0000000..9c1ad31 --- /dev/null +++ b/app/src/main/java/com/luca0n/joguitos/pluck/exceptions/ProxyException.java @@ -0,0 +1,38 @@ +/* +Pluck: an open source trivia game for Android + +Copyright (C) 2021 Joguitos do luca0N! + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . + +This game is a fork of LibreTrivia, and its source code is available at +. + +Contact us at . +*/ + +package com.luca0n.joguitos.pluck.exceptions; + +import java.lang.Throwable; + +public class ProxyException extends Exception { + public ProxyException(){ + super(); + } + public ProxyException(String message){ + super(message); + } + public ProxyException(Throwable cause){ + super(cause); + } +} diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/exceptions/UnknownProxyProtocolException.java b/app/src/main/java/com/luca0n/joguitos/pluck/exceptions/UnknownProxyProtocolException.java new file mode 100644 index 0000000..74afebb --- /dev/null +++ b/app/src/main/java/com/luca0n/joguitos/pluck/exceptions/UnknownProxyProtocolException.java @@ -0,0 +1,40 @@ +/* +Pluck: an open source trivia game for Android + +Copyright (C) 2021 Joguitos do luca0N! + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . + +This game is a fork of LibreTrivia, and its source code is available at +. + +Contact us at . +*/ + +package com.luca0n.joguitos.pluck.exceptions; + +import java.lang.Throwable; + +import com.luca0n.joguitos.pluck.exceptions.ProxyException; + +public class UnknownProxyProtocolException extends ProxyException { + public UnknownProxyProtocolException(){ + super(); + } + public UnknownProxyProtocolException(String message){ + super(message); + } + public UnknownProxyProtocolException(Throwable cause){ + super(cause); + } +} diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/util/ApiUtil.java b/app/src/main/java/com/luca0n/joguitos/pluck/util/ApiUtil.java index 997865f..5590876 100644 --- a/app/src/main/java/com/luca0n/joguitos/pluck/util/ApiUtil.java +++ b/app/src/main/java/com/luca0n/joguitos/pluck/util/ApiUtil.java @@ -44,11 +44,15 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Collections; +import java.lang.IndexOutOfBoundsException; import com.luca0n.joguitos.pluck.R; import com.luca0n.joguitos.pluck.PluckApplication; import com.luca0n.joguitos.pluck.exceptions.NoTriviaResultsException; import com.luca0n.joguitos.pluck.exceptions.InvalidTriviaFormatException; +import com.luca0n.joguitos.pluck.exceptions.ProxyException; +import com.luca0n.joguitos.pluck.exceptions.UnknownProxyProtocolException; +import com.luca0n.joguitos.pluck.exceptions.MalformedProxyAddressException; import com.luca0n.joguitos.pluck.trivia.TriviaQuery; import com.luca0n.joguitos.pluck.trivia.TriviaQuestion; import com.luca0n.joguitos.pluck.trivia.TriviaCategory; @@ -72,7 +76,7 @@ public class ApiUtil { return builder.toString(); } - private static String GET(String query) throws IOException { + private static String GET(String query) throws IOException, ProxyException { String response; URL url = new URL(query); @@ -81,17 +85,56 @@ public class ApiUtil { // Check if the player wants to play the game using Tor. Context c = PluckApplication.getAppContext(); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c); - String key = c.getResources().getString(R.string.pref_network_tor); + String keyTor = c.getResources().getString(R.string.pref_network_tor); // Connect without Tor by default. - boolean useTor = sp.getBoolean(key, false); + boolean useTor = sp.getBoolean(keyTor, false); // Default Orbot SOCKS5 address. String orbotSocks5Hostname = "127.0.0.1"; int orbotSocks5Port = 9050; + // Check if the player wants to play the game using a custom proxy. + String keyProxy = c.getResources().getString(R.string.pref_network_proxy); + boolean useProxy = sp.getBoolean(keyProxy, false); + // Get the player custom proxy address. + String keyProxyAddress = c.getResources().getString(R.string.pref_network_proxy_address); + String proxyAddress = sp.getString(keyProxyAddress, null); + if (useTor) connection = (HttpURLConnection) url.openConnection( new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(orbotSocks5Hostname, orbotSocks5Port))); - else + else if (useProxy){ + // The provided custom proxy should be in the following format: + // ://:. + + // Make sure the player specified the proxy address. + if (proxyAddress == null) + throw new MalformedProxyAddressException("A proxy address was expected, but was not specified."); + + // We need to parse the proxy address in order to get the proxy protocol. + Proxy.Type protocol = null; + if (proxyAddress.startsWith("http:")) + protocol = Proxy.Type.HTTP; + else if (proxyAddress.startsWith("socks:") + || proxyAddress.startsWith("socks5:")) + protocol = Proxy.Type.SOCKS; + else + // Unknown proxy type. + throw new UnknownProxyProtocolException("The client was unable to get the proxy type based on the specified proxy address. It should start with the proxy protocol, like \"http://\" or \"socks5\"."); + + // We'll need to get the hostname and the port. + String proxyHostname; + int proxyPort; + try { + proxyHostname = proxyAddress.split(":")[1].substring(2); // use substring() in order to ignore the "//". + proxyPort = Integer.parseInt(proxyAddress.split(":")[2]); + } catch (IndexOutOfBoundsException e){ + // Throw a new MalformedProxyAddressException exception if the app can't recognize the proxy address. + throw new MalformedProxyAddressException(); + } + + connection = (HttpURLConnection) url.openConnection( + new Proxy(protocol, new InetSocketAddress(proxyHostname, proxyPort))); + }else connection = (HttpURLConnection) url.openConnection(); try { @@ -105,7 +148,7 @@ public class ApiUtil { } public static String GET(TriviaQuery query) - throws IOException { + throws IOException, ProxyException { return GET(query.toString()); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2c29282..a9ea438 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -95,7 +95,7 @@ Contact us at . Error - Network error!\n\nCould not connect to a network. Check if your custom server address is correct. If Tor is enabled in the game settings, check if Orbot is running and then try again. + Network error!\n\nCould not connect to a network. Check the system network settings, the game network settings and then try again. No trivia results!\n\nWas not able to find trivia questions that satisfied all options. Server response error!\n\nCould not parse the server response. The server response may be in a unsupported version. If you are using a custom server, make sure the server address points to a valid address and then try again. An unknown error occurred!