PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
ConsoleReaderChildProcessDaemon.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
22declare(strict_types=1);
23
24namespace pocketmine\console;
25
28use Symfony\Component\Filesystem\Path;
29use function base64_encode;
30use function fgets;
31use function fopen;
32use function mt_rand;
33use function preg_replace;
34use function proc_close;
35use function proc_open;
36use function proc_terminate;
37use function rtrim;
38use function sprintf;
39use function stream_select;
40use function trim;
41use const PHP_BINARY;
42
58 public const TOKEN_DELIMITER = ":";
59 public const TOKEN_HASH_ALGO = "xxh3";
60
61 private \PrefixedLogger $logger;
63 private $subprocess;
65 private $socket;
66 private int $commandTokenSeed;
67
68 public function __construct(
69 \Logger $logger
70 ){
71 $this->logger = new \PrefixedLogger($logger, "Console Reader Daemon");
72 $this->commandTokenSeed = mt_rand();
73 $this->prepareSubprocess();
74 }
75
76 private function prepareSubprocess() : void{
77 //Windows sucks, and likes to corrupt UTF-8 file paths when they travel to the subprocess, so we base64 encode
78 //the path to avoid the problem. This is an abysmally shitty hack, but here we are :(
79 $sub = Utils::assumeNotFalse(proc_open(
80 [
81 PHP_BINARY,
82 '-dopcache.enable_cli=0',
83 '-r',
84 sprintf('require base64_decode("%s", true);', base64_encode(Path::join(__DIR__, 'ConsoleReaderChildProcess.php'))),
85 (string) $this->commandTokenSeed
86 ],
87 [
88 1 => ['socket'],
89 2 => fopen("php://stderr", "w"),
90 ],
91 $pipes
92 ), "Something has gone horribly wrong");
93
94 $this->subprocess = $sub;
95 $this->socket = $pipes[1];
96 }
97
98 private function shutdownSubprocess() : void{
99 //we have no way to signal to the subprocess to shut down gracefully; besides, Windows sucks, and the subprocess
100 //gets stuck in a blocking fgets() read because stream_select() is a hunk of junk (hence the separate process in
101 //the first place).
102 proc_terminate($this->subprocess);
103 proc_close($this->subprocess);
104 }
105
106 public function readLine() : ?string{
107 $r = [$this->socket];
108 $w = null;
109 $e = null;
110 if(stream_select($r, $w, $e, 0, 0) === 1){
111 $line = fgets($this->socket);
112 if($line === false){
113 $this->logger->debug("Lost connection to subprocess, restarting (maybe the child process was killed from outside?)");
114 $this->shutdownSubprocess();
115 $this->prepareSubprocess();
116 return null;
117 }
118 $line = rtrim($line, "\n");
119
120 if($line === ""){
121 //keepalive
122 return null;
123 }
124
125 $command = ConsoleReaderChildProcessUtils::parseMessage($line, $this->commandTokenSeed);
126 if($command === null){
127 //this is not a command - it may be some kind of error output from the subprocess
128 //write it directly to the console
129 $this->logger->warning("Unexpected output from child process: $line");
130 return null;
131 }
132
133 $command = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", trim($command)) ?? throw new AssumptionFailedError("This regex is assumed to be valid");
134 $command = preg_replace('/[[:cntrl:]]/', '', $command) ?? throw new AssumptionFailedError("This regex is assumed to be valid");
135
136 return $command !== "" ? $command : null;
137 }
138
139 return null;
140 }
141
142 public function quit() : void{
143 $this->shutdownSubprocess();
144 }
145}