Added Tor support

Based on #2.
This commit is contained in:
luca0N! 2021-03-03 22:15:53 -03:00
parent aec090b22e
commit fb2d81550c
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
1 changed files with 66 additions and 42 deletions

View File

@ -23,6 +23,10 @@ Contact us at <joguitos+pluck@luca0n.com>.
package com.luca0n.joguitos.pluck.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@ -33,10 +37,14 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import com.luca0n.joguitos.pluck.R;
import com.luca0n.joguitos.pluck.PluckApplication;
import com.luca0n.joguitos.pluck.exceptions.NoTriviaResultsException;
import com.luca0n.joguitos.pluck.trivia.TriviaQuery;
import com.luca0n.joguitos.pluck.trivia.TriviaQuestion;
@ -46,60 +54,76 @@ import com.luca0n.joguitos.pluck.trivia.TriviaType;
public class ApiUtil {
private static String readStream(InputStream in) throws IOException {
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(in), 1000);
private static String readStream(InputStream in) throws IOException {
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(in), 1000);
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
builder.append(line);
}
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
builder.append(line);
}
in.close();
return builder.toString();
}
in.close();
return builder.toString();
}
private static String GET(String query) throws IOException {
String response;
private static String GET(String query) throws IOException {
String response;
URL url = new URL(query);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
URL url = new URL(query);
HttpURLConnection connection;
try {
InputStream in = new BufferedInputStream(connection.getInputStream());
response = readStream(in);
} finally {
connection.disconnect();
}
// 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);
// Connect without Tor by default.
boolean useTor = sp.getBoolean(key, false);
// Default Orbot SOCKS5 address.
String orbotSocks5Hostname = "127.0.0.1";
int orbotSocks5Port = 9050;
return response;
}
if (useTor)
connection = (HttpURLConnection) url.openConnection(
new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(orbotSocks5Hostname, orbotSocks5Port)));
else
connection = (HttpURLConnection) url.openConnection();
public static String GET(TriviaQuery query) throws IOException {
return GET(query.toString());
}
try {
InputStream in = new BufferedInputStream(connection.getInputStream());
response = readStream(in);
} finally {
connection.disconnect();
}
public static ArrayList<TriviaQuestion> jsonToQuestionArray(String json) throws NoTriviaResultsException {
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
return response;
}
if (jsonObject.get("response_code").getAsInt() == 1) {
throw new NoTriviaResultsException();
}
public static String GET(TriviaQuery query) throws IOException {
return GET(query.toString());
}
JsonArray jsonArray = jsonObject.getAsJsonArray("results");
public static ArrayList<TriviaQuestion> jsonToQuestionArray(String json) throws NoTriviaResultsException {
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
ArrayList<TriviaQuestion> questions = new ArrayList<>();
if (jsonObject.get("response_code").getAsInt() == 1) {
throw new NoTriviaResultsException();
}
for (JsonElement element : jsonArray) {
JsonObject object = element.getAsJsonObject();
TriviaType type = TriviaType.get(object.get("type").getAsString());
JsonArray jsonArray = jsonObject.getAsJsonArray("results");
if (type == TriviaType.MULTIPLE) {
questions.add(TriviaQuestionMultiple.fromJson(object));
} else {
questions.add(TriviaQuestionBoolean.fromJson(object));
}
}
ArrayList<TriviaQuestion> questions = new ArrayList<>();
return questions;
}
for (JsonElement element : jsonArray) {
JsonObject object = element.getAsJsonObject();
TriviaType type = TriviaType.get(object.get("type").getAsString());
if (type == TriviaType.MULTIPLE) {
questions.add(TriviaQuestionMultiple.fromJson(object));
} else {
questions.add(TriviaQuestionBoolean.fromJson(object));
}
}
return questions;
}
}