Skip to content

Commit

Permalink
some fixes are not yet complete
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaileke committed Aug 13, 2024
1 parent df75ce4 commit 64cb140
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 27 deletions.
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;
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 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool
$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,7 +103,7 @@ private function handle(OfflineMessage $packet, InternetAddress $address) : bool
$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");
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 = [];

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)));
}
}

0 comments on commit 64cb140

Please sign in to comment.