PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
RCON.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
26 namespace pocketmine\network\rcon;
27 
33 
34 
35 class RCON{
37  private $server;
38  private $socket;
39  private $password;
41  private $workers = [];
42  private $clientsPerThread;
43 
44  public function __construct(Server $server, $password, $port = 19132, $interface = "0.0.0.0", $threads = 1, $clientsPerThread = 50){
45  $this->server = $server;
46  $this->workers = [];
47  $this->password = (string) $password;
48  $this->server->getLogger()->info("Starting remote control listener");
49  if($this->password === ""){
50  $this->server->getLogger()->critical("RCON can't be started: Empty password");
51 
52  return;
53  }
54  $this->threads = (int) max(1, $threads);
55  $this->clientsPerThread = (int) max(1, $clientsPerThread);
56  $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
57  if($this->socket === false or !socket_bind($this->socket, $interface, (int) $port) or !socket_listen($this->socket)){
58  $this->server->getLogger()->critical("RCON can't be started: " . socket_strerror(socket_last_error()));
59  $this->threads = 0;
60  return;
61  }
62  socket_set_block($this->socket);
63 
64  for($n = 0; $n < $this->threads; ++$n){
65  $this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread);
66  }
67  socket_getsockname($this->socket, $addr, $port);
68  $this->server->getLogger()->info("RCON running on $addr:$port");
69  $this->server->getScheduler()->scheduleRepeatingTask(new CallbackTask([$this, "check"]), 3);
70  }
71 
72  public function stop(){
73  for($n = 0; $n < $this->threads; ++$n){
74  $this->workers[$n]->close();
75  usleep(50000);
76  $this->workers[$n]->kill();
77  }
78  @socket_close($this->socket);
79  $this->threads = 0;
80  }
81 
82  public function check(){
83  for($n = 0; $n < $this->threads; ++$n){
84  if($this->workers[$n]->isTerminated() === true){
85  $this->workers[$n] = new RCONInstance($this->socket, $this->password, $this->clientsPerThread);
86  }elseif($this->workers[$n]->isWaiting()){
87  if($this->workers[$n]->response !== ""){
88  $this->server->getLogger()->info($this->workers[$n]->response);
89  $this->workers[$n]->synchronized(function (RCONInstance $thread){
90  $thread->notify();
91  }, $this->workers[$n]);
92  }else{
93 
94  $response = new RemoteConsoleCommandSender();
95  $command = $this->workers[$n]->cmd;
96 
97  $this->server->getPluginManager()->callEvent($ev = new RemoteServerCommandEvent($response, $command));
98 
99  if(!$ev->isCancelled()){
100  $this->server->dispatchCommand($ev->getSender(), $ev->getCommand());
101  }
102 
103  $this->workers[$n]->response = TextFormat::clean($response->getMessage());
104  $this->workers[$n]->synchronized(function (RCONInstance $thread){
105  $thread->notify();
106  }, $this->workers[$n]);
107  }
108  }
109  }
110  }
111 
112 }