commit 45cb9eb63e652524b8ca9857733650be0dbcba0a Author: Ghost_chu <2908803755@qq.com> Date: Thu Apr 11 21:39:18 2019 +0800 push to git diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..f21f39a --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4b661a5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/AdvancedChatFilter.iml b/AdvancedChatFilter.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/AdvancedChatFilter.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..85e34be --- /dev/null +++ b/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + cn.dejavu + AdvancedChatFilter + 1.0-SNAPSHOT + + + bungeecord-repo + https://oss.sonatype.org/content/repositories/snapshots + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + + + + + + + + net.md-5 + bungeecord-api + 1.12-SNAPSHOT + jar + provided + + + net.md-5 + bungeecord-api + 1.12-SNAPSHOT + javadoc + provided + + + + + \ No newline at end of file diff --git a/src/main/java/cn/dejavu/AdvancedChatFilter/Main.java b/src/main/java/cn/dejavu/AdvancedChatFilter/Main.java new file mode 100644 index 0000000..72f74a9 --- /dev/null +++ b/src/main/java/cn/dejavu/AdvancedChatFilter/Main.java @@ -0,0 +1,104 @@ +package cn.dejavu.AdvancedChatFilter; + +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.config.ServerInfo; +import net.md_5.bungee.api.connection.Connection; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import net.md_5.bungee.api.event.ChatEvent; +import net.md_5.bungee.api.plugin.Listener; +import net.md_5.bungee.api.plugin.Plugin; +import net.md_5.bungee.config.Configuration; +import net.md_5.bungee.config.ConfigurationProvider; +import net.md_5.bungee.config.YamlConfiguration; +import net.md_5.bungee.event.EventHandler; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.logging.Level; + +public class Main extends Plugin implements Listener { + Configuration configuration; + static Main instance; + private List censorWords = new ArrayList<>(); + @Override + public void onEnable() { + instance = this; + getLogger().info("AdvancedChatFilter by Deja_Vu deleveop Team"); + getLogger().info("Loading config..."); + loadConfig(); + getProxy().getPluginManager().registerListener(this,this); + censorWords=configuration.getStringList("words"); + + } + @EventHandler + public void onChat(ChatEvent e){ + String msg = e.getMessage(); + ProxiedPlayer sender = (ProxiedPlayer) e.getSender(); + Connection receiver = e.getReceiver(); + //First check is or not contain censorWords. + for (String singleCensorWord : censorWords){ + boolean contain = msg.contains(singleCensorWord); + if(contain){ + if(msg.startsWith("/")) + return; //Ignore command + //Check is it contain censor username player in same server with detect player + String bungeeServer = sender.getServer().getInfo().getName(); + ServerInfo inServer = getProxy().getServerInfo(bungeeServer); + Collection playerInSameServer = inServer.getPlayers(); + + for (ProxiedPlayer inSameServerPlayer : playerInSameServer) { + if (inSameServerPlayer.getName().contains(singleCensorWord)) + return; //Maybe just call that player + } + + //All not, 盘他 + processCensorWord(sender,receiver,singleCensorWord); + e.setCancelled(true); + return; + } + } + + + } + + private void processCensorWord(ProxiedPlayer sender, Connection receiver, String singleCensorWord){ + sender.sendMessage(ChatMessageType.CHAT, new TextComponent(configuration.getString("msg.censor-word-detected"))); + new censorWebhook(sender,receiver,singleCensorWord); + } + + private void loadConfig(){ + if (!getDataFolder().exists()) + getDataFolder().mkdir(); + File file = new File(getDataFolder(), "config.yml"); + + if (!file.exists()) { + try (InputStream in = getResourceAsStream("config.yml")) { + Files.copy(in, file.toPath()); //Create default config file. + } catch (IOException e) { + e.printStackTrace(); + } + } + + try { + configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(new File(getDataFolder(), "config.yml")); + }catch (IOException e){ + getLogger().log(Level.SEVERE,"Failed to load configuration."); + e.printStackTrace(); + } + } + + private void saveConfig(){ + try { + ConfigurationProvider.getProvider(YamlConfiguration.class).save(configuration, new File(getDataFolder(), "config.yml")); + }catch (IOException e){ + getLogger().log(Level.SEVERE,"Failed to save configuration."); + e.printStackTrace(); + } + } +} diff --git a/src/main/java/cn/dejavu/AdvancedChatFilter/censorWebhook.java b/src/main/java/cn/dejavu/AdvancedChatFilter/censorWebhook.java new file mode 100644 index 0000000..346e9ff --- /dev/null +++ b/src/main/java/cn/dejavu/AdvancedChatFilter/censorWebhook.java @@ -0,0 +1,67 @@ +package cn.dejavu.AdvancedChatFilter; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import net.md_5.bungee.api.connection.Connection; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.io.PrintWriter; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.logging.Level; + +public class censorWebhook { + private ExecutorService executor = Executors.newFixedThreadPool(1); + public censorWebhook (ProxiedPlayer sender, Connection receiver, String singleCensorWord){ + submit(sender.getUniqueId(),sender.getName(),System.currentTimeMillis(),receiver.getAddress().toString(),singleCensorWord); + } + + private void submit(UUID uuid, String username, long timeStamp, String receiverIPAddress, String censorWord){ + executor.submit(new Runnable() { + @Override + public void run() { + try{ + URL url = new URL(Main.instance.configuration.getString("submit.url")); + URLConnection conn = url.openConnection(); + conn.setRequestProperty("accept", "*/*"); + conn.setRequestProperty("connection", "Keep-Alive"); + conn.setRequestProperty("user-agent","AdvancedChatFilter"); + conn.setDoOutput(true); + conn.setDoInput(true); + PrintWriter out = new PrintWriter(conn.getOutputStream()); + Map map = new HashMap<>(); + SubmitData data = new SubmitData(Main.instance.configuration.getString("submit.key"),uuid.toString(),username,String.valueOf(username),receiverIPAddress,censorWord); + Gson gson = new Gson(); + gson.toJson(data); + out.print(data); + out.flush(); + out.close(); + }catch (Exception ex){ + Main.instance.getLogger().log(Level.WARNING,"Failed to submit censor data."); + ex.printStackTrace(); + } + + + } + }); + + } +} +class SubmitData{ + String key;String uuid; String username; String time; String receiverip; String censorword; + public SubmitData(String key,String uuid, String username, String time, String receiverip, String censorword){ + this.key=key; + this.uuid=uuid; + this.username=username; + this.time=time; + this.receiverip=receiverip; + this.censorword=censorword; + } +} \ No newline at end of file diff --git a/src/main/resources/bungee.yml b/src/main/resources/bungee.yml new file mode 100644 index 0000000..e5827f2 --- /dev/null +++ b/src/main/resources/bungee.yml @@ -0,0 +1,4 @@ +name: AdvancedChatFilter +main: cn.dejavu.AdvancedChatFilter.Main +version: 1.0 +author: Ghost_chu \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..1ef6e40 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,10 @@ +msg: + censor-word-detected: "§c[Deja Vu] 您发出的聊天信息包含敏感词汇,因此您的信息已被拒收并记录在案,管理员将会对您的聊天信息进行审查" +submit: + url: "https://api.deja_vu.cn/censor/submit.php" + #服务器端应拒绝错误Key的数据提交 + key: "@!#$%$^()(*&^%$#@!#$%^&*(*&^%$#ADF_DEJA-VU@#$%^&*&^%$#@!@#$%^&*(*&^%$#$%^&*(" +word: + - "敏感词1" + - "敏感词2" + - "敏感词3" \ No newline at end of file