Allow specifying a key for encrypted resource packs (#5297)

This commit is contained in:
BrandPVP 2022-09-28 19:27:33 +03:00 committed by GitHub
parent 1c7b1e9e5d
commit b21cd82e94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 2 deletions

View File

@ -10,3 +10,4 @@ resource_stack:
# - natural.zip
# - vanilla.zip
#If you want to force clients to use vanilla resources, you must place a vanilla resource pack in your resources folder and add it to the stack here.
#To specify a resource encryption key, put the key in the <resource>.key file alongside the resource pack. Example: vanilla.zip.key

View File

@ -65,9 +65,19 @@ class ResourcePacksPacketHandler extends PacketHandler{
){}
public function setUp() : void{
$resourcePackEntries = array_map(static function(ResourcePack $pack) : ResourcePackInfoEntry{
$resourcePackEntries = array_map(function(ResourcePack $pack) : ResourcePackInfoEntry{
//TODO: more stuff
return new ResourcePackInfoEntry($pack->getPackId(), $pack->getPackVersion(), $pack->getPackSize(), "", "", "", false);
$encryptionKey = $this->resourcePackManager->getPackEncryptionKey($pack->getPackId());
return new ResourcePackInfoEntry(
$pack->getPackId(),
$pack->getPackVersion(),
$pack->getPackSize(),
$encryptionKey ?? "",
"",
$pack->getPackId(),
false
);
}, $this->resourcePackManager->getResourceStack());
//TODO: support forcing server packs
$this->session->sendDataPacket(ResourcePacksInfoPacket::create($resourcePackEntries, [], $this->resourcePackManager->resourcePacksRequired(), false, false));

View File

@ -23,12 +23,14 @@ declare(strict_types=1);
namespace pocketmine\resourcepacks;
use pocketmine\errorhandler\ErrorToExceptionHandler;
use pocketmine\utils\Config;
use Webmozart\PathUtil\Path;
use function array_keys;
use function copy;
use function count;
use function file_exists;
use function file_get_contents;
use function gettype;
use function is_array;
use function is_dir;
@ -49,6 +51,12 @@ class ResourcePackManager{
/** @var ResourcePack[] */
private array $uuidList = [];
/**
* @var string[]
* @phpstan-var array<string, string>
*/
private array $encryptionKeys = [];
/**
* @param string $path Path to resource-packs directory.
*/
@ -106,6 +114,17 @@ class ResourcePackManager{
if($newPack instanceof ResourcePack){
$this->resourcePacks[] = $newPack;
$this->uuidList[strtolower($newPack->getPackId())] = $newPack;
$keyPath = Path::join($this->path, $pack . ".key");
if(file_exists($keyPath)){
try{
$this->encryptionKeys[strtolower($newPack->getPackId())] = ErrorToExceptionHandler::trapAndRemoveFalse(
fn() => file_get_contents($keyPath)
);
}catch(\ErrorException $e){
throw new ResourcePackException("Could not read encryption key file: " . $e->getMessage(), 0, $e);
}
}
}else{
throw new ResourcePackException("Format not recognized");
}
@ -153,4 +172,11 @@ class ResourcePackManager{
public function getPackIdList() : array{
return array_keys($this->uuidList);
}
/**
* Returns the key with which the pack was encrypted, or null if the pack has no key.
*/
public function getPackEncryptionKey(string $id) : ?string{
return $this->encryptionKeys[strtolower($id)] ?? null;
}
}