Skip to content

Commit 187caa1

Browse files
committed
added proxy support
1 parent 9615d90 commit 187caa1

File tree

11 files changed

+72
-34
lines changed

11 files changed

+72
-34
lines changed

Lib/build.gradle

+4
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

+35-11
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

+7-6
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) {

Lib/src/main/java/com/textuality/keybase/lib/prover/Coinbase.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
import org.json.JSONObject;
2525

2626
import java.net.MalformedURLException;
27+
import java.net.Proxy;
2728
import java.net.URL;
2829

2930
public class Coinbase extends Prover {
3031

3132
@Override
32-
public boolean fetchProofData() {
33+
public boolean fetchProofData(Proxy proxy) {
3334

3435
try {
35-
JSONObject sigJSON = readSig(mProof.getSigId());
36+
JSONObject sigJSON = readSig(mProof.getSigId(), proxy);
3637

3738
String proofUrl = mProof.getProofUrl();
3839

Lib/src/main/java/com/textuality/keybase/lib/prover/DNS.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@
2323
import org.json.JSONException;
2424
import org.json.JSONObject;
2525

26+
import java.net.Proxy;
2627
import java.util.List;
2728

2829
public class DNS extends Prover {
2930

3031
private String mDomain = null;
3132

3233
@Override
33-
public boolean fetchProofData() {
34+
public boolean fetchProofData(Proxy proxy) {
3435

3536
try {
36-
JSONObject sigJSON = readSig(mProof.getSigId());
37+
JSONObject sigJSON = readSig(mProof.getSigId(), proxy);
3738

3839
// the magic string is the base64 of the SHA of the raw message
3940
mShortenedMessageHash = JWalk.getString(sigJSON, "sig_id_short");

Lib/src/main/java/com/textuality/keybase/lib/prover/GitHub.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@
2424
import org.json.JSONException;
2525
import org.json.JSONObject;
2626

27+
import java.net.Proxy;
28+
2729
public class GitHub extends Prover {
2830

2931
private static final String[] sAllowedApiBases = {
3032
"https://gist.githubusercontent.com/", "https://gist.github.com/"
3133
};
3234

3335
@Override
34-
public boolean fetchProofData() {
36+
public boolean fetchProofData(Proxy proxy) {
3537

3638
try {
37-
JSONObject sigJSON = readSig(mProof.getSigId());
39+
JSONObject sigJSON = readSig(mProof.getSigId(), proxy);
3840

3941
// find the URL for the markdown form of the gist
4042
String markdownURL = JWalk.getString(sigJSON, "api_url");

Lib/src/main/java/com/textuality/keybase/lib/prover/HackerNews.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
import org.json.JSONObject;
2525

2626
import java.net.MalformedURLException;
27+
import java.net.Proxy;
2728
import java.net.URL;
2829

2930
public class HackerNews extends Prover {
3031

3132
@Override
32-
public boolean fetchProofData() {
33+
public boolean fetchProofData(Proxy proxy) {
3334

3435
try {
35-
JSONObject sigJSON = readSig(mProof.getSigId());
36+
JSONObject sigJSON = readSig(mProof.getSigId(), proxy);
3637

3738
// the magic string is the base64 of the SHA of the raw message
3839
mShortenedMessageHash = JWalk.getString(sigJSON, "sig_id_short");

Lib/src/main/java/com/textuality/keybase/lib/prover/Prover.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.IOException;
3131
import java.io.InputStream;
3232
import java.net.MalformedURLException;
33+
import java.net.Proxy;
3334
import java.net.URL;
3435
import java.security.MessageDigest;
3536
import java.security.NoSuchAlgorithmException;
@@ -91,7 +92,7 @@ public Prover(Proof proof) {
9192
mProof = proof;
9293
}
9394

94-
abstract public boolean fetchProofData();
95+
abstract public boolean fetchProofData(Proxy proxy);
9596

9697
public String getPgpMessage() {
9798
return mPgpMessage;
@@ -109,10 +110,10 @@ public List<String> getLog() {
109110
return mLog;
110111
}
111112

112-
JSONObject readSig(String sigId) throws JSONException, KeybaseException {
113+
JSONObject readSig(String sigId, Proxy proxy) throws JSONException, KeybaseException {
113114

114115
// fetch the sig
115-
JSONObject sigJSON = Search.getFromKeybase("_/api/1.0/sig/get.json?sig_id=", sigId);
116+
JSONObject sigJSON = Search.getFromKeybase("_/api/1.0/sig/get.json?sig_id=", sigId, proxy);
116117
mLog.add("Successfully retrieved sig from Keybase");
117118

118119
sigJSON = JWalk.getArray(sigJSON, "sigs").getJSONObject(0);

Lib/src/main/java/com/textuality/keybase/lib/prover/Reddit.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@
2626
import org.json.JSONObject;
2727

2828
import java.net.MalformedURLException;
29+
import java.net.Proxy;
2930
import java.net.URL;
3031

3132
public class Reddit extends Prover {
3233

3334
private String mApiUrl = null;
3435

3536
@Override
36-
public boolean fetchProofData() {
37+
public boolean fetchProofData(Proxy proxy) {
3738

3839
try {
39-
JSONObject sigJSON = readSig(mProof.getSigId());
40+
JSONObject sigJSON = readSig(mProof.getSigId(), proxy);
4041

4142
// the magic string is the base64 of the SHA of the raw message
4243
mShortenedMessageHash = JWalk.getString(sigJSON, "sig_id_short");

Lib/src/main/java/com/textuality/keybase/lib/prover/Twitter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.json.JSONObject;
2525

2626
import java.net.MalformedURLException;
27+
import java.net.Proxy;
2728
import java.net.URL;
2829

2930
public class Twitter extends Prover {
@@ -33,11 +34,11 @@ public Twitter(Proof proof) {
3334
}
3435

3536
@Override
36-
public boolean fetchProofData() {
37+
public boolean fetchProofData(Proxy proxy) {
3738

3839
String tweetUrl = null;
3940
try {
40-
JSONObject sigJSON = readSig(mProof.getSigId());
41+
JSONObject sigJSON = readSig(mProof.getSigId(), proxy);
4142

4243
// the magic string is the base64 of the SHA of the raw message
4344
mShortenedMessageHash = JWalk.getString(sigJSON, "sig_id_short");

Lib/src/main/java/com/textuality/keybase/lib/prover/Website.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.json.JSONObject;
2626

2727
import java.net.MalformedURLException;
28+
import java.net.Proxy;
2829
import java.net.URL;
2930

3031
public class Website extends Prover {
@@ -34,10 +35,10 @@ public Website(Proof proof) {
3435
}
3536

3637
@Override
37-
public boolean fetchProofData() {
38+
public boolean fetchProofData(Proxy proxy) {
3839

3940
try {
40-
JSONObject sigJSON = readSig(mProof.getSigId());
41+
JSONObject sigJSON = readSig(mProof.getSigId(), proxy);
4142

4243
// find the .well-known URL
4344
String wellKnownUrl = JWalk.getString(sigJSON, "api_url");

0 commit comments

Comments
 (0)