| 1 | package net.minecraft.server.network;
|
| 2 |
|
| 3 | import com.google.gson.JsonArray;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import com.mojang.authlib.GameProfile;
|
| 6 | import java.io.IOException;
|
| 7 | import java.io.InputStream;
|
| 8 | import java.net.HttpURLConnection;
|
| 9 | import java.net.URI;
|
| 10 | import java.net.URL;
|
| 11 | import java.nio.charset.StandardCharsets;
|
| 12 | import java.util.Base64;
|
| 13 | import java.util.concurrent.Executor;
|
| 14 | import java.util.concurrent.ExecutorService;
|
| 15 | import net.minecraft.network.chat.FilterMask;
|
| 16 | import net.minecraft.util.GsonHelper;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public class LegacyTextFilter extends ServerTextFilter {
|
| 20 | private static final String ENDPOINT = "v1/chat";
|
| 21 | private final URL joinEndpoint;
|
| 22 | private final LegacyTextFilter.JoinOrLeaveEncoder joinEncoder;
|
| 23 | private final URL leaveEndpoint;
|
| 24 | private final LegacyTextFilter.JoinOrLeaveEncoder leaveEncoder;
|
| 25 | private final String authKey;
|
| 26 |
|
| 27 | private LegacyTextFilter(
|
| 28 | final URL chatEndpoint,
|
| 29 | final ServerTextFilter.MessageEncoder chatEncoder,
|
| 30 | final URL joinEndpoint,
|
| 31 | final LegacyTextFilter.JoinOrLeaveEncoder joinEncoder,
|
| 32 | final URL leaveEndpoint,
|
| 33 | final LegacyTextFilter.JoinOrLeaveEncoder leaveEncoder,
|
| 34 | final String authKey,
|
| 35 | final ServerTextFilter.IgnoreStrategy chatIgnoreStrategy,
|
| 36 | final ExecutorService workerPool
|
| 37 | ) {
|
| 38 | super(chatEndpoint, chatEncoder, chatIgnoreStrategy, workerPool);
|
| 39 | this.joinEndpoint = joinEndpoint;
|
| 40 | this.joinEncoder = joinEncoder;
|
| 41 | this.leaveEndpoint = leaveEndpoint;
|
| 42 | this.leaveEncoder = leaveEncoder;
|
| 43 | this.authKey = authKey;
|
| 44 | }
|
| 45 |
|
| 46 | public static @Nullable ServerTextFilter createTextFilterFromConfig(final String config) {
|
| 47 | try {
|
| 48 | JsonObject parsedConfig = GsonHelper.parse(config);
|
| 49 | URI host = new URI(GsonHelper.getAsString(parsedConfig, "apiServer"));
|
| 50 | String key = GsonHelper.getAsString(parsedConfig, "apiKey");
|
| 51 | if (key.isEmpty()) {
|
| 52 | throw new IllegalArgumentException("Missing API key");
|
| 53 | }
|
| 54 |
|
| 55 | int ruleId = GsonHelper.getAsInt(parsedConfig, "ruleId", 1);
|
| 56 | String serverId = GsonHelper.getAsString(parsedConfig, "serverId", "");
|
| 57 | String roomId = GsonHelper.getAsString(parsedConfig, "roomId", "Java:Chat");
|
| 58 | int hashesToDrop = GsonHelper.getAsInt(parsedConfig, "hashesToDrop", -1);
|
| 59 | int maxConcurrentRequests = GsonHelper.getAsInt(parsedConfig, "maxConcurrentRequests", 7);
|
| 60 | JsonObject endpoints = GsonHelper.getAsJsonObject(parsedConfig, "endpoints", null);
|
| 61 | String chatEndpointConfig = getEndpointFromConfig(endpoints, "chat", "v1/chat");
|
| 62 | boolean isLegacyChatEndpoint = chatEndpointConfig.equals("v1/chat");
|
| 63 | URL chatEndpoint = host.resolve("/" + chatEndpointConfig).toURL();
|
| 64 | URL joinEndpoint = getEndpoint(host, endpoints, "join", "v1/join");
|
| 65 | URL leaveEndpoint = getEndpoint(host, endpoints, "leave", "v1/leave");
|
| 66 | LegacyTextFilter.JoinOrLeaveEncoder commonJoinOrLeaveEncoder = user -> {
|
| 67 | JsonObject object = new JsonObject();
|
| 68 | object.addProperty("server", serverId);
|
| 69 | object.addProperty("room", roomId);
|
| 70 | object.addProperty("user_id", user.id().toString());
|
| 71 | object.addProperty("user_display_name", user.name());
|
| 72 | return object;
|
| 73 | };
|
| 74 | ServerTextFilter.MessageEncoder chatEncoder;
|
| 75 | if (isLegacyChatEndpoint) {
|
| 76 | chatEncoder = (sender, message) -> {
|
| 77 | JsonObject object = new JsonObject();
|
| 78 | object.addProperty("rule", ruleId);
|
| 79 | object.addProperty("server", serverId);
|
| 80 | object.addProperty("room", roomId);
|
| 81 | object.addProperty("player", sender.id().toString());
|
| 82 | object.addProperty("player_display_name", sender.name());
|
| 83 | object.addProperty("text", message);
|
| 84 | object.addProperty("language", "*");
|
| 85 | return object;
|
| 86 | };
|
| 87 | } else {
|
| 88 | String ruleIdStr = String.valueOf(ruleId);
|
| 89 | chatEncoder = (sender, message) -> {
|
| 90 | JsonObject object = new JsonObject();
|
| 91 | object.addProperty("rule_id", ruleIdStr);
|
| 92 | object.addProperty("category", serverId);
|
| 93 | object.addProperty("subcategory", roomId);
|
| 94 | object.addProperty("user_id", sender.id().toString());
|
| 95 | object.addProperty("user_display_name", sender.name());
|
| 96 | object.addProperty("text", message);
|
| 97 | object.addProperty("language", "*");
|
| 98 | return object;
|
| 99 | };
|
| 100 | }
|
| 101 |
|
| 102 | ServerTextFilter.IgnoreStrategy ignoreStrategy = ServerTextFilter.IgnoreStrategy.select(hashesToDrop);
|
| 103 | ExecutorService workerPool = createWorkerPool(maxConcurrentRequests);
|
| 104 | String encodedKey = Base64.getEncoder().encodeToString(key.getBytes(StandardCharsets.US_ASCII));
|
| 105 | return new LegacyTextFilter(
|
| 106 | chatEndpoint, chatEncoder, joinEndpoint, commonJoinOrLeaveEncoder, leaveEndpoint, commonJoinOrLeaveEncoder, encodedKey, ignoreStrategy, workerPool
|
| 107 | );
|
| 108 | } catch (Exception e) {
|
| 109 | LOGGER.warn("Failed to parse chat filter config {}", config, e);
|
| 110 | return null;
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public TextFilter createContext(final GameProfile gameProfile) {
|
| 116 | return new ServerTextFilter.PlayerContext(gameProfile) {
|
| 117 | @Override
|
| 118 | public void join() {
|
| 119 | LegacyTextFilter.this.processJoinOrLeave(this.profile, LegacyTextFilter.this.joinEndpoint, LegacyTextFilter.this.joinEncoder, this.streamExecutor);
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public void leave() {
|
| 124 | LegacyTextFilter.this.processJoinOrLeave(this.profile, LegacyTextFilter.this.leaveEndpoint, LegacyTextFilter.this.leaveEncoder, this.streamExecutor);
|
| 125 | }
|
| 126 | };
|
| 127 | }
|
| 128 |
|
| 129 | private void processJoinOrLeave(final GameProfile user, final URL endpoint, final LegacyTextFilter.JoinOrLeaveEncoder encoder, final Executor executor) {
|
| 130 | executor.execute(() -> {
|
| 131 | JsonObject object = encoder.encode(user);
|
| 132 |
|
| 133 | try {
|
| 134 | this.processRequest(object, endpoint);
|
| 135 | } catch (Exception e) {
|
| 136 | LOGGER.warn("Failed to send join/leave packet to {} for player {}", endpoint, user, e);
|
| 137 | }
|
| 138 | });
|
| 139 | }
|
| 140 |
|
| 141 | private void processRequest(final JsonObject payload, final URL url) throws IOException {
|
| 142 | HttpURLConnection connection = this.makeRequest(payload, url);
|
| 143 |
|
| 144 | try (InputStream is = connection.getInputStream()) {
|
| 145 | this.drainStream(is);
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | protected void setAuthorizationProperty(final HttpURLConnection connection) {
|
| 151 | connection.setRequestProperty("Authorization", "Basic " + this.authKey);
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | protected FilteredText filterText(final String message, final ServerTextFilter.IgnoreStrategy ignoreStrategy, final JsonObject result) {
|
| 156 | boolean response = GsonHelper.getAsBoolean(result, "response", false);
|
| 157 | if (response) {
|
| 158 | return FilteredText.passThrough(message);
|
| 159 | }
|
| 160 |
|
| 161 | String filteredMessage = GsonHelper.getAsString(result, "hashed", null);
|
| 162 | if (filteredMessage == null) {
|
| 163 | return FilteredText.fullyFiltered(message);
|
| 164 | }
|
| 165 |
|
| 166 | JsonArray removedChars = GsonHelper.getAsJsonArray(result, "hashes");
|
| 167 | FilterMask mask = this.parseMask(message, removedChars, ignoreStrategy);
|
| 168 | return new FilteredText(message, mask);
|
| 169 | }
|
| 170 |
|
| 171 | @FunctionalInterface
|
| 172 | private interface JoinOrLeaveEncoder {
|
| 173 | JsonObject encode(GameProfile profile);
|
| 174 | }
|
| 175 | }
|
| 176 |
|