Skip to content

Commit 1fec14c

Browse files
committed
added proxy support
1 parent 9615d90 commit 1fec14c

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

Lib/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
apply plugin: 'com.android.library'
22

3+
dependencies {
4+
compile 'com.squareup.okhttp:okhttp:2.4.0'
5+
}
6+
37
android {
48
compileSdkVersion rootProject.ext.compileSdkVersion
59
buildToolsVersion rootProject.ext.buildToolsVersion

Lib/src/main/java/com/textuality/keybase/lib/Search.java

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import android.util.Log;
2020

21+
import com.squareup.okhttp.OkHttpClient;
22+
import com.squareup.okhttp.Request;
23+
import com.squareup.okhttp.Response;
2124
import org.json.JSONArray;
2225
import org.json.JSONException;
2326
import org.json.JSONObject;
@@ -26,36 +29,53 @@
2629
import java.io.IOException;
2730
import java.io.InputStream;
2831
import java.net.HttpURLConnection;
32+
import java.net.Proxy;
2933
import java.net.URL;
3034
import java.net.URLEncoder;
35+
import java.sql.Time;
3136
import java.util.Iterator;
3237
import java.util.NoSuchElementException;
38+
import java.util.concurrent.TimeUnit;
3339

3440
public class Search {
3541

3642
private static final String TAG = "KEYBASE-LIB";
3743

38-
public static Iterable<Match> search(String query) throws KeybaseException {
39-
JSONObject result = getFromKeybase("_/api/1.0/user/autocomplete.json?q=", query);
44+
public static Iterable<Match> search(String query, Proxy proxy) throws KeybaseException {
45+
JSONObject result = getFromKeybase("_/api/1.0/user/autocomplete.json?q=", query, proxy);
4046
try {
4147
return new MatchIterator(JWalk.getArray(result, "completions"));
4248
} catch (JSONException e) {
4349
throw KeybaseException.keybaseScrewup(e);
4450
}
4551
}
4652

47-
public static JSONObject getFromKeybase(String path, String query) throws KeybaseException {
53+
public static JSONObject getFromKeybase(String path, String query, Proxy proxy) throws KeybaseException {
4854
try {
4955
String url = "https://keybase.io/" + path + URLEncoder.encode(query, "utf8");
5056

5157
URL realUrl = new URL(url);
52-
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
53-
conn.setConnectTimeout(5000); // TODO: Reasonable values for keybase
54-
conn.setReadTimeout(25000);
55-
conn.connect();
56-
int response = conn.getResponseCode();
58+
59+
OkHttpClient client = new OkHttpClient();
60+
client.setProxy(proxy);
61+
62+
if (proxy != null) {
63+
client.setConnectTimeout(30000, TimeUnit.MILLISECONDS);
64+
client.setReadTimeout(40000, TimeUnit.MILLISECONDS);
65+
} else {
66+
client.setConnectTimeout(5000, TimeUnit.MILLISECONDS); // TODO: Reasonable values for keybase
67+
client.setReadTimeout(25000, TimeUnit.MILLISECONDS);
68+
}
69+
70+
tempIpTest(client);
71+
72+
Response resp = client.newCall(new Request.Builder().url(realUrl).build()).execute();
73+
74+
int response = resp.code();
75+
76+
String text = resp.body().string();
77+
5778
if (response >= 200 && response < 300) {
58-
String text = snarf(conn.getInputStream());
5979
try {
6080
JSONObject json = new JSONObject(text);
6181
if (JWalk.getInt(json, "status", "code") != 0) {
@@ -66,14 +86,18 @@ public static JSONObject getFromKeybase(String path, String query) throws Keybas
6686
throw KeybaseException.keybaseScrewup(e);
6787
}
6888
} else {
69-
String message = snarf(conn.getErrorStream());
70-
throw KeybaseException.networkScrewup("Keybase.io query error (status=" + response + "): " + message);
89+
throw KeybaseException.networkScrewup("Keybase.io query error (status=" + response + "): " + text);
7190
}
7291
} catch (Exception e) {
7392
throw KeybaseException.networkScrewup(e);
7493
}
7594
}
7695

96+
public static void tempIpTest(OkHttpClient client) throws IOException {
97+
Log.e("PHILIP","ipTest: "+ client.newCall(new Request.Builder().url("https://wtfismyip.com/text").build())
98+
.execute().body().string());
99+
}
100+
77101
public static String snarf(InputStream in)
78102
throws IOException {
79103
byte[] buf = new byte[1024];

Lib/src/main/java/com/textuality/keybase/lib/User.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,27 @@
2020
import org.json.JSONException;
2121
import org.json.JSONObject;
2222

23+
import java.net.Proxy;
2324
import java.util.Iterator;
2425

2526
public class User {
2627

2728
private final JSONObject mJson;
2829

29-
public static User findByUsername(String username) throws KeybaseException {
30-
JSONObject json = Search.getFromKeybase("_/api/1.0/user/lookup.json?username=", username);
30+
public static User findByUsername(String username, Proxy proxy) throws KeybaseException {
31+
JSONObject json = Search.getFromKeybase("_/api/1.0/user/lookup.json?username=", username, proxy);
3132
try {
3233
json = JWalk.getObject(json, "them");
3334
} catch (JSONException e) {
3435
throw KeybaseException.keybaseScrewup(e);
3536
}
3637
return new User(json);
3738
}
38-
public static String keyForUsername(String username) throws KeybaseException {
39-
return findByUsername(username).getKey();
39+
public static String keyForUsername(String username, Proxy proxy) throws KeybaseException {
40+
return findByUsername(username, proxy).getKey();
4041
}
41-
public static User findByFingerprint(String fingerprint) throws KeybaseException {
42-
JSONObject json = Search.getFromKeybase("_/api/1.0/user/lookup.json?key_fingerprint=", fingerprint);
42+
public static User findByFingerprint(String fingerprint, Proxy proxy) throws KeybaseException {
43+
JSONObject json = Search.getFromKeybase("_/api/1.0/user/lookup.json?key_fingerprint=", fingerprint, proxy);
4344
try {
4445
JSONArray them = JWalk.getArray(json, "them");
4546
if (them.length() != 1) {

0 commit comments

Comments
 (0)