mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-11-17 22:35:05 -05:00
gave HKP server proper error handling, going through all IPs of a pool until a response is received, reporting "too many responses", "no keys found", "insufficient query" correctly
Update issue 9 Proper error handling added.
This commit is contained in:
parent
96162b6608
commit
e4dd80005c
@ -194,6 +194,7 @@
|
|||||||
<string name="noKeysExported">No keys exported.</string>
|
<string name="noKeysExported">No keys exported.</string>
|
||||||
<string name="keyCreationElGamalInfo">Note: only subkeys support ElGamal, and for ElGamal the nearest keysize of 1536, 2048, 3072, 4096, or 8192 will be used.</string>
|
<string name="keyCreationElGamalInfo">Note: only subkeys support ElGamal, and for ElGamal the nearest keysize of 1536, 2048, 3072, 4096, or 8192 will be used.</string>
|
||||||
<string name="keyNotFound">Couldn\'t find key %08X.</string>
|
<string name="keyNotFound">Couldn\'t find key %08X.</string>
|
||||||
|
<string name="keysFound">Found %s key(s).</string>
|
||||||
|
|
||||||
<!-- error_lowerCase: phrases, no punctuation, all lowercase,
|
<!-- error_lowerCase: phrases, no punctuation, all lowercase,
|
||||||
they will be put after "errorMessage", e.g. "Error: file not found" -->
|
they will be put after "errorMessage", e.g. "Error: file not found" -->
|
||||||
|
@ -3,10 +3,13 @@ package org.thialfihar.android.apg;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.InetAddress;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
@ -16,6 +19,25 @@ import java.util.regex.Pattern;
|
|||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
|
|
||||||
public class HkpKeyServer extends KeyServer {
|
public class HkpKeyServer extends KeyServer {
|
||||||
|
private static class HttpError extends Exception {
|
||||||
|
private static final long serialVersionUID = 1718783705229428893L;
|
||||||
|
private int mCode;
|
||||||
|
private String mData;
|
||||||
|
|
||||||
|
public HttpError(int code, String data) {
|
||||||
|
super("" + code + ": " + data);
|
||||||
|
mCode = code;
|
||||||
|
mData = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCode() {
|
||||||
|
return mCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getData() {
|
||||||
|
return mData;
|
||||||
|
}
|
||||||
|
}
|
||||||
private String mHost;
|
private String mHost;
|
||||||
private short mPort = 11371;
|
private short mPort = 11371;
|
||||||
|
|
||||||
@ -36,29 +58,90 @@ public class HkpKeyServer extends KeyServer {
|
|||||||
mPort = port;
|
mPort = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
static private String readAll(InputStream in, String encoding)
|
||||||
List<KeyInfo> search(String query)
|
throws IOException {
|
||||||
throws MalformedURLException, IOException {
|
|
||||||
Vector<KeyInfo> results = new Vector<KeyInfo>();
|
|
||||||
|
|
||||||
String url = "http://" + mHost + ":" + mPort + "/pks/lookup?op=index&search=" +
|
|
||||||
URLEncoder.encode(query, "utf8");
|
|
||||||
URL realUrl = new URL(url);
|
|
||||||
URLConnection conn = realUrl.openConnection();
|
|
||||||
InputStream is = conn.getInputStream();
|
|
||||||
ByteArrayOutputStream raw = new ByteArrayOutputStream();
|
ByteArrayOutputStream raw = new ByteArrayOutputStream();
|
||||||
|
|
||||||
byte buffer[] = new byte[1 << 16];
|
byte buffer[] = new byte[1 << 16];
|
||||||
int n = 0;
|
int n = 0;
|
||||||
while ((n = is.read(buffer)) != -1) {
|
while ((n = in.read(buffer)) != -1) {
|
||||||
raw.write(buffer, 0, n);
|
raw.write(buffer, 0, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
String encoding = conn.getContentEncoding();
|
|
||||||
if (encoding == null) {
|
if (encoding == null) {
|
||||||
encoding = "utf8";
|
encoding = "utf8";
|
||||||
}
|
}
|
||||||
String data = raw.toString(encoding);
|
return raw.toString(encoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String query(String request)
|
||||||
|
throws QueryException, HttpError {
|
||||||
|
InetAddress ips[];
|
||||||
|
try {
|
||||||
|
ips = InetAddress.getAllByName(mHost);
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
throw new QueryException(e.toString());
|
||||||
|
}
|
||||||
|
for (int i = 5; i < ips.length; ++i) {
|
||||||
|
try {
|
||||||
|
String url = "http://" + ips[i].getHostAddress() + ":" + mPort + request;
|
||||||
|
URL realUrl = new URL(url);
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
|
||||||
|
conn.connect();
|
||||||
|
int response = conn.getResponseCode();
|
||||||
|
if (response >= 200 && response < 300) {
|
||||||
|
return readAll(conn.getInputStream(), conn.getContentEncoding());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String data = readAll(conn.getErrorStream(), conn.getContentEncoding());
|
||||||
|
throw new HttpError(response, data);
|
||||||
|
}
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
// nothing to do, try next IP
|
||||||
|
} catch (IOException e) {
|
||||||
|
// nothing to do, try next IP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new QueryException("querying server(s) for '" + mHost + "' failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
List<KeyInfo> search(String query)
|
||||||
|
throws QueryException, TooManyResponses, InsufficientQuery {
|
||||||
|
Vector<KeyInfo> results = new Vector<KeyInfo>();
|
||||||
|
|
||||||
|
if (query.length() < 3) {
|
||||||
|
throw new InsufficientQuery();
|
||||||
|
}
|
||||||
|
|
||||||
|
String encodedQuery;
|
||||||
|
try {
|
||||||
|
encodedQuery = URLEncoder.encode(query, "utf8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String request = "/pks/lookup?op=index&search=" + encodedQuery;
|
||||||
|
|
||||||
|
String data = null;
|
||||||
|
try {
|
||||||
|
data = query(request);
|
||||||
|
} catch (HttpError e) {
|
||||||
|
if (e.getCode() == 404) {
|
||||||
|
return results;
|
||||||
|
} else {
|
||||||
|
System.out.println(e.getData());
|
||||||
|
if (e.getData().toLowerCase().contains("no keys found")) {
|
||||||
|
return results;
|
||||||
|
} else if (e.getData().toLowerCase().contains("too many")) {
|
||||||
|
throw new TooManyResponses();
|
||||||
|
} else if (e.getData().toLowerCase().contains("insufficient")) {
|
||||||
|
throw new InsufficientQuery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new QueryException("querying server(s) for '" + mHost + "' failed");
|
||||||
|
}
|
||||||
|
|
||||||
Matcher matcher = PUB_KEY_LINE.matcher(data);
|
Matcher matcher = PUB_KEY_LINE.matcher(data);
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
KeyInfo info = new KeyInfo();
|
KeyInfo info = new KeyInfo();
|
||||||
@ -94,21 +177,15 @@ public class HkpKeyServer extends KeyServer {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
String get(long keyId)
|
String get(long keyId)
|
||||||
throws MalformedURLException, IOException {
|
throws QueryException {
|
||||||
String url = "http://" + mHost + ":" + mPort +
|
String request = "/pks/lookup?op=get&search=0x" + Apg.keyToHex(keyId);
|
||||||
"/pks/lookup?op=get&search=0x" + Apg.keyToHex(keyId);
|
|
||||||
URL realUrl = new URL(url);
|
|
||||||
URLConnection conn = realUrl.openConnection();
|
|
||||||
InputStream is = conn.getInputStream();
|
|
||||||
ByteArrayOutputStream raw = new ByteArrayOutputStream();
|
|
||||||
|
|
||||||
byte buffer[] = new byte[1 << 16];
|
String data = null;
|
||||||
int n = 0;
|
try {
|
||||||
while ((n = is.read(buffer)) != -1) {
|
data = query(request);
|
||||||
raw.write(buffer, 0, n);
|
} catch (HttpError e) {
|
||||||
|
throw new QueryException("not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
String data = raw.toString();
|
|
||||||
Matcher matcher = Apg.PGP_PUBLIC_KEY.matcher(data);
|
Matcher matcher = Apg.PGP_PUBLIC_KEY.matcher(data);
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
return matcher.group(1);
|
return matcher.group(1);
|
||||||
|
@ -1,13 +1,23 @@
|
|||||||
package org.thialfihar.android.apg;
|
package org.thialfihar.android.apg;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
public abstract class KeyServer {
|
public abstract class KeyServer {
|
||||||
|
static public class QueryException extends Exception {
|
||||||
|
private static final long serialVersionUID = 2703768928624654512L;
|
||||||
|
public QueryException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static public class TooManyResponses extends Exception {
|
||||||
|
private static final long serialVersionUID = 2703768928624654513L;
|
||||||
|
}
|
||||||
|
static public class InsufficientQuery extends Exception {
|
||||||
|
private static final long serialVersionUID = 2703768928624654514L;
|
||||||
|
}
|
||||||
static public class KeyInfo implements Serializable {
|
static public class KeyInfo implements Serializable {
|
||||||
private static final long serialVersionUID = -7797972113284992662L;
|
private static final long serialVersionUID = -7797972113284992662L;
|
||||||
Vector<String> userIds;
|
Vector<String> userIds;
|
||||||
@ -18,6 +28,6 @@ public abstract class KeyServer {
|
|||||||
int size;
|
int size;
|
||||||
String algorithm;
|
String algorithm;
|
||||||
}
|
}
|
||||||
abstract List<KeyInfo> search(String query) throws MalformedURLException, IOException;
|
abstract List<KeyInfo> search(String query) throws QueryException, TooManyResponses, InsufficientQuery;
|
||||||
abstract String get(long keyId) throws MalformedURLException, IOException;
|
abstract String get(long keyId) throws QueryException;
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
package org.thialfihar.android.apg;
|
package org.thialfihar.android.apg;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import org.thialfihar.android.apg.KeyServer.InsufficientQuery;
|
||||||
import org.thialfihar.android.apg.KeyServer.KeyInfo;
|
import org.thialfihar.android.apg.KeyServer.KeyInfo;
|
||||||
|
import org.thialfihar.android.apg.KeyServer.QueryException;
|
||||||
|
import org.thialfihar.android.apg.KeyServer.TooManyResponses;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@ -65,14 +66,13 @@ public class KeyServerQueryActivity extends BaseActivity {
|
|||||||
search(query);
|
search(query);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
mQuery.setText("cartman");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void search(String query) {
|
private void search(String query) {
|
||||||
showDialog(Id.dialog.querying);
|
showDialog(Id.dialog.querying);
|
||||||
mQueryType = Id.query.search;
|
mQueryType = Id.query.search;
|
||||||
mQueryString = query;
|
mQueryString = query;
|
||||||
|
mAdapter.setKeys(new Vector<KeyInfo>());
|
||||||
startThread();
|
startThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,16 +90,18 @@ public class KeyServerQueryActivity extends BaseActivity {
|
|||||||
Message msg = new Message();
|
Message msg = new Message();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
HkpKeyServer server = new HkpKeyServer("198.128.3.63");//"pool.sks-keyservers.net");
|
HkpKeyServer server = new HkpKeyServer("pool.sks-keyservers.net");
|
||||||
if (mQueryType == Id.query.search) {
|
if (mQueryType == Id.query.search) {
|
||||||
mSearchResult = server.search(mQueryString);
|
mSearchResult = server.search(mQueryString);
|
||||||
} else if (mQueryType == Id.query.get) {
|
} else if (mQueryType == Id.query.get) {
|
||||||
mKeyData = server.get(mQueryId);
|
mKeyData = server.get(mQueryId);
|
||||||
}
|
}
|
||||||
} catch (MalformedURLException e) {
|
} catch (QueryException e) {
|
||||||
error = "" + e;
|
|
||||||
} catch (IOException e) {
|
|
||||||
error = "" + e;
|
error = "" + e;
|
||||||
|
} catch (InsufficientQuery e) {
|
||||||
|
error = "Insufficient query.";
|
||||||
|
} catch (TooManyResponses e) {
|
||||||
|
error = "Too many responses.";
|
||||||
}
|
}
|
||||||
|
|
||||||
data.putInt(Apg.EXTRA_STATUS, Id.message.done);
|
data.putInt(Apg.EXTRA_STATUS, Id.message.done);
|
||||||
@ -127,6 +129,7 @@ public class KeyServerQueryActivity extends BaseActivity {
|
|||||||
|
|
||||||
if (mQueryType == Id.query.search) {
|
if (mQueryType == Id.query.search) {
|
||||||
if (mSearchResult != null) {
|
if (mSearchResult != null) {
|
||||||
|
Toast.makeText(this, getString(R.string.keysFound, mSearchResult.size()), Toast.LENGTH_SHORT).show();
|
||||||
mAdapter.setKeys(mSearchResult);
|
mAdapter.setKeys(mSearchResult);
|
||||||
}
|
}
|
||||||
} else if (mQueryType == Id.query.get) {
|
} else if (mQueryType == Id.query.get) {
|
||||||
|
Loading…
Reference in New Issue
Block a user