Solved Get the IP they connect through?

Discussion in 'Plugin Development' started by _Die, Feb 21, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    _Die

    Hey, so I am changing my server's IP to a domain and I was wondering if I could code a plugin were when the connect through the old IP it kicks them. I know kicking and all that it's just I don't know how to find the IP they connect though. Thanks in advance :p
     
  2. Offline

    flyguy23ndm

    Hello, this is my first time answering a question on a forum so I hope it goes well. I think this idea is interesting and I think I could understand what you meant (it was worded a bit oddly).
    So, to find the ip address that a player connected from is relatively simple, but finding the ip address they connected to (as to distantiatefrom different host name) is slightly more difficult. But Bukkit has a way to handle this. Utilizing the event PlayerLoginEvnet (look it up in the javadocs here https://hub.spigotmc.org/javadocs/bukkit/) you can find the ip address a player tried to connect to. I have written a simple plugin to track these event and print out the data. Here is the source:

    Code:
    public class Test extends JavaPlugin implements Listener
    {
        @Override
        public void onEnable()
        {
            //setup the listener, this is straightforward and for simplicity sake, the main class is the listener.
            getServer().getPluginManager().registerEvents(this, this);
        }
    
        @Override
        public void onDisable()
        {
            //Remove the listener, this is good for the saving memory
            PlayerJoinEvent.getHandlerList().unregister((Listener) this);
        }
    
        @EventHandler
        public void onLogin(PlayerLoginEvent e)
        {   
            //report the event to the server log
            String msg = e.getPlayer().getName() + " has joined to " + e.getHostname();
            Bukkit.getLogger().log(Level.INFO, msg);
        }
    }
    This is how the output appeared (at least the relevant information with my ip and host name censored):
    This was a connection with my public ip inputted into the server address. It shows that I tried to connect to a list of numbers that is my ip then a port. (censored ip)
    [21:29:58] [Server thread/INFO]: flyguy23ndm has joined to xxx.xx.xxx.xxx:2002

    This was a connection using my purchased domain name as the inputted server address. It shows that I treid to connect to a text address as opposed to a list of numbers. (censored domain name)
    [21:30:10] [Server thread/INFO]: flyguy23ndm has joined to xxxxx.com:2002
    I'm sure that through some string parsing and a blacklist of non-wanted domain names you could setup what you want. The code could even be something as simple as this:

    Code:
    if(blacklistedDomains.contains(e.getHostname()))
        e.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Don't use this host name anymore, use (insert host name here)");
    Hope this solves your problem ;).
     
    Last edited: Feb 21, 2015
  3. Offline

    _Die

    @flyguy23ndm Thanks, that was very helpful :) (Nice first job!)
     
Thread Status:
Not open for further replies.

Share This Page