Skip to content

Instantly share code, notes, and snippets.

@PEMapModder
Created October 25, 2015 16:02
Show Gist options
  • Select an option

  • Save PEMapModder/463c74eb084669826c20 to your computer and use it in GitHub Desktop.

Select an option

Save PEMapModder/463c74eb084669826c20 to your computer and use it in GitHub Desktop.
name: BlockedIP
author: PEMapModder
version: 1.0.0
api: [1.0.0]
main: BlcokedIP\BlockedIP
<?php
namespace BlockedIP;
use pocketmine\Player;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerPreLoginEVent;
use pocketmine\plugin\PluginBase;
class BlockedIP extends PluginBase implements Listener{
private $blockedIPs = [];
private $whiteIPs = [];
public function onLogin(PlayerPreLoginEvent $event){
if($this->checkIp($event->getPlayer(), $reason) === self::IP_CHECK_BANNED){
$event->setCancelled();
$event->setKickMessage($reason->reason);
}
}
// API START
public function addBlockedIp($ip, $reason){
$this->blockedIPs[$ip] = $reason;
foreach($this->getServer()->getOnlinePlayers() as $player){
if($player->getAddress() === $ip){
$player->kick($reason->reason);
}
}
}
public function whitelistIp($ip){
$this->whiteIPs[$ip] = true;
}
const IP_CHECK_BANNED = 1;
const IP_CHECK_OK = 0;
const IP_CHECK_LOADING = 2;
public function checkIp(Player $player, &$reason = null){
if(isset($this->blockedIPs[$ip = $player->getAddress()])){
$reason = $this->blockedIPs[$ip]->reason;
return self::IP_CHECK_BANNED;
}
if(isset($this->whiteIPs[$ip])){
return self::IP_CHECK_OK;
}
$task = new CheckAddressTask;
$task->key = $this->getConfig()->get("apiKey");
$task->ip = $ip;
$this->getServer()->getScheduler()->scheduleAsyncTask($task);
return self::IP_CHECK_LOADING;
}
// API END
}
<?php
namespace BlockedIP;
use pocketmine\scheduler\AsyncTask;
use pocketmine\utils\Utils;
class CheckAddressTask extends AsyncTask{
public $key;
public $ip;
private $success = false;
public function onRun(){
$result = Utils::getURL("http://www.example.com/blockscript/detector.php?blockscript=api&api_key=$this->key&action=test_ipv4&ip=$this->ip");
$xml = simplexml_load_string($result);
if(is_object($xml)){
$status = $xml->status;
if($status === "SUCCESS"){
$this->success = true;
$ip = $xml->ip;
if($ip->blocked === "YES"){
$this->setResult($ip);
}
}
}
}
public function onCompletion(Server $server){
$main = $server->getPluginManager()->getPlugin("BlockedIP");
if($main instanceof BlockedIP){
$result = $this->getResult();
if(is_object($result)){
$main->addBlockedIp($this->ip, $result);
}else{
$main->whitelistIp($this->ip);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment