Repository navigation
Expand file tree
/
Copy pathRouterosAPI.php
More file actions
204 lines (188 loc) · 6.05 KB
/
Copy pathRouterosAPI.php
File metadata and controls
204 lines (188 loc) · 6.05 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* RouterOS API client (port 8728) — implementasi minimal & mandiri.
* Protokol: length-prefixed words + sentences, diakhiri word kosong.
*/
class RouterosAPI
{
/** @var resource|null */
private $socket;
public function connect(string $host, string $user, string $pass, int $port = 8728, int $timeout = 10): bool
{
$this->socket = @fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$this->socket) {
throw new RuntimeException("Koneksi MikroTik gagal ($host:$port): $errstr");
}
stream_set_timeout($this->socket, $timeout);
// Coba login legacy (plaintext) dulu.
$this->writeSentence(['/login', "=name=$user", "=password=$pass"]);
$resp = $this->readSentence();
if ($resp === false) {
return false;
}
if (isset($resp[0]) && $resp[0] === '!done') {
return true;
}
// Login challenge-response (RouterOS >= 6.43).
$ret = null;
foreach ($resp as $word) {
if (strpos($word, '=ret=') === 0) {
$ret = substr($word, 5);
break;
}
}
if ($ret !== null) {
$hash = md5(chr(0) . $pass . pack('H*', $ret));
$this->writeSentence(['/login', "=name=$user", "=response=00$hash"]);
$resp2 = $this->readSentence();
return $resp2 !== false && isset($resp2[0]) && $resp2[0] === '!done';
}
return false;
}
public function disconnect(): void
{
if (is_resource($this->socket)) {
fclose($this->socket);
}
$this->socket = null;
}
/** Kirim perintah, kembalikan array kalimat (array of words). */
public function comm(string $command, array $attrs = []): array
{
$words = array_merge([$command], $attrs);
$this->writeSentence($words);
$sentences = [];
while (true) {
$resp = $this->readSentence();
if ($resp === false) {
break;
}
$sentences[] = $resp;
$first = $resp[0] ?? '';
if ($first === '!done' || $first === '!trap' || $first === '!fatal') {
break;
}
}
return $sentences;
}
/** Ambil semua simple queue (id, name, target, max-limit). */
public function getSimpleQueues(): array
{
$resp = $this->comm('/queue/simple/print', ['=.proplist=.id,name,target,max-limit']);
$queues = [];
foreach ($resp as $sentence) {
if (($sentence[0] ?? '') !== '!re') {
continue;
}
$q = [];
foreach ($sentence as $word) {
if ($word === '' || $word[0] !== '=') {
continue;
}
[$k, $v] = explode('=', substr($word, 1), 2);
if ($k === '.id') {
$k = 'id';
} elseif ($k === 'max-limit') {
$k = 'max_limit';
}
$q[$k] = $v;
}
$queues[] = $q;
}
return $queues;
}
/** Ubah max-limit sebuah queue berdasarkan .id. */
public function setMaxLimit(string $id, string $maxLimit): void
{
$this->comm('/queue/simple/set', ["=.id=$id", "=max-limit=$maxLimit"]);
}
private function writeSentence(array $words): void
{
$out = '';
foreach ($words as $w) {
$out .= $this->writeWord($w);
}
$out .= chr(0); // penanda akhir kalimat
fwrite($this->socket, $out);
}
private function writeWord(string $w): string
{
$len = strlen($w);
if ($len < 0x80) {
return chr($len) . $w;
}
if ($len < 0x4000) {
$len |= 0x8000;
return chr(($len >> 8) & 0xFF) . chr($len & 0xFF) . $w;
}
if ($len < 0x200000) {
$len |= 0xC00000;
return chr(($len >> 16) & 0xFF) . chr(($len >> 8) & 0xFF) . chr($len & 0xFF) . $w;
}
if ($len < 0x10000000) {
$len |= 0xE0000000;
return chr(($len >> 24) & 0xFF) . chr(($len >> 16) & 0xFF) . chr(($len >> 8) & 0xFF) . chr($len & 0xFF) . $w;
}
return chr(0xF0) . pack('N', $len) . $w;
}
/** @return array|false */
private function readSentence()
{
$words = [];
while (true) {
$w = $this->readWord();
if ($w === false) {
return false;
}
if ($w === '') {
break;
}
$words[] = $w;
}
return $words;
}
/** @return string|false */
private function readWord()
{
$b = $this->readBytes(1);
if ($b === false) {
return false;
}
$byte = ord($b);
$len = 0;
if (($byte & 0x80) === 0) {
$len = $byte;
} elseif (($byte & 0xC0) === 0x80) {
$len = ($byte & 0x3F) << 8;
$len += ord($this->readBytes(1));
} elseif (($byte & 0xE0) === 0xC0) {
$len = ($byte & 0x1F) << 16;
$len += ord($this->readBytes(1)) << 8;
$len += ord($this->readBytes(1));
} elseif (($byte & 0xF0) === 0xE0) {
$len = ($byte & 0x0F) << 24;
$len += ord($this->readBytes(1)) << 16;
$len += ord($this->readBytes(1)) << 8;
$len += ord($this->readBytes(1));
} else {
$len = ord($this->readBytes(1)) << 24;
$len += ord($this->readBytes(1)) << 16;
$len += ord($this->readBytes(1)) << 8;
$len += ord($this->readBytes(1));
}
return $this->readBytes($len);
}
/** @return string|false */
private function readBytes(int $n)
{
$data = '';
while (strlen($data) < $n) {
$chunk = fread($this->socket, $n - strlen($data));
if ($chunk === false || $chunk === '') {
return false;
}
$data .= $chunk;
}
return $data;
}
}