Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie data #229

Open
wants to merge 35 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3e2e59e
Cookie added
ismaileke Jul 28, 2024
963c533
Cookie added 2
ismaileke Jul 28, 2024
c22a7d1
Update OpenConnectionRequest2.php
ismaileke Jul 28, 2024
7425ec5
extra byte
ismaileke Jul 28, 2024
80e89f0
some fixes
ismaileke Jul 29, 2024
c58e264
updated unconnected message handler
ismaileke Jul 29, 2024
967555e
oops
ismaileke Jul 29, 2024
78f5cf5
Update OpenConnectionRequest2.php
ismaileke Jul 29, 2024
3cdf46e
Cookie Class
ismaileke Jul 30, 2024
1c94a8c
againagainagain
ismaileke Jul 30, 2024
440196c
Update Cookie.php
ismaileke Jul 30, 2024
1287f4c
Update Cookie.php
ismaileke Jul 30, 2024
3764e4b
for now
ismaileke Aug 3, 2024
312e11e
fix
ismaileke Aug 3, 2024
9078818
some fixes
ismaileke Aug 3, 2024
9c11bb4
okay
ismaileke Aug 3, 2024
ba4c754
is it right to add that lol
ismaileke Aug 3, 2024
9555243
client supports security
ismaileke Aug 7, 2024
ec8532b
added static function
ismaileke Aug 8, 2024
884bbff
non-static funcs
ismaileke Aug 9, 2024
b6c85e7
fix nullable
ismaileke Aug 9, 2024
15d1b13
use method
ismaileke Aug 9, 2024
75e8526
i forgot
ismaileke Aug 9, 2024
ff2a37e
fix but not finished
ismaileke Aug 9, 2024
8e6c677
uhhh
ismaileke Aug 9, 2024
f8250af
short usage
ismaileke Aug 10, 2024
df75ce4
Update src/protocol/OpenConnectionReply1.php
ismaileke Aug 10, 2024
64cb140
some fixes are not yet complete
ismaileke Aug 13, 2024
b9bb269
random_int
ismaileke Aug 13, 2024
65e2da8
Limits
ismaileke Aug 13, 2024
0e8b732
new name CookieCache
ismaileke Aug 13, 2024
bd80d49
delete the cookie when player log out
ismaileke Sep 20, 2024
cb35ab3
CookieCache::check() fix?
ismaileke Sep 20, 2024
41ac282
snake case :|
ismaileke Sep 20, 2024
7d1586f
pls gimme blue tick
ismaileke Sep 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/protocol/OfflineMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ protected function writeMagic(BinaryStream $out){
public function isValid() : bool{
return $this->magic === self::MAGIC;
}
}
}
7 changes: 3 additions & 4 deletions src/protocol/OpenConnectionReply1.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ class OpenConnectionReply1 extends OfflineMessage{

public int $serverID;
public bool $serverSecurity;
ismaileke marked this conversation as resolved.
Show resolved Hide resolved
public ?int $cookie;
public ?int $cookie = null;
public int $mtuSize;

public static function create(int $serverId, ?int $cookie, int $mtuSize) : self{
$result = new self;
$result->serverID = $serverId;
$result->serverSecurity = $cookie !== null;
$result->cookie = $cookie;
$result->mtuSize = $mtuSize;
return $result;
Expand All @@ -38,8 +37,8 @@ public static function create(int $serverId, ?int $cookie, int $mtuSize) : self{
protected function encodePayload(PacketSerializer $out) : void{
$this->writeMagic($out);
$out->putLong($this->serverID);
$out->putByte($this->serverSecurity ? 1 : 0);
if ($this->serverSecurity && $this->cookie !== null) {
$out->putByte($this->cookie !== null ? 1 : 0);
if ($this->cookie !== null) {
$out->putInt($this->cookie);
}
$out->putShort($this->mtuSize);
Expand Down
8 changes: 4 additions & 4 deletions src/server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use raklib\protocol\NACK;
use raklib\protocol\Packet;
use raklib\protocol\PacketSerializer;
use raklib\utils\Cookie;
use raklib\utils\CookieCache;
use raklib\utils\ExceptionTraceCleaner;
use raklib\utils\InternetAddress;
use function asort;
Expand Down Expand Up @@ -78,7 +78,7 @@ class Server implements ServerInterface{

protected int $nextSessionId = 0;

public ?Cookie $cookie = null;
public ?CookieCache $cookie = null;

/**
* @phpstan-param positive-int $recvMaxSplitParts
Expand All @@ -104,7 +104,7 @@ public function __construct(
$this->unconnectedMessageHandler = new UnconnectedMessageHandler($this, $protocolAcceptor);

// If you don't want to use security on the server, just delete this line.
$this->cookie = new Cookie();
$this->cookie = new CookieCache();
}

public function getPort() : int{
Expand All @@ -119,7 +119,7 @@ public function getLogger() : \Logger{
return $this->logger;
}

public function getCookie() : ?Cookie {
public function getCookie() : ?CookieCache {
return $this->cookie;
}

Expand Down
9 changes: 4 additions & 5 deletions src/server/UnconnectedMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use raklib\protocol\UnconnectedPingOpenConnections;
use raklib\protocol\UnconnectedPong;
use raklib\utils\InternetAddress;
use raklib\utils\Cookie;
use raklib\utils\CookieCache;
use function get_class;
use function min;
use function ord;
Expand Down Expand Up @@ -83,9 +83,8 @@
$this->server->getLogger()->notice("Refused connection from $address due to incompatible RakNet protocol version (version $packet->protocol)");
}else{
$cookie = null;
if ($this->server->getCookie() instanceof Cookie) {
$this->server->getCookie()->add($address);
$cookie = $this->server->getCookie()->get($address);
if ($this->server->getCookie() instanceof CookieCache) {
$cookie = $this->server->getCookie()->add($address);
}
//IP header size (20 bytes) + UDP header size (8 bytes)
$this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), $cookie, $packet->mtuSize + 28), $address);
Expand All @@ -104,8 +103,8 @@
$this->server->getLogger()->debug("Not creating session for $address due to session already opened");
return true;
}
if ($this->server->getCookie() instanceof Cookie) { // womp womp
if ($this->server->getCookie() instanceof CookieCache) { // womp womp
if (!$this->server->getCookie()->check($address, $packet->cookie)) {

Check failure on line 107 in src/server/UnconnectedMessageHandler.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Parameter #2 $cookie of method raklib\utils\CookieCache::check() expects int, int|null given.

Check failure on line 107 in src/server/UnconnectedMessageHandler.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Parameter #2 $cookie of method raklib\utils\CookieCache::check() expects int, int|null given.
// Disconnect if OpenConnectionReply1 and the cookie in the OpenConnectionRequest2 packet do not match
$this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies");
return true;
Expand Down
19 changes: 6 additions & 13 deletions src/utils/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,13 @@
use function random_int;
use function crc32;

final class Cookie{
final class CookieCache{

/**
* @var array<string, int> $cookies
*/
private array $cookies = [];
ismaileke marked this conversation as resolved.
Show resolved Hide resolved

public function get(InternetAddress $address) : ?int{
if (isset($this->cookies[$address->toString()])) {
return $this->cookies[$address->toString()];
}
return null;
}

public function check(InternetAddress $address, int $cookie) : bool{
$addressStr = $address->toString();

Expand All @@ -48,13 +41,13 @@ public function check(InternetAddress $address, int $cookie) : bool{
return false;
}

public function add(InternetAddress $address) : void{
if (!isset($this->cookies[$address->toString()])) {
$this->cookies[$address->toString()] = $this->generate($address);
}
public function add(InternetAddress $address) : int{
$cookie = $this->generate($address);
$this->cookies[$address->toString()] = $cookie;
return $cookie;
}

private function generate(InternetAddress $address) : int{
return crc32(Binary::writeLInt(random_int(0, 0xffffffff)) . Binary::writeLShort($address->getPort()) . $address->getIp());
return crc32(Binary::writeLInt(random_int(0, 0xffffffff)));
ismaileke marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading