Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/GlobalServer/GlobalServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
Expand All @@ -44,6 +46,7 @@ public class GlobalServer implements Runnable
private List<Room> rooms;

private long lastRoomCheck;
private Map<String, Integer> ipConnectionCount;

private static final String STATUS_FILE = "/home/graphwar/graphwar/status.html";
private static final long STATUS_INTERVAL = 30 * 1000;
Expand All @@ -52,6 +55,7 @@ public GlobalServer()
{
this.players = new Vector<LobbyPlayer>();
this.rooms = new Vector<Room>();
this.ipConnectionCount = new HashMap<String, Integer>();

lastRoomCheck = System.currentTimeMillis();

Expand Down Expand Up @@ -184,13 +188,47 @@ public void removePlayer(LobbyPlayer player)
{
players.remove(player);
}

decrementIpCount(player.getIpAddress());

if(player.getRoom() != null)
{
removeRoom(player.getRoom());
}
}

public void rejectPlayer(LobbyPlayer player)
{
player.disconnect();

synchronized(players)
{
players.remove(player);
}

decrementIpCount(player.getIpAddress());
}

private void decrementIpCount(String ip)
{
synchronized(ipConnectionCount)
{
Integer c = ipConnectionCount.get(ip);
if(c != null)
{
int newCount = c.intValue() - 1;
if(newCount <= 0)
{
ipConnectionCount.remove(ip);
}
else
{
ipConnectionCount.put(ip, newCount);
}
}
}
}

public void removeRoom(Room room)
{
String message = NetworkProtocol.CLOSE_ROOM+"&"+room.getRoomID();
Expand Down Expand Up @@ -364,6 +402,22 @@ private void waitConnection() throws IOException

if(connected)
{
String ip = tempSocket.getInetAddress().getHostAddress();

int count;
synchronized(ipConnectionCount)
{
Integer c = ipConnectionCount.get(ip);
count = (c == null) ? 0 : c.intValue();
}

if(count >= Constants.MAX_CONNECTIONS_PER_IP)
{
System.out.println("Rejected connection from " + ip + ": too many connections (" + count + ")");
try { tempSocket.close(); } catch (IOException e) { }
return;
}

Connection connection = new Connection(tempSocket);

LobbyPlayer player = new LobbyPlayer(connection, this);
Expand All @@ -373,6 +427,11 @@ private void waitConnection() throws IOException
this.players.add(player);
}

synchronized(ipConnectionCount)
{
ipConnectionCount.put(ip, count + 1);
}

new Thread(player).start();
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/GlobalServer/LobbyPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ public void run()

System.out.println("New name: "+name);

if(name == null)
{
this.globalServer.rejectPlayer(this);
return;
}

if(name.length() > Constants.MAX_NAME_LENGTH || name.indexOf('&') != -1)
{
System.out.println("Rejected invalid name from " + getIpAddress() + ": length=" + name.length());
this.globalServer.rejectPlayer(this);
return;
}

if(name.compareTo(Constants.DUMMY_NAME) == 0)
{
this.dummy = true;
Expand Down
3 changes: 3 additions & 0 deletions src/GraphServer/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class Constants

public static final int TURN_TIME = 60000;

public static final int MAX_NAME_LENGTH = 256;
public static final int MAX_CONNECTIONS_PER_IP = 32;

public static final int MAX_PLAYERS = 10;
public static final int MAX_SOLDIERS_PER_PLAYER = 4;
public static final int MAX_CLIENTS = 10;
Expand Down