-
-
Notifications
You must be signed in to change notification settings - Fork 673
/
SessionPaths.php
321 lines (287 loc) · 9.12 KB
/
SessionPaths.php
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
declare(strict_types=1);
/**
* Session paths module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <[email protected]>
* @copyright 2016-2023 Daniil Gentili <[email protected]>
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
* @link https://docs.madelineproto.xyz MadelineProto documentation
*/
namespace danog\MadelineProto;
use AssertionError;
use danog\MadelineProto\Ipc\IpcState;
use const LOCK_EX;
use const LOCK_SH;
use const PHP_MAJOR_VERSION;
use const PHP_MINOR_VERSION;
use const PHP_VERSION;
use function Amp\File\createDirectory;
use function Amp\File\deleteFile;
use function Amp\File\exists;
use function Amp\File\isDirectory;
use function Amp\File\isFile;
use function Amp\File\move;
use function Amp\File\openFile;
use function Amp\File\write;
/**
* Session path information.
*
* @internal
*/
final class SessionPaths
{
/**
* Header for session files.
*/
private const PHP_HEADER = '<?php __HALT_COMPILER();';
private const VERSION_OLD = 2;
private const VERSION_SERIALIZATION_AWARE = 3;
/**
* Session directory path.
*/
private string $sessionDirectoryPath;
/**
* Session path.
*/
private string $sessionPath;
/**
* Session lock path.
*/
private string $lockPath;
/**
* IPC socket path.
*/
private string $ipcPath;
/**
* IPC callback socket path.
*/
private string $ipcCallbackPath;
/**
* IPC light state path.
*/
private string $ipcStatePath;
/**
* Light state path.
*/
private string $lightStatePath;
/**
* Light state.
*/
private ?LightState $lightState = null;
/**
* Construct session info from session name.
*
* @param string $session Session name
*/
public function __construct(string $session)
{
$session = Tools::absolute($session);
$this->sessionDirectoryPath = $session;
$this->sessionPath = $session.DIRECTORY_SEPARATOR."safe.php";
$this->lightStatePath = $session.DIRECTORY_SEPARATOR."lightState.php";
$this->lockPath = $session.DIRECTORY_SEPARATOR."lock";
$this->ipcPath = $session.DIRECTORY_SEPARATOR."ipc";
$this->ipcCallbackPath = $session.DIRECTORY_SEPARATOR."callback.ipc";
$this->ipcStatePath = $session.DIRECTORY_SEPARATOR."ipcState.php";
if (!exists($session)) {
createDirectory($session);
}
$session = realpath($session);
if (str_starts_with($session, '/storage/emulated/')
|| str_starts_with($session, '/sdcard/')
) {
throw new AssertionError("The MadelineProto session folder cannot be stored in /sdcard, please move the session folder to the termux \$HOME folder, see here for more info: https://wiki.termux.com/wiki/Internal_and_external_storage");
}
if (!isDirectory($session) && isFile("$session.safe.php")) {
deleteFile($session);
createDirectory($session);
foreach (['safe.php', 'lightState.php', 'lock', 'ipc', 'callback.ipc', 'ipcState.php'] as $part) {
if (exists("$session.$part")) {
move("$session.$part", $session.DIRECTORY_SEPARATOR."$part");
}
if (exists("$session.$part.lock")) {
move("$session.$part.lock", $session.DIRECTORY_SEPARATOR."$part.lock");
}
}
}
}
/**
* Deletes session.
*/
public function delete(): void
{
if (file_exists($this->sessionDirectoryPath)) {
foreach (scandir($this->sessionDirectoryPath) as $f) {
if ($f === '.' || $f === '..') {
continue;
}
unlink($this->sessionDirectoryPath.DIRECTORY_SEPARATOR.$f);
}
rmdir($this->sessionDirectoryPath);
}
}
/**
* Serialize object to file.
*/
public function serialize(object|string $object, string $path): void
{
Logger::log("Waiting for exclusive lock of $path.lock...");
$unlock = Tools::flock("$path.lock", LOCK_EX, 0.1);
try {
Logger::log("Got exclusive lock of $path.lock...");
$object = self::PHP_HEADER
.\chr(self::VERSION_SERIALIZATION_AWARE)
.\chr(PHP_MAJOR_VERSION)
.\chr(PHP_MINOR_VERSION)
.\chr(Magic::$can_use_igbinary ? 1 : 0)
.(Magic::$can_use_igbinary ? igbinary_serialize($object) : serialize($object));
write(
"$path.temp.php",
$object,
);
move("$path.temp.php", $path);
} finally {
$unlock();
}
}
/**
* Deserialize new object.
*
* @param string $path Object path, defaults to session path
*/
public function unserialize(string $path = ''): object|string|null
{
$path = $path ?: $this->sessionPath;
if (!exists($path)) {
return null;
}
$headerLen = \strlen(self::PHP_HEADER);
Logger::log("Waiting for shared lock of $path.lock...", Logger::ULTRA_VERBOSE);
$unlock = Tools::flock("$path.lock", LOCK_SH, 0.1);
try {
Logger::log("Got shared lock of $path.lock...", Logger::ULTRA_VERBOSE);
$file = openFile($path, 'rb');
clearstatcache(true, $path);
$size = filesize($path);
$file->seek($headerLen++);
$v = \ord($file->read(null, 1));
if ($v >= self::VERSION_OLD) {
$php = $file->read(null, 2);
$major = \ord($php[0]);
$minor = \ord($php[1]);
if (version_compare("$major.$minor", PHP_VERSION) > 0) {
throw new Exception("Cannot deserialize session created on newer PHP $major.$minor, currently using PHP ".PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.', please upgrade to the latest version of PHP!');
}
$headerLen += 2;
}
$igbinary = false;
if ($v >= self::VERSION_SERIALIZATION_AWARE) {
$igbinary = (bool) \ord($file->read(null, 1));
if ($igbinary && !Magic::$can_use_igbinary) {
throw Exception::extension('igbinary');
}
$headerLen++;
}
$unserialized = $file->read(null, $size - $headerLen) ?? '';
$unserialized = $igbinary ? igbinary_unserialize($unserialized) : unserialize($unserialized);
$file->close();
} finally {
$unlock();
}
return $unserialized;
}
/**
* Get session path.
*/
public function __toString(): string
{
return $this->sessionDirectoryPath;
}
/**
* Get session directory path.
*/
public function getSessionDirectoryPath(): string
{
return $this->sessionDirectoryPath;
}
/**
* Get session path.
*/
public function getSessionPath(): string
{
return $this->sessionPath;
}
/**
* Get lock path.
*/
public function getLockPath(): string
{
return $this->lockPath;
}
/**
* Get IPC socket path.
*/
public function getIpcPath(): string
{
return $this->ipcPath;
}
/**
* Get IPC light state path.
*/
public function getIpcStatePath(): string
{
return $this->ipcStatePath;
}
/**
* Get IPC state.
*/
public function getIpcState(): ?IpcState
{
return $this->unserialize($this->ipcStatePath);
}
/**
* Store IPC state.
*/
public function storeIpcState(IpcState $state): void
{
$this->serialize($state, $this->getIpcStatePath());
}
/**
* Get light state path.
*/
public function getLightStatePath(): string
{
return $this->lightStatePath;
}
/**
* Get light state.
*/
public function getLightState(): LightState
{
/** @var LightState */
return $this->lightState ??= $this->unserialize($this->lightStatePath);
}
/**
* Store light state.
*/
public function storeLightState(MTProto $state): void
{
$this->lightState = new LightState($state);
$this->serialize($this->lightState, $this->getLightStatePath());
}
/**
* Get IPC callback socket path.
*/
public function getIpcCallbackPath(): string
{
return $this->ipcCallbackPath;
}
}