blob: d117fb1128c7214eb6b4ef117587a52cf8957cc5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
package moe.nea89;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.event.ProxyPingEvent;
import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class ServerlistCheck extends Plugin implements Listener {
private File hashFile;
private Set<Integer> oldHashes = new HashSet<>();
private final Set<Integer> newHashes = new HashSet<>();
@Override
public void onEnable() {
hashFile = new File(getDataFolder(), "iphashes.txt");
hashFile.getParentFile().mkdirs();
if (!hashFile.exists()) {
try {
hashFile.createNewFile();
} catch (IOException e) {
System.err.println("Could not create iphashes.txt file. This is probably a permission error");
}
}
try {
oldHashes = Files.readAllLines(hashFile.toPath()).stream().map(Integer::parseInt).collect(Collectors.toSet());
} catch (IOException e) {
System.err.println("Server list checker could not load hash file.");
}
getProxy().getPluginManager().registerListener(this, this);
getProxy().getPluginManager().registerCommand(this, new Command("uniqueserverlists", "serverlist.unique") {
@Override
public void execute(CommandSender sender, String[] args) {
sender.sendMessage(new TextComponent(String.format("There have been %d detected unique server list pings.", oldHashes.size() + newHashes.size())));
}
});
getProxy().getPluginManager().registerCommand(this, new Command("uniqueserverlistsave") {
@Override
public void execute(CommandSender sender, String[] args) {
save();
}
});
}
@Override
public void onDisable() {
save();
}
public void save() {
synchronized (newHashes) {
try (BufferedWriter w = Files.newBufferedWriter(hashFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.APPEND)) {
for (int h : newHashes) {
w.write(h + "\n");
}
} catch (IOException e) {
System.err.println("Server list checker failed to write hashes");
}
oldHashes.addAll(newHashes);
newHashes.clear();
}
}
public int condenseIP(InetAddress address) {
int i = 0;
for (byte x : address.getAddress()) {
i = i * 31 + x;
}
return i;
}
@EventHandler
public void onPing(ProxyPingEvent event) {
synchronized (newHashes) {
int ipHash = condenseIP(event.getConnection().getAddress().getAddress());
if (!oldHashes.contains(ipHash))
newHashes.add(ipHash);
}
}
}
|