Made block/unblock command come from the config.

This commit is contained in:
CodeForFame 2011-06-21 19:46:47 -05:00
parent c578718409
commit c696c3ef94
3 changed files with 40 additions and 69 deletions

View File

@ -18,29 +18,30 @@ public class Config {
public static String MYSQL_USER; public static String MYSQL_USER;
public static String MYSQL_PASS; public static String MYSQL_PASS;
public static String SERVER_IP, SERVER_NAME, RSCD_HOME, CONF_DIR,
SERVER_LOCATION, LS_IP;
public static String SERVER_IP, SERVER_NAME, RSCD_HOME, CONF_DIR, SERVER_LOCATION, LS_IP; public static int SERVER_PORT, SERVER_VERSION, MAX_PLAYERS, LS_PORT,
SERVER_NUM;
public static int SERVER_PORT, SERVER_VERSION, MAX_PLAYERS, LS_PORT, SERVER_NUM;
public static long START_TIME; public static long START_TIME;
public static boolean members, f2pWildy; public static boolean members, f2pWildy;
public static double expRate, subExpRate; public static double expRate, subExpRate;
public static String[] pmods, mods, admins; public static String[] pmods, mods, admins;
public static String UNUSED_IP;
public static int IP_BAN_REMOVAL_DELAY; public static int IP_BAN_REMOVAL_DELAY;
public static int GARBAGE_COLLECT_INTERVAL; public static int GARBAGE_COLLECT_INTERVAL;
public static int SAVE_INTERVAL; public static int SAVE_INTERVAL;
public static String DATE_FORMAT; public static String DATE_FORMAT;
public static String BLOCK_COMMAND;
public static String UNBLOCK_COMMAND;
static { static {
loadEnv(); loadEnv();
} }
/** /**
* Called to load config settings from the given file * Called to load config settings from the given file
* *
@ -71,28 +72,33 @@ public class Config {
LS_IP = props.getProperty("lsip"); LS_IP = props.getProperty("lsip");
LS_PORT = Integer.parseInt(props.getProperty("lsport")); LS_PORT = Integer.parseInt(props.getProperty("lsport"));
SERVER_NUM = Integer.parseInt(props.getProperty("servernum")); SERVER_NUM = Integer.parseInt(props.getProperty("servernum"));
members = Boolean.parseBoolean(props.getProperty("members", "false")); members = Boolean.parseBoolean(props.getProperty("members", "false"));
f2pWildy = Boolean.parseBoolean(props.getProperty("f2pWildy", "true")); f2pWildy = Boolean.parseBoolean(props.getProperty("f2pWildy", "true"));
expRate = Double.parseDouble(props.getProperty("expRate")); expRate = Double.parseDouble(props.getProperty("expRate"));
subExpRate = Double.parseDouble(props.getProperty("subExpRate")); subExpRate = Double.parseDouble(props.getProperty("subExpRate"));
pmods = props.getProperty("pmods").replaceAll(", +", ",").split(","); pmods = props.getProperty("pmods").replaceAll(", +", ",").split(",");
mods = props.getProperty("mods").replaceAll(", +", ",").split(","); mods = props.getProperty("mods").replaceAll(", +", ",").split(",");
admins = props.getProperty("admins").replaceAll(", +", ",").split(","); admins = props.getProperty("admins").replaceAll(", +", ",").split(",");
UNUSED_IP = props.getProperty("unused-ip"); IP_BAN_REMOVAL_DELAY = Integer.parseInt(props
IP_BAN_REMOVAL_DELAY = Integer.parseInt(props.getProperty("ip-ban-removal-delay")); .getProperty("ip-ban-removal-delay"));
BLOCK_COMMAND = props.getProperty("block-command");
GARBAGE_COLLECT_INTERVAL = Integer.parseInt(props.getProperty("garbage-collect-interval")); UNBLOCK_COMMAND = props.getProperty("unblock-command");
GARBAGE_COLLECT_INTERVAL = Integer.parseInt(props
.getProperty("garbage-collect-interval"));
SAVE_INTERVAL = Integer.parseInt(props.getProperty("save-interval")); SAVE_INTERVAL = Integer.parseInt(props.getProperty("save-interval"));
DATE_FORMAT = props.getProperty("date-format"); DATE_FORMAT = props.getProperty("date-format");
props.clear(); props.clear();
Constants.GameServer.MOTD = "@yel@Welcome to @whi@" + Config.SERVER_NAME + "@yel@ - World @whi@" + (Config.SERVER_NUM == 0 ? 2 : Config.SERVER_NUM) + " (" + (Config.members ? "P2P" : "F2P") + ")"; Constants.GameServer.MOTD = "@yel@Welcome to @whi@"
+ Config.SERVER_NAME + "@yel@ - World @whi@"
+ (Config.SERVER_NUM == 0 ? 2 : Config.SERVER_NUM) + " ("
+ (Config.members ? "P2P" : "F2P") + ")";
} }
/** /**

View File

@ -10,36 +10,6 @@ import org.moparscape.msc.config.Config
object OSLevelBlocking { object OSLevelBlocking {
private val block_ = {
println(System.getProperty("os.name"))
if (System.getProperty("os.name") startsWith "(?i)linux") {
def b(ip: String) {
Runtime.getRuntime().exec("sudo route add -host " + ip + " reject")
}
b _
} else {
// Windows blocking - untested - won't work on Windows 7
def b(ip: String) {
Runtime.getRuntime().exec("route ADD " + ip + " MASK 255.255.255.255 " + Config.UNUSED_IP)
}
b _
}
}
private val unblock_ = {
if (System.getProperty("os.name") startsWith "(?i)linux") {
def u(ip: String) {
Runtime.getRuntime().exec("sudo route del " + ip + " reject")
}
u _
} else {
// Windows blocking - untested - won't work on Windows 7
def b(ip: String) {
Runtime.getRuntime().exec("route DELETE " + ip)
}
b _
}
}
private val blocked = new CopyOnWriteArrayList[String]; private val blocked = new CopyOnWriteArrayList[String];
private val events = Server.getServer().getEngine().getEventHandler() private val events = Server.getServer().getEngine().getEventHandler()
@ -49,26 +19,21 @@ object OSLevelBlocking {
events.add(new DelayedEvent(null, Config.IP_BAN_REMOVAL_DELAY) { events.add(new DelayedEvent(null, Config.IP_BAN_REMOVAL_DELAY) {
def run() { def run() {
unblock(ip) try {
Runtime.getRuntime.exec(Config.UNBLOCK_COMMAND.replaceAll("${ip}", ip));
blocked.remove(ip)
Logger.println("Unblocked " + ip)
} catch {
case e: Exception => {
Logger.error(e)
Logger.println("Failed to unblock " + ip)
}
}
} }
}) })
block_(ip) Runtime.getRuntime.exec(Config.BLOCK_COMMAND.replaceAll("${ip}", ip));
blocked.add(ip) blocked.add(ip)
Logger.println("Blocked " + ip) Logger.println("Blocked " + ip)
} }
} }
def unblock(ip: String) {
try {
unblock_(ip)
blocked.remove(ip)
Logger.println("Unblocked " + ip)
} catch {
case e: Exception => {
Logger.error(e)
Logger.println("Failed to unblock " + ip)
}
}
}
} }

View File

@ -26,9 +26,9 @@
<entry key="mods">None</entry> <entry key="mods">None</entry>
<entry key="admins">None</entry> <entry key="admins">None</entry>
<!-- This only needs to be specified if you're using a Windows machine -->
<entry key="unused-ip">192.168.0.255</entry>
<entry key="ip-ban-removal-delay">1800000</entry> <entry key="ip-ban-removal-delay">1800000</entry>
<entry key="block-command">sudo route add -host ${ip} reject</entry>
<entry key="unblock-command">sudo route del -host ${ip} reject</entry>
<!-- Every 100 minutes --> <!-- Every 100 minutes -->
<entry key="garbage-collect-interval">6000000</entry> <entry key="garbage-collect-interval">6000000</entry>