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

Commit

Permalink
Code -> smale code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
EinfacheSache committed Jul 27, 2023
1 parent 1bc7630 commit eaf5265
Showing 1 changed file with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.List;
import java.util.logging.Level;

/**
* Represents a very tiny alternative to ProtocolLib.
Expand Down Expand Up @@ -50,17 +49,17 @@ public ProxyProtocol(NeoProtectSpigot instance) {
this.instance = instance;

try {
instance.getLogger().info("Proceeding with the server channel injection...");
instance.getCore().info("Proceeding with the server channel injection...");
registerChannelHandler();
} catch (IllegalArgumentException ex) {
// Damn you, late bind
instance.getLogger().info("Delaying server channel injection due to late bind.");
instance.getCore().info("Delaying server channel injection due to late bind.");

new BukkitRunnable() {
@Override
public void run() {
registerChannelHandler();
instance.getLogger().info("Late bind injection successful.");
instance.getCore().info("Late bind injection successful.");
}
}.runTask(instance);
}
Expand Down Expand Up @@ -95,7 +94,7 @@ protected void initChannel(Channel channel) {
instance.getCore().debug("Connecting finished");

} catch (Exception ex) {
instance.getLogger().log(Level.SEVERE, "Cannot inject incoming channel " + channel, ex);
instance.getCore().severe("Cannot inject incoming channel " + channel, ex);
}
}

Expand Down Expand Up @@ -139,7 +138,7 @@ private void registerChannelHandler() {
serverChannel.pipeline().addFirst(serverChannelHandler);
looking = false;

this.instance.getLogger().info("Found the server channel and added the handler. Injection successfully!");
this.instance.getCore().info("Found the server channel and added the handler. Injection successfully!");
}
}
}
Expand All @@ -165,8 +164,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
ctx.channel().close();

// Logging for the lovely server admins :)
instance.getLogger().warning("Error: The server was unable to set the IP address from the 'HAProxyMessage'. Therefore we closed the channel.");
exception.printStackTrace();
instance.getCore().severe("Error: The server was unable to set the IP address from the 'HAProxyMessage'. Therefore we closed the channel.", exception);
}
}
}
Expand All @@ -189,4 +187,35 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
ctx.fireChannelRead(msg);
}
}

public static boolean isIPInRange(String ipAddress, String ipRange) {
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 eaf5265

Please sign in to comment.