Compare commits

...

6 commits

10 changed files with 160 additions and 19 deletions

View file

@ -5,14 +5,14 @@
Before contributing to NostalgiaCore, please read this.
## I want to create an issue
* First, use the [Issue Search](https://github.com/kotyaralih/NostalgiaCore/search?ref=cmdform&type=Issues) to check if anyone has reported it.
* First, use the [Issue Search](https://github.com/oldminecraftcommunity/NostalgiaCore/search?ref=cmdform&type=Issues) to check if anyone has reported it.
* Is your issue related to a Plugin? If so, please contact their original author instead of reporting it here.
* And no, we won't update a Plugin because you need it.
* When reporting, give as much info as you can, and if the Issue is a crash, give the Crash Dump.
* Issues should be written in English.
## I want to contribute code
* Use the [Pull Request](https://github.com/kotyaralih/NostalgiaCore/pull/new) system, your request will be checked and discussed.
* Use the [Pull Request](https://github.com/oldminecraftcommunity/NostalgiaCore/pull/new) system, your request will be checked and discussed.
* __Create a single branch for that pull request__
* If you want to be part of NostalgiaCore, we will ask you to.
* Code using the syntax as in NostalgiaCore. See below for an example.

View file

@ -222,8 +222,8 @@ class Player{
$this->chatMessagesQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0);
$this->chatMessagesQueue->data = [];
//$this->server->schedule(20 * 60, [$this, "clearQueue"], [], true);
$this->server->schedule(20 * 60, [$this, "clearQueue"], [], true);
$this->server->interface->maxACKsAllowed["{$this->ip}:{$this->port}"] = 0;
$this->evid[] = $this->server->event("server.close", [$this, "close"]);
console("[DEBUG] New Session started with $ip:$port. MTU {$this->MTU}, Client ID {$this->clientID}", true, true, 2);
}
@ -2021,6 +2021,10 @@ class Player{
//$this->entity->close();
}
}
public function __destruct(){ //pwease dont leak memory >_<
unset($this->server->interface->maxACKsAllowed["{$this->ip}:{$this->port}"]);
}
public function save(){
if($this->entity instanceof Entity){
@ -3271,23 +3275,27 @@ class Player{
$t->spawn($this);
}else{
$nbt = new NBT();
$nbt->load($packet->namedtag);
$d = array_shift($nbt->tree);
if($d["id"] !== TILE_SIGN){
$d = $nbt->loadSignNBT($packet->namedtag);
if($d === false){
$t->spawn($this);
}else{
$t->setText($d["Text1"], $d["Text2"], $d["Text3"], $d["Text4"]);
}
}
}
break;
case ProtocolInfo::SET_ENTITY_LINK_PACKET:
if($this->spawned === false){
break;
}
if($this->entity->riding != 0){
$this->entity->setRiding(null);
}
break;
case ProtocolInfo::PLAYER_INPUT_PACKET:
if($this->spawned === false){
break;
}
$this->isJumping = $packet->isJumping;
$this->isSneaking = $packet->isSneaking;
$this->entity->moveForward = $packet->moveForward;
@ -3566,8 +3574,10 @@ class Player{
foreach($packet->packets as $count){
if(isset($this->packetAlwaysRecoverQueue[$count])){
$this->resendQueue[$count] = $this->packetAlwaysRecoverQueue[$count];
$this->resendQueue[$count]->incrementAllowedAck = false;
}else if(isset($this->recoveryQueue[$count])){
$this->resendQueue[$count] =& $this->recoveryQueue[$count];
$this->resendQueue[$count]->incrementAllowedAck = false;
$this->lag[] = $time - $this->recoveryQueue[$count]->sendtime;
unset($this->recoveryQueue[$count]);
}else{ //lost and wont be recovered
@ -3587,8 +3597,8 @@ class Player{
}else if(isset($this->recoveryQueue[$count])){
$this->lag[] = $time - $this->recoveryQueue[$count]->sendtime;
unset($this->recoveryQueue[$count]);
unset($this->resendQueue[$count]);
}
unset($this->resendQueue[$count]);
++$this->packetStats[0];
}
break;

View file

@ -5,7 +5,8 @@ class PocketMinecraftServer{
const TICK_F20TPS = 1;
const TICK_NETWAIT = 2;
public $tCnt, $ticks;
public $extraprops, $serverID, $interface, $database, $version, $invisible, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $eidCnt, $custom, $description, $motd, $port, $saveEnabled;
public $extraprops, $serverID, $interface, $database, $version, $invisible, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $eidCnt, $description, $motd, $port, $saveEnabled;
public $custom, $customLastTime = [];
/**
* @var ServerAPI
*/
@ -627,7 +628,8 @@ class PocketMinecraftServer{
$pk->ip = $packet->ip;
$pk->port = $packet->port;
$this->send($pk);
$this->custom["times_" . $CID] = ($this->custom["times_" . $CID] + 1) % strlen($this->description);
$this->custom["times_$CID"] = ($this->custom["times_$CID"] + 1) % strlen($this->description);
$this->customLastTime[$CID] = microtime(true);
break;
case RakNetInfo::OPEN_CONNECTION_REQUEST_1:
if($packet->structure !== RakNetInfo::STRUCTURE){
@ -677,6 +679,14 @@ class PocketMinecraftServer{
public function tick(){
$time = microtime(true);
if($this->lastTick <= ($time - 0.05)){
foreach($this->customLastTime as $CID => $lastTime){
//server is removed from the list every 3 seconds(will also add a tiny 1 second delay so it will def not be updated incorrectly <3)
if(($time - $lastTime) > 4){
unset($this->custom["times_$CID"]);
unset($this->customLastTime[$CID]);
}
}
$this->tickMeasure[] = $this->lastTick = $time;
unset($this->tickMeasure[key($this->tickMeasure)]);
++$this->ticks;

View file

@ -37,18 +37,43 @@ class MinecraftInterface{
return $this->parsePacket($buf, $source, $port);
}
/**
* Stores max ACK packets that the server is allowed to handle.
*
* @var array
*/
public $maxACKsAllowed = [];
private function parsePacket($buffer, $source, $port){
$pid = ord($buffer[0]);
if(RakNetInfo::isValid($pid)){
$packet = RakNetParser::parse($buffer);
$id = "$source:$port";
$ackCnt = 0;
$acks = $this->maxACKsAllowed[$id] ?? 0;
$packet = RakNetParser::parse($buffer, $acks, $ackCnt);
if($packet !== false){
if($ackCnt != 0 && isset($this->maxACKsAllowed[$id]) && $packet->pid() != RakNetInfo::NACK){
$this->maxACKsAllowed[$id] = $acks - $ackCnt;
}
$packet->ip = $source;
$packet->port = $port;
if(EventHandler::callEvent(new PacketReceiveEvent($packet)) === BaseEvent::DENY){
return false;
}
return $packet;
}else if($ackCnt > 0){
$CID = PocketMinecraftServer::clientID($source, $port);
$server = ServerAPI::request();
$c = $server->clients[$CID] ?? false;
if($c !== false){
$username = $c->iusername == "" ? $id : $c->iusername;
ConsoleAPI::warn("Player '$username' sent too many ACK packets (allowed: {$acks}, attempted {$ackCnt} or more)!");
$c->close("too many ack packets");
}else{
//ConsoleAPI::warn("$id sent too many ACK packets (allowed: {$acks}, attempted {$ackCnt} or more)!");
}
}
return false;
}elseif($pid === 0xfe and $buffer[1] === "\xfd" and ServerAPI::request()->api->query instanceof QueryHandler){
@ -75,6 +100,14 @@ class MinecraftInterface{
return 0;
}elseif($packet instanceof RakNetPacket){
RakNetCodec::encode($packet);
if($packet->pid() >= RakNetInfo::DATA_PACKET_0 && $packet->pid() <= RakNetInfo::DATA_PACKET_F && $packet->incrementAllowedAck){
$id = "{$packet->ip}:{$packet->port}";
if(isset($this->maxACKsAllowed[$id])){
++$this->maxACKsAllowed[$id];
}else{
$this->maxACKsAllowed[$id] = 1;
}
}
}
$write = $this->socket->write($packet->buffer, $packet->ip, $packet->port);
$this->bandwidth[1] += $write;

View file

@ -2,7 +2,10 @@
class UDPSocket{
public $connected, $sock, $server, $port;
public $connected;
public $sock;
public $server;
public $port;
function __construct($server, $port, $listen = false, $serverip = "0.0.0.0"){
$this->server = $server;
$this->port = $port;

View file

@ -5,6 +5,7 @@ class RakNetPacket extends Packet{
public $packetID;
public $seqNumber;
public $sendtime;
public $incrementAllowedAck = true;
public function __construct($packetID){
$this->packetID = (int) $packetID;

View file

@ -1,7 +1,7 @@
<?php
class RakNetParser{
public static function parse(&$buffer){
public static function parse(&$buffer, $maxACKAllowed = -1, &$ackCnt = 0){
$offset = 0;
++$offset;
$packet = new RakNetPacket(ord(substr($buffer, $offset - 1, 1)));
@ -60,6 +60,7 @@ class RakNetParser{
$offset += 2;
$count = Utils::readShort(substr($buffer, $offset - 2, 2), false);
$packet->packets = [];
$acks = 0;
for($i = 0; $i < $count && isset($buffer[$offset]); ++$i){
++$offset;
if(ord(substr($buffer, $offset - 1, 1)) === 0){
@ -67,17 +68,29 @@ class RakNetParser{
$start = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3)));
$offset += 3;
$end = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3)));
if(($end - $start) > 4096){
$end = $start + 4096;
$acks += ($end - $start)+1;
if($acks <= 0 || $maxACKAllowed >= 0 && ($maxACKAllowed - $acks) < 0){
$packet = false;
break;
}
/*if(($end - $start) > 4096){ //not really needed?
$end = $start + 4096;
}*/
for($c = $start; $c <= $end; ++$c){
$packet->packets[] = $c;
}
}else{
$offset += 3;
$acks += 1;
if($maxACKAllowed >= 0 && ($maxACKAllowed - $acks) < 0){
$packet = false;
break;
}
$packet->packets[] = Utils::readTriad(strrev(substr($buffer, $offset - 3, 3)));
}
}
$ackCnt = $acks;
break;
default:
$packet = false;

View file

@ -22,6 +22,62 @@ class NBT{
$this->binary .= $bin;
}
/**
* A function to safely load sign nbt
* @param string $str raw nbt data
* @return false|array array with sign data, false if failed
*/
public function loadSignNBT($str){
$this->offset = 0;
$this->binary = (string) $str;
//sign nbt in vanilla is
/*
* [
* "id": "Sign",
* "x": <int>,
* "y": <int>,
* "z": <int>,
* "Text1": <string>, //max 15 chars
* "Text2": <string>, //^
* "Text3": <string>, //^
* "Text4": <string> //^
* ]
*/
$tag = ord($this->read(1));
if($tag == self::TAG_COMPOUND){
$name = $this->readTAG_STRING_maxlen(2);
if($name != "") return false;
$value = [];
for($n = 0; $n < 8; ++$n){
$tag = ord($this->read(1));
if($tag == self::TAG_END){
break;
}else if($tag == self::TAG_INT){
$name = $this->readTAG_STRING_maxlen(1);
if($name != "x" && $name != "y" && $name != "z") return false;
$value[$name] = $this->readTAG_INT();
}else if($tag == self::TAG_STRING){
$name = $this->readTAG_STRING_maxlen(5);
if($name == "id"){
$s = $this->readTAG_STRING_maxlen(4);
if($s != "Sign") return false;
$value[$name] = $s;
}else if($name == "Text1" || $name == "Text2" || $name == "Text3" || $name == "Text4"){
$value[$name] = $this->readTAG_STRING_maxlen(15*4); //mcpe 0.8.1 supports unicode, so the max char size 4 bytes
}else{
return false;
}
}else{
return false;
}
}
if(count($value) != 8) return false;
return $value;
}else{
return false;
}
}
public function load($str){
$this->offset = 0;
$this->binary = (string) $str;
@ -66,7 +122,7 @@ class NBT{
break;
default:
echo bin2hex(substr($this->binary, $this->offset - 1)) . PHP_EOL . PHP_EOL;
die("Invalid NBT Tag $tag");
throw new NBTException("Invalid NBT Tag $tag");
}
$node[$name] = $value;
}
@ -83,7 +139,14 @@ class NBT{
private function feof(){
return !isset($this->binary[$this->offset]);
}
//i guess i could have modified old method?
//*but it will make it slower </3*
//*the nbt needs to be completely rewritten anyway this is barely usable </3*
public function readTAG_STRING_maxlen($ml){
$len = Utils::readLShort($this->read(2), false);
return $this->read($len <= $ml ? $len : $ml);
}
public function readTAG_STRING(){
return $this->read(Utils::readLShort($this->read(2), false));
}

View file

@ -0,0 +1,8 @@
<?php
class NBTException extends Exception
{
public function __construct($message){
parent::__construct($message);
}
}

View file

@ -36,7 +36,7 @@ if [ "$POCKETMINE_FILE" == "" ]; then
POCKETMINE_FILE="./PocketMine-MP.php"
else
echo​ ​"​PocketMine-MP.php not found​"
​ echo​ ​"​Downloads can be found at https://github.com/kotyaralih/NostalgiaCore"
​ echo​ ​"​Downloads can be found at https://github.com/oldminecraftcommunity/NostalgiaCore"
exit 1
fi
fi