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

Commit

Permalink
Changes -> Changes for the network update to DataPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
EinfacheSache committed Jul 27, 2023
1 parent eaf5265 commit f482bcc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 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.0-Beta</version>
<version>1.2.1-Beta</version>
<packaging>jar</packaging>

<name>NeoProtect</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ protected void initChannel(Channel channel) {
return;
}

if (instance.getCore().isSetup() && (instance.getCore().getRestAPI().getNeoServerIPs() == null || !instance.getCore().getRestAPI().getNeoServerIPs().toList().
contains(((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress()))) {
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;
Expand Down Expand Up @@ -191,4 +191,37 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}
});
}

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

long targetIntAddress = ipToDecimal(ipAddress);

int range = Integer.parseInt(ipRange.split("/")[1]);
String startIP = ipRange.split("/")[0];

long startIntAddress = ipToDecimal(startIP);

return targetIntAddress <= (startIntAddress + (long) (32 - range) * (32 - range)) && targetIntAddress >= startIntAddress;
}

public static long ipToDecimal(String ipAddress) throws IllegalArgumentException {
String[] parts = ipAddress.split("\\.");
if (parts.length != 4) {
return -1;
}

long decimal = 0;
for (int i = 0; i < 4; i++) {
int octet = Integer.parseInt(parts[i]);
if (octet < 0 || octet > 255) {
return -1;
}
decimal += (long) (octet * Math.pow(256, 3 - i));
}

return decimal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ protected void initChannel(Channel channel) {
return;
}

if (instance.getCore().getRestAPI().getNeoServerIPs() == null || !instance.getCore().getRestAPI().getNeoServerIPs().toList().
contains(((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress())) {
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("Player connected over IP (" + channel.remoteAddress() + ") doesn't match to Neo-IPs (warning)");
return;
}
Expand Down Expand Up @@ -189,6 +190,10 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
}

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

long targetIntAddress = ipToDecimal(ipAddress);

int range = Integer.parseInt(ipRange.split("/")[1]);
Expand All @@ -199,8 +204,6 @@ public static boolean isIPInRange(String ipAddress, String ipRange) {
return targetIntAddress <= (startIntAddress + (long) (32 - range) * (32 - range)) && targetIntAddress >= startIntAddress;
}



public static long ipToDecimal(String ipAddress) throws IllegalArgumentException {
String[] parts = ipAddress.split("\\.");
if (parts.length != 4) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ protected void initChannel(Channel channel) {
return;
}

if (instance.getCore().isSetup() && (instance.getCore().getRestAPI().getNeoServerIPs() == null || !instance.getCore().getRestAPI().getNeoServerIPs().toList().
contains(((InetSocketAddress) channel.remoteAddress()).getAddress().getHostAddress()))) {
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;
Expand Down Expand Up @@ -188,4 +188,37 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}
});
}

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

long targetIntAddress = ipToDecimal(ipAddress);

int range = Integer.parseInt(ipRange.split("/")[1]);
String startIP = ipRange.split("/")[0];

long startIntAddress = ipToDecimal(startIP);

return targetIntAddress <= (startIntAddress + (long) (32 - range) * (32 - range)) && targetIntAddress >= startIntAddress;
}

public static long ipToDecimal(String ipAddress) throws IllegalArgumentException {
String[] parts = ipAddress.split("\\.");
if (parts.length != 4) {
return -1;
}

long decimal = 0;
for (int i = 0; i < 4; i++) {
int octet = Integer.parseInt(parts[i]);
if (octet < 0 || octet > 255) {
return -1;
}
decimal += (long) (octet * Math.pow(256, 3 - i));
}

return decimal;
}
}

0 comments on commit f482bcc

Please sign in to comment.