curl get requests support custom headers now, event to get content of thread output, shutdown function which saves world if server stopped with error

This commit is contained in:
GameHerobrine 2022-08-12 20:02:00 +03:00
parent 0088f2d86d
commit 4bd682b08e
5 changed files with 43 additions and 16 deletions

View file

@ -1754,7 +1754,7 @@ class Player{
$this->entity->isInMinecart = true; $this->entity->isInMinecart = true;
$pk = new SetEntityLinkPacket(); $pk = new SetEntityLinkPacket();
$pk->rider = $target->eid; $pk->rider = $target->eid;
$pk->riding = 0; $pk->riding = $this->entity->eid;
$pk->type = 2; $pk->type = 2;
$this->dataPacket($pk); $this->dataPacket($pk);
$needsBreak = true; $needsBreak = true;

View file

@ -21,15 +21,17 @@ class PocketMinecraftServer{
$this->serverip = $serverip; $this->serverip = $serverip;
$this->load(); $this->load();
} }
private function load(){ private function load(){
global $dolog; global $dolog;
EntityRegistry::registerEntities();
$this->version = new VersionString(); $this->version = new VersionString();
/*if(defined("DEBUG") and DEBUG >= 0){ /*if(defined("DEBUG") and DEBUG >= 0){
@cli_set_process_title("NostalgiaCore ".MAJOR_VERSION); @cli_set_process_title("NostalgiaCore ".MAJOR_VERSION);
}*/ }*/
console("[INFO] Starting Minecraft PE server on " . ($this->serverip === "0.0.0.0" ? "*" : $this->serverip) . ":" . $this->port); console("[INFO] Starting Minecraft PE server on " . ($this->serverip === "0.0.0.0" ? "*" : $this->serverip) . ":" . $this->port);
EntityRegistry::registerEntities();
define("BOOTUP_RANDOM", Utils::getRandomBytes(16)); define("BOOTUP_RANDOM", Utils::getRandomBytes(16));
$this->serverID = $this->serverID === false ? Utils::readLong(substr(Utils::getUniqueID(true, $this->serverip . $this->port), 8)) : $this->serverID; $this->serverID = $this->serverID === false ? Utils::readLong(substr(Utils::getUniqueID(true, $this->serverip . $this->port), 8)) : $this->serverID;
$this->seed = $this->seed === false ? Utils::readInt(Utils::getRandomBytes(4, false)) : $this->seed; $this->seed = $this->seed === false ? Utils::readInt(Utils::getRandomBytes(4, false)) : $this->seed;
@ -95,7 +97,14 @@ class PocketMinecraftServer{
} }
$dolog = $this->extraprops->get("save-console-data"); $dolog = $this->extraprops->get("save-console-data");
} }
public function onShutdown(){
console("Saving...");
$save = $this->saveEnabled;
$this->saveEnabled = true;
$this->api->level->saveAll();
$this->saveEnabled = $save;
console("Shutting Down...");
}
public function startDatabase(){ public function startDatabase(){
$this->preparedSQL = new stdClass(); $this->preparedSQL = new stdClass();
$this->preparedSQL->entity = new stdClass(); $this->preparedSQL->entity = new stdClass();
@ -214,7 +223,10 @@ class PocketMinecraftServer{
$type = (int) $type; $type = (int) $type;
switch($type){ switch($type){
case ASYNC_CURL_GET: case ASYNC_CURL_GET:
$d .= Utils::writeShort(strlen($data["url"])) . $data["url"] . (isset($data["timeout"]) ? Utils::writeShort($data["timeout"]) : Utils::writeShort(10)); if(isset($data["headers"])){
$jsstr = json_encode($data["headers"]);
}
$d .= Utils::writeShort(strlen($data["url"])) . $data["url"] . (isset($data["timeout"]) ? Utils::writeShort($data["timeout"]) : Utils::writeShort(10)) . (isset($data["headers"]) ? Utils::writeShort(strlen($jsstr)) . $jsstr : Utils::writeShort(1) . " ");
break; break;
case ASYNC_CURL_POST: case ASYNC_CURL_POST:
$d .= Utils::writeShort(strlen($data["url"])) . $data["url"] . (isset($data["timeout"]) ? Utils::writeShort($data["timeout"]) : Utils::writeShort(10)); $d .= Utils::writeShort(strlen($data["url"])) . $data["url"] . (isset($data["timeout"]) ? Utils::writeShort($data["timeout"]) : Utils::writeShort(10));
@ -286,6 +298,7 @@ class PocketMinecraftServer{
$len = Utils::readInt(substr($this->asyncThread->output, $offset, 4)); $len = Utils::readInt(substr($this->asyncThread->output, $offset, 4));
$offset += 4; $offset += 4;
$data["result"] = substr($this->asyncThread->output, $offset, $len); $data["result"] = substr($this->asyncThread->output, $offset, $len);
$this->dhandle("async.curl.get", ["response" => $data["result"]]);
$offset += $len; $offset += $len;
break; break;
} }

View file

@ -541,13 +541,14 @@ class Entity extends Position{
DATA FLAGS IDS DATA FLAGS IDS
0 - fire 0 - fire
1 - crouching 1 - crouching
2 - riding <?> - no idea why doesnt it work
4 - inAction(ex.: using a bow) 4 - inAction(ex.: using a bow)
*/ */
public function getMetadata(){ public function getMetadata(){
$flags = 0; $flags = 0;
$flags |= $this->fire > 0 ? 0b1 : 0; $flags ^= $this->fire > 0 ? 0b1 : 0;
$flags |= ($this->crouched ? 0b1 : 0) << 1; $flags ^= ($this->crouched ? 0b1 : 0) << 1;
$flags |= ($this->inAction ? 0b1:0) << 4; $flags ^= ($this->inAction ? 0b1 : 0) << 4;
$d = array( $d = array(
0 => array("type" => 0, "value" => $flags), 0 => array("type" => 0, "value" => $flags),
1 => array("type" => 1, "value" => $this->air), 1 => array("type" => 1, "value" => $this->air),

View file

@ -128,13 +128,27 @@ class Utils{
} }
public static function curl_get($page, $timeout = 10){ public static function makeHeaders($json){
$arr = json_decode($json);
$rarr = [];
foreach ($arr as $key => $value){
$rarr[] = $key.": ".$value;
}
return $rarr;
}
public static function curl_get($page, $timeout = 10, $headers = " "){
if(Utils::$online === false){ if(Utils::$online === false){
return false; return false;
} }
if($headers != " "){
$headers = Utils::makeHeaders($headers);
}else{
$headers = ["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"];
}
$ch = curl_init($page); $ch = curl_init($page);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"]); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

View file

@ -8,7 +8,6 @@ class AsyncMultipleQueue extends Thread{
public $input; public $input;
public $output; public $output;
public $stop; public $stop;
public function __construct(){ public function __construct(){
$this->input = ""; $this->input = "";
$this->output = ""; $this->output = "";
@ -24,8 +23,8 @@ class AsyncMultipleQueue extends Thread{
case ASYNC_CURL_GET: case ASYNC_CURL_GET:
$url = $this->get(Utils::readShort($this->get(2), false)); $url = $this->get(Utils::readShort($this->get(2), false));
$timeout = Utils::readShort($this->get(2)); $timeout = Utils::readShort($this->get(2));
$headers = $this->get(Utils::readShort($this->get(2), false));
$res = (string) Utils::curl_get($url, $timeout); $res = (string) Utils::curl_get($url, $timeout, $headers);
//$this->lock(); //$this->lock();
$this->output .= Utils::writeInt($rID) . Utils::writeShort(ASYNC_CURL_GET) . Utils::writeInt(strlen($res)) . $res; $this->output .= Utils::writeInt($rID) . Utils::writeShort(ASYNC_CURL_GET) . Utils::writeInt(strlen($res)) . $res;
//$this->unlock(); //$this->unlock();