-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathContribute.java
More file actions
89 lines (77 loc) · 3.46 KB
/
Copy pathContribute.java
File metadata and controls
89 lines (77 loc) · 3.46 KB
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
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Contribute {
private static final String IP_DELIMETER = ".";
private static final int HASH_LENGTH = 40;
private static final String HASH_ALGO = "SHA-1";
public static void main(String[] args) throws Exception {
System.out.println(isBanned("example.minecraft.com"));
}
public static String hashServer(String serverAddress) {
try {
MessageDigest digest = MessageDigest.getInstance(HASH_ALGO);
byte[] inputBytes = serverAddress.getBytes(StandardCharsets.UTF_8);
byte[] hashedBytes = digest.digest(inputBytes);
return String.format("%0" + (hashedBytes.length << 1) + "x", new BigInteger(1, hashedBytes));
} catch (NoSuchAlgorithmException noSuchAlgorithmException) {
//ignore - sha1 is available in all vm
return "";
}
}
public static boolean isBanned(String serverAddress) throws IOException {
File bannedList = new File("./blockedservers.txt");
List<String> lines = Files.readAllLines(bannedList.toPath());
lines.parallelStream()
.filter(line -> !line.startsWith(line))
.filter(line -> !line.trim().isEmpty())
.map(line -> line.substring(0, HASH_LENGTH))
.anyMatch(hash -> {
if (hash.equals(hashServer(serverAddress.trim().toLowerCase()))) {
return true;
}
//\\ is used for escaping regEx
List<String> components = new ArrayList<>(Arrays.asList(serverAddress.split("\\" + IP_DELIMETER)));
boolean isIp = components.size() == 4;
if (isIp) {
for (String component : components) {
try {
int number = Integer.parseInt(component);
if (number < 0 || number > 255) {
//ip range is from 0.0.0.0 to 255.255.255.255
isIp = false;
break;
}
} catch (NumberFormatException numberFormatException) {
//not a ip
isIp = false;
break;
}
}
}
while (components.size() > 1) {
components.remove(isIp ? (components.size() - 1) : 0);
String toTest;
if (isIp) {
//example: 0.0.0.*
toTest = String.join(IP_DELIMETER, components) + ".*";
} else {
//example: *.server.com
toTest = ("*." + String.join(IP_DELIMETER, components));
}
if (hash.equals(hashServer(toTest))) {
return true;
}
}
return false;
});
return false;
}
}