Implemented plugin loading whitelist/blacklist by config file (#2783)

This commit is contained in:
Dylan T 2019-03-24 16:18:13 +00:00 committed by GitHub
parent aea775c7c6
commit 9c76fb7d96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 131 additions and 4 deletions

1
.gitignore vendored
View File

@ -11,6 +11,7 @@ crashdumps/*
*.phar
server.properties
/pocketmine.yml
/plugin_list.yml
memory_dumps/*
resource_packs/

View File

@ -0,0 +1,8 @@
#This configuration file allows you to control which plugins are loaded on your server.
#List behaviour
# - blacklist: Only plugins which ARE NOT listed will load.
# - whitelist: Only plugins which ARE listed will load.
mode: blacklist
#List names of plugins here.
plugins: []

View File

@ -82,6 +82,7 @@ use pocketmine\permission\DefaultPermissions;
use pocketmine\permission\PermissionManager;
use pocketmine\plugin\PharPluginLoader;
use pocketmine\plugin\Plugin;
use pocketmine\plugin\PluginGraylist;
use pocketmine\plugin\PluginLoadOrder;
use pocketmine\plugin\PluginManager;
use pocketmine\plugin\ScriptPluginLoader;
@ -106,6 +107,7 @@ use function array_shift;
use function array_sum;
use function base64_encode;
use function bin2hex;
use function copy;
use function count;
use function define;
use function explode;
@ -147,6 +149,7 @@ use function strtolower;
use function time;
use function touch;
use function trim;
use function yaml_parse;
use const DIRECTORY_SEPARATOR;
use const PHP_EOL;
use const PHP_INT_MAX;
@ -1227,7 +1230,19 @@ class Server{
$this->resourceManager = new ResourcePackManager($this->getDataPath() . "resource_packs" . DIRECTORY_SEPARATOR, $this->logger);
$this->pluginManager = new PluginManager($this, ((bool) $this->getProperty("plugins.legacy-data-dir", true)) ? null : $this->getDataPath() . "plugin_data" . DIRECTORY_SEPARATOR);
$pluginGraylist = null;
$graylistFile = $this->dataPath . "plugin_list.yml";
if(!file_exists($graylistFile)){
copy(\pocketmine\RESOURCE_PATH . 'plugin_list.yml', $graylistFile);
}
try{
$pluginGraylist = PluginGraylist::fromArray(yaml_parse(file_get_contents($graylistFile)));
}catch(\InvalidArgumentException $e){
$this->logger->emergency("Failed to load $graylistFile: " . $e->getMessage());
$this->forceShutdown();
return;
}
$this->pluginManager = new PluginManager($this, ((bool) $this->getProperty("plugins.legacy-data-dir", true)) ? null : $this->getDataPath() . "plugin_data" . DIRECTORY_SEPARATOR, $pluginGraylist);
$this->profilingTickRate = (float) $this->getProperty("settings.profile-report-trigger", 20);
$this->pluginManager->registerInterface(new PharPluginLoader($this->autoloader));
$this->pluginManager->registerInterface(new ScriptPluginLoader());

View File

@ -0,0 +1,91 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* 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.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\plugin;
use Particle\Validator\Validator;
use function array_filter;
use function array_flip;
use function count;
use function implode;
class PluginGraylist{
/** @var string[] */
private $plugins;
/** @var bool */
private $isWhitelist = false;
public function __construct(array $plugins = [], bool $whitelist = false){
$this->plugins = array_flip($plugins);
$this->isWhitelist = $whitelist;
}
/**
* @return string[]
*/
public function getPlugins() : array{
return array_flip($this->plugins);
}
/**
* @return bool
*/
public function isWhitelist() : bool{
return $this->isWhitelist;
}
/**
* Returns whether the given name is permitted by this graylist.
*
* @param string $name
*
* @return bool
*/
public function isAllowed(string $name) : bool{
return $this->isWhitelist() === isset($this->plugins[$name]);
}
public static function fromArray(array $array) : PluginGraylist{
$v = new Validator();
$v->required("mode")->inArray(['whitelist', 'blacklist'], true);
$v->required("plugins")->isArray()->allowEmpty(true)->callback(function(array $elements) : bool{ return count(array_filter($elements, '\is_string')) === count($elements); });
$result = $v->validate($array);
if($result->isNotValid()){
$messages = [];
foreach($result->getFailures() as $f){
$messages[] = $f->format();
}
throw new \InvalidArgumentException("Invalid data: " . implode(", ", $messages));
}
return new PluginGraylist($array["plugins"], $array["mode"] === 'whitelist');
}
public function toArray() : array{
return [
"mode" => $this->isWhitelist ? 'whitelist' : 'blacklist',
"plugins" => $this->plugins
];
}
}

View File

@ -80,12 +80,15 @@ class PluginManager{
/** @var string|null */
private $pluginDataDirectory;
/** @var PluginGraylist|null */
private $graylist;
/**
* @param Server $server
* @param null|string $pluginDataDirectory
* @param Server $server
* @param null|string $pluginDataDirectory
* @param PluginGraylist|null $graylist
*/
public function __construct(Server $server, ?string $pluginDataDirectory){
public function __construct(Server $server, ?string $pluginDataDirectory, ?PluginGraylist $graylist = null){
$this->server = $server;
$this->pluginDataDirectory = $pluginDataDirectory;
if($this->pluginDataDirectory !== null){
@ -95,6 +98,8 @@ class PluginManager{
throw new \RuntimeException("Plugin data path $this->pluginDataDirectory exists and is not a directory");
}
}
$this->graylist = $graylist;
}
/**
@ -265,6 +270,13 @@ class PluginManager{
}
}
if($this->graylist !== null and !$this->graylist->isAllowed($name)){
$this->server->getLogger()->notice($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [
$name,
"Disallowed by graylist"
]));
continue;
}
$plugins[$name] = $file;
$softDependencies[$name] = $description->getSoftDepend();