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
This commit is contained in:
Maxime Dor
2018-03-11 18:28:48 +01:00
parent c3385b38dc
commit 10f9126cb6

View File

@@ -22,9 +22,6 @@ import java.util.Optional;
// FIXME placeholder, this must go in matrix-java-sdk for 1.0 // FIXME placeholder, this must go in matrix-java-sdk for 1.0
public class IdentityServerUtils { 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 Logger log = LoggerFactory.getLogger(IdentityServerUtils.class);
private static JsonParser parser = new JsonParser(); private static JsonParser parser = new JsonParser();
@@ -35,9 +32,7 @@ public class IdentityServerUtils {
try { try {
// FIXME use Apache HTTP client // FIXME use Apache HTTP client
HttpURLConnection rootSrvConn = (HttpURLConnection) new URL( HttpURLConnection rootSrvConn = (HttpURLConnection) new URL(remote + "/_matrix/identity/api/v1/").openConnection();
remote + "/_matrix/identity/api/v1/lookup?medium=" + THREEPID_TEST_MEDIUM + "&address=" + THREEPID_TEST_ADDRESS
).openConnection();
// TODO turn this into a configuration property // TODO turn this into a configuration property
rootSrvConn.setConnectTimeout(2000); rootSrvConn.setConnectTimeout(2000);
@@ -53,11 +48,6 @@ public class IdentityServerUtils {
return false; 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; return true;
} catch (IllegalArgumentException | IOException | JsonParseException e) { } catch (IllegalArgumentException | IOException | JsonParseException e) {
log.info("{} is not a usable Identity Server: {}", remote, e.getMessage()); log.info("{} is not a usable Identity Server: {}", remote, e.getMessage());
@@ -84,39 +74,35 @@ public class IdentityServerUtils {
List<SRVRecord> srvRecords = new ArrayList<>(); List<SRVRecord> srvRecords = new ArrayList<>();
Record[] records = new Lookup(lookupDns, Type.SRV).run(); Record[] records = new Lookup(lookupDns, Type.SRV).run();
if (records != null) { if (records == null || records.length == 0) {
for (Record record : records) { log.info("No SRV record for {}", lookupDns);
log.info("Record: {}", record.toString()); return Optional.empty();
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) { for (Record record : records) {
String baseUrl = "https://" + srvRecord.getTarget().toString(true) + ":" + srvRecord.getPort(); 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); log.info("Found Identity Server for domain {} at {}", domainOrUrl, baseUrl);
return Optional.of(baseUrl); return Optional.of(baseUrl);
} }
} else {
log.info("No SRV record for {}", lookupDns);
} }
log.info("Performing basic lookup using domain name {}", domainOrUrl); log.info("Found no Identity server for domain {} at {}");
String baseUrl = "https://" + domainOrUrl; return Optional.empty();
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();
}
} catch (TextParseException e) { } catch (TextParseException e) {
log.warn(domainOrUrl + " is not a valid domain name"); log.warn(domainOrUrl + " is not a valid domain name");
return Optional.empty(); return Optional.empty();