Skip to content

Commit 902e228

Browse files
committed
Prevent connections to local addresses
1 parent 485c42c commit 902e228

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/main/java/net/lenni0451/miniconnect/server/LobbyServerHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import net.raphimc.netminecraft.packet.Packet;
1515
import org.apache.commons.lang3.tuple.Pair;
1616

17+
import java.nio.channels.ClosedChannelException;
1718
import java.util.concurrent.TimeUnit;
1819

1920
public class LobbyServerHandler extends SimpleChannelInboundHandler<Packet> {
@@ -70,7 +71,8 @@ protected void channelRead0(ChannelHandlerContext ctx, Packet packet) {
7071
}
7172

7273
@Override
73-
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
74+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
75+
if (cause instanceof ClosedChannelException) return;
7476
cause.printStackTrace();
7577
ctx.close();
7678
}

src/main/java/net/lenni0451/miniconnect/server/states/play/screen/impl/MainScreen.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
import net.lenni0451.miniconnect.server.states.play.screen.Screen;
1818
import net.lenni0451.miniconnect.server.states.play.screen.ScreenHandler;
1919
import net.lenni0451.miniconnect.utils.ChannelUtils;
20+
import net.lenni0451.miniconnect.utils.InetUtils;
2021
import net.raphimc.minecraftauth.MinecraftAuth;
2122
import net.raphimc.minecraftauth.step.msa.StepMsaDeviceCode;
2223
import net.raphimc.netminecraft.packet.impl.play.S2CPlayDisconnectPacket;
2324
import net.raphimc.viaproxy.saves.impl.accounts.MicrosoftAccount;
2425

26+
import java.net.InetAddress;
2527
import java.util.concurrent.TimeoutException;
2628

2729
import static net.lenni0451.miniconnect.server.states.play.screen.ItemBuilder.item;
@@ -52,7 +54,8 @@ public void init(ScreenHandler screenHandler, ItemList itemList) {
5254
playerConfig.chatListener = s -> {
5355
try {
5456
HostAndPort hostAndPort = HostAndPort.fromString(s);
55-
if (hostAndPort.getHost().isBlank()) throw new IllegalStateException();
57+
if (hostAndPort.getHost().isBlank()) throw new IllegalArgumentException();
58+
if (InetUtils.isLocal(InetAddress.getByName(hostAndPort.getHost()))) throw new IllegalArgumentException();
5659
playerConfig.serverAddress = hostAndPort.getHost();
5760
playerConfig.serverPort = hostAndPort.getPortOrDefault(-1);
5861
} catch (Throwable t) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.lenni0451.miniconnect.utils;
2+
3+
import java.net.InetAddress;
4+
5+
public class InetUtils {
6+
7+
public static boolean isLocal(final InetAddress address) {
8+
if (address.isAnyLocalAddress() || address.isLoopbackAddress()) return true;
9+
byte[] addressBytes = address.getAddress();
10+
11+
if (addressBytes.length == 4) { // Check for IPv4 local address ranges
12+
if (addressBytes[0] == 10) return true; // 10.0.0.0/8
13+
if (addressBytes[0] == (byte) 172 && addressBytes[1] >= 16 && addressBytes[1] <= 31) return true; // 172.16.0.0/12
14+
if (addressBytes[0] == (byte) 192 && addressBytes[1] == (byte) 168) return true; // 192.168.0.0/16
15+
}
16+
if (addressBytes.length == 16) { // Check for IPv6 local address ranges
17+
return (addressBytes[0] == (byte) 0xfe && (addressBytes[1] & (byte) 0xc0) == (byte) 0x80); // fe80::/10
18+
}
19+
return false;
20+
}
21+
22+
}

0 commit comments

Comments
 (0)