composer.json000064400000000475144760115660007306 0ustar00{ "name": "hsk99/webman-statistic", "type": "library", "license": "MIT", "homepage": "http://hsk99.com.cn", "authors": [ { "name": "hsk99", "email": "3024186605@qq.com", "homepage": "http://hsk99.com.cn" } ], "require": {}, "autoload": { "psr-4": { "Hsk99\\WebmanStatistic\\": "src" } } }src/Install.php000064400000003017144760115660007465 0ustar00 'config/plugin/hsk99/statistic', ]; /** * Install * @return void */ public static function install() { static::installByRelation(); } /** * Uninstall * @return void */ public static function uninstall() { self::uninstallByRelation(); } /** * installByRelation * @return void */ public static function installByRelation() { foreach (static::$pathRelation as $source => $dest) { if ($pos = strrpos($dest, '/')) { $parent_dir = base_path() . '/' . substr($dest, 0, $pos); if (!is_dir($parent_dir)) { mkdir($parent_dir, 0777, true); } } //symlink(__DIR__ . "/$source", base_path()."/$dest"); copy_dir(__DIR__ . "/$source", base_path() . "/$dest"); } } /** * uninstallByRelation * @return void */ public static function uninstallByRelation() { foreach (static::$pathRelation as $source => $dest) { $path = base_path() . "/$dest"; if (!is_dir($path) && !is_file($path)) { continue; } /*if (is_link($path) { unlink($path); }*/ remove_dir($path); } } } src/Statistic.php000064400000003565144760115660010036 0ustar00getRealIp($safe_mode = true); $controller = $request->controller; $action = $request->action; $transfer = $controller . '::' . $action; $project = config('plugin.hsk99.statistic.app.project'); $unique = StatisticClient::tick($project, $ip, $transfer); $response = $next($request); $code = $response->getStatusCode(); $success = $code < 400; $details = [ 'ip' => $request->getRealIp($safe_mode = true) ?? '', // 请求客户端IP 'url' => $request->fullUrl() ?? '', // 请求URL 'method' => $request->method() ?? '', // 请求方法 'request_param' => $request->all() ?? [], // 请求参数 'request_header' => $request->header() ?? [], // 请求头 'cookie' => $request->cookie() ?? [], // 请求cookie 'session' => $request->session()->all() ?? [], // 请求session 'response_code' => $response->getStatusCode() ?? '', // 响应码 'response_header' => $response->getHeaders() ?? [], // 响应头 'response_body' => $success ?: (string)$response->rawBody(), // 响应数据(发生异常) ]; StatisticClient::report($unique, $project, $ip, $transfer, $success, $code, json_encode($details, 320)); return $response; } } src/StatisticClient.php000064400000010123144760115660011161 0ustar00 self::MAX_CHAR_VALUE) { $project = substr($project, 0, self::MAX_CHAR_VALUE); } if (strlen($transfer) > self::MAX_CHAR_VALUE) { $transfer = substr($transfer, 0, self::MAX_CHAR_VALUE); } if (strlen($ip) > self::MAX_CHAR_VALUE) { $ip = substr($ip, 0, self::MAX_CHAR_VALUE); } $projectLen = strlen($project); $ipLen = strlen($ip); $transferLen = strlen($transfer); $detailsLen = self::MAX_UDP_PACKGE_SIZE - self::PACKAGE_FIXED_LENGTH - $projectLen - $transferLen; if (strlen($details) > $detailsLen) { $details = substr($details, 0, $detailsLen); } return pack('CCCfCNnN', $projectLen, $ipLen, $transferLen, $costTime, $success ? 1 : 0, $code, strlen($details), time()) . $project . $ip . $transfer . $details; } /** * 发送数据 * * @author HSK * @date 2021-12-12 22:25:55 * * @param string $buffer * * @return boolean */ protected static function sendData(string $buffer): bool { try { if (empty(self::$remoteAddress)) { self::$remoteAddress = config('plugin.hsk99.statistic.app.address'); } $socket = stream_socket_client(self::$remoteAddress); if (!$socket) { return false; } return stream_socket_sendto($socket, $buffer) == strlen($buffer); } catch (\Throwable $th) { return false; } } } src/ThinkOrm.php000064400000003406144760115660007614 0ustar00 1) { $transfer = substr($sql, strpos($sql, "s ] ") + 4); $cost = $result[1]; } else { $transfer = $sql;; $cost = 0; } break; default: $transfer = $sql;; $cost = 0; break; } StatisticClient::report('', config('plugin.hsk99.statistic.app.project') . 'Sql', '127.0.0.1', $transfer, true, 1, json_encode([ 'sql' => $sql, 'runtime' => $cost . 's', 'master' => $master, 'worker' => $worker->name, ], 320), $cost); }); } } } src/config/plugin/hsk99/statistic/app.php000064400000000221144760115660014352 0ustar00 true, 'project' => 'webman', 'sql_report' => false, 'address' => 'udp://127.0.0.1:8789', ]; src/config/plugin/hsk99/statistic/bootstrap.php000064400000000076144760115660015617 0ustar00 [ Hsk99\WebmanStatistic\Statistic::class, ] ];