-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinux.php
More file actions
442 lines (378 loc) · 17.2 KB
/
Copy pathlinux.php
File metadata and controls
442 lines (378 loc) · 17.2 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
declare(strict_types=1);
// Early debug logging
@file_put_contents('/app/logs/byedpi/php_debug.log', "[" . date('Y-m-d H:i:s') . "] Script started\n", FILE_APPEND);
set_time_limit(60);
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Type: application/json; charset=utf-8");
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
@file_put_contents('/app/logs/byedpi/php_debug.log', "[" . date('Y-m-d H:i:s') . "] OPTIONS request\n", FILE_APPEND);
http_response_code(204);
header('Content-Length: 0');
exit();
}
@file_put_contents('/app/logs/byedpi/php_debug.log', "[" . date('Y-m-d H:i:s') . "] Request method: " . $_SERVER['REQUEST_METHOD'] . "\n", FILE_APPEND);
function send_json_response(array $response): void {
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit();
}
function is_port_in_use(int $port): array {
if ($port < 1 || $port > 65535) {
return ['error' => 'Неверный номер порта.'];
}
// Use netstat instead of lsof which is broken in qemu environment
$cmd = "netstat -tlnp 2>/dev/null | grep ':$port '";
$output = shell_exec($cmd) ?? '';
$output = trim($output);
if (!empty($output)) {
return ['in_use' => true];
}
return ['in_use' => false];
}
// Debug logging function
function debug_log($msg) {
$log_file = '/app/logs/byedpi/php_debug.log';
$ts = date('Y-m-d H:i:s');
@file_put_contents($log_file, "[$ts] $msg\n", FILE_APPEND);
}
function find_process(int $port, string $real_file_path): array {
debug_log("find_process: port=$port, real_file_path=$real_file_path");
if ($port < 1 || $port > 65535) {
debug_log("find_process: invalid port");
return ['error' => 'Неверный номер порта.'];
}
if (empty($real_file_path)) {
debug_log("find_process: empty real_file_path");
return ['error' => 'Путь не может быть пустым.'];
}
$netstat_cmd = "netstat -tlnp 2>/dev/null | grep ':$port '";
$netstat_output = shell_exec($netstat_cmd) ?? '';
$netstat_output = trim($netstat_output);
debug_log("find_process: netstat output: $netstat_output");
if (empty($netstat_output)) {
debug_log("find_process: port not in use");
return ['exists' => false];
}
if (preg_match('/LISTEN\\s+(\\d+)\//', $netstat_output, $matches)) {
$pid = $matches[1];
debug_log("find_process: pid from netstat: $pid");
} else {
$ps_output = shell_exec("ps aux | grep ciadpi | grep -v grep") ?? '';
debug_log("find_process: ps aux output: $ps_output");
$ps_lines = explode("\n", trim($ps_output));
foreach ($ps_lines as $line) {
if (strpos($line, "-p $port") !== false || strpos($line, "--port $port") !== false) {
$parts = preg_split('/\\s+/', trim($line));
if (count($parts) >= 2 && is_numeric($parts[1])) {
$pid = $parts[1];
debug_log("find_process: pid from ps: $pid, line: $line");
break;
}
}
}
if (!isset($pid)) {
debug_log("find_process: no pid found");
return ['exists' => false];
}
}
$ps_cmd = "ps -p $pid -o args --no-headers 2>/dev/null";
$cmdline = shell_exec($ps_cmd) ?? '';
$cmdline = trim($cmdline);
debug_log("find_process: cmdline for pid $pid: $cmdline");
if (empty($cmdline)) {
debug_log("find_process: empty cmdline");
return ['error' => 'Не удалось получить командную строку процесса.'];
}
$real_basename = basename($real_file_path);
$has_correct_port = strpos($cmdline, "-p $port") !== false || strpos($cmdline, "--port $port") !== false;
$is_correct_binary = (strpos($cmdline, $real_file_path) !== false ||
strpos($cmdline, $real_basename) !== false ||
strpos($cmdline, 'ciadpi') !== false ||
strpos($cmdline, '/app/byedpi/ciadpi') !== false);
debug_log("find_process: has_correct_port=$has_correct_port, is_correct_binary=$is_correct_binary");
if ($is_correct_binary && $has_correct_port) {
debug_log("find_process: process found, pid=$pid");
return [
'exists' => true,
'pid' => (int)$pid,
'cmd' => $cmdline
];
}
debug_log("find_process: process not ours");
return ['exists' => false];
}
function start_process(string $real_file_path, int $port, string $args): array {
if ($port < 1 || $port > 65535) {
return ['error' => 'Неверный номер порта.'];
}
if (empty($real_file_path)) {
return ['error' => 'Путь не может быть пустым.'];
}
// Ensure the binary exists and is executable
if (!file_exists($real_file_path)) {
return ['error' => 'Исполняемый файл не найден: ' . $real_file_path];
}
if (!is_executable($real_file_path)) {
return ['error' => 'Файл не является исполняемым: ' . $real_file_path];
}
// Escape the path to handle spaces and special characters
$escaped_path = escapeshellarg($real_file_path);
$escaped_args = escapeshellarg($args);
// Create log directory if it doesn't exist
$log_dir = '/app/logs/byedpi';
if (!is_dir($log_dir)) {
@mkdir($log_dir, 0755, true);
}
$log_file = "$log_dir/port_${port}.log";
// Build command with proper escaping and logging
$command = "nohup $escaped_path --port $port $args > $log_file 2>&1 & echo \$!";
$output = shell_exec($command);
$pid = trim($output ?? '');
if (!is_numeric($pid) || $pid <= 0) {
return ['error' => 'Не удалось получить PID процесса.'];
}
// Wait a moment and verify the process started
usleep(500000); // 0.5 seconds
if (!posix_kill((int)$pid, 0)) {
return ['error' => 'Процесс не запустился или завершился сразу после запуска.'];
}
return ['result' => true, 'pid' => (int)$pid];
}
function kill_process(int $pid): array {
if ($pid <= 0) {
return ['error' => 'PID должен быть положительным числом.'];
}
shell_exec("kill -9 $pid 2>/dev/null");
return ['result' => true];
}
function validate_request_data(array $data): array {
$required_fields = ['action', 'real_file_path', 'port'];
foreach ($required_fields as $field) {
if (!array_key_exists($field, $data)) {
return ['error' => "Отсутствует обязательное поле: $field"];
}
}
$action = $data['action'];
$real_file_path = $data['real_file_path'];
$port = $data['port'];
$allowed_actions = ['check', 'check_and_start', 'check_and_kill'];
if (!in_array($action, $allowed_actions)) {
return ['error' => 'Недопустимое действие.'];
}
if (!is_string($real_file_path) || empty(trim($real_file_path)) || !file_exists($real_file_path) || !is_file($real_file_path)) {
return ['error' => 'Путь к файлу некорректен или файл не существует.'];
}
if (!is_numeric($port) || intval($port) != $port || $port < 1 || $port > 65535) {
return ['error' => 'Порт должен быть целым числом от 1 до 65535.'];
}
$arguments = $data['arguments'] ?? '';
if (!is_string($arguments)) {
return ['error' => 'Аргументы должны быть строкой.'];
}
$hosts_file_name = $data['hosts_file_name'] ?? null;
if ($hosts_file_name !== null && !is_string($hosts_file_name)) {
return ['error' => 'Имя файла hosts должно быть строкой.'];
}
return [
'action' => $action,
'real_file_path' => $real_file_path,
'port' => (int)$port,
'arguments' => $arguments,
'hosts_file_name' => $hosts_file_name
];
}
function parse_cmd(string $cmd, int $port): array {
$port_flag = "--port $port";
$pos = strpos($cmd, $port_flag);
if ($pos === false) {
return ['arguments' => '', 'hosts_file' => false];
}
$start = $pos + strlen($port_flag);
$args_str = trim(substr($cmd, $start));
$args_array = preg_split('/\s+/', $args_str);
$arguments = [];
$hosts_file = false;
$i = 0;
while ($i < count($args_array)) {
if ($args_array[$i] === '--hosts') {
$hosts_file = true;
$i += 2;
} else {
$arguments[] = $args_array[$i];
$i++;
}
}
return [
'arguments' => implode(' ', $arguments),
'hosts_file' => $hosts_file
];
}
function get_current_state(int $port, string $real_file_path): array {
debug_log("get_current_state: port=$port, real_file_path=$real_file_path");
$port_status = is_port_in_use($port);
debug_log("get_current_state: port_status=" . json_encode($port_status));
if (isset($port_status['error'])) {
debug_log("get_current_state: error in port_status: " . $port_status['error']);
return ['type' => 'error', 'error' => $port_status['error']];
}
if (!$port_status['in_use']) {
debug_log("get_current_state: port free");
return ['type' => 'free'];
}
$process_info = find_process($port, $real_file_path);
debug_log("get_current_state: process_info=" . json_encode($process_info));
if (isset($process_info['error'])) {
debug_log("get_current_state: error in process_info: " . $process_info['error']);
return ['type' => 'error', 'error' => $process_info['error']];
}
if ($process_info['exists']) {
$parsed = parse_cmd($process_info['cmd'], $port);
debug_log("get_current_state: process in_use_by_us, pid=" . $process_info['pid']);
return [
'type' => 'in_use_by_us',
'pid' => $process_info['pid'],
'cmd' => $process_info['cmd'],
'arguments' => $parsed['arguments'],
'hosts_file' => $parsed['hosts_file']
];
}
debug_log("get_current_state: port in use by others");
return ['type' => 'in_use_by_others'];
}
function wait_for_state(int $port, string $real_file_path, string $expected_state, int $max_attempts, int $interval): array {
for ($attempt = 0; $attempt < $max_attempts; $attempt++) {
$state = get_current_state($port, $real_file_path);
if ($state['type'] === $expected_state) {
return $state;
}
if ($state['type'] === 'error') {
return $state;
}
sleep($interval);
}
return ['type' => 'error', 'error' => "Не удалось достичь состояния '$expected_state' после $max_attempts попыток."];
}
function build_response(string $action, array $state, string $real_file_path, int $port): array {
$base_response = [
'action' => $action,
'real_file_path' => $real_file_path,
'port' => $port,
'hosts_file' => false,
'arguments' => '',
'result' => true
];
switch ($state['type']) {
case 'error':
return ['error.st' => true, 'message' => $state['error']];
case 'free':
return array_merge($base_response, ['state' => 'free', 'message' => 'Порт свободен']);
case 'in_use_by_us':
return array_merge($base_response, [
'state' => 'in_use_by_us',
'pid' => $state['pid'],
'hosts_file' => $state['hosts_file'],
'arguments' => $state['arguments'],
'message' => 'Порт занят нашей программой'
]);
case 'in_use_by_others':
return array_merge($base_response, [
'state' => 'in_use_by_others',
'message' => 'Порт занят другой программой'
]);
}
}
$input_data = file_get_contents('php://input');
@file_put_contents('/app/logs/byedpi/php_debug.log', "[" . date('Y-m-d H:i:s') . "] Input data: " . $input_data . "\n", FILE_APPEND);
$request_data = json_decode($input_data, true);
@file_put_contents('/app/logs/byedpi/php_debug.log', "[" . date('Y-m-d H:i:s') . "] JSON decode result: " . json_encode($request_data) . "\n", FILE_APPEND);
if (json_last_error() !== JSON_ERROR_NONE) {
@file_put_contents('/app/logs/byedpi/php_debug.log', "[" . date('Y-m-d H:i:s') . "] JSON error: " . json_last_error_msg() . "\n", FILE_APPEND);
send_json_response(['error' => true, 'message' => 'Некорректный формат JSON']);
}
$validation = validate_request_data($request_data);
@file_put_contents('/app/logs/byedpi/php_debug.log', "[" . date('Y-m-d H:i:s') . "] Validation result: " . json_encode($validation) . "\n", FILE_APPEND);
if (isset($validation['error'])) {
send_json_response(['error' => true, 'message' => $validation['error']]);
}
$action = $validation['action'];
$real_file_path = $validation['real_file_path'];
$port = $validation['port'];
$arguments = $validation['arguments'];
$hosts_file_name = $validation['hosts_file_name'];
$args = $arguments;
if ($hosts_file_name !== null) {
$args = '--hosts ' . escapeshellarg($hosts_file_name) . ' ' . $arguments;
}
$max_attempts = 10;
$interval = 1;
switch ($action) {
case 'check':
$state = get_current_state($port, $real_file_path);
send_json_response(build_response($action, $state, $real_file_path, $port));
break;
case 'check_and_start':
$state = get_current_state($port, $real_file_path);
if ($state['type'] === 'error') {
send_json_response(['error' => true, 'message' => $state['error']]);
}
if ($state['type'] === 'in_use_by_us') {
$response = build_response($action, $state, $real_file_path, $port);
$response['message'] = 'Процесс уже запущен';
send_json_response($response);
}
if ($state['type'] === 'in_use_by_others') {
send_json_response([
'action' => $action,
'real_file_path' => $real_file_path,
'port' => $port,
'message' => 'Порт занят другой программой',
'result' => false
]);
}
$start_result = start_process($real_file_path, $port, $args);
if (isset($start_result['error'])) {
send_json_response(['error' => true, 'message' => $start_result['error']]);
}
$state = wait_for_state($port, $real_file_path, 'in_use_by_us', $max_attempts, $interval);
$response = build_response($action, $state, $real_file_path, $port);
$response['message'] = $state['type'] === 'in_use_by_us' ? 'Процесс успешно запущен' : 'Не удалось запустить процесс';
$response['result'] = $state['type'] === 'in_use_by_us';
send_json_response($response);
break;
case 'check_and_kill':
$state = get_current_state($port, $real_file_path);
if ($state['type'] === 'error') {
send_json_response(['error' => true, 'message' => $state['error']]);
}
if ($state['type'] === 'free') {
$response = build_response($action, $state, $real_file_path, $port);
$response['message'] = 'Порт свободен';
send_json_response($response);
}
if ($state['type'] === 'in_use_by_others') {
send_json_response([
'action' => $action,
'real_file_path' => $real_file_path,
'port' => $port,
'message' => 'Порт занят другой программой, невозможно остановить',
'result' => false
]);
}
$kill_result = kill_process($state['pid']);
if (isset($kill_result['error'])) {
send_json_response(['error' => true, 'message' => $kill_result['error']]);
}
$state = wait_for_state($port, $real_file_path, 'free', $max_attempts, $interval);
$response = build_response($action, $state, $real_file_path, $port);
$response['message'] = $state['type'] === 'free' ? 'Процесс успешно остановлен' : 'Не удалось остановить процесс';
$response['result'] = $state['type'] === 'free';
send_json_response($response);
break;
default:
send_json_response(['error' => true, 'message' => 'Неизвестное действие']);
}
?>