2016-09-03 04:24:22 +05:30
|
|
|
<?php
|
2019-12-05 03:45:45 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-09-03 04:24:22 +05:30
|
|
|
namespace api\modules\session\controllers;
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
use api\controllers\Controller;
|
2016-09-05 20:25:38 +05:30
|
|
|
use api\modules\session\exceptions\ForbiddenOperationException;
|
2016-09-08 15:37:43 +05:30
|
|
|
use api\modules\session\exceptions\IllegalArgumentException;
|
2016-09-05 20:25:38 +05:30
|
|
|
use api\modules\session\exceptions\SessionServerException;
|
2016-09-06 22:40:42 +05:30
|
|
|
use api\modules\session\filters\RateLimiter;
|
2016-09-06 15:26:39 +05:30
|
|
|
use api\modules\session\models\HasJoinedForm;
|
2016-09-03 04:24:22 +05:30
|
|
|
use api\modules\session\models\JoinForm;
|
2016-09-05 20:25:38 +05:30
|
|
|
use api\modules\session\models\protocols\LegacyJoin;
|
2016-09-06 15:26:39 +05:30
|
|
|
use api\modules\session\models\protocols\ModernHasJoined;
|
2016-09-05 20:25:38 +05:30
|
|
|
use api\modules\session\models\protocols\ModernJoin;
|
2016-09-08 15:37:43 +05:30
|
|
|
use common\models\Account;
|
2016-09-06 15:26:39 +05:30
|
|
|
use common\models\Textures;
|
2016-09-08 15:37:43 +05:30
|
|
|
use Ramsey\Uuid\Uuid;
|
2016-09-05 20:25:38 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\web\Response;
|
2016-09-03 04:24:22 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
class SessionController extends Controller {
|
2016-09-03 04:24:22 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function behaviors(): array {
|
2016-09-03 04:24:22 +05:30
|
|
|
$behaviors = parent::behaviors();
|
|
|
|
unset($behaviors['authenticator']);
|
2016-09-06 22:40:42 +05:30
|
|
|
$behaviors['rateLimiting'] = [
|
|
|
|
'class' => RateLimiter::class,
|
|
|
|
'only' => ['has-joined', 'has-joined-legacy'],
|
2016-09-19 03:31:19 +05:30
|
|
|
'authserverDomain' => Yii::$app->params['authserverHost'],
|
2016-09-06 22:40:42 +05:30
|
|
|
];
|
2016-09-03 04:24:22 +05:30
|
|
|
|
|
|
|
return $behaviors;
|
|
|
|
}
|
|
|
|
|
2019-12-05 03:45:45 +05:30
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws ForbiddenOperationException
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
*/
|
|
|
|
public function actionJoin(): array {
|
2016-09-05 20:25:38 +05:30
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
|
|
|
|
|
|
|
$data = Yii::$app->request->post();
|
|
|
|
$protocol = new ModernJoin($data['accessToken'] ?? '', $data['selectedProfile'] ?? '', $data['serverId'] ?? '');
|
|
|
|
$joinForm = new JoinForm($protocol);
|
2019-12-05 03:45:45 +05:30
|
|
|
$joinForm->join(); // will throw an exception in case of any error
|
2016-09-03 04:24:22 +05:30
|
|
|
|
|
|
|
return ['id' => 'OK'];
|
|
|
|
}
|
|
|
|
|
2019-12-05 03:45:45 +05:30
|
|
|
public function actionJoinLegacy(): string {
|
2016-09-05 20:25:38 +05:30
|
|
|
Yii::$app->response->format = Response::FORMAT_RAW;
|
|
|
|
|
|
|
|
$data = Yii::$app->request->get();
|
|
|
|
$protocol = new LegacyJoin($data['user'] ?? '', $data['sessionId'] ?? '', $data['serverId'] ?? '');
|
|
|
|
$joinForm = new JoinForm($protocol);
|
|
|
|
try {
|
|
|
|
$joinForm->join();
|
|
|
|
} catch (SessionServerException $e) {
|
|
|
|
Yii::$app->response->statusCode = $e->statusCode;
|
|
|
|
if ($e instanceof ForbiddenOperationException) {
|
|
|
|
$message = 'Ely.by authorization required';
|
|
|
|
} else {
|
|
|
|
$message = $e->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'OK';
|
2016-09-03 04:24:22 +05:30
|
|
|
}
|
|
|
|
|
2019-12-05 03:45:45 +05:30
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws ForbiddenOperationException
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
*/
|
|
|
|
public function actionHasJoined(): array {
|
2016-09-06 15:26:39 +05:30
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON;
|
|
|
|
|
|
|
|
$data = Yii::$app->request->get();
|
|
|
|
$protocol = new ModernHasJoined($data['username'] ?? '', $data['serverId'] ?? '');
|
|
|
|
$hasJoinedForm = new HasJoinedForm($protocol);
|
|
|
|
$account = $hasJoinedForm->hasJoined();
|
|
|
|
$textures = new Textures($account);
|
|
|
|
|
|
|
|
return $textures->getMinecraftResponse();
|
|
|
|
}
|
|
|
|
|
2019-12-05 03:45:45 +05:30
|
|
|
public function actionHasJoinedLegacy(): string {
|
2016-09-06 15:26:39 +05:30
|
|
|
Yii::$app->response->format = Response::FORMAT_RAW;
|
|
|
|
|
|
|
|
$data = Yii::$app->request->get();
|
|
|
|
$protocol = new ModernHasJoined($data['user'] ?? '', $data['serverId'] ?? '');
|
|
|
|
$hasJoinedForm = new HasJoinedForm($protocol);
|
|
|
|
try {
|
|
|
|
$hasJoinedForm->hasJoined();
|
2017-02-13 16:14:14 +05:30
|
|
|
} catch (ForbiddenOperationException $e) {
|
|
|
|
return 'NO';
|
2016-09-06 15:26:39 +05:30
|
|
|
} catch (SessionServerException $e) {
|
|
|
|
Yii::$app->response->statusCode = $e->statusCode;
|
|
|
|
|
2017-02-13 16:14:14 +05:30
|
|
|
return $e->getMessage();
|
2016-09-06 15:26:39 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return 'YES';
|
|
|
|
}
|
|
|
|
|
2019-12-05 03:45:45 +05:30
|
|
|
/**
|
|
|
|
* @param string $uuid
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws ForbiddenOperationException
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
*/
|
|
|
|
public function actionProfile(string $uuid): array {
|
2016-09-08 15:37:43 +05:30
|
|
|
try {
|
|
|
|
$uuid = Uuid::fromString($uuid)->toString();
|
2018-04-18 02:17:25 +05:30
|
|
|
} catch (\InvalidArgumentException $e) {
|
2016-09-08 15:37:43 +05:30
|
|
|
throw new IllegalArgumentException('Invalid uuid format.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = Account::findOne(['uuid' => $uuid]);
|
|
|
|
if ($account === null) {
|
|
|
|
throw new ForbiddenOperationException('Invalid uuid.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return (new Textures($account))->getMinecraftResponse();
|
|
|
|
}
|
|
|
|
|
2016-09-03 04:24:22 +05:30
|
|
|
}
|