From 10f9126cb6b686e0bbc48c772686206463891620 Mon Sep 17 00:00:00 2001 From: Maxime Dor Date: Sun, 11 Mar 2018 18:28:48 +0100 Subject: [PATCH] Better federation auto-discovery - Use the new status check endpoint at /_matrix/identity/api/v1 - Enforce DNS SRV existence before asking remote server for data --- .../mxisd/matrix/IdentityServerUtils.java | 62 +++++++------------ 1 file changed, 24 insertions(+), 38 deletions(-) diff --git a/src/main/java/io/kamax/mxisd/matrix/IdentityServerUtils.java b/src/main/java/io/kamax/mxisd/matrix/IdentityServerUtils.java index 7dbaef8..77cc18e 100644 --- a/src/main/java/io/kamax/mxisd/matrix/IdentityServerUtils.java +++ b/src/main/java/io/kamax/mxisd/matrix/IdentityServerUtils.java @@ -22,9 +22,6 @@ import java.util.Optional; // FIXME placeholder, this must go in matrix-java-sdk for 1.0 public class IdentityServerUtils { - public static final String THREEPID_TEST_MEDIUM = "email"; - public static final String THREEPID_TEST_ADDRESS = "mxisd-email-forever-unknown@forever-invalid.kamax.io"; - private static Logger log = LoggerFactory.getLogger(IdentityServerUtils.class); private static JsonParser parser = new JsonParser(); @@ -35,9 +32,7 @@ public class IdentityServerUtils { try { // FIXME use Apache HTTP client - HttpURLConnection rootSrvConn = (HttpURLConnection) new URL( - remote + "/_matrix/identity/api/v1/lookup?medium=" + THREEPID_TEST_MEDIUM + "&address=" + THREEPID_TEST_ADDRESS - ).openConnection(); + HttpURLConnection rootSrvConn = (HttpURLConnection) new URL(remote + "/_matrix/identity/api/v1/").openConnection(); // TODO turn this into a configuration property rootSrvConn.setConnectTimeout(2000); @@ -53,11 +48,6 @@ public class IdentityServerUtils { return false; } - if (el.getAsJsonObject().has("address")) { - log.debug("IS {} did not send back a JSON object for single 3PID lookup"); - return false; - } - return true; } catch (IllegalArgumentException | IOException | JsonParseException e) { log.info("{} is not a usable Identity Server: {}", remote, e.getMessage()); @@ -84,39 +74,35 @@ public class IdentityServerUtils { List srvRecords = new ArrayList<>(); Record[] records = new Lookup(lookupDns, Type.SRV).run(); - if (records != null) { - for (Record record : records) { - log.info("Record: {}", record.toString()); - if (record.getType() == Type.SRV) { - if (record instanceof SRVRecord) { - srvRecords.add((SRVRecord) record); - } else { - log.warn("We requested SRV records but we got {} instead!", record.getClass().getName()); - } - } else { - log.warn("We request SRV type records but we got type #{} instead!", record.getType()); - } - } - srvRecords.sort(Comparator.comparingInt(SRVRecord::getPriority)); + if (records == null || records.length == 0) { + log.info("No SRV record for {}", lookupDns); + return Optional.empty(); + } - for (SRVRecord srvRecord : srvRecords) { - String baseUrl = "https://" + srvRecord.getTarget().toString(true) + ":" + srvRecord.getPort(); + for (Record record : records) { + log.info("Record: {}", record.toString()); + if (record.getType() == Type.SRV) { + if (record instanceof SRVRecord) { + srvRecords.add((SRVRecord) record); + } else { + log.warn("We requested SRV records but we got {} instead!", record.getClass().getName()); + } + } else { + log.warn("We request SRV type records but we got type #{} instead!", record.getType()); + } + } + srvRecords.sort(Comparator.comparingInt(SRVRecord::getPriority)); + + for (SRVRecord srvRecord : srvRecords) { + String baseUrl = "https://" + srvRecord.getTarget().toString(true) + ":" + srvRecord.getPort(); + if (isUsable(baseUrl)) { log.info("Found Identity Server for domain {} at {}", domainOrUrl, baseUrl); return Optional.of(baseUrl); } - } else { - log.info("No SRV record for {}", lookupDns); } - log.info("Performing basic lookup using domain name {}", domainOrUrl); - String baseUrl = "https://" + domainOrUrl; - if (isUsable(baseUrl)) { - log.info("Found Identity Server for domain {} at {}", domainOrUrl, baseUrl); - return Optional.of(baseUrl); - } else { - log.info("{} is not a usable Identity Server", baseUrl); - return Optional.empty(); - } + log.info("Found no Identity server for domain {} at {}"); + return Optional.empty(); } catch (TextParseException e) { log.warn(domainOrUrl + " is not a valid domain name"); return Optional.empty();