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;
@ -62,7 +70,23 @@ public class ApiUtil {
String response;
URL url = new URL(query);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
HttpURLConnection connection;
// 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;
if (useTor)
connection = (HttpURLConnection) url.openConnection(
new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(orbotSocks5Hostname, orbotSocks5Port)));
else
connection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(connection.getInputStream());