From e178184df9e03bb031d3fd9a802a1f759b6e3025 Mon Sep 17 00:00:00 2001 From: PeratX <1215714524@qq.com> Date: Sun, 8 Jul 2018 14:30:23 +0800 Subject: [PATCH] Fixed crash when HTTPS response is not correct --- README.md | 2 +- .../daedalus/provider/HttpsIetfProvider.java | 11 +- .../daedalus/provider/HttpsJsonProvider.java | 109 +++++++++--------- 3 files changed, 64 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 0f277be..38f4c7e 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Supported DNS Query Methods: * UDP * TCP * DNS over TLS ([RFC7858](https://tools.ietf.org/html/rfc7858)) -* DNS over HTTPS ([IETF darft](https://tools.ietf.org/html/draft-ietf-doh-dns-over-https-11)) +* DNS over HTTPS ([IETF darft](https://tools.ietf.org/html/draft-ietf-doh-dns-over-https-12)) * DNS over HTTPS ([Google JSON](https://developers.google.com/speed/public-dns/docs/dns-over-https))
diff --git a/app/src/main/java/org/itxtech/daedalus/provider/HttpsIetfProvider.java b/app/src/main/java/org/itxtech/daedalus/provider/HttpsIetfProvider.java index 6199c73..81d2758 100644 --- a/app/src/main/java/org/itxtech/daedalus/provider/HttpsIetfProvider.java +++ b/app/src/main/java/org/itxtech/daedalus/provider/HttpsIetfProvider.java @@ -65,11 +65,14 @@ public class HttpsIetfProvider extends HttpsProvider { } @Override - public void onResponse(Call call, Response response) throws IOException { + public void onResponse(Call call, Response response) { if (response.isSuccessful()) { - result = new DnsMessage(response.body().bytes()).asBuilder() - .setId(id).build().toArray(); - completed = true; + try { + result = new DnsMessage(response.body().bytes()).asBuilder() + .setId(id).build().toArray(); + completed = true; + } catch (Exception ignored) {//throw IllegalArgumentException when response is not correct + } } } }); diff --git a/app/src/main/java/org/itxtech/daedalus/provider/HttpsJsonProvider.java b/app/src/main/java/org/itxtech/daedalus/provider/HttpsJsonProvider.java index 7365f97..5bd001e 100644 --- a/app/src/main/java/org/itxtech/daedalus/provider/HttpsJsonProvider.java +++ b/app/src/main/java/org/itxtech/daedalus/provider/HttpsJsonProvider.java @@ -72,64 +72,67 @@ public class HttpsJsonProvider extends HttpsProvider { } @Override - public void onResponse(Call call, Response response) throws IOException { + public void onResponse(Call call, Response response) { if (response.isSuccessful()) { - JsonObject jsonObject = new JsonParser().parse(response.body().string()).getAsJsonObject(); - DnsMessage.Builder msg = message.asBuilder() - .setRecursionDesired(jsonObject.get("RD").getAsBoolean()) - .setRecursionAvailable(jsonObject.get("RA").getAsBoolean()) - .setAuthenticData(jsonObject.get("AD").getAsBoolean()) - .setCheckingDisabled(jsonObject.get("CD").getAsBoolean()); - if (jsonObject.has("Answer")) { - JsonArray answers = jsonObject.get("Answer").getAsJsonArray(); - for (JsonElement answer : answers) { - JsonObject ans = answer.getAsJsonObject(); - Record.TYPE type = Record.TYPE.getType(ans.get("type").getAsInt()); - String data = ans.get("data").getAsString(); - Data recordData = null; - switch (type) { - case A: - recordData = new A(data); - break; - case AAAA: - recordData = new AAAA(data); - break; - case CNAME: - recordData = new CNAME(data); - break; - case MX: - recordData = new MX(5, data); - break; - case SOA: - String[] sections = data.split(" "); - if (sections.length == 7) { - recordData = new SOA(sections[0], sections[1], - Long.valueOf(sections[2]), Integer.valueOf(sections[3]), - Integer.valueOf(sections[4]), Integer.valueOf(sections[5]), - Long.valueOf(sections[6])); - } - break; - case DNAME: - recordData = new DNAME(data); - break; - case NS: - recordData = new NS(DnsName.from(data)); - break; - case TXT: - recordData = new TXT(data.getBytes()); - break; + try { + JsonObject jsonObject = new JsonParser().parse(response.body().string()).getAsJsonObject(); + DnsMessage.Builder msg = message.asBuilder() + .setRecursionDesired(jsonObject.get("RD").getAsBoolean()) + .setRecursionAvailable(jsonObject.get("RA").getAsBoolean()) + .setAuthenticData(jsonObject.get("AD").getAsBoolean()) + .setCheckingDisabled(jsonObject.get("CD").getAsBoolean()); + if (jsonObject.has("Answer")) { + JsonArray answers = jsonObject.get("Answer").getAsJsonArray(); + for (JsonElement answer : answers) { + JsonObject ans = answer.getAsJsonObject(); + Record.TYPE type = Record.TYPE.getType(ans.get("type").getAsInt()); + String data = ans.get("data").getAsString(); + Data recordData = null; + switch (type) { + case A: + recordData = new A(data); + break; + case AAAA: + recordData = new AAAA(data); + break; + case CNAME: + recordData = new CNAME(data); + break; + case MX: + recordData = new MX(5, data); + break; + case SOA: + String[] sections = data.split(" "); + if (sections.length == 7) { + recordData = new SOA(sections[0], sections[1], + Long.valueOf(sections[2]), Integer.valueOf(sections[3]), + Integer.valueOf(sections[4]), Integer.valueOf(sections[5]), + Long.valueOf(sections[6])); + } + break; + case DNAME: + recordData = new DNAME(data); + break; + case NS: + recordData = new NS(DnsName.from(data)); + break; + case TXT: + recordData = new TXT(data.getBytes()); + break; - } - if (recordData != null) { - msg.addAnswer(new Record<>(ans.get("name").getAsString(), - type, 1, - ans.get("TTL").getAsLong(), - recordData)); + } + if (recordData != null) { + msg.addAnswer(new Record<>(ans.get("name").getAsString(), + type, 1, + ans.get("TTL").getAsLong(), + recordData)); + } } } + result = msg.setQrFlag(true).build().toArray(); + completed = true; + } catch (Exception ignored) {//throw com.google.gson.JsonSyntaxException when response is not correct } - result = msg.setQrFlag(true).build().toArray(); - completed = true; } } });