Thread: [508] Anti-flood protection.

Page 1 of 7 123 ... LastLast
Results 1 to 10 of 67
  1. #1 [508] Anti-flood protection. 
    Registered Member
    Gravediggah's Avatar
    Join Date
    Nov 2008
    Age
    34
    Posts
    202
    Thanks given
    0
    Thanks received
    5
    Rep Power
    265
    Author:
    Gravediggah

    Tested base:
    Palidino

    Credits:
    -Me, 100%

    You can use this to protect your server from server flooders/crashers like SYI.
    Change imports if you're not using Palidino's base.


    Step 1. Create a new file called Protect.java in package palidino76.rs2.net

    Code:
    package palidino76.rs2.net;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import palidino76.rs2.Engine;
    import palidino76.rs2.Server;
    import palidino76.rs2.players.Player;
    import palidino76.rs2.util.Misc;
    
    /**
     * Server anti-flood protection.
     *
     * @author Gravediggah
     */
    public class Protect {
    
        /**
         * Maximum amount of connections per host.
         */
        static final int MAX_CONNECTIONS = 6;
        /**
         * Minimum length of a pattern to be handled as a pattern.
         * For example 'SYIpker' has a length of 7 characters.
         */
        static final int SIZE_PATTERN = 7;
        /**
         * Maximum amount of players with the same username
         * pattern are allowed. Note: A pattern is seen as a
         * pattern when the length of a pattern is larger than
         * the length defined in SIZE_PATTERN;
         */
        static final int MAX_PATTERN_COUNT = 6;
        /**
         * If set to true, player's host will be checked.
         */
        static final boolean CHECK_HOST = true;
        /**
         * If set to true, player will be checked for patterns.
         */
        static final boolean CHECK_PATTERN = true;
        /**
         * If set to true, actions related to bans will be logged.
         */
        static final boolean LOG_BANS = true;
    
        /**
         * Performs all enabled checks.
         *
         * @param p The player to check.
         */
        public static void checkPlayer(Player p) {
            if (p != null) {
                if (CHECK_HOST) {
                    checkHost(p);
                }
    
                if (CHECK_PATTERN) {
                    checkPattern(p);
                }
            }
    
        }
    
        /**
         * Checks if the Player's host exceeds the maximum
         * amount of connections per host and, if exceeded,
         * IP-ban the player's host.
         *
         * @param p The player to check.
         */
        public static void checkHost(Player p) {
            if (p == null) {
                return;
            }
    
            String cHost = getHost(p);
            int cCount = 0;
    
            for (Player o : Engine.players) {
                if (o != null) {
                    if (getHost(o).equals(cHost)) {
                        cCount++;
                    }
                }
            }
    
            if (cCount >= MAX_CONNECTIONS && cHost.length() > 0) {
                sendLog("Too much connections: " + cHost + " (" + cCount + " connections)");
                killByHost(cHost);
            }
        }
    
        /**
         * This method is used to process an ip-ban.
         * 
         * @param host The host to ban.
         */
        public static void banHost(String host) {
            if (host.length() > 0) {
                if (!isBannedHost(host)) {
                    addBan(host, true);
                }
                killByHost(host);
            }
        }
    
        /**
         * This method is used to process a single-user ban.
         *
         * @param p The player to ban.
         */
        public static void banUser(Player p) {
            if (p != null) {
                if (p.username.length() > 0) {
                    if (!isBannedUser(p.username)) {
                        addBan(p.username, false);
                    }
                    p.disconnected[0] = true;
                }
            }
        }
    
        /**
         * Disconnects all players with the given host.
         *
         * @param host The host to disconnect.
         */
        public static void killByHost(String host) {
            for (Player o : Engine.players) {
                if (o != null) {
                    if (getHost(o).equals(host)) {
                        if (!isBannedUser(o.username)) {
                            banUser(o);
                        }
                        o.disconnected[0] = true;
                    }
                }
            }
        }
    
        /**
         * Checks the Player's username for a pattern, compared
         * to all other Players' usernames. IP-ban all Players
         * with this pattern, if the pattern applies to the defined
         * restrictions: SIZE_PATTERN / MAX_PATTERN_COUNT.
         *
         * @param p The player to check.
         */
        public static void checkPattern(Player p) {
            if (p == null) {
                return;
            }
    
            String pattern = p.username;
    
            for (Player o : Engine.players) {
                if (o != null) {
                    String foundPattern = equString(pattern, o.username);
                    if (foundPattern.length() >= SIZE_PATTERN) {
                        pattern = foundPattern;
                    }
                }
            }
    
            if (pattern.length() >= SIZE_PATTERN) {
                if (pattern.length() > SIZE_PATTERN) {
                    pattern = pattern.substring(0, SIZE_PATTERN).toLowerCase();
                }
                if (countPattern(pattern) >= MAX_PATTERN_COUNT) {
                    sendLog("Banning pattern: " + pattern);
                    for (Player o : Engine.players) {
                        if (o != null) {
                            if (o.username.toLowerCase().startsWith(pattern)) {
                                //Ban all hosts with this pattern, possibility of proxies.
                                banHost(getHost(o));
                            }
                        }
                    }
                }
            }
        }
    
        /**
         * This method is used to count the amount of players with
         * a username starting with the given pattern.
         *
         * @param pattern The pattern to count.
         * @return Amount of usernames, starting with the given pattern.
         */
        public static int countPattern(String pattern) {
            int pCount = 0;
            for (Player o : Engine.players) {
                if (o != null) {
                    if (o.username.toLowerCase().startsWith(pattern.toLowerCase())) {
                        pCount++;
                    }
                }
            }
            return pCount;
        }
    
        /**
         * Returns the pattern of two usernames.
         *
         * @param a The first username.
         * @param b The second username.
         * @return The pattern of two usernames.
         */
        public static String equString(String a, String b) {
            String equ = "";
    
            for (int i = 0; i < a.length(); i++) {
                if (b.length() > i) {
                    if (a.charAt(i) == b.charAt(i)) {
                        equ = equ + a.charAt(i);
                    } else {
                        return equ;
                    }
                } else {
                    return equ;
                }
            }
            return equ;
        }
    
        /**
         * Returns the host of a player.
         *
         * @param p The player to get the host of.
         * @return The host of the given player.
         */
        public static String getHost(Player p) {
            if (p != null) {
                return p.socket.socket.getInetAddress().getHostAddress();
            } else {
                return "";
            }
        }
    
        /**
         * This method is used to apply a ban. It saves the
         * host or username to the ban-file and loaded lists.
         *
         * @param banStr The value to ban: Host or Username.
         * @param ipban If true, IP-ban host. If false, ban username.
         */
        public static void addBan(String banStr, boolean ipban) {
            BufferedWriter bw = null;
            String toFile;
            if (ipban) {
                toFile = "./data/banned/bannedhosts.dat";
                Server.socketListener.banHost(banStr);
                sendLog("Banned host: " + banStr);
            } else {
                toFile = "./data/banned/bannedusers.dat";
                Server.banUser(banStr);
                sendLog("Banned user: " + banStr);
            }
            try {
                FileWriter fileWriter = new FileWriter(toFile, true);
    
                bw = new BufferedWriter(fileWriter);
                bw.write(banStr);
                bw.newLine();
                bw.flush();
                bw.close();
                fileWriter = null;
                bw = null;
            } catch (Exception exception) {
                Misc.println("Critical error while writing data: " + toFile);
            }
        }
    
        /**
         * Checks if the given host is in the bannedhosts array.
         *
         * @param host The host to check.
         * @return Host is banned.
         */
        public static boolean isBannedHost(String host) {
            return Server.socketListener.checkBanned(host);
        }
    
        /**
         * Checks if the given username is in the bannedusers array.
         *
         * @param host The username to check.
         * @return Username is banned.
         */
        public static boolean isBannedUser(String user) {
            if (user == null) {
                return false;
            }
            for (int i = 0; i < Server.bannedUsers.length; i++) {
                if (Server.bannedUsers[i] != null && user.equalsIgnoreCase(Server.bannedUsers[i])) {
                    return true;
                }
            }
            return false;
        }
    
        /**
         * Logs actions if LOG_BANS is set to true.
         *
         * @param s The text to log.
         */
        public static void sendLog(String s) {
            if (LOG_BANS) {
                System.out.println("[SERVER-PROTECT] " + s);
            }
        }
    }
    Step 2. Open up SocketListener.java

    Add this method:

    Code:
         /**
         * This method is used to add a given host to the
         * loaded bannedhosts array.
         *
         * @param host The host to add.
         */
        public void banHost(String host) {
            for(int i=0;i<bannedHosts.length;i++) {
                if(bannedHosts[i]==null) {
                    bannedHosts[i]=host;
                    return;
                }
            }
        }

    Step 3. Open up Server.java


    Add this method:

    Code:
        /**
         * This method is used to add a given username to the
         * loaded bannedusers array.
         *
         * @param username The username to add.
         */
        public static void banUser(String username) {
            for (int i = 0; i < bannedUsers.length; i++) {
                if (bannedUsers[i] == null) {
                    bannedUsers[i] = username;
                }
            }
        }
    Step 4. Open up Login.java

    Add this import:

    Code:
    import palidino76.rs2.net.Protect;
    Find:

    Code:
                p.online = true;
    Add this to next line:

    Code:
                Protect.checkPlayer(p);
    Step 5. Compile and run. Don't forget to compile Protect.java!
    Reply With Quote  
     

  2. #2  
    Banned

    Join Date
    Feb 2008
    Age
    29
    Posts
    998
    Thanks given
    1
    Thanks received
    11
    Rep Power
    0
    Those variables should only be capitalized if they are constant. Those variables are changed during execution therefore are not constant and should not be capitalized. Also, why is everything static?
    Reply With Quote  
     

  3. #3  
    Runebay™
    Guest
    Thanks, may add this when my server gets popular again
    Reply With Quote  
     

  4. #4  
    I sfogliare Rune-server ad alta


    Join Date
    Nov 2008
    Age
    30
    Posts
    1,702
    Thanks given
    41
    Thanks received
    18
    Rep Power
    791
    Good job.
    But my crasher will prevail.
    Reply With Quote  
     

  5. #5  
    Banned

    Join Date
    Feb 2008
    Age
    29
    Posts
    998
    Thanks given
    1
    Thanks received
    11
    Rep Power
    0
    Quote Originally Posted by 'Shawn View Post
    Good job.
    But my crasher will prevail.
    fail*
    Reply With Quote  
     

  6. #6  
    Registered Member
    Gravediggah's Avatar
    Join Date
    Nov 2008
    Age
    34
    Posts
    202
    Thanks given
    0
    Thanks received
    5
    Rep Power
    265
    Quote Originally Posted by _Anthony View Post
    Those variables should only be capitalized if they are constant. Those variables are changed therefore are not constant and should not be capitalized. Also, why is everything static?
    Those variables are considered constant in this case.
    The reason everything is static, is: so you can access the class
    from anywhere without creating an instance of the Protect class.

    I mean, people could add commands like ::ipban or ::banuser and
    use this class without creating a new instance.

    Tho, you do have a point. If I'd declare one public instance of the class in Engine.java, this
    would be easier for some purposes. (No need for importing and no need for static methods).
    Reply With Quote  
     

  7. #7  
    Banned

    Join Date
    Feb 2008
    Age
    29
    Posts
    998
    Thanks given
    1
    Thanks received
    11
    Rep Power
    0
    Quote Originally Posted by Gravediggah View Post
    Those variables are considered constant in this case.
    How so? There values are changed during execution... therefore they aren't constant.
    Reply With Quote  
     

  8. #8  
    Registered Member
    Gravediggah's Avatar
    Join Date
    Nov 2008
    Age
    34
    Posts
    202
    Thanks given
    0
    Thanks received
    5
    Rep Power
    265
    Quote Originally Posted by _Anthony View Post
    How so? There values are changed during execution... therefore they aren't constant.
    Which variables are you aiming at?

    EDIT: Nvm, I forgot to make the variables final Updated now.
    Reply With Quote  
     

  9. #9  
    Respected Donater

    Mario's Avatar
    Join Date
    Sep 2007
    Age
    30
    Posts
    1,255
    Thanks given
    32
    Thanks received
    15
    Rep Power
    778
    Good job man
    [rsimg]1042[/rsimg][rsimg]1044[/rsimg][rsimg]1046[/rsimg][rsimg]1038[/rsimg][rsimg]1048[/rsimg][rsimg]1040[/rsimg]Mario[rsimg]1040[/rsimg][rsimg]1048[/rsimg][rsimg]1038[/rsimg][rsimg]1046[/rsimg][rsimg]1044[/rsimg][rsimg]1042[/rsimg]

    Reply With Quote  
     

  10. #10  
    Community Veteran


    Join Date
    Dec 2008
    Posts
    4,263
    Thanks given
    405
    Thanks received
    436
    Rep Power
    1674
    fucking nice rep!
    Reply With Quote  
     

Page 1 of 7 123 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •