Skip to content
This repository has been archived by the owner on Oct 14, 2023. It is now read-only.

Commit

Permalink
Changes -> Added /np directConnectWhitelist to allow connections via …
Browse files Browse the repository at this point in the history
…IP + language changes
  • Loading branch information
EinfacheSache committed Jul 29, 2023
1 parent 70af3a9 commit f6a3c0b
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 87 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.cubeattack</groupId>
<artifactId>neoprotect</artifactId>
<version>1.2.1-Beta</version>
<version>1.2.2-Beta</version>
<packaging>jar</packaging>

<name>NeoProtect</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void onTabComplete(TabCompleteEvent event) {
tabListOne.add("setup");

if (instance.getCore().isSetup()) {
tabListOne.add("directConnectWhitelist");
tabListOne.add("setgameshield");
tabListOne.add("setbackend");
tabListOne.add("analytics");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,34 @@ protected void initChannel(Channel channel) {

instance.getCore().debug("Open channel (" + channel.remoteAddress().toString() + ")");

AtomicReference<InetSocketAddress> inetAddress = new AtomicReference<>();

initChannelMethod.invoke(bungeeChannelInitializer, channel);

if (channel.localAddress().toString().startsWith("local:")) {
instance.getCore().debug("Detected bedrock player (return)");
return;
}

if (instance.getCore().isSetup() && (instance.getCore().getRestAPI().getNeoServerIPs() == null ||
instance.getCore().getRestAPI().getNeoServerIPs().toList().stream().noneMatch(ipRange -> isIPInRange((String) ipRange, ((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress())))) {
channel.close();
instance.getCore().debug("Close connection IP (" + channel.remoteAddress() + ") doesn't match to Neo-IPs (close / return)");
return;
}
AtomicReference<InetSocketAddress> playerAddress = new AtomicReference<>();
String sourceAddress = ((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress();

instance.getCore().debug("Adding Handler...");
if (!instance.getCore().getDirectConnectWhitelist().contains(sourceAddress)) {
if (instance.getCore().isSetup() && (instance.getCore().getRestAPI().getNeoServerIPs() == null ||
instance.getCore().getRestAPI().getNeoServerIPs().toList().stream().noneMatch(ipRange -> isIPInRange((String) ipRange, sourceAddress)))) {
channel.close();
instance.getCore().debug("Close connection IP (" + channel.remoteAddress() + ") doesn't match to Neo-IPs (close / return)");
return;
}

if (instance.getCore().isSetup() && Config.isProxyProtocol()) {
instance.getCore().debug("Plugin is setup & ProxyProtocol is on (addProxyProtocolHandler)");
addProxyProtocolHandler(channel, inetAddress);
}
instance.getCore().debug("Adding handler...");

if (instance.getCore().isSetup() && Config.isProxyProtocol()) {
addProxyProtocolHandler(channel, playerAddress);
instance.getCore().debug("Plugin is setup & ProxyProtocol is on (Added proxyProtocolHandler)");
}

addKeepAlivePacketHandler(channel, inetAddress, instance);
addKeepAlivePacketHandler(channel, playerAddress, instance);
instance.getCore().debug("Added KeepAlivePacketHandler");
}

instance.getCore().debug("Connecting finished");

Expand Down Expand Up @@ -193,7 +197,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}

public static boolean isIPInRange(String ipAddress, String ipRange) {
if(!ipRange.contains("/")){
if (!ipRange.contains("/")) {
ipRange = ipRange + "/32";
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/cubeattack/neoprotect/core/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Core {
private final NeoProtectPlugin plugin;
private final Localization localization;
private final List<Object> PLAYER_IN_SETUP = new ArrayList<>();
private final List<String> directConnectWhitelist= new ArrayList<>();
private final ConcurrentHashMap<KeepAliveResponseKey, Long> pingMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Long, Timestamp> timestampsMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, ArrayList<DebugPingResponse>> debugPingResponses = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -107,6 +108,10 @@ public List<Object> getPlayerInSetup() {
return PLAYER_IN_SETUP;
}

public List<String> getDirectConnectWhitelist() {
return directConnectWhitelist;
}

public ConcurrentHashMap<KeepAliveResponseKey, Long> getPingMap() {
return pingMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,21 @@ private void command(ExecutorBuilder executorBuilder) {
break;
}

case "directconnectwhitelist": {
directConnectWhitelist(args);
break;
}

case "toggle": {
toggle(args);
break;
}

case "analytics": {
analytics();
break;
}

case "whitelist": case "blacklist": {
firewall(args);
break;
Expand All @@ -94,11 +104,6 @@ private void command(ExecutorBuilder executorBuilder) {
break;
}

case "analytics": {
analytics();
break;
}

case "setgameshield": {
if (args.length == 1) {
gameshieldSelector();
Expand Down Expand Up @@ -143,6 +148,15 @@ private void iPanic(String[] args) {
}
}

private void directConnectWhitelist(String[] args) {
if (args.length == 2) {
instance.getCore().getDirectConnectWhitelist().add(args[1]);
instance.sendMessage(sender, localization.get("command.directconnectwhitelist", args[1]));
} else {
instance.sendMessage(sender, localization.get("usage.directconnectwhitelist"));
}
}

private void toggle(String[] args) {
if (args.length != 2) {
instance.sendMessage(sender, localization.get("usage.toggle"));
Expand All @@ -169,6 +183,40 @@ private void toggle(String[] args) {
}
}

private void analytics() {
instance.sendMessage(sender, "§7§l--------- §bAnalytics §7§l---------");
JSONObject analytics = instance.getCore().getRestAPI().getAnalytics();
instance.getCore().getRestAPI().getAnalytics().keySet().forEach(ak -> {
if (ak.equals("bandwidth")) {
return;
}

if (ak.equals("traffic")) {
instance.sendMessage(sender, ak.replace("traffic", "bandwidth") + ": " +
new DecimalFormat("#.####").format((float) analytics.getInt(ak) * 8 / (1000 * 1000)) + " mbit/s");
JSONObject traffic = instance.getCore().getRestAPI().getTraffic();

AtomicReference<String> trafficUsed = new AtomicReference<>();
AtomicReference<String> trafficAvailable = new AtomicReference<>();
traffic.keySet().forEach(bk -> {
if (bk.equals("used")) {
trafficUsed.set(traffic.getFloat(bk) / (1000 * 1000 * 1000) + " gb");
}
if (bk.equals("available")) {
trafficAvailable.set(String.valueOf(traffic.getLong(bk)).equals("999999999") ? "unlimited" : traffic.getLong(bk) + " gb");
}
});
instance.sendMessage(sender, "bandwidth used" + ": " + trafficUsed.get() + "/" + trafficAvailable.get());

return;
}

instance.sendMessage(sender, ak
.replace("onlinePlayers", "online players")
.replace("cps", "connections/s") + ": " + analytics.get(ak));
});
}

private void firewall(String[] args) {
if (args.length == 1) {
instance.sendMessage(sender, "§7§l----- §bFirewall (" + args[0].toUpperCase() + ")§7§l -----");
Expand Down Expand Up @@ -211,40 +259,6 @@ private void firewall(String[] args) {
}
}

private void analytics() {
instance.sendMessage(sender, "§7§l--------- §bAnalytics §7§l---------");
JSONObject analytics = instance.getCore().getRestAPI().getAnalytics();
instance.getCore().getRestAPI().getAnalytics().keySet().forEach(ak -> {
if (ak.equals("bandwidth")) {
return;
}

if (ak.equals("traffic")) {
instance.sendMessage(sender, ak.replace("traffic", "bandwidth") + ": " +
new DecimalFormat("#.####").format((float) analytics.getInt(ak) * 8 / (1000 * 1000)) + " mbit/s");
JSONObject traffic = instance.getCore().getRestAPI().getTraffic();

AtomicReference<String> trafficUsed = new AtomicReference<>();
AtomicReference<String> trafficAvailable = new AtomicReference<>();
traffic.keySet().forEach(bk -> {
if (bk.equals("used")) {
trafficUsed.set(traffic.getFloat(bk) / (1000 * 1000 * 1000) + " gb");
}
if (bk.equals("available")) {
trafficAvailable.set(String.valueOf(traffic.getLong(bk)).equals("999999999") ? "unlimited" : traffic.getLong(bk) + " gb");
}
});
instance.sendMessage(sender, "bandwidth used" + ": " + trafficUsed.get() + "/" + trafficAvailable.get());

return;
}

instance.sendMessage(sender, ak
.replace("onlinePlayers", "online players")
.replace("cps", "connections/s") + ": " + analytics.get(ak));
});
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private void debugTool(String[] args) {

Expand Down Expand Up @@ -381,7 +395,7 @@ public void run() {
configuration.save(file);
instance.getCore().getDebugPingResponses().clear();
instance.sendMessage(sender, localization.get("debug.finished.first") + " (took " + (System.currentTimeMillis() - startTime) + "ms)");
instance.sendMessage(sender, localization.get("debug.finished.second") + file.getAbsolutePath() + " " + localization.get("utils.copy"), "COPY_TO_CLIPBOARD", file.getAbsolutePath(), null, null);
instance.sendMessage(sender, localization.get("debug.finished.second") + file.getAbsolutePath() + localization.get("utils.copy"), "COPY_TO_CLIPBOARD", file.getAbsolutePath(), null, null);
instance.getCore().setDebugRunning(false);
} catch (Exception ex) {
instance.getCore().severe(ex.getMessage(), ex);
Expand Down Expand Up @@ -455,6 +469,7 @@ private void showHelp() {
instance.sendMessage(sender, " - /np whitelist (add/remove) (ip)");
instance.sendMessage(sender, " - /np blacklist (add/remove) (ip)");
instance.sendMessage(sender, " - /np debugTool (cancel / amount)");
instance.sendMessage(sender, " - /np directConnectWhitelist (ip)");
instance.sendMessage(sender, " - /np setgameshield");
instance.sendMessage(sender, " - /np setbackend");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Comman
list.add("setup");

if (instance.getCore().isSetup()) {
list.add("directConnectWhitelist");
list.add("setgameshield");
list.add("setbackend");
list.add("analytics");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ private void createServerChannelHandler() {
@Override
protected void initChannel(Channel channel) {

if (!Config.isProxyProtocol() | !instance.getCore().isSetup()) {
instance.getCore().debug("Plugin is not setup / ProxyProtocol is off (return)");
if (!Config.isProxyProtocol() | !instance.getCore().isSetup() | instance.getCore().getDirectConnectWhitelist().contains(((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress())) {
instance.getCore().debug("Plugin is not setup / ProxyProtocol is off / Player is on DirectConnectWhitelist (return)");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public CompletableFuture<List<String>> suggestAsync(Invocation invocation) {
list.add("setup");

if (instance.getCore().isSetup()) {
list.add("directConnectWhitelist");
list.add("setgameshield");
list.add("setbackend");
list.add("analytics");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ protected void initChannel(Channel channel) {

instance.getCore().debug("Open channel (" + channel.remoteAddress().toString() + ")");

AtomicReference<InetSocketAddress> inetAddress = new AtomicReference<>();

initChannelMethod.getMethod().setAccessible(true);
initChannelMethod.invoke(oldInitializer, channel);

Expand All @@ -63,21 +61,27 @@ protected void initChannel(Channel channel) {
return;
}

if (instance.getCore().isSetup() && (instance.getCore().getRestAPI().getNeoServerIPs() == null ||
instance.getCore().getRestAPI().getNeoServerIPs().toList().stream().noneMatch(ipRange -> isIPInRange((String) ipRange, ((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress())))) {
channel.close();
instance.getCore().debug("Close connection IP (" + channel.remoteAddress() + ") doesn't match to Neo-IPs (close / return)");
return;
}
AtomicReference<InetSocketAddress> playerAddress = new AtomicReference<>();
String sourceAddress = ((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress();

instance.getCore().debug("Adding Handler...");
if (!instance.getCore().getDirectConnectWhitelist().contains(sourceAddress)) {
if (instance.getCore().isSetup() && (instance.getCore().getRestAPI().getNeoServerIPs() == null ||
instance.getCore().getRestAPI().getNeoServerIPs().toList().stream().noneMatch(ipRange -> isIPInRange((String) ipRange, sourceAddress)))) {
channel.close();
instance.getCore().debug("Close connection IP (" + channel.remoteAddress() + ") doesn't match to Neo-IPs (close / return)");
return;
}

if (instance.getCore().isSetup() && Config.isProxyProtocol()) {
instance.getCore().debug("Plugin is setup & ProxyProtocol is on (addProxyProtocolHandler)");
addProxyProtocolHandler(channel, inetAddress);
}
instance.getCore().debug("Adding handler...");

if (instance.getCore().isSetup() && Config.isProxyProtocol()) {
addProxyProtocolHandler(channel, playerAddress);
instance.getCore().debug("Plugin is setup & ProxyProtocol is on (Added proxyProtocolHandler)");
}

addKeepAlivePacketHandler(channel, inetAddress, velocityServer, instance);
addKeepAlivePacketHandler(channel, playerAddress, velocityServer, instance);
instance.getCore().debug("Added KeepAlivePacketHandler");
}

instance.getCore().debug("Connecting finished");

Expand Down Expand Up @@ -190,7 +194,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}

public static boolean isIPInRange(String ipAddress, String ipRange) {
if(!ipRange.contains("/")){
if (!ipRange.contains("/")) {
ipRange = ipRange + "/32";
}

Expand Down
18 changes: 10 additions & 8 deletions src/main/resources/language_de.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apikey.invalid=§cDer eingegebene API-KEY ist ungültig
apikey.valid=§aDer eingegebene API-KEY ist gültig
apikey.find5Den API-KEY finden Sie in Ihrem Panel \n§6https://panel.neoprotect.net/profile
apikey.findbDen API-KEY finden Sie in Ihrem Panel \n§7https://panel.neoprotect.net/profile

setup.required.first=NeoProtectPlugin wurde nicht vollständig konfiguriert
setup.required.second=Geben zum Konfigurieren im Chat '/np setup' ein
Expand All @@ -13,29 +13,31 @@ console.command=Sie können diesen Befehl nicht über die Konsole ausführen. Bi
command.setup=Geben Sie nun den API-KEY im Chat ein
command.ipanic=Der Panikmodus wurde {0}
command.toggle={0} wurde {1}
command.firewall.notfound=§cIP '{0}' ist nicht in der Firewall ({1})
command.firewall.ip-invalid=§cIP '{0}' ist nicht gültig
command.firewall.notfound=§cIP ''{0}'' ist nicht in der Firewall ({1})
command.firewall.ip-invalid=§cIP ''{0}''ist nicht gültig
command.directconnectwhitelist=§cIP ''{0}'' wurde zur DirectConnectWhitelist hinzugefügt

usage.debug=Bitte geben Sie /np debugTool (cancel / zahl) ein
usage.ipanic=Bitte geben Sie /np ipanic ein
usage.toggle=Bitte geben Sie /np toggle (Option) ein
usage.firewall=Bitte geben Sie /np (whitelist/blacklist) (add/remove) (ip)
usage.setbackend=Bitte geben Sie /np setbackend ein
usage.setgameshield=Bitte geben Sie /np setgameshield ein
usage.directconnectwhitelist=Bitte geben Sie /np directConnectWhitelist (ip)

select.gameshield=Wählen Sie nun das Gameshield aus
select.backend=Wählen Sie nun das Backend aus

set.backend=Backend ''{0}'' wurde festgelegt
set.gameshield=Gameshield wurde ''{0}'' wurde festgelegt

invalid.backend=§cDas Backend wurde nicht gefunden
invalid.gameshield=§cDas Gameshield wurde nicht gefunden
invalid.backend=§cDas Backend ''{0}'' wurde nicht gefunden
invalid.gameshield=§cDas Gameshield ''{0}'' wurde nicht gefunden

hover.gameshield5Gameshield {0} \n§6ID {1}
hover.backend5Backend {0}:{1} \n§6ID {2}
hover.gameshieldbGameshield {0} \n§7ID {1}
hover.backendbBackend {0}:{1} \n§7ID {2}

utils.copy=(kopieren)
utils.copy=§7 (kopieren)
utils.click=§7 (klick)
utils.activated=§aaktiviert
utils.deactivated=§cdeaktiviert
Expand Down
Loading

0 comments on commit f6a3c0b

Please sign in to comment.