Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need help with flood controll script Rs2HD

843790Jul 15 2010 — edited Jul 15 2010
Dear readers, i'm a codener of runescape private servers and i want to build a code to get people who flood my server outside. But i have a problem, ok first 1 you have a client a client connects you're server and you're server on you're pc right. Okay, somebody made a server crasher to crash my server and my idee was to add a anti-flood script. I keep searching on the internet and i found something, i must replace this with mines under here is the code displayed :

ConnectionThrottleFilter.java
package com.rs2hd.net;
 
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.mina.common.IoFilterAdapter;
import org.apache.mina.common.IoSession;
 
import com.rs2hd.util.log.Logger;
 
/**
* A {@link IoFilter} which blocks connections from connecting
* at a rate faster than the specified interval.
*
* @author The Apache MINA Project (dev@mina.apache.org)
* @version $Rev$, $Date$
*/
public class ConnectionThrottleFilter extends IoFilterAdapter {
	private long allowedInterval;
	private Map<InetAddress, Long> clients;
	private Map<InetAddress, Integer> counts;
	private Set<InetAddress> connectedAddresses;
	/**
	 * Connection holder
	 */
	public static ArrayList<InetAddress> connected = new ArrayList<InetAddress>();
	
	/**
	* Constructor that takes in a specified wait time.
	*
	* @param allowedInterval
	* 	The number of milliseconds a client is allowed to wait
	* 	before making another successful connection
	*
	*/
	public ConnectionThrottleFilter(long allowedInterval) {
		this.allowedInterval = allowedInterval;
		clients = Collections.synchronizedMap(new HashMap<InetAddress, Long>());
		counts = Collections.synchronizedMap(new HashMap<InetAddress, Integer>());
		connectedAddresses = new HashSet<InetAddress>();
	}
	
	/**
	* Sets the interval between connections from a client.
	* This value is measured in milliseconds.
	*
	* @param allowedInterval
	* 	The number of milliseconds a client is allowed to wait
	* 	before making another successful connection
	*/
	public void setAllowedInterval(long allowedInterval) {
		this.allowedInterval = allowedInterval;
	}
	
	public void delayClient(IoSession session, int delay) {
		long d = System.currentTimeMillis() - delay;
		clients.put(getAddress(session), d);
	}
	
	private InetAddress getAddress(IoSession io) {
		return ((InetSocketAddress)io.getRemoteAddress()).getAddress();
	}
	/**

	/**
	* Method responsible for deciding if a connection is OK
	* to continue
	*
	* @param session
	* 	The new session that will be verified
	* @return
	* 	True if the session meets the criteria, otherwise false
	*/
	public boolean isConnectionOk(IoSession session) {
		InetAddress addr = getAddress(session);
		if(!connected.contains(((InetSocketAddress)session.getRemoteAddress()).getAddress()))
		{
			//System.out.println(session.getRemoteAddress().)
			connected.add(((InetSocketAddress)session.getRemoteAddress()).getAddress());
			long now = System.currentTimeMillis();
			if (clients.containsKey(addr)) {
				long lastConnTime = clients.get(addr);
				
				if (now - lastConnTime < allowedInterval) {
					int c = 20;
					if(!counts.containsKey(addr))
						counts.put(addr, 0);
					else c = counts.get(addr) + 0;
					if(c >= 350) {
						
						c = 0;
					}
					counts.put(addr, c);
					counts.put(addr, c);
					Logger.getInstance().error("["+addr+"] Session dropped (delay="+(now-lastConnTime)+"ms)");
					return false;
				} else {
					clients.put(addr, now);
					return true;
				}
			} else {
				clients.put(addr, now);
				fluships();//Gets rid of 1 connection if its validated so that a user can login
				//because a login == 2 connections
				return true;
			}
		}
		return false;
	}
	
	public void closedSession(IoSession io) {
		connectedAddresses.remove(getAddress(io));
		connected.remove(getAddress(io));
	}
	
	public void acceptedLogin(IoSession io) {
		connectedAddresses.add(getAddress(io));
		connected.remove(getAddress(io));
	}
	
	public boolean isConnected(IoSession io) {
		return connectedAddresses.contains(getAddress(io));
	}
	
	public int[] getSizes() { 
		return new int[] { clients.size(), counts.size(), connectedAddresses.size() }; 
	}
	
	public void connectionOk(IoSession io) {
		counts.remove(getAddress(io));
		connected.remove(getAddress(io));
	}
	
	@Override
	public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
		if (!isConnectionOk(session)) {
			session.close();
			return;
		}
		nextFilter.sessionCreated(session);
	}
	
	public static void fluships()
	{
		connected.clear();
	}
}
ConnectionHandler.java
package com.rs2hd.net;
import java.net.InetSocketAddress;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoHandler;
import org.apache.mina.common.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import com.rs2hd.Constants;
import com.rs2hd.GameEngine;
import com.rs2hd.model.Player;
import com.rs2hd.model.World;
import com.rs2hd.net.codec.CodecFactory;
import com.rs2hd.util.log.Logger;
 
/**
 * Handles incoming events from MINA.
 * @author phaze
 *
 */
public class ConnectionHandler implements IoHandler {
	/**
	 * Logger instance.
	 */
	private Logger logger = Logger.getInstance();
	
	/**
	 * The game engine.
	 */
	private GameEngine engine;
	
	/**
	 * Creates the connection handler.
	 * @param engine
	 */
	public ConnectionHandler(GameEngine engine) {
		this.engine = engine;
	}
 
	@Override
	public void exceptionCaught(IoSession session, Throwable throwable) throws Exception {
		//logger.error("Exception caught: " + session + ": " + throwable.getMessage() + ".");
		//logger.stackTrace(throwable);
	}
 
	@Override
	public void messageReceived(IoSession session, Object data) throws Exception {
		Packet packet = (Packet) data;
		Player player = (Player) session.getAttachment();
		player.addPacketToQueue(packet);
	}
 
	@Override
	public void messageSent(IoSession session, Object data) throws Exception {
	}
 
	@Override
	public void sessionClosed(IoSession session) throws Exception {
		if(session.getAttachment() != null) {
			World.getInstance().unregister((Player) session.getAttachment());
		}
		logger.debug("Session has been closed: " + session.getRemoteAddress().toString());
		// TODO remove player from lists here
	}
 
	@Override
	public void sessionCreated(IoSession session) throws Exception {
	}
 
	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		logger.debug("Session is now idle: " + session.getRemoteAddress().toString());
		ConnectionThrottleFilter.connected.remove(((InetSocketAddress)session.getRemoteAddress()).getAddress());
		session.close();
	}
	
}
Ok i pasted it into my server but now the problem, ok you have 2 connections 1 to load the client and 1 to login but when you typed you're password wrong you need to wait a long time to login again. My question is how do i add more ip connections ? I hope some guy will helps and i'm able to pay for it if you like, i got teamviewer on my pc and more.
Edited by: mr_avatar3 on Jul 15, 2010 1:12 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 12 2010
Added on Jul 15 2010
1 comment
151 views