forked from PocketDock/PocketDockConsole
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDCApp.php
More file actions
141 lines (133 loc) · 5.79 KB
/
PDCApp.php
File metadata and controls
141 lines (133 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace PocketDockConsole;
use pocketmine\utils\TextFormat;
use pocketmine\utils\Terminal;
class PDCApp extends \Wrench\Application\Application {
protected $clients = array();
protected $lastTimestamp = null;
protected $autharray = array();
protected $connectedips;
public function __construct($thread, $password) {
$this->thread = $thread;
$this->password = $password;
}
/**
* @see Wrench\Application.Application::onConnect()
*/
public function onConnect($client) {
$this->thread->log(TextFormat::AQUA . "Connection from: " . $client->getIp());
$client->send(Terminal::toANSI(TextFormat::AQUA . "[PocketDockConsole] " . json_encode(array('info' => $client->getIp() . ' connected')) . "\r\n"));
$client->send(Terminal::toANSI(TextFormat::YELLOW . "[PocketDockConsole] Please authenticate " . $client->getIp() . ". Type your password and press enter.\r\n"));
$this->thread->connectedips.= $client->getIp() . ";";
$this->clients[] = $client;
}
/**
* @see Wrench\Application.Application::onUpdate()
*/
public function onUpdate() {
$this->lastTimestamp = time();
foreach ($this->autharray as $sendto) {
$stuffArray = explode("\n", $this->thread->stuffToSend);
if (count($stuffArray) == $this->thread->lastLine) {
} else {
for ($i = $this->thread->lastLine - 1;$i <= count($stuffArray);$i++) {
if (isset($stuffArray[$i])) {
$line = trim($stuffArray[$i]) . "\r\n";
if ($line === "\r\n") {
} else {
$sendto->send($line);
}
}
}
//$this->thread->lastLine = count($stuffArray);
$this->thread->lastLine = 1;
$this->thread->stuffToSend = "";
$this->thread->clearstream = true;
$sendto->send($this->thread->stuffTitle);
}
$jsonArray = explode("\n", $this->thread->jsonStream);
if (count($jsonArray) == $this->thread->lastLineJSON) {
} else {
for ($i = $this->thread->lastLineJSON - 1;$i <= count($jsonArray);$i++) {
if (isset($jsonArray[$i])) {
$line = trim($jsonArray[$i]) . "\r\n";
if ($line === "\r\n") {
} else {
$sendto->send($line);
}
}
}
//$this->thread->lastLineJSON = count($jsonArray);
$this->thread->lastLineJSON = 1;
$this->thread->jsonStream = "";
$sendto->send($this->thread->stuffTitle);
}
}
if ($this->thread->clienttokill !== "") {
foreach ($this->clients as $authed) {
$ip = $authed->getIp();
if ($ip == $this->thread->clienttokill) {
$iparray = explode(";", $this->thread->connectedips);
unset($iparray[array_search($ip, $iparray) ]);
$this->thread->connectedips = implode(";", $iparray);
$authed->close();
unset($this->autharray[array_search($authed, $this->autharray) ]);
unset($this->clients[array_search($authed, $this->clients) ]);
$this->thread->clienttokill = "";
}
}
}
}
/**
* Handle data received from a client
*
* @param Payload $payload A payload object, that supports __toString()
* @param Connection $connection
*/
public function onData($payload, $connection) {
$payloado = $payload;
$payload = trim($payload);
if (in_array($connection, $this->autharray)) {
$this->thread->buffer.= $payloado;
if (stripos($payloado, "json") != - 1) {
} else {
$connection->send($payload . "\r\n");
}
} elseif ($this->tryAuth($payload)) {
$this->autharray[] = $connection;
$connection->send(Terminal::toANSI(TextFormat::DARK_GREEN . "[PocketDockConsole] Authenticated! Now accepting commands\r\n"));
$this->thread->log(TextFormat::DARK_GREEN . "Successful login from: " . $connection->getIp() . "!");
$stuffArray = explode("\n", $this->thread->stuffToSend);
$stuffArray = array_reverse($stuffArray);
for ($i = $this->thread->backlog;$i >= 0;$i--) {
if (isset($stuffArray[$i])) {
$line = trim($stuffArray[$i]) . "\r\n";
if ($line === "\r\n") {
} else {
$connection->send($line);
}
}
}
$connection->send($this->thread->stuffTitle);
} else {
$connection->send(Terminal::toANSI(TextFormat::DARK_RED . "[PocketDockConsole] Failed login attempt, this event will be recorded!\r\n"));
$this->thread->log(TextFormat::DARK_RED . "Failed login attempt from: " . $connection->getIp() . "!");
}
}
public function tryAuth($password) {
if ($password == $this->password) {
$this->thread->sendUpdate = true;
return true;
} else {
return false;
}
}
public function onDisconnect($conn) {
$ip = $conn->getIp();
$iparray = explode(";", $this->thread->connectedips);
unset($iparray[array_search($ip, $iparray) ]);
$this->thread->connectedips = implode(";", $iparray);
unset($this->autharray[array_search($conn, $this->autharray) ]);
unset($this->clients[array_search($conn, $this->clients) ]);
}
}