Skip to content

Commit

Permalink
Better federation auto-discovery
Browse files Browse the repository at this point in the history
- Use the new status check endpoint at /_matrix/identity/api/v1
- Enforce DNS SRV existence before asking remote server for data
  • Loading branch information
Maxime Dor committed Mar 11, 2018
1 parent c3385b3 commit 10f9126
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions src/main/java/io/kamax/mxisd/matrix/IdentityServerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
// 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();

Expand All @@ -35,9 +32,7 @@ public static boolean isUsable(String remote) {

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);

Expand All @@ -53,11 +48,6 @@ public static boolean isUsable(String remote) {
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());
Expand All @@ -84,39 +74,35 @@ public static Optional<String> findIsUrlForDomain(String domainOrUrl) {

List<SRVRecord> 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());
}
if (records == null || records.length == 0) {
log.info("No SRV record for {}", lookupDns);
return Optional.empty();
}

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 request SRV type records but we got type #{} instead!", record.getType());
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));
}
srvRecords.sort(Comparator.comparingInt(SRVRecord::getPriority));

for (SRVRecord srvRecord : srvRecords) {
String baseUrl = "https://" + srvRecord.getTarget().toString(true) + ":" + srvRecord.getPort();
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();
Expand Down

0 comments on commit 10f9126

Please sign in to comment.