Advertisement
WalshyDev

TextFilterClient

Mar 15th, 2021 (edited)
1,836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.79 KB | None | 0 0
  1. package net.minecraft.server.network;
  2.  
  3. import com.google.common.collect.ImmutableList;
  4. import com.google.gson.JsonObject;
  5. import com.google.gson.internal.Streams;
  6. import com.google.gson.stream.JsonReader;
  7. import com.google.gson.stream.JsonWriter;
  8. import com.mojang.authlib.GameProfile;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.io.OutputStreamWriter;
  13. import java.net.HttpURLConnection;
  14. import java.net.URL;
  15. import java.nio.charset.StandardCharsets;
  16. import java.util.List;
  17. import java.util.Optional;
  18. import java.util.concurrent.CompletableFuture;
  19. import java.util.concurrent.Executor;
  20. import java.util.concurrent.ExecutorService;
  21. import java.util.concurrent.ThreadFactory;
  22. import java.util.concurrent.atomic.AtomicInteger;
  23. import java.util.function.Function;
  24. import java.util.function.Supplier;
  25. import net.minecraft.SharedConstants;
  26. import net.minecraft.Util;
  27. import net.minecraft.server.network.TextFilter;
  28. import net.minecraft.util.GsonHelper;
  29. import net.minecraft.util.thread.ProcessorMailbox;
  30. import org.apache.logging.log4j.LogManager;
  31. import org.apache.logging.log4j.Logger;
  32.  
  33. public class TextFilterClient implements AutoCloseable {
  34.    private static final Logger LOGGER = LogManager.getLogger();
  35.    private static final AtomicInteger WORKER_COUNT = new AtomicInteger(1);
  36.    private static final ThreadFactory THREAD_FACTORY = (debug0) -> {
  37.       Thread debug1 = new Thread(debug0);
  38.       debug1.setName("Chat-Filter-Worker-" + WORKER_COUNT.getAndIncrement());
  39.       return debug1;
  40.    };
  41.    private final URL chatEndpoint;
  42.    private final URL joinEndpoint;
  43.    private final URL leaveEndpoint;
  44.    private final String authKey;
  45.    private final int ruleId;
  46.    private final String serverId;
  47.    private final TextFilterClient.IgnoreStrategy chatIgnoreStrategy;
  48.    private final ExecutorService workerPool;
  49.  
  50.    private void processJoinOrLeave(GameProfile debug1, URL debug2, Executor debug3) {
  51.       JsonObject debug4 = new JsonObject();
  52.       debug4.addProperty("server", this.serverId);
  53.       debug4.addProperty("room", "Chat");
  54.       debug4.addProperty("user_id", debug1.getId().toString());
  55.       debug4.addProperty("user_display_name", debug1.getName());
  56.       debug3.execute(() -> {
  57.          try {
  58.             this.processRequest(debug4, debug2);
  59.          } catch (Exception var5) {
  60.             LOGGER.warn((String)"Failed to send join/leave packet to {} for player {}", (Object)debug2, debug1, var5);
  61.          }
  62.  
  63.       });
  64.    }
  65.  
  66.    private CompletableFuture requestMessageProcessing(GameProfile debug1, String debug2, TextFilterClient.IgnoreStrategy debug3, Executor debug4) {
  67.       if(debug2.isEmpty()) {
  68.          return CompletableFuture.completedFuture(Optional.of(""));
  69.       } else {
  70.          JsonObject debug5 = new JsonObject();
  71.          debug5.addProperty("rule", (Number)Integer.valueOf(this.ruleId));
  72.          debug5.addProperty("server", this.serverId);
  73.          debug5.addProperty("room", "Chat");
  74.          debug5.addProperty("player", debug1.getId().toString());
  75.          debug5.addProperty("player_display_name", debug1.getName());
  76.          debug5.addProperty("text", debug2);
  77.          return CompletableFuture.supplyAsync(() -> {
  78.             try {
  79.                JsonObject debug4 = this.processRequestResponse(debug5, this.chatEndpoint);
  80.                boolean debug5x = GsonHelper.getAsBoolean(debug4, "response", false);
  81.                if(debug5x) {
  82.                   return Optional.of(debug2);
  83.                } else {
  84.                   String debug6 = GsonHelper.getAsString(debug4, "hashed", (String)null);
  85.                   if(debug6 == null) {
  86.                      return Optional.empty();
  87.                   } else {
  88.                      int debug7 = GsonHelper.getAsJsonArray(debug4, "hashes").size();
  89.                      return debug3.shouldIgnore(debug6, debug7)?Optional.empty():Optional.of(debug6);
  90.                   }
  91.                }
  92.             } catch (Exception var8) {
  93.                LOGGER.warn((String)"Failed to validate message \'{}\'", (Object)debug2, (Object)var8);
  94.                return Optional.empty();
  95.             }
  96.          }, debug4);
  97.       }
  98.    }
  99.  
  100.    public void close() {
  101.       this.workerPool.shutdownNow();
  102.    }
  103.  
  104.    private void drainStream(InputStream debug1) throws IOException {
  105.       byte[] debug2 = new byte[1024];
  106.  
  107.       while(debug1.read(debug2) != -1) {
  108.          ;
  109.       }
  110.  
  111.    }
  112.  
  113.    private JsonObject processRequestResponse(JsonObject debug1, URL debug2) throws IOException {
  114.       HttpURLConnection debug3 = this.makeRequest(debug1, debug2);
  115.       InputStream debug4 = debug3.getInputStream();
  116.       Throwable var5 = null;
  117.  
  118.       JsonObject var6;
  119.       try {
  120.          if(debug3.getResponseCode() == 204) {
  121.             var6 = new JsonObject();
  122.             return var6;
  123.          }
  124.  
  125.          try {
  126.             var6 = Streams.parse(new JsonReader(new InputStreamReader(debug4))).getAsJsonObject();
  127.          } finally {
  128.             this.drainStream(debug4);
  129.          }
  130.       } catch (Throwable var23) {
  131.          var5 = var23;
  132.          throw var23;
  133.       } finally {
  134.          if(debug4 != null) {
  135.             if(var5 != null) {
  136.                try {
  137.                   debug4.close();
  138.                } catch (Throwable var21) {
  139.                   var5.addSuppressed(var21);
  140.                }
  141.             } else {
  142.                debug4.close();
  143.             }
  144.          }
  145.  
  146.       }
  147.  
  148.       return var6;
  149.    }
  150.  
  151.    private void processRequest(JsonObject debug1, URL debug2) throws IOException {
  152.       HttpURLConnection debug3 = this.makeRequest(debug1, debug2);
  153.       InputStream debug4 = debug3.getInputStream();
  154.       Throwable var5 = null;
  155.  
  156.       try {
  157.          this.drainStream(debug4);
  158.       } catch (Throwable var14) {
  159.          var5 = var14;
  160.          throw var14;
  161.       } finally {
  162.          if(debug4 != null) {
  163.             if(var5 != null) {
  164.                try {
  165.                   debug4.close();
  166.                } catch (Throwable var13) {
  167.                   var5.addSuppressed(var13);
  168.                }
  169.             } else {
  170.                debug4.close();
  171.             }
  172.          }
  173.  
  174.       }
  175.  
  176.    }
  177.  
  178.    private HttpURLConnection makeRequest(JsonObject debug1, URL debug2) throws IOException {
  179.       HttpURLConnection debug3 = (HttpURLConnection)debug2.openConnection();
  180.       debug3.setConnectTimeout(15000);
  181.       debug3.setReadTimeout(2000);
  182.       debug3.setUseCaches(false);
  183.       debug3.setDoOutput(true);
  184.       debug3.setDoInput(true);
  185.       debug3.setRequestMethod("POST");
  186.       debug3.setRequestProperty("Content-Type", "application/json; charset=utf-8");
  187.       debug3.setRequestProperty("Accept", "application/json");
  188.       debug3.setRequestProperty("Authorization", "Basic " + this.authKey);
  189.       debug3.setRequestProperty("User-Agent", "Minecraft server" + SharedConstants.getCurrentVersion().getName());
  190.       OutputStreamWriter debug4 = new OutputStreamWriter(debug3.getOutputStream(), StandardCharsets.UTF_8);
  191.       Throwable var5 = null;
  192.  
  193.       try {
  194.          JsonWriter debug6 = new JsonWriter(debug4);
  195.          Throwable var7 = null;
  196.  
  197.          try {
  198.             Streams.write(debug1, debug6);
  199.          } catch (Throwable var30) {
  200.             var7 = var30;
  201.             throw var30;
  202.          } finally {
  203.             if(debug6 != null) {
  204.                if(var7 != null) {
  205.                   try {
  206.                      debug6.close();
  207.                   } catch (Throwable var29) {
  208.                      var7.addSuppressed(var29);
  209.                   }
  210.                } else {
  211.                   debug6.close();
  212.                }
  213.             }
  214.  
  215.          }
  216.       } catch (Throwable var32) {
  217.          var5 = var32;
  218.          throw var32;
  219.       } finally {
  220.          if(debug4 != null) {
  221.             if(var5 != null) {
  222.                try {
  223.                   debug4.close();
  224.                } catch (Throwable var28) {
  225.                   var5.addSuppressed(var28);
  226.                }
  227.             } else {
  228.                debug4.close();
  229.             }
  230.          }
  231.  
  232.       }
  233.  
  234.       int debug41 = debug3.getResponseCode();
  235.       if(debug41 >= 200 && debug41 < 300) {
  236.          return debug3;
  237.       } else {
  238.          throw new TextFilterClient.RequestFailedException(debug41 + " " + debug3.getResponseMessage());
  239.       }
  240.    }
  241.  
  242.    public TextFilter createContext(GameProfile debug1) {
  243.       return new TextFilterClient.PlayerContext(debug1);
  244.    }
  245.  
  246.    @FunctionalInterface
  247.    public interface IgnoreStrategy {
  248.       TextFilterClient.IgnoreStrategy NEVER_IGNORE = (debug0, debug1) -> {
  249.          return false;
  250.       };
  251.       TextFilterClient.IgnoreStrategy IGNORE_FULLY_FILTERED = (debug0, debug1) -> {
  252.          return debug0.length() == debug1;
  253.       };
  254.  
  255.       boolean shouldIgnore(String var1, int var2);
  256.    }
  257.  
  258.    class PlayerContext implements TextFilter {
  259.       private final GameProfile profile;
  260.       private final Executor streamExecutor;
  261.  
  262.       private PlayerContext(GameProfile debug2) {
  263.          this.profile = debug2;
  264.          ProcessorMailbox debug3 = ProcessorMailbox.create(TextFilterClient.this.workerPool, "chat stream for " + debug2.getName());
  265.          this.streamExecutor = debug3::tell;
  266.       }
  267.  
  268.       public void join() {
  269.          TextFilterClient.this.processJoinOrLeave(this.profile, TextFilterClient.this.joinEndpoint, this.streamExecutor);
  270.       }
  271.  
  272.       public void leave() {
  273.          TextFilterClient.this.processJoinOrLeave(this.profile, TextFilterClient.this.leaveEndpoint, this.streamExecutor);
  274.       }
  275.  
  276.       public CompletableFuture processMessageBundle(List debug1) {
  277.          List debug2 = (List)debug1.stream().map((debug1) -> {
  278.             return TextFilterClient.this.requestMessageProcessing(this.profile, debug1, TextFilterClient.this.chatIgnoreStrategy, this.streamExecutor);
  279.          }).collect(ImmutableList.toImmutableList());
  280.          return Util.sequence(debug2).thenApply((debug0) -> {
  281.             return Optional.of(debug0.stream().map((debug0) -> {
  282.                return (String)debug0.orElse("");
  283.             }).collect(ImmutableList.toImmutableList()));
  284.          }).exceptionally((debug0) -> {
  285.             return Optional.empty();
  286.          });
  287.       }
  288.  
  289.       public CompletableFuture processStreamMessage(String debug1) {
  290.          return TextFilterClient.this.requestMessageProcessing(this.profile, debug1, TextFilterClient.this.chatIgnoreStrategy, this.streamExecutor);
  291.       }
  292.  
  293.       // $FF: synthetic method
  294.       PlayerContext(GameProfile debug2, Object debug3) {
  295.          this(debug2);
  296.       }
  297.    }
  298.  
  299.    public static class RequestFailedException extends RuntimeException {
  300.       private RequestFailedException(String debug1) {
  301.          super(debug1);
  302.       }
  303.  
  304.       // $FF: synthetic method
  305.       RequestFailedException(String debug1, Object debug2) {
  306.          this(debug1);
  307.       }
  308.    }
  309. }
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement