| 1 | package net.minecraft.server.network;
|
| 2 |
|
| 3 | import com.google.gson.JsonArray;
|
| 4 | import com.google.gson.JsonElement;
|
| 5 | import com.google.gson.JsonObject;
|
| 6 | import com.microsoft.aad.msal4j.ClientCredentialFactory;
|
| 7 | import com.microsoft.aad.msal4j.ClientCredentialParameters;
|
| 8 | import com.microsoft.aad.msal4j.ConfidentialClientApplication;
|
| 9 | import com.microsoft.aad.msal4j.IAuthenticationResult;
|
| 10 | import com.microsoft.aad.msal4j.IClientCertificate;
|
| 11 | import java.io.InputStream;
|
| 12 | import java.net.HttpURLConnection;
|
| 13 | import java.net.MalformedURLException;
|
| 14 | import java.net.URI;
|
| 15 | import java.net.URL;
|
| 16 | import java.nio.file.Files;
|
| 17 | import java.nio.file.Path;
|
| 18 | import java.util.HashSet;
|
| 19 | import java.util.Locale;
|
| 20 | import java.util.Set;
|
| 21 | import java.util.concurrent.ExecutorService;
|
| 22 | import net.minecraft.util.GsonHelper;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | public class PlayerSafetyServiceTextFilter extends ServerTextFilter {
|
| 26 | private final ConfidentialClientApplication client;
|
| 27 | private final ClientCredentialParameters clientParameters;
|
| 28 | private final Set<String> fullyFilteredEvents;
|
| 29 | private final int connectionReadTimeoutMs;
|
| 30 |
|
| 31 | private PlayerSafetyServiceTextFilter(
|
| 32 | final URL chatEndpoint,
|
| 33 | final ServerTextFilter.MessageEncoder chatEncoder,
|
| 34 | final ServerTextFilter.IgnoreStrategy chatIgnoreStrategy,
|
| 35 | final ExecutorService workerPool,
|
| 36 | final ConfidentialClientApplication client,
|
| 37 | final ClientCredentialParameters clientParameters,
|
| 38 | final Set<String> fullyFilteredEvents,
|
| 39 | final int connectionReadTimeoutMs
|
| 40 | ) {
|
| 41 | super(chatEndpoint, chatEncoder, chatIgnoreStrategy, workerPool);
|
| 42 | this.client = client;
|
| 43 | this.clientParameters = clientParameters;
|
| 44 | this.fullyFilteredEvents = fullyFilteredEvents;
|
| 45 | this.connectionReadTimeoutMs = connectionReadTimeoutMs;
|
| 46 | }
|
| 47 |
|
| 48 | public static @Nullable ServerTextFilter createTextFilterFromConfig(final String textFilteringConfig) {
|
| 49 | JsonObject parsedConfig = GsonHelper.parse(textFilteringConfig);
|
| 50 | URI host = URI.create(GsonHelper.getAsString(parsedConfig, "apiServer"));
|
| 51 | String apiPath = GsonHelper.getAsString(parsedConfig, "apiPath");
|
| 52 | String scope = GsonHelper.getAsString(parsedConfig, "scope");
|
| 53 | String serverId = GsonHelper.getAsString(parsedConfig, "serverId", "");
|
| 54 | String applicationId = GsonHelper.getAsString(parsedConfig, "applicationId");
|
| 55 | String tenantId = GsonHelper.getAsString(parsedConfig, "tenantId");
|
| 56 | String roomId = GsonHelper.getAsString(parsedConfig, "roomId", "Java:Chat");
|
| 57 | String certificatePath = GsonHelper.getAsString(parsedConfig, "certificatePath");
|
| 58 | String certificatePassword = GsonHelper.getAsString(parsedConfig, "certificatePassword", "");
|
| 59 | int hashesToDrop = GsonHelper.getAsInt(parsedConfig, "hashesToDrop", -1);
|
| 60 | int maxConcurrentRequests = GsonHelper.getAsInt(parsedConfig, "maxConcurrentRequests", 7);
|
| 61 | JsonArray fullyFilteredEvents = GsonHelper.getAsJsonArray(parsedConfig, "fullyFilteredEvents");
|
| 62 | Set<String> fullyFilteredEventsSet = new HashSet<>();
|
| 63 | fullyFilteredEvents.forEach(elements -> fullyFilteredEventsSet.add(GsonHelper.convertToString(elements, "filteredEvent")));
|
| 64 | int connectionReadTimeoutMs = GsonHelper.getAsInt(parsedConfig, "connectionReadTimeoutMs", 2000);
|
| 65 |
|
| 66 | URL chatEndpoint;
|
| 67 | try {
|
| 68 | chatEndpoint = host.resolve(apiPath).toURL();
|
| 69 | } catch (MalformedURLException e) {
|
| 70 | throw new RuntimeException(e);
|
| 71 | }
|
| 72 |
|
| 73 | ServerTextFilter.MessageEncoder chatEncoder = (sender, message) -> {
|
| 74 | JsonObject object = new JsonObject();
|
| 75 | object.addProperty("userId", sender.id().toString());
|
| 76 | object.addProperty("userDisplayName", sender.name());
|
| 77 | object.addProperty("server", serverId);
|
| 78 | object.addProperty("room", roomId);
|
| 79 | object.addProperty("area", "JavaChatRealms");
|
| 80 | object.addProperty("data", message);
|
| 81 | object.addProperty("language", "*");
|
| 82 | return object;
|
| 83 | };
|
| 84 | ServerTextFilter.IgnoreStrategy ignoreStrategy = ServerTextFilter.IgnoreStrategy.select(hashesToDrop);
|
| 85 | ExecutorService workerPool = createWorkerPool(maxConcurrentRequests);
|
| 86 |
|
| 87 | IClientCertificate certificate;
|
| 88 | try (InputStream inputStream = Files.newInputStream(Path.of(certificatePath))) {
|
| 89 | certificate = ClientCredentialFactory.createFromCertificate(inputStream, certificatePassword);
|
| 90 | } catch (Exception e) {
|
| 91 | LOGGER.warn("Failed to open certificate file");
|
| 92 | return null;
|
| 93 | }
|
| 94 |
|
| 95 | ConfidentialClientApplication client;
|
| 96 | try {
|
| 97 | client = ConfidentialClientApplication.builder(applicationId, certificate)
|
| 98 | .sendX5c(true)
|
| 99 | .executorService(workerPool)
|
| 100 | .authority(String.format(Locale.ROOT, "https://login.microsoftonline.com/%s/", tenantId))
|
| 101 | .build();
|
| 102 | } catch (Exception e) {
|
| 103 | LOGGER.warn("Failed to create confidential client application");
|
| 104 | return null;
|
| 105 | }
|
| 106 |
|
| 107 | ClientCredentialParameters parameters = ClientCredentialParameters.builder(Set.of(scope)).build();
|
| 108 | return new PlayerSafetyServiceTextFilter(
|
| 109 | chatEndpoint, chatEncoder, ignoreStrategy, workerPool, client, parameters, fullyFilteredEventsSet, connectionReadTimeoutMs
|
| 110 | );
|
| 111 | }
|
| 112 |
|
| 113 | private IAuthenticationResult aquireIAuthenticationResult() {
|
| 114 | return this.client.acquireToken(this.clientParameters).join();
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected void setAuthorizationProperty(final HttpURLConnection connection) {
|
| 119 | IAuthenticationResult authenticationResult = this.aquireIAuthenticationResult();
|
| 120 | connection.setRequestProperty("Authorization", "Bearer " + authenticationResult.accessToken());
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | protected FilteredText filterText(final String message, final ServerTextFilter.IgnoreStrategy ignoreStrategy, final JsonObject response) {
|
| 125 | JsonObject result = GsonHelper.getAsJsonObject(response, "result", null);
|
| 126 | if (result == null) {
|
| 127 | return FilteredText.fullyFiltered(message);
|
| 128 | }
|
| 129 |
|
| 130 | boolean filtered = GsonHelper.getAsBoolean(result, "filtered", true);
|
| 131 | if (!filtered) {
|
| 132 | return FilteredText.passThrough(message);
|
| 133 | }
|
| 134 |
|
| 135 | for (JsonElement element : GsonHelper.getAsJsonArray(result, "events", new JsonArray())) {
|
| 136 | JsonObject object = element.getAsJsonObject();
|
| 137 | String event = GsonHelper.getAsString(object, "id", "");
|
| 138 | if (this.fullyFilteredEvents.contains(event)) {
|
| 139 | return FilteredText.fullyFiltered(message);
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | JsonArray redactedTextIndices = GsonHelper.getAsJsonArray(result, "redactedTextIndex", new JsonArray());
|
| 144 | return new FilteredText(message, this.parseMask(message, redactedTextIndices, ignoreStrategy));
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | protected int connectionReadTimeout() {
|
| 149 | return this.connectionReadTimeoutMs;
|
| 150 | }
|
| 151 | }
|
| 152 |
|