PocketMine-MP 5.15.1 git-ed158f8a1b0cfe334ac5f45febc0f633602014f2
SendUsageTask.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\stats;
25
35use Ramsey\Uuid\Uuid;
36use function array_map;
37use function array_values;
38use function count;
39use function json_encode;
40use function md5;
41use function microtime;
42use function php_uname;
43use function strlen;
44use const JSON_THROW_ON_ERROR;
45use const PHP_VERSION;
46
48
49 public const TYPE_OPEN = 1;
50 public const TYPE_STATUS = 2;
51 public const TYPE_CLOSE = 3;
52
53 public string $endpoint;
54 public string $data;
55
60 public function __construct(Server $server, int $type, array $playerList = []){
61 $endpoint = "http://" . $server->getConfigGroup()->getPropertyString(YmlServerProperties::ANONYMOUS_STATISTICS_HOST, "stats.pocketmine.net") . "/";
62
63 $data = [];
64 $data["uniqueServerId"] = $server->getServerUniqueId()->toString();
65 $data["uniqueMachineId"] = Utils::getMachineUniqueId()->toString();
66 $data["uniqueRequestId"] = Uuid::uuid3($server->getServerUniqueId()->toString(), microtime(false))->toString();
67
68 switch($type){
69 case self::TYPE_OPEN:
70 $data["event"] = "open";
71
72 $version = VersionInfo::VERSION();
73
74 $data["server"] = [
75 "port" => $server->getPort(),
76 "software" => $server->getName(),
77 "fullVersion" => $version->getFullVersion(true),
78 "version" => $version->getFullVersion(false),
79 "build" => $version->getBuild(),
80 "api" => $server->getApiVersion(),
81 "minecraftVersion" => $server->getVersion(),
83 ];
84
85 $data["system"] = [
86 "operatingSystem" => Utils::getOS(),
87 "cores" => Utils::getCoreCount(),
88 "phpVersion" => PHP_VERSION,
89 "machine" => php_uname("a"),
90 "release" => php_uname("r"),
91 "platform" => php_uname("i")
92 ];
93
94 $data["players"] = [
95 "count" => 0,
96 "limit" => $server->getMaxPlayers()
97 ];
98
99 $plugins = [];
100
101 foreach($server->getPluginManager()->getPlugins() as $p){
102 $d = $p->getDescription();
103
104 $plugins[$d->getName()] = [
105 "name" => $d->getName(),
106 "version" => $d->getVersion(),
107 "enabled" => $p->isEnabled()
108 ];
109 }
110
111 $data["plugins"] = $plugins;
112
113 break;
114 case self::TYPE_STATUS:
115 $data["event"] = "status";
116
117 $data["server"] = [
118 "ticksPerSecond" => $server->getTicksPerSecondAverage(),
119 "tickUsage" => $server->getTickUsageAverage(),
120 "ticks" => $server->getTick()
121 ];
122
123 //This anonymizes the user ids so they cannot be reversed to the original
124 $playerList = array_map('md5', $playerList);
125
126 $players = array_map(function(Player $p) : string{ return md5($p->getUniqueId()->getBytes()); }, $server->getOnlinePlayers());
127
128 $data["players"] = [
129 "count" => count($players),
130 "limit" => $server->getMaxPlayers(),
131 "currentList" => array_values($players),
132 "historyList" => array_values($playerList)
133 ];
134
136 $data["system"] = [
137 "mainMemory" => $info[0],
138 "totalMemory" => $info[1],
139 "availableMemory" => $info[2],
140 "threadCount" => Process::getThreadCount()
141 ];
142
143 break;
144 case self::TYPE_CLOSE:
145 $data["event"] = "close";
146 $data["crashing"] = $server->isRunning();
147 break;
148 }
149
150 $this->endpoint = $endpoint . "api/post";
151 $this->data = json_encode($data, /*JSON_PRETTY_PRINT |*/ JSON_THROW_ON_ERROR);
152 }
153
154 public function onRun() : void{
155 Internet::postURL($this->endpoint, $this->data, 5, [
156 "Content-Type: application/json",
157 "Content-Length: " . strlen($this->data)
158 ]);
159 }
160}
__construct(Server $server, int $type, array $playerList=[])
static getAdvancedMemoryUsage()
Definition: Process.php:55
static getMachineUniqueId(string $extra="")
Definition: Utils.php:199
static getOS(bool $recalculate=false)
Definition: Utils.php:275