From 842e01ffac3e55bdb328be0a5fd4943494b2a206 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Thu, 6 Dec 2012 21:25:40 +0100 Subject: [PATCH] Whitelist, Banned IPs, more properties --- .gitignore | 2 ++ classes/PocketMinecraftServer.class.php | 20 ++++++++++++++++++++ classes/Session.class.php | 12 +++++++++--- client.php | 1 + common/config.php | 1 - common/default.properties | 2 ++ server.php | 22 +++++++++++++++++++--- 7 files changed, 53 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 8e58a873e..9171a6910 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ *.log *.bat server.properties +white-list.txt +banned-ips.txt ################# ## Eclipse diff --git a/classes/PocketMinecraftServer.class.php b/classes/PocketMinecraftServer.class.php index 331536505..e6e2fe163 100644 --- a/classes/PocketMinecraftServer.class.php +++ b/classes/PocketMinecraftServer.class.php @@ -40,6 +40,8 @@ class PocketMinecraftServer{ $this->eidCnt = 1; $this->maxClients = 20; $this->description = ""; + $this->whitelist = false; + $this->bannedIPs = array(); $this->motd = "Welcome to ".$name; $this->serverID = $serverID === false ? Utils::readLong(Utils::getRandomBytes(8)):$serverID; $this->seed = $seed === false ? Utils::readInt(Utils::getRandomBytes(4)):((int) $seed); @@ -51,6 +53,7 @@ class PocketMinecraftServer{ //$this->event("onTick", "onTick", true); $this->event("onChat", "eventHandler", true); $this->action(1000000, '$this->time += 10;$this->trigger("onTimeChange", $this->time);'); + $this->action(1000000 * 60, '$this->reloadConfig();'); $this->action(1000000 * 60 * 10, '$this->custom = array();'); $this->setType("normal"); $this->interface = new MinecraftInterface("255.255.255.255", $this->protocol, $this->port, true, false); @@ -58,6 +61,7 @@ class PocketMinecraftServer{ $this->action(1000000 * 3 * 60, '$this->chat(false, "This server uses Pocket-Minecraft-PHP");'); sleep(2); $this->action(1000000 * 3 * 60, '$this->chat(false, "Check it at http://bit.ly/RE7uaW");'); + $this->reloadConfig(); console("[INFO] Server Name: ".$this->name); console("[INFO] Server GUID: ".$this->serverID); console("[INFO] Protocol Version: ".$this->protocol); @@ -67,6 +71,13 @@ class PocketMinecraftServer{ $this->stop = false; } + public function reloadConfig(){ + if($this->whitelist === true or is_array($this->whitelist)){ + $this->whitelist = explode("\n", str_replace(array(" ","\t","\r"), "", file_get_contents(FILE_PATH."white-list.txt"))); + } + $this->bannedIPs = explode("\n", str_replace(array(" ","\t","\r"), "", file_get_contents(FILE_PATH."banned-ips.txt"))); + } + public function close($reason = "stop"){ $this->chat(false, "Stopping server..."); $this->stop = true; @@ -139,6 +150,9 @@ class PocketMinecraftServer{ }else{ switch($packet["pid"]){ case 0x02: + if(in_array($packet["ip"], $this->bannedIPs)){ + break; + } if(!isset($this->custom["times_".$CID])){ $this->custom["times_".$CID] = 0; } @@ -154,6 +168,9 @@ class PocketMinecraftServer{ $this->custom["times_".$CID] = ($this->custom["times_".$CID] + 1) % strlen($this->description); break; case 0x05: + if(in_array($packet["ip"], $this->bannedIPs)){ + break; + } if(count($this->clients) >= $this->maxClients){ break; } @@ -175,6 +192,9 @@ class PocketMinecraftServer{ } break; case 0x07: + if(in_array($packet["ip"], $this->bannedIPs)){ + break; + } if(count($this->clients) >= $this->maxClients){ break; } diff --git a/classes/Session.class.php b/classes/Session.class.php index 994cca9cd..5c3529017 100644 --- a/classes/Session.class.php +++ b/classes/Session.class.php @@ -52,13 +52,15 @@ class Session{ } } - public function close($reason = "server stop"){ + public function close($reason = "server stop", $msg = true){ foreach($this->evid as $ev){ $this->server->deleteEvent($ev[0], $ev[1]); } $this->connected = false; - $this->server->trigger("onChat", $this->username." left the game"); - console("[DEBUG] Session with ".$this->ip.":".$this->port." closed due to ".$reason, true, true, 2); + if($msg === true){ + $this->server->trigger("onChat", $this->username." left the game"); + } + console("[INFO] Session with ".$this->ip.":".$this->port." closed due to ".$reason); unset($this->server->clients[$this->CID]); } @@ -132,6 +134,10 @@ class Session{ break; case MC_LOGIN: $this->username = $data["username"]; + if($this->server->whitelist !== false and !in_array($this->username, $this->server->whitelist)){ + $this->close("\"".$this->username."\" not being on white-list", false); + break; + } console("[INFO] Player \"".$this->username."\" connected from ".$this->ip.":".$this->port); $this->evid[] = array("onTimeChange", $this->server->event("onTimeChange", array($this, "eventHandler"))); $this->evid[] = array("onChat", $this->server->event("onChat", array($this, "eventHandler"))); diff --git a/client.php b/client.php index 22b29013d..cb3df70be 100644 --- a/client.php +++ b/client.php @@ -28,6 +28,7 @@ the Free Software Foundation, either version 3 of the License, or require_once("common/dependencies.php"); require_once("classes/PocketMinecraftClient.class.php"); file_put_contents("packets.log", ""); +define("DEBUG", 2); $client = new PocketMinecraftClient("shoghicp"); console("[INFO] Searching servers..."); diff --git a/common/config.php b/common/config.php index 8b28b06fe..bd6b8b4a0 100644 --- a/common/config.php +++ b/common/config.php @@ -34,6 +34,5 @@ set_include_path(get_include_path() . PATH_SEPARATOR . FILE_PATH . PATH_SEPARATO ini_set("memory_limit", "512M"); define("CURRENT_PROTOCOL", 5); define("CURRENT_VERSION", 1); -define("DEBUG", 2); define("LOG", true); define("MAGIC", "\x00\xff\xff\x00\xfe\xfe\xfe\xfe\xfd\xfd\xfd\xfd\x12\x34\x56\x78"); \ No newline at end of file diff --git a/common/default.properties b/common/default.properties index 01ad5500f..a661d36e5 100644 --- a/common/default.properties +++ b/common/default.properties @@ -9,5 +9,7 @@ seed=false server-id=false server-type=normal max-players=20 +white-list=false +debug=1 spawn=128.5;100;128.5 regenerate-config=true \ No newline at end of file diff --git a/server.php b/server.php index 35cf6a36b..165cbfc0f 100644 --- a/server.php +++ b/server.php @@ -30,12 +30,22 @@ require_once("classes/PocketMinecraftServer.class.php"); file_put_contents("packets.log", ""); file_put_contents("console.log", ""); -$prop = @file_get_contents(FILE_PATH."server.properties"); -if(trim($prop) == ""){ +if(!file_exists(FILE_PATH."white-list.txt")){ + console("[WARNING] No white-list.txt found, creating blank file"); + file_put_contents(FILE_PATH."white-list.txt", ""); +} + +if(!file_exists(FILE_PATH."banned-ips.txt")){ + console("[WARNING] No banned-ips.txt found, creating blank file"); + file_put_contents(FILE_PATH."banned-ips.txt", ""); +} + +if(!file_exists(FILE_PATH."server.properties")){ console("[WARNING] No server.properties found, using default settings"); copy(FILE_PATH."common/default.properties", FILE_PATH."server.properties"); - $prop = file_get_contents(FILE_PATH."server.properties"); + } +$prop = file_get_contents(FILE_PATH."server.properties"); $prop = explode("\n", str_replace("\r", "", $prop)); $config = array(); foreach($prop as $line){ @@ -54,6 +64,7 @@ foreach($prop as $line){ case "gamemode": case "max-players": case "port": + case "debug": $v = (int) $v; break; case "seed": @@ -64,23 +75,28 @@ foreach($prop as $line){ $v = explode(";", $v); $v = array("x" => floatval($v[0]), "y" => floatval($v[1]), "z" => floatval($v[2])); break; + case "white-list": case "regenerate-config": $v = trim($v) == "true" ? true:false; break; } $config[$n] = $v; } +define("DEBUG", $config["debug"]); $server = new PocketMinecraftServer($config["server-name"], $config["gamemode"], $config["seed"], $config["protocol"], $config["port"], $config["server-id"]); $server->setType($config["type"]); $server->maxClients = $config["max-players"]; $server->description = $config["description"]; $server->motd = $config["motd"]; +$server->whitelist = $config["white-list"]; +$server->reloadConfig(); if($config["regenerate-config"] == true){ $config["seed"] = $server->seed; $config["server-id"] = $server->serverID; $config["regenerate-config"] = "false"; + $config["white-list"] = $config["whitelist"] === true ? "true":"false"; $config["spawn"] = implode(";", $config["spawn"]); $prop = "#Pocket Minecraft PHP server properties\r\n#".date("D M j H:i:s T Y")."\r\n"; foreach($config as $n => $v){