Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider XMPP domains that are listed as a service provider fair to report (release) #4

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ <h1>
PubSub Server Info Plugin Changelog
</h1>

<p><b>1.2.0</b> -- January 8, 2024</p>
<ul>
<li>Consider XMPP domains that are listed as a service provider fair to report.</li>
</ul>

<p><b>1.1.0</b> -- December 23, 2023</p>
<ul>
<li>Updated the implementation to the new draft of the ProtoXEP. This version addresses privacy concerns by reporting names of remote domains only when they opt-in.</li>
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<description>${project.description}</description>
<author>Guus der Kinderen</author>
<version>${project.version}</version>
<date>2023-12-23</date>
<date>2024-01-08</date>
<minServerVersion>4.8.0</minServerVersion>
</plugin>
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>org.igniterealtime.openfire.plugins</groupId>
<artifactId>pubsubserverinfo</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.2.1-SNAPSHOT</version>

<name>PubSub Server Info</name>
<description>Exposes basic server information through a public Pub/Sub node.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class PubSubServerInfoPlugin implements Plugin

private OptInDetector optInDetector;

private XmppProviderDAO xmppProviderDAO;

private final TimerTask task = new TimerTask()
{
@Override
Expand All @@ -78,6 +80,7 @@ public void run()
@Override
public void initializePlugin( PluginManager manager, File pluginDirectory )
{
xmppProviderDAO = new XmppProviderDAO();
optInDetector = new OptInDetector();
ServerSessionEventDispatcher.addListener(optInDetector);
InterceptorManager.getInstance().addInterceptor(optInDetector);
Expand All @@ -100,6 +103,8 @@ public void destroyPlugin()

// Clear out items from the node, but do not delete the node. In case of a plugin reload, it is preferred to retain the node config and subscribers.
clearPubSubNode();

xmppProviderDAO = null;
}

public void clearPubSubNode() {
Expand Down Expand Up @@ -169,7 +174,7 @@ void addRemoteDomain(final Element federation, String domainName, String type) {
Log.trace("add remote domain: {} type: {}", domainName, type);
final Element remoteDomain = federation.addElement("remote-domain");
final Element connection = remoteDomain.addElement("connection");
if (optInDetector.optsIn(domainName)) {
if (xmppProviderDAO.isXmppProvider(new JID(null, domainName, null)) || optInDetector.optsIn(domainName)) {
remoteDomain.addAttribute("name", domainName);
connection.addAttribute("type", type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.igniterealtime.openfire.plugin.pubsubserverinfo;

import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;

import javax.annotation.Nonnull;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class XmppProviderDAO
{
private static final Logger Log = LoggerFactory.getLogger(XmppProviderDAO.class);

private Instant lastRefresh = Instant.EPOCH;
private static final Duration TTL = Duration.ofHours(12);
private Set<JID> data = new HashSet<>();

public static Set<JID> requestServiceProviders() {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://data.xmpp.net/providers/v2/providers-Ds.json"))
.build();

try {
return client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(XmppProviderDAO::parse)
.join();
} catch (Exception e) {
Log.warn("Unable to obtain collection of XMPP service providers.", e);
return Collections.emptySet();
}
}

public static Set<JID> parse(final String data) {
return new JSONArray(data).toList().stream()
.map(Object::toString)
.map(JID::new)
.filter(jid -> jid.getNode() == null && jid.getResource() == null)
.collect(Collectors.toSet());
}
public synchronized Set<JID> getXmppProviders() {
if (data.isEmpty() || Duration.between(lastRefresh, Instant.now()).compareTo(TTL) > 0) {
data = requestServiceProviders();
lastRefresh = Instant.now();
}
return data;
}

public boolean isXmppProvider(final @Nonnull JID address) {
return getXmppProviders().contains(address);
}
}