Added custom proxy support
This commit is contained in:
parent
7ed43c5491
commit
c4faa2e831
|
@ -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;
|
||||
|
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
This game is a fork of LibreTrivia, and its source code is available at
|
||||
<https://github.com/tryton-vanmeer/LibreTrivia>.
|
||||
|
||||
Contact us at <joguitos+pluck@luca0n.com>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
This game is a fork of LibreTrivia, and its source code is available at
|
||||
<https://github.com/tryton-vanmeer/LibreTrivia>.
|
||||
|
||||
Contact us at <joguitos+pluck@luca0n.com>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
This game is a fork of LibreTrivia, and its source code is available at
|
||||
<https://github.com/tryton-vanmeer/LibreTrivia>.
|
||||
|
||||
Contact us at <joguitos+pluck@luca0n.com>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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:
|
||||
// <protocol>://<hostname>:<port>.
|
||||
|
||||
// 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());
|
||||
}
|
||||
|
||||
|
|
|
@ -95,7 +95,7 @@ Contact us at <joguitos+pluck@luca0n.com>.
|
|||
|
||||
<!-- Error Strings -->
|
||||
<string name="error">Error</string>
|
||||
<string name="error_network"><b>Network error!</b>\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.</string>
|
||||
<string name="error_network"><b>Network error!</b>\n\nCould not connect to a network. Check the system network settings, the game network settings and then try again.</string>
|
||||
<string name="error_no_trivia_results"><b>No trivia results!</b>\n\nWas not able to find trivia questions that satisfied all options.</string>
|
||||
<string name="error_server_response_invalid"><b>Server response error!</b>\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.</string>
|
||||
<string name="error_unknown"><b>An unknown error occurred!</b></string>
|
||||
|
|
Loading…
Reference in New Issue