0

I have a simple IP Regex for my Minecraft Bukkit server that will listen on players talking in chat, and if the chat message contains a valid IPv4 address will replace it with my servers IP. Obviously the purpose is to stop people coming in and spamming to join their server then leave. Now the simple regex works nice, but people have already thought of this, and they'll do

'Join my server! 127 . 0 . 0 . 1 !!!'

My current code,

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerChat(PlayerChatEvent event) {
    String msg = event.getMessage();
    String ipRegex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
    String ipfixed = msg.replaceAll(ipRegex, "mc.blockie.net");
    event.setMessage(ipfixed);
}

Brainstorming for just a few minutes the only thing I really could come up with is to remove all the space out of the chat string, then do my regex check, but that wouldn't work, because obviously every chat message sent wouldn't have any spaces, that's honestly as far as what I have tried.

Necro.
  • 987
  • 6
  • 17
  • 29

2 Answers2

1

Try following regular expression:

"(\\d{1,3}\\s*\\.\\s*){3}\\d{1,3}"

used \\s* to match optional spaces.

falsetru
  • 357,413
  • 63
  • 732
  • 636
1

You could add some optional spaces in the middle:

String ipRegex = "\\d{1,3}(?:\\s*\\.\\s*\\d{1,3}){3}";

I'm not savvy when it comes to networking, but it seems like they could be bypassing this too by using stuff like 127-0-0-1, right?

Jerry
  • 70,495
  • 13
  • 100
  • 144
  • 1
    I'd suppose so yes. I pretty much knew from the start I can't make it 100% no IP's in the chat. But from my experience from what I've seen, people come in the server, spam it twice, and my spam protection temp bans them for 10 seconds, giving me enough time to ban them, so if I'm lucky they come in, spam my IP address twice, and then get kicked and banned :D – Necro. Aug 10 '13 at 05:59
  • 1
    @Necro. Oh, that makes things a little easier then! Good luck with banning those spammers ^^b – Jerry Aug 10 '13 at 06:01