mince/src/RemoteV2.php

132 lines
4.2 KiB
PHP

<?php
namespace Mince;
use RuntimeException;
use Index\Serialisation\Serialiser;
class RemoteV2 {
public function __construct(
private string $endPoint,
private string $secretKey
) {}
public function getInfo(): object {
return $this->getRequest('GET', '/');
}
public function getWhitelist(string $serverId): object {
return $this->getRequest('GET', '/whitelist', ['server' => $serverId]);
}
public function addToWhitelist(string $serverId, array $userNames): object {
return $this->postRequest('POST', '/whitelist', [
'server' => $serverId,
'names' => json_encode($userNames),
]);
}
public function removeFromWhitelist(string $serverId, array $userNames): object {
return $this->getRequest('DELETE', '/whitelist', [
'server' => $serverId,
'names' => json_encode($userNames),
]);
}
public function createSignature(string $method, string $path, array $params, int $time = -1): string {
if($time < 0)
$time = time();
ksort($params);
$compare = [];
// other sides supports arrays, not gonna bother here
foreach($params as $name => $value)
$compare[] = "{$name}:{$value}";
$input = "{$time}%{$method} {$path}%" . implode('#', $compare);
return Serialiser::uriBase64()->serialise(
hash_hmac('sha256', $input, $this->secretKey, true)
);
}
public function getRequest(string $method, string $path, array $params = []): mixed {
$time = time();
$sign = $this->createSignature($method, $path, $params, $time);
$url = $this->endPoint . $path;
if(!empty($params))
$url .= '?' . http_build_query($params);
$request = curl_init($url);
curl_setopt_array($request, [
CURLOPT_AUTOREFERER => false,
CURLOPT_FAILONERROR => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TCP_FASTOPEN => true,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_MAXREDIRS => 2,
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
CURLOPT_TIMEOUT => 5,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_USERAGENT => 'mc.flashii.net',
CURLOPT_HTTPHEADER => [
"X-Mince-Signature: {$sign}",
"X-Mince-Timestamp: {$time}",
],
]);
$response = curl_exec($request);
curl_close($request);
if(empty($response))
throw new RuntimeException('Empty response.');
$response = json_decode($response);
if(!empty($response->error))
throw new RuntimeException($response->error);
return $response;
}
public function postRequest(string $method, string $path, array $params): mixed {
$time = time();
$sign = $this->createSignature($method, $path, $params, $time);
$url = $this->endPoint . $path;
$request = curl_init($url);
curl_setopt_array($request, [
CURLOPT_AUTOREFERER => false,
CURLOPT_FAILONERROR => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TCP_FASTOPEN => true,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_MAXREDIRS => 2,
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
CURLOPT_TIMEOUT => 5,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_USERAGENT => 'mc.flashii.net',
CURLOPT_HTTPHEADER => [
"X-Mince-Signature: {$sign}",
"X-Mince-Timestamp: {$time}",
],
]);
$response = curl_exec($request);
curl_close($request);
if(empty($response))
throw new RuntimeException('Empty response.');
$response = json_decode($response);
if(!empty($response->error))
throw new RuntimeException($response->error);
return $response;
}
}