Reputation: 987
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.
Upvotes: 0
Views: 474
Reputation: 368894
Try following regular expression:
"(\\d{1,3}\\s*\\.\\s*){3}\\d{1,3}"
used \\s*
to match optional spaces.
Upvotes: 1
Reputation: 71538
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?
Upvotes: 1