From fb2d81550c5639c2b11c422a03802196d7631c01 Mon Sep 17 00:00:00 2001 From: "luca0N\\!" Date: Wed, 3 Mar 2021 22:15:53 -0300 Subject: [PATCH] Added Tor support Based on #2. --- .../luca0n/joguitos/pluck/util/ApiUtil.java | 108 +++++++++++------- 1 file changed, 66 insertions(+), 42 deletions(-) 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 1d6c8ce..c4b4e83 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 @@ -23,6 +23,10 @@ Contact us at . 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 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 jsonToQuestionArray(String json) throws NoTriviaResultsException { + JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject(); - ArrayList 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 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; + } }