Thread: Limiting Connections Per IP

Page 1 of 5 123 ... LastLast
Results 1 to 10 of 43
  1. #1 Limiting Connections Per IP 
    Registered Member

    Join Date
    Jan 2008
    Posts
    2,340
    Thanks given
    20
    Thanks received
    575
    Rep Power
    1202
    Description: Limiting Connections Per IP.

    How it Works: Each time someone tries to connect to the server, the list simply adds that IP to the array. Once the list see's that same IP appearing a number of times, it simply rejects it.

    Difficulty: 2/10

    Assumed Knowledge: Importing, declaring, some knowledge about lists.

    Tested Server: Pali 508, but will work for every base.

    Files/Classes Modified: SocketListener.java, Player.java

    Alright, i saw Miescooldude release a way of using Lists to block connections on 317's, and i'm pretty sure not everyone saw it or if it's still around, so i'll just post the way to do it w/ 508s

    Before i begin - i would like to explain what a list is just so we know what we're doing.

    A list is an array of an object in java, if you know what an array is then this will make sense etc... kind of consuing because a list is an object itself. You might want to read miescooldude's tutorial to understand more about lists.

    Anyway, lets get down to it.

    Step 1)

    We need to import list's right? So lets put this in our imports in the SocketLister class.

    Code:
    import java.util.*;
    You don't really need to import the whole folder since we are only using Lists so just import Lists + Arraylists:

    Code:
    import java.util.ArrayList;
    import java.util.List;
    Now, we need to declare our list, so lets call our list... connectionsList

    Code:
    public static List connectionsList = new ArrayList();

    Step 2)

    Now, with our list declared and having our lists imported, let's skip on over to the part where the list adds the persons IP upon connecting.

    Now, in the method where a persons connection is accepted (Its in socketListener go find out where.)

    You would add:

    Code:
    connectionsList.add(getAddress(socket));
    Step 3)

    Now, if you would want to make it so its just one IP per connection, you would just put this in the method on the top:

    Code:
    if(!connectionsList.contains(getAddress(socket)))
    But if you were wanting to make it more connections, you'd declare this:

    Code:
    	public int totalConnections(String connection) {
    		int count = 0;
    		for(Object o : connectionsList.toArray()) {
    			if(o.toString().equals(connection)) {
    				count++;
    			}
    		}
    		return count;
    	}
    And replace

    Code:
    if(!connectionsList.contains(getAddress(socket)))
    with

    Code:
    if(totalConnections(getAddress(socket)) < #LimitHere)
    Instead of

    #LimitHere you'd put the max connections you want

    So if you want it 3 connections per 1 IP it'd be

    Code:
    if(totalConnections(getAddress(socket)) < 4)
    and this would be 2

    Code:
    if(totalConnections(getAddress(socket)) < 3)
    And so on.

    Step 4)

    That's right, the final step is to get the list to remove the persons connection when they log out otherwise their IP will stay in the list lal.

    So in the player class, in the destruct method, simply add this line:

    Code:
    Server.socketListener.connectionsList.remove(Server.socketListener.getAddress(this.socket.socket));
    And compile, and you are done!

    Post all errors you get here.

    Correct me on anything if i did anything wrong.

    I hope you enjoyed my crappy work )";

    Credits:
    100% Meiscooldude

    Dont forget to import your Server class if you get any errors.

    Code:
    import palidino76.rs2.Server;
     

  2. #2  
    Registered Member Tzar's Avatar
    Join Date
    Aug 2007
    Posts
    1,046
    Thanks given
    54
    Thanks received
    12
    Rep Power
    260
    I get this error:
    Code:
    .\palidino76\rs2\players\Player.java:1658: package Server does not exist
        server.socketListener.connectionsList.remove(Server.socketListener.getAddres
    s(this.socket.socket));
                                                           ^
    .\palidino76\rs2\players\Player.java:1658: package server.socketListener does no
    t exist
        server.socketListener.connectionsList.remove(Server.socketListener.getAddres
    s(this.socket.socket));
                             ^
    Even if I have this in server.java:
    Code:
        /**
         * Listens for incoming connections and accepts them.
         */
        public static SocketListener socketListener;







     

  3. #3  
    Registered Member
    AndyJay's Avatar
    Join Date
    Jun 2008
    Posts
    1,285
    Thanks given
    38
    Thanks received
    48
    Rep Power
    84
    I just did this exactly how you said. Got errors in player.java but fixed it by importing Server.java
    Compiled fine apart from saying SocketListener uses unsafe or unchecked operations, tried to log in and it said socket closed. Then I tried again and it said java heap...


    @ Kadir
    In player.java and this to your imports.
    import Bulby.Server; Change bulby to whatever you use.. Pali or whatever
     

  4. #4  
    Registered Member Tzar's Avatar
    Join Date
    Aug 2007
    Posts
    1,046
    Thanks given
    54
    Thanks received
    12
    Rep Power
    260
    Is this also 1 ip for localhost?
    Because I can access with 2 accounts from my ip..
    Heres what the run looks like in SocketListener(dont know if i placed it in right place):

    Code:
        public void run() {
            Socket socket = null;
    
            while (serverRunning) {
                try {
                if(!connectionsList.contains(getAddress(socket)))
                    socket = serverChannel.accept();
                    socket.setSoTimeout(1);
                    socket.setTcpNoDelay(true);
                    appendConnection(getAddress(socket));
                    Misc.println("Connection recieved from: " + getAddress(socket));
                    if (checkBanned(getAddress(socket))) {
                        socket.close();
                        continue;
                    }
                    int id = getFreeId();
    
                    sockets[id] = socket;
                    Server.engine.addConnection(socket, id);
                } catch (Exception e) {}
            }
        }







     

  5. #5  
    Registered Member

    Join Date
    Jan 2008
    Posts
    2,340
    Thanks given
    20
    Thanks received
    575
    Rep Power
    1202
    You never added

    Code:
    connectionsList.add(getAddress(socket));
     

  6. #6  
    Pie ownz all!
    Pie's Avatar
    Join Date
    Oct 2008
    Age
    28
    Posts
    998
    Thanks given
    1
    Thanks received
    10
    Rep Power
    530
    Very nice micheal, ill test it later
     

  7. #7  
    Community Veteran


    Join Date
    Dec 2008
    Posts
    4,263
    Thanks given
    405
    Thanks received
    436
    Rep Power
    1674
    very nice works to
     

  8. #8  
    Lighten
    Guest
    good job tho.

    Edit: fixed thanks. rep.
     

  9. #9  
    Registered Member Tzar's Avatar
    Join Date
    Aug 2007
    Posts
    1,046
    Thanks given
    54
    Thanks received
    12
    Rep Power
    260
    Quote Originally Posted by Lil_Michael View Post
    You never added

    Code:
    connectionsList.add(getAddress(socket));

    So it would be like this?

    Code:
        public void run() {
            Socket socket = null;
    
            while (serverRunning) {
                try {
                if(!connectionsList.contains(getAddress(socket)))
                    socket = serverChannel.accept();
                    socket.setSoTimeout(1);
                    socket.setTcpNoDelay(true);
                    appendConnection(getAddress(socket));
                    connectionsList.add(getAddress(socket));
                    Misc.println("Connection recieved from: " + getAddress(socket));
                    if (checkBanned(getAddress(socket))) {
                        socket.close();
                        continue;
                    }
                    int id = getFreeId();
    
                    sockets[id] = socket;
                    Server.engine.addConnection(socket, id);
                } catch (Exception e) {}
            }
        }







     

  10. #10  
    Hi.

    'Mystic Flow's Avatar
    Join Date
    Nov 2007
    Posts
    7,146
    Thanks given
    256
    Thanks received
    1,252
    Rep Power
    3714
    HELP!@!@!@!@!@

    Note: .\palidino76\rs2\net\SocketListener.java uses unchecked or unsafe operatio
    ns.
    Note: Recompile with -Xlint:unchecked for details.
    Press any key to continue . . .



     

Page 1 of 5 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
  •