BanAPI added (username, IP, whitelist)

This commit is contained in:
Shoghi Cervantes Pueyo 2013-01-16 19:52:51 +01:00
parent aa92e83733
commit 136fd205f0
5 changed files with 185 additions and 85 deletions

1
.gitignore vendored
View File

@ -6,6 +6,7 @@ logs/*
server.properties
white-list.txt
banned-ips.txt
banned.txt
#################
## Eclipse

177
src/API/BanAPI.php Normal file
View File

@ -0,0 +1,177 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
class BanAPI{
private $server;
private $whitelist;
private $banned;
private $bannedIPs;
function __construct(PocketMinecraftServer $server){
$this->server = $server;
}
public function init(){
console("[INFO] Loading authentication lists...");
$this->whitelist = new Config(FILE_PATH."white-list.txt", CONFIG_LIST);
$this->bannedIPs = new Config(FILE_PATH."banned-ips.txt", CONFIG_LIST);
$this->banned = new Config(FILE_PATH."banned.txt", CONFIG_LIST);
$this->server->api->console->register("banip", "Manages IP Banning", array($this, "commandHandler"));
$this->server->api->console->register("ban", "Manages Bannning", array($this, "commandHandler"));
$this->server->api->console->register("whitelist", "Manages White-listing", array($this, "commandHandler"));
}
public function commandHandler($cmd, $params){
switch($cmd){
case "whitelist":
$p = strtolower(array_shift($params));
switch($p){
case "remove":
$user = trim(implode(" ", $params));
$this->whitelist->remove($user);
$this->whitelist->save();
console("[INFO] Player \"$user\" removed from white-list");
break;
case "add":
$user = trim(implode(" ", $params));
$this->whitelist->set($user);
$this->whitelist->save();
console("[INFO] Player \"$user\" added to white-list");
break;
case "reload":
$this->whitelist = $this->load(FILE_PATH."white-list.txt", CONFIG_LIST);
break;
case "list":
console("[INFO] White-list: ".implode(", ", $this->whitelist->getAll(true)));
break;
case "on":
case "true":
case "1":
console("[INFO] White-list turned on");
$this->server->api->setProperty("white-list", true);
break;
case "off":
case "false":
case "0":
console("[INFO] White-list turned off");
$this->server->api->setProperty("white-list", false);
break;
default:
console("[INFO] Usage: /whitelist <on | off | add | remove | reload | list> [username]");
break;
}
break;
case "banip":
$p = strtolower(array_shift($params));
switch($p){
case "pardon":
case "remove":
$ip = trim(implode($params));
$this->bannedIPs->remove($ip);
$this->bannedIPs->save();
console("[INFO] IP \"$ip\" removed from ban list");
break;
case "add":
case "ban":
$ip = trim(implode($params));
$this->bannedIPs->set($ip);
$this->bannedIPs->save();
console("[INFO] IP \"$ip\" added to ban list");
break;
case "reload":
$this->bannedIPs = new Config(FILE_PATH."banned-ips.txt", CONFIG_LIST);
break;
case "list":
console("[INFO] IP ban list: ".implode(", ", $this->bannedIPs->getAll(true)));
break;
default:
console("[INFO] Usage: /banip <add | remove | list | reload> [IP]");
break;
}
break;
case "ban":
$p = strtolower(array_shift($params));
switch($p){
case "pardon":
case "remove":
$user = trim(implode($params));
$this->banned->remove($user);
$this->banned->save();
console("[INFO] Player \"$user\" removed from ban list");
break;
case "add":
case "ban":
$user = trim(implode($params));
$this->banned->set($user);
$this->banned->save();
$player = $this->server->api->player->get($user);
if($player !== false){
$player->close("You have been banned");
}
$this->server->api->chat->broadcast("$user has been banned");
console("[INFO] Player \"$user\" added to ban list");
break;
case "reload":
$this->banned = new Config(FILE_PATH."banned.txt", CONFIG_LIST);
break;
case "list":
console("[INFO] Ban list: ".implode(", ", $this->banned->getAll(true)));
break;
default:
console("[INFO] Usage: /ban <add | remove | list | reload> [player]");
break;
}
break;
}
}
public function isIPBanned($ip){
if($this->server->api->dhandle("api.ban.ip.check", $ip) === false){
return true;
}elseif($this->bannedIPs->exists($ip)){
return true;
}
return false;
}
public function isBanned($username){
if($this->server->api->dhandle("api.ban.check", $username) === false){
return true;
}elseif($this->banned->exists($username)){
return true;
}
return false;
}
public function inWhitelist($username){
if($this->server->api->dhandle("api.ban.whitelist.check", $ip) === false){
return true;
}elseif($this->whitelist->exists($username)){
return true;
}
return false;
}
}

View File

@ -92,36 +92,6 @@ class ConsoleAPI{
$this->server->api->restart = true;
$this->server->close();
break;*/
case "banip":
$p = strtolower(array_shift($params));
switch($p){
case "pardon":
case "remove":
$ip = trim(implode($params));
$this->server->bannedIPs->remove($packet["ip"]);
$this->server->bannedIPs->save();
console("[INFO] IP \"$ip\" removed from ban list");
$this->server->reloadConfig();
break;
case "add":
case "ban":
$ip = trim(implode($params));
$this->server->bannedIPs->set($packet["ip"]);
$this->server->bannedIPs->save();
console("[INFO] IP \"$ip\" added to ban list");
$this->server->reloadConfig();
break;
case "reload":
$this->server->reloadConfig();
break;
case "list":
console("[INFO] IP ban list: ".implode(", ", $this->server->bannedIPs->getAll(true)));
break;
default:
console("[INFO] Usage: /banip <add | remove | list | reload> [IP]");
break;
}
break;
case "gamemode":
$s = trim(array_shift($params));
if($s == "" or (((int) $s) !== 0 and ((int) $s) !== 1)){
@ -149,50 +119,6 @@ class ConsoleAPI{
}
$this->server->api->chat->broadcast($s);
break;
case "whitelist":
$p = strtolower(array_shift($params));
switch($p){
case "remove":
$user = trim(implode(" ", $params));
$this->server->whitelist->remove($user);
$this->server->whitelist->save();
console("[INFO] Player \"$user\" removed from white-list");
$this->server->reloadConfig();
break;
case "add":
$user = trim(implode(" ", $params));
$this->server->whitelist->set($user);
$this->server->whitelist->save();
console("[INFO] Player \"$user\" added to white-list");
$this->server->reloadConfig();
break;
case "reload":
$this->server->reloadConfig();
break;
case "list":
if(($this->server->whitelist instanceof Config) === false){
console("[INFO] No White-list");
}else{
console("[INFO] White-list: ".implode(", ", $this->server->whitelist->getAll(true)));
}
break;
case "on":
case "true":
case "1":
console("[INFO] White-list turned on");
$this->server->api->setProperty("white-list", true);
break;
case "off":
case "false":
case "0":
console("[INFO] White-list turned off");
$this->server->api->setProperty("white-list", false);
break;
default:
console("[INFO] Usage: /whitelist <on | off | add | remove | reload | list> [username]");
break;
}
break;
case "save-all":
$this->server->save();
break;
@ -211,8 +137,6 @@ class ConsoleAPI{
console("[INFO] /invisible: Manages server visibility");
console("[INFO] /say: Broadcasts mesages");
console("[INFO] /save-all: Saves pending changes");
console("[INFO] /whitelist: Manages whitelisting");
console("[INFO] /banip: Manages IP ban");
console("[INFO] /stop: Stops the server");
//console("[INFO] /restart: Restarts the server");
foreach($this->help as $c => $h){

View File

@ -285,9 +285,11 @@ class Player{
break;
}
$o = $this->server->api->player->getOffline($this->username);
if($this->server->whitelist !== false and $this->server->whitelist->exists($this->username)){
if($this->server->whitelist === true and !$this->server->api->ban->inWhitelist($this->username)){
$this->close("\"\x1b[33m".$this->username."\x1b[0m\" not being on white-list", false);
break;
}elseif($this->server->api->ban->isBanned($this->username) or $this->server->api->ban->isIPBanned($this->ip)){
$this->close("\"\x1b[33m".$this->username."\x1b[0m\" is banned!", false);
}
$u = $this->server->api->player->get($this->username);
$c = $this->server->api->player->getByClientID($this->clientID);

View File

@ -65,7 +65,6 @@ class PocketMinecraftServer{
$this->scheduleCnt = 0;
$this->description = "";
$this->whitelist = false;
$this->bannedIPs = array();
$this->clients = array();
$this->spawn = array("x" => 128.5,"y" => 100,"z" => 128.5);
$this->time = 0;
@ -135,10 +134,7 @@ class PocketMinecraftServer{
}
public function reloadConfig(){
if($this->whitelist === true or ($this->whitelist instanceof Config)){
$this->whitelist = new Config(FILE_PATH."white-list.txt", CONFIG_LIST);
}
$this->bannedIPs = new Config(FILE_PATH."banned-ips.txt", CONFIG_LIST);
}
public function debugInfo($console = false){
@ -412,7 +408,7 @@ class PocketMinecraftServer{
), false, $packet["ip"], $packet["port"]);
break;
}
if($this->bannedIPs->exists($packet["ip"])){
if($this->api->ban->isIPBanned($packet["ip"])){
$this->send(0x1c, array(
$data[0],
$this->serverID,
@ -439,7 +435,7 @@ class PocketMinecraftServer{
$this->custom["times_".$CID] = ($this->custom["times_".$CID] + 1) % strlen($this->description);
break;
case 0x05:
if($this->bannedIPs->exists($packet["ip"]) or count($this->clients) >= $this->maxClients){
if($this->api->ban->isIPBanned($packet["ip"]) or count($this->clients) >= $this->maxClients){
$this->send(0x80, array(
0,
0x00,
@ -475,7 +471,7 @@ class PocketMinecraftServer{
}
break;
case 0x07:
if($this->bannedIPs->exists($packet["ip"]) or count($this->clients) >= $this->maxClients){
if($this->api->ban->isIPBanned($packet["ip"]) or count($this->clients) >= $this->maxClients){
$this->send(0x80, array(
0,
0x00,