Skip to content

Commit

Permalink
added static function
Browse files Browse the repository at this point in the history
  • Loading branch information
ismaileke committed Aug 8, 2024
1 parent 9555243 commit ec8532b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/protocol/OpenConnectionReply1.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class OpenConnectionReply1 extends OfflineMessage{

public int $serverID;
public bool $serverSecurity;
public int|null $cookie;
public ?int $cookie;
public int $mtuSize;

public static function create(int $serverId, bool $serverSecurity, int $cookie = null, int $mtuSize) : self{
public static function create(int $serverId, bool $serverSecurity, ?int $cookie = null, int $mtuSize) : self{
$result = new self;
$result->serverID = $serverId;
$result->serverSecurity = $serverSecurity;
Expand Down
4 changes: 2 additions & 2 deletions src/protocol/OpenConnectionRequest2.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class OpenConnectionRequest2 extends OfflineMessage{

protected function encodePayload(PacketSerializer $out) : void{
$this->writeMagic($out);
if (Cookie::$serverHasSecurity) {
if (Cookie::hasServerSecurity()) {
$out->putInt($this->cookie);
$out->putBool($this->clientSupportsSecurity);
}
Expand All @@ -41,7 +41,7 @@ protected function encodePayload(PacketSerializer $out) : void{

protected function decodePayload(PacketSerializer $in) : void{
$this->readMagic($in);
if (Cookie::$serverHasSecurity) {
if (Cookie::hasServerSecurity()) {
$this->cookie = $in->getInt();
$this->clientSupportsSecurity = $in->getBool();
}
Expand Down
6 changes: 3 additions & 3 deletions src/server/UnconnectedMessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ 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 (Cookie::$serverHasSecurity) {
if (Cookie::hasServerSecurity()) {
Cookie::add($address);
$cookie = Cookie::get($address);
}
//IP header size (20 bytes) + UDP header size (8 bytes)
$this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), Cookie::$serverHasSecurity, $cookie, $packet->mtuSize + 28), $address);
$this->server->sendPacket(OpenConnectionReply1::create($this->server->getID(), Cookie::hasServerSecurity(), $cookie, $packet->mtuSize + 28), $address);
}
}elseif($packet instanceof OpenConnectionRequest2){
// The client may not send such data even though serverSecurity is enabled, and if we try to decode this, we may encounter an error
Expand All @@ -104,7 +104,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 (Cookie::$serverHasSecurity) {
if (Cookie::hasServerSecurity()) {
if (!Cookie::check($address, $packet->cookie)) {
// Disconnect if OpenConnectionReply1 and the cookie in the OpenCnnectionRequest2 packet do not match
$this->server->getLogger()->debug("Not creating session for $address due to session mismatched cookies");
Expand Down
4 changes: 4 additions & 0 deletions src/utils/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public static function get(InternetAddress $address) : int{
return 0;
}

public static function hasServerSecurity () : bool {
return self::$serverHasSecurity;
}

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

Expand Down

0 comments on commit ec8532b

Please sign in to comment.