Skip to main content

mkt_ksa_geo_sec/api/
device.rs

1/******************************************************************************************
2      📍 منصة تحليل الأمان الجغرافي MKT KSA – تطوير منصور بن خالد
3* 📄 رخصة Apache 2.0 – يسمح بالاستخدام والتعديل بشرط النسبة وعدم تقديم ضمانات.
4* MKT KSA Geolocation Security – Developed by Mansour Bin Khalid (KSA 🇸🇦)
5* Licensed under Apache 2.0 – https://www.apache.org/licenses/LICENSE-2.0
6* © 2025 All rights reserved.
7
8     اسم الملف: device.rs
9    المسار: src/api/device.rs
10
11    وظيفة الملف:
12    هذا الملف مسؤول عن جميع العمليات المتعلقة بتحليل بصمة الجهاز عبر واجهة برمجة التطبيقات (API).
13    يوفر نقطة نهاية (Endpoint) لتحليل بصمة الجهاز، حيث يستقبل الطلبات التي تحتوي على بيانات نظام التشغيل، معلومات الجهاز، وبيانات البيئة،
14    ثم يمررها إلى محرك التحليل في طبقة core (AdaptiveFingerprintEngine)، ويعيد النتيجة النهائية بشكل JSON.
15    يتحقق من صلاحية المستخدم عبر JWT قبل تنفيذ التحليل، ويضمن أن كل عملية تحليل تتم بشكل آمن وموثوق.
16    الملف مصمم ليكون نقطة مركزية لأي نظام خارجي أو واجهة مستخدم ترغب في تحليل أو التحقق من بصمة الأجهزة.
17
18    File name: device.rs
19    Path: src/api/device.rs
20
21    File purpose:
22    This file is responsible for all operations related to device fingerprint analysis via the API.
23    It provides an endpoint for device fingerprint analysis, receiving requests containing OS data, device info, and environment data,
24    then passing them to the analysis engine in the core layer (AdaptiveFingerprintEngine), and returning the final result as JSON.
25    It verifies user authorization via JWT before performing the analysis, ensuring every analysis operation is secure and reliable.
26    The file is designed as a central point for any external system or user interface wishing to analyze or verify device fingerprints.
27******************************************************************************************/
28use crate::api::api_error;
29use crate::api::authorize_request;
30use crate::api::ok_json_with_trace;
31use crate::api::parse_json_payload;
32use crate::api::BearerToken;
33use crate::AppState;
34use actix_web::http::StatusCode;
35use actix_web::{post, web, HttpRequest, Responder};
36use serde::Deserialize;
37
38/// نموذج الطلب لتحليل بصمة الجهاز.
39/// Request model for device fingerprint analysis.
40#[derive(Deserialize)]
41pub struct DeviceResolveRequest {
42    pub os: String, // نظام التشغيل للجهاز
43    // Device operating system
44    pub device_info: String, // معلومات الجهاز (موديل، نوع...)
45    // Device information (model, type, ...)
46    pub environment_data: String, // بيانات البيئة (شبكة، موقع، إلخ)
47                                  // Environment data (network, location, etc.)
48}
49
50/// نقطة نهاية لحل بصمة الجهاز عبر POST /device/resolve
51/// Endpoint to resolve device fingerprint via POST /device/resolve
52#[post("/device/resolve")]
53pub async fn resolve_device(
54    app_data: web::Data<AppState>,
55    req: HttpRequest,
56    bearer: BearerToken,
57    payload_bytes: web::Bytes,
58) -> impl Responder {
59    if let Err(resp) = authorize_request(&app_data, &req, &bearer, &payload_bytes).await {
60        return resp;
61    }
62
63    let payload: DeviceResolveRequest = match parse_json_payload(&payload_bytes) {
64        Ok(v) => v,
65        Err(resp) => return resp,
66    };
67
68    // --- تمرير الطلب لمحرك core ---
69    let engine = &app_data.x_engine.fp_engine;
70    match engine
71        .generate_fingerprint(&payload.os, &payload.device_info, &payload.environment_data)
72        .await
73    {
74        Ok(result) => ok_json_with_trace(&req, result), // إعادة نتيجة التحليل بنجاح
75        // Return analysis result on success
76        Err(_) => api_error(
77            StatusCode::INTERNAL_SERVER_ERROR,
78            "DEVICE_FINGERPRINT_INTERNAL_ERROR",
79            "Internal error while processing device fingerprint",
80        ),
81    }
82}