mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-20 19:49:44 +00:00
support php8019 and some cleanup (by @D-aHTuCT)
This commit is contained in:
parent
cf0f9df267
commit
b60f0877e1
376 changed files with 6528 additions and 13472 deletions
|
|
@ -1,34 +1,16 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
Implementation of the Source RCON Protocol to allow remote console commands
|
||||
Source: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
|
||||
*/
|
||||
|
||||
class RCON{
|
||||
|
||||
private $socket, $password, $workers, $threads, $clientsPerThread;
|
||||
|
||||
|
||||
public function __construct($password, $port = 19132, $interface = "0.0.0.0", $threads = 1, $clientsPerThread = 50){
|
||||
$this->workers = array();
|
||||
$this->workers = [];
|
||||
$this->password = (string) $password;
|
||||
console("[INFO] Starting remote control listener");
|
||||
if($this->password === ""){
|
||||
|
|
@ -39,7 +21,7 @@ class RCON{
|
|||
$this->clientsPerThread = (int) max(1, $clientsPerThread);
|
||||
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
|
||||
if($this->socket === false or !socket_bind($this->socket, $interface, (int) $port) or !socket_listen($this->socket)){
|
||||
console("[ERROR] RCON can't be started: ".socket_strerror(socket_last_error()));
|
||||
console("[ERROR] RCON can't be started: " . socket_strerror(socket_last_error()));
|
||||
return;
|
||||
}
|
||||
@socket_set_block($this->socket);
|
||||
|
|
@ -48,9 +30,9 @@ class RCON{
|
|||
}
|
||||
@socket_getsockname($this->socket, $addr, $port);
|
||||
console("[INFO] RCON running on $addr:$port");
|
||||
ServerAPI::request()->schedule(2, array($this, "check"), array(), true);
|
||||
ServerAPI::request()->schedule(2, [$this, "check"], [], true);
|
||||
}
|
||||
|
||||
|
||||
public function stop(){
|
||||
for($n = 0; $n < $this->threads; ++$n){
|
||||
$this->workers[$n]->close();
|
||||
|
|
@ -61,10 +43,10 @@ class RCON{
|
|||
@socket_close($this->socket);
|
||||
$this->threads = 0;
|
||||
}
|
||||
|
||||
|
||||
public function check(){
|
||||
for($n = 0; $n < $this->threads; ++$n){
|
||||
if($this->workers[$n]->isTerminated() === true){
|
||||
if($this->workers[$n]->isTerminated()){
|
||||
$this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread);
|
||||
}elseif($this->workers[$n]->isWaiting()){
|
||||
if($this->workers[$n]->response !== ""){
|
||||
|
|
@ -81,6 +63,7 @@ class RCON{
|
|||
}
|
||||
|
||||
class RCONInstance extends Thread{
|
||||
|
||||
public $stop;
|
||||
public $cmd;
|
||||
public $response;
|
||||
|
|
@ -96,25 +79,113 @@ class RCONInstance extends Thread{
|
|||
$this->password = $password;
|
||||
$this->maxClients = (int) $maxClients;
|
||||
for($n = 0; $n < $this->maxClients; ++$n){
|
||||
$this->{"client".$n} = null;
|
||||
$this->{"status".$n} = 0;
|
||||
$this->{"timeout".$n} = 0;
|
||||
$this->{"client" . $n} = null;
|
||||
$this->{"status" . $n} = 0;
|
||||
$this->{"timeout" . $n} = 0;
|
||||
}
|
||||
$this->start();
|
||||
}
|
||||
|
||||
private function writePacket($client, $requestID, $packetType, $payload){
|
||||
$pk = Utils::writeLInt((int) $requestID)
|
||||
. Utils::writeLInt((int) $packetType)
|
||||
. $payload
|
||||
. "\x00\x00"; //Terminate payload and packet
|
||||
return socket_write($client, Utils::writeLInt(strlen($pk)).$pk);
|
||||
|
||||
public function close(){
|
||||
$this->stop = true;
|
||||
}
|
||||
|
||||
|
||||
public function run(){
|
||||
while($this->stop !== true){
|
||||
usleep(2000);
|
||||
$r = [$socket = $this->socket];
|
||||
$w = null;
|
||||
$e = null;
|
||||
if(socket_select($r, $w, $e, 0) === 1){
|
||||
if(($client = socket_accept($this->socket)) !== false){
|
||||
socket_set_block($client);
|
||||
socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1);
|
||||
$done = false;
|
||||
for($n = 0; $n < $this->maxClients; ++$n){
|
||||
if($this->{"client" . $n} === null){
|
||||
$this->{"client" . $n} = $client;
|
||||
$this->{"status" . $n} = 0;
|
||||
$this->{"timeout" . $n} = microtime(true) + 5;
|
||||
$done = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if($done === false){
|
||||
@socket_close($client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for($n = 0; $n < $this->maxClients; ++$n){
|
||||
$client = &$this->{"client" . $n};
|
||||
if($client !== null){
|
||||
if($this->{"status" . $n} !== -1 and $this->stop !== true){
|
||||
if($this->{"status" . $n} === 0 and $this->{"timeout" . $n} < microtime(true)){ //Timeout
|
||||
$this->{"status" . $n} = -1;
|
||||
break;
|
||||
}
|
||||
$p = $this->readPacket($client, $size, $requestID, $packetType, $payload);
|
||||
if($p === false){
|
||||
$this->{"status" . $n} = -1;
|
||||
break;
|
||||
}elseif($p === null){
|
||||
break;
|
||||
}
|
||||
|
||||
switch($packetType){
|
||||
case 3: //Login
|
||||
if($this->{"status" . $n} !== 0){
|
||||
$this->{"status" . $n} = -1;
|
||||
break;
|
||||
}
|
||||
if($payload === $this->password){
|
||||
@socket_getpeername($client, $addr, $port);
|
||||
$this->response = "[INFO] Successful Rcon connection from: /$addr:$port";
|
||||
$this->wait();
|
||||
$this->response = "";
|
||||
$this->writePacket($client, $requestID, 2, "");
|
||||
$this->{"status" . $n} = 1;
|
||||
}else{
|
||||
$this->{"status" . $n} = -1;
|
||||
$this->writePacket($client, -1, 2, "");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 2: //Command
|
||||
if($this->{"status" . $n} !== 1){
|
||||
$this->{"status" . $n} = -1;
|
||||
break;
|
||||
}
|
||||
if(strlen($payload) > 0){
|
||||
$this->cmd = ltrim($payload);
|
||||
$this->wait();
|
||||
$this->writePacket($client, $requestID, 0, str_replace("\n", "\r\n", trim($this->response)));
|
||||
$this->response = "";
|
||||
$this->cmd = "";
|
||||
}
|
||||
break;
|
||||
}
|
||||
usleep(1);
|
||||
}else{
|
||||
@socket_set_option($client, SOL_SOCKET, SO_LINGER, ["l_onoff" => 1, "l_linger" => 1]);
|
||||
@socket_shutdown($client, 2);
|
||||
@socket_set_block($client);
|
||||
@socket_read($client, 1);
|
||||
@socket_close($client);
|
||||
$this->{"status" . $n} = 0;
|
||||
$this->{"client" . $n} = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($this->socket, $this->cmd, $this->response, $this->stop);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
private function readPacket($client, &$size, &$requestID, &$packetType, &$payload){
|
||||
@socket_set_nonblock($client);
|
||||
$d = socket_read($client, 4);
|
||||
if($this->stop === true){
|
||||
if($this->stop){
|
||||
return false;
|
||||
}elseif($d === false){
|
||||
return null;
|
||||
|
|
@ -131,100 +202,12 @@ class RCONInstance extends Thread{
|
|||
$payload = rtrim(socket_read($client, $size + 2)); //Strip two null bytes
|
||||
return true;
|
||||
}
|
||||
|
||||
public function close(){
|
||||
$this->stop = true;
|
||||
}
|
||||
|
||||
public function run(){
|
||||
while($this->stop !== true){
|
||||
usleep(2000);
|
||||
$r = array($socket = $this->socket);
|
||||
$w = null;
|
||||
$e = null;
|
||||
if(socket_select($r, $w, $e, 0) === 1){
|
||||
if(($client = socket_accept($this->socket)) !== false){
|
||||
socket_set_block($client);
|
||||
socket_set_option($client, SOL_SOCKET, SO_KEEPALIVE, 1);
|
||||
$done = false;
|
||||
for($n = 0; $n < $this->maxClients; ++$n){
|
||||
if($this->{"client".$n} === null){
|
||||
$this->{"client".$n} = $client;
|
||||
$this->{"status".$n} = 0;
|
||||
$this->{"timeout".$n} = microtime(true) + 5;
|
||||
$done = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if($done === false){
|
||||
@socket_close($client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for($n = 0; $n < $this->maxClients; ++$n){
|
||||
$client = &$this->{"client".$n};
|
||||
if($client !== null){
|
||||
if($this->{"status".$n} !== -1 and $this->stop !== true){
|
||||
if($this->{"status".$n} === 0 and $this->{"timeout".$n} < microtime(true)){ //Timeout
|
||||
$this->{"status".$n} = -1;
|
||||
break;
|
||||
}
|
||||
$p = $this->readPacket($client, $size, $requestID, $packetType, $payload);
|
||||
if($p === false){
|
||||
$this->{"status".$n} = -1;
|
||||
break;
|
||||
}elseif($p === null){
|
||||
break;
|
||||
}
|
||||
|
||||
switch($packetType){
|
||||
case 3: //Login
|
||||
if($this->{"status".$n} !== 0){
|
||||
$this->{"status".$n} = -1;
|
||||
break;
|
||||
}
|
||||
if($payload === $this->password){
|
||||
@socket_getpeername($client, $addr, $port);
|
||||
$this->response = "[INFO] Successful Rcon connection from: /$addr:$port";
|
||||
$this->wait();
|
||||
$this->response = "";
|
||||
$this->writePacket($client, $requestID, 2, "");
|
||||
$this->{"status".$n} = 1;
|
||||
}else{
|
||||
$this->{"status".$n} = -1;
|
||||
$this->writePacket($client, -1, 2, "");
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 2: //Command
|
||||
if($this->{"status".$n} !== 1){
|
||||
$this->{"status".$n} = -1;
|
||||
break;
|
||||
}
|
||||
if(strlen($payload) > 0){
|
||||
$this->cmd = ltrim($payload);
|
||||
$this->wait();
|
||||
$this->writePacket($client, $requestID, 0, str_replace("\n", "\r\n", trim($this->response)));
|
||||
$this->response = "";
|
||||
$this->cmd = "";
|
||||
}
|
||||
break;
|
||||
}
|
||||
usleep(1);
|
||||
}else{
|
||||
@socket_set_option($client, SOL_SOCKET, SO_LINGER, array("l_onoff" => 1, "l_linger" => 1));
|
||||
@socket_shutdown($client, 2);
|
||||
@socket_set_block($client);
|
||||
@socket_read($client, 1);
|
||||
@socket_close($client);
|
||||
$this->{"status".$n} = 0;
|
||||
$this->{"client".$n} = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
unset($this->socket, $this->cmd, $this->response, $this->stop);
|
||||
exit(0);
|
||||
private function writePacket($client, $requestID, $packetType, $payload){
|
||||
$pk = Utils::writeLInt((int) $requestID)
|
||||
. Utils::writeLInt((int) $packetType)
|
||||
. $payload
|
||||
. "\x00\x00"; //Terminate payload and packet
|
||||
return socket_write($client, Utils::writeLInt(strlen($pk)) . $pk);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue