PocketMine-MP 5.30.2 git-98f04176111e5ecab5e8814ffc69d992bfb64939
Loading...
Searching...
No Matches
LoginPacket.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol;
16
19use function strlen;
20
21class LoginPacket extends DataPacket implements ServerboundPacket{
22 public const NETWORK_ID = ProtocolInfo::LOGIN_PACKET;
23
24 public int $protocol;
25 public string $authInfoJson;
26 public string $clientDataJwt;
27
31 public static function create(int $protocol, string $authInfoJson, string $clientDataJwt) : self{
32 $result = new self;
33 $result->protocol = $protocol;
34 $result->authInfoJson = $authInfoJson;
35 $result->clientDataJwt = $clientDataJwt;
36 return $result;
37 }
38
39 public function canBeSentBeforeLogin() : bool{
40 return true;
41 }
42
43 protected function decodePayload(PacketSerializer $in) : void{
44 $this->protocol = $in->getInt();
45 $this->decodeConnectionRequest($in->getString());
46 }
47
48 protected function decodeConnectionRequest(string $binary) : void{
49 $connRequestReader = new BinaryStream($binary);
50
51 $authInfoJsonLength = $connRequestReader->getLInt();
52 if($authInfoJsonLength <= 0){
53 //technically this is always positive; the problem results because getLInt() is implicitly signed
54 //this is inconsistent with many other methods, but we can't do anything about that for now
55 throw new PacketDecodeException("Length of auth info JSON must be positive");
56 }
57 $this->authInfoJson = $connRequestReader->get($authInfoJsonLength);
58
59 $clientDataJwtLength = $connRequestReader->getLInt();
60 if($clientDataJwtLength <= 0){
61 //technically this is always positive; the problem results because getLInt() is implicitly signed
62 //this is inconsistent with many other methods, but we can't do anything about that for now
63 throw new PacketDecodeException("Length of clientData JWT must be positive");
64 }
65 $this->clientDataJwt = $connRequestReader->get($clientDataJwtLength);
66 }
67
68 protected function encodePayload(PacketSerializer $out) : void{
69 $out->putInt($this->protocol);
70 $out->putString($this->encodeConnectionRequest());
71 }
72
73 protected function encodeConnectionRequest() : string{
74 $connRequestWriter = new BinaryStream();
75
76 $connRequestWriter->putLInt(strlen($this->authInfoJson));
77 $connRequestWriter->put($this->authInfoJson);
78
79 $connRequestWriter->putLInt(strlen($this->clientDataJwt));
80 $connRequestWriter->put($this->clientDataJwt);
81
82 return $connRequestWriter->getBuffer();
83 }
84
85 public function handle(PacketHandlerInterface $handler) : bool{
86 return $handler->handleLogin($this);
87 }
88}
handle(PacketHandlerInterface $handler)
static create(int $protocol, string $authInfoJson, string $clientDataJwt)