diff --git a/server/Fury/Modules/ErrorHandler/ErrorHandler.php b/server/Fury/Modules/ErrorHandler/ErrorHandler.php index 5e44091..341156c 100644 --- a/server/Fury/Modules/ErrorHandler/ErrorHandler.php +++ b/server/Fury/Modules/ErrorHandler/ErrorHandler.php @@ -90,7 +90,6 @@ E_USER_ERROR => "E_USER_ERROR", E_USER_WARNING => "E_USER_WARNING", E_USER_NOTICE => "E_USER_NOTICE", - E_STRICT => "E_STRICT", E_RECOVERABLE_ERROR => "E_RECOVERABLE_ERROR", E_DEPRECATED => "E_DEPRECATED", E_USER_DEPRECATED => "E_USER_DEPRECATED", diff --git a/server/SHServ/EventsHandlers.php b/server/SHServ/EventsHandlers.php index e56c415..915e53f 100644 --- a/server/SHServ/EventsHandlers.php +++ b/server/SHServ/EventsHandlers.php @@ -33,7 +33,7 @@ events() -> handler("kernel:CallControl.leading_call", function(Array $params) { app() -> devtools -> loging_action_call( - $params["action"], + is_string($params["action"]) ? $params["action"] : "anon", $params["type"], $params["params"] ); diff --git a/server/SHServ/Routes.php b/server/SHServ/Routes.php index 8a5e7c6..7d7e9ad 100644 --- a/server/SHServ/Routes.php +++ b/server/SHServ/Routes.php @@ -51,6 +51,12 @@ // } // ); + + $this -> router -> uri("/test/device-scanner", function($args){ + $device_scanner = new Tools\DeviceScanner(); + $devices = $device_scanner -> scan_range("192.168.68.2", "192.168.68.254"); + var_dump($device_scanner -> filter_by_status($devices, "normal")); + }); } protected function get_routes() { diff --git a/server/SHServ/Tools/DeviceScanner.php b/server/SHServ/Tools/DeviceScanner.php new file mode 100644 index 0000000..4c86ea3 --- /dev/null +++ b/server/SHServ/Tools/DeviceScanner.php @@ -0,0 +1,159 @@ + generate_ip_range($start_ip, $end_ip); + if (empty($ips)) { + return []; + } + + return $this -> scan_ips($ips, $port, $timeout); + } + + /** + * Фильтр по статусу. + * + * @param array $devices + * @param string|string[] $status + * @return array + */ + public function filter_by_status(array $devices, $status): array { + $statuses = is_array($status) ? $status : [$status]; + $statuses = array_map('strval', $statuses); + + return array_values(array_filter($devices, function ($device) use ($statuses) { + if (!isset($device['status'])) { + return false; + } + return in_array((string)$device['status'], $statuses, true); + })); + } + + /** + * Генерация диапазона IP (start..end включительно). + */ + private function generate_ip_range(string $start_ip, string $end_ip): array { + $start = ip2long($start_ip); + $end = ip2long($end_ip); + + if ($start === false || $end === false || $start > $end) { + return []; + } + + $ips = []; + for ($ip_long = $start; $ip_long <= $end; $ip_long++) { + $ips[] = long2ip($ip_long); + } + + return $ips; + } + + /** + * Параллельное сканирование IP с curl_multi. + */ + private function scan_ips(array $ips, int $port, float $timeout): array { + $multi = curl_multi_init(); + $handles = []; + + foreach ($ips as $ip) { + $url = "http://{$ip}:{$port}/about"; + + $ch = curl_init(); + curl_setopt_array($ch, [ + CURLOPT_URL => $url, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_CONNECTTIMEOUT => $timeout, + CURLOPT_TIMEOUT => $timeout, + CURLOPT_FOLLOWLOCATION => false, + CURLOPT_NOBODY => false, + CURLOPT_HEADER => false, + ]); + + curl_multi_add_handle($multi, $ch); + + // тут больше НЕ приводим $ch к строке + $handles[] = [ + 'handle' => $ch, + 'ip' => $ip, + 'url' => $url, + ]; + } + + $running = null; + do { + $status = curl_multi_exec($multi, $running); + if ($status > 0) { + break; + } + + // на некоторых системах curl_multi_select может возвращать -1 + $select_result = curl_multi_select($multi, 0.1); + if ($select_result === -1) { + // маленькая пауза, чтобы не крутить цикл + usleep(100000); + } + } while ($running > 0); + + $devices = []; + + foreach ($handles as $item) { + $ch = $item['handle']; + $ip = $item['ip']; + $url = $item['url']; + + $body = curl_multi_getcontent($ch); + $curl_err = curl_errno($ch); + $http = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + curl_multi_remove_handle($multi, $ch); + curl_close($ch); + + if ($curl_err !== 0 || $http !== 200 || !$body) { + continue; + } + + $json = json_decode($body, true); + if (!is_array($json)) { + continue; + } + + if (!isset($json['ip_address'])) { + $json['ip_address'] = $ip; + } + if (!isset($json['server'])) { + $json['server'] = "http://{$ip}"; + } + + $json['_meta'] = [ + 'ip' => $ip, + 'url' => $url, + 'http_code' => $http, + ]; + + $devices[] = $json; + } + + curl_multi_close($multi); + + return $devices; + } + +} diff --git a/server/SHServ/config.php b/server/SHServ/config.php index 1d7ddb6..78e1e9b 100644 --- a/server/SHServ/config.php +++ b/server/SHServ/config.php @@ -7,7 +7,7 @@ "db" => [ "dblib" => "mysql", "host" => "localhost", - "dbname" => "smart-home-serv", + "dbname" => "smart-home-server", "charset" => "utf8", "user" => "eugene", "password" => "root"