@anonympins/fingerprint 0.3.5 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,118 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ /**
6
+ * CLI Script d'auto-tuning périodique des configurations de sécurité.
7
+ * Ce script lit la configuration courante, récupère les logs, les assainit,
8
+ * exécute l'optimiseur génétique, et écrase le fichier JSON d'origine.
9
+ */
10
+
11
+ $autoloaderPaths = [
12
+ __DIR__ . '/../../vendor/autoload.php',
13
+ __DIR__ . '/../vendor/autoload.php',
14
+ __DIR__ . '/../../../autoload.php',
15
+ ];
16
+
17
+ $autoloaded = false;
18
+ foreach ($autoloaderPaths as $path) {
19
+ if (file_exists($path)) {
20
+ require_once $path;
21
+ $autoloaded = true;
22
+ break;
23
+ }
24
+ }
25
+
26
+ if (!$autoloaded) {
27
+ fwrite(STDERR, "Erreur : Impossible de charger l'autoloader. Exécutez 'composer install'.\n");
28
+ exit(1);
29
+ }
30
+
31
+ use Anonympins\Fingerprint\Utils\RequestUtils;
32
+ use Anonympins\Fingerprint\Store\StoreManager;
33
+ use Anonympins\Fingerprint\Optimization\Optimization;
34
+
35
+ // 1. Récupération des arguments CLI
36
+ $configPath = $argv[1] ?? null;
37
+ if (!$configPath) {
38
+ echo "Usage: php auto-tune.php [chemin_vers_security-config.json]\n";
39
+ exit(1);
40
+ }
41
+
42
+ if (!file_exists($configPath)) {
43
+ fwrite(STDERR, "Erreur : Fichier de configuration introuvable : {$configPath}\n");
44
+ exit(1);
45
+ }
46
+
47
+ $config = json_decode(file_get_contents($configPath), true);
48
+ if (json_last_error() !== JSON_ERROR_NONE) {
49
+ fwrite(STDERR, "Erreur : Fichier de configuration JSON invalide.\n");
50
+ exit(1);
51
+ }
52
+
53
+ // 2. Connexion au store pour récupérer les logs accumulés
54
+ $store = StoreManager::getStore();
55
+ if (!$store) {
56
+ fwrite(STDERR, "Erreur : Aucun store de persistance actif.\n");
57
+ exit(1);
58
+ }
59
+
60
+ $rawTrafficLogs = $store->get('traffic_logs') ?? [];
61
+ if (empty($rawTrafficLogs)) {
62
+ echo "[AutoTuning] Aucun log de trafic disponible pour l'optimisation.\n";
63
+ exit(0);
64
+ }
65
+
66
+ // 3. Nettoyage des données pour prévenir l'empoisonnement (Sybil attacks)
67
+ $sanitizedLogs = RequestUtils::sanitizeTrafficData($rawTrafficLogs);
68
+
69
+ $minDataPoints = $config['autotuning']['minDataPoints'] ?? 200;
70
+ if (count($sanitizedLogs) < $minDataPoints) {
71
+ echo "[AutoTuning] Reporté : Pas assez de données assainies (" . count($sanitizedLogs) . "/{$minDataPoints}).\n";
72
+ exit(0);
73
+ }
74
+
75
+ echo "[AutoTuning] Lancement de l'optimisation sur " . count($sanitizedLogs) . " données de trafic...\n";
76
+
77
+ // 4. Résolution du Front de Pareto
78
+ $paretoFront = Optimization::solveFullSecurityTuning(['trafficData' => $sanitizedLogs]);
79
+ if (empty($paretoFront)) {
80
+ fwrite(STDERR, "[AutoTuning] L'optimisation n'a retourné aucun résultat.\n");
81
+ exit(1);
82
+ }
83
+
84
+ // 5. Sélection de la solution la plus équilibrée
85
+ $bestSolution = $paretoFront[0];
86
+ $minDistance = sqrt(pow($bestSolution['objectives'][0], 2) + pow($bestSolution['objectives'][1], 2));
87
+ foreach ($paretoFront as $candidate) {
88
+ $distance = sqrt(pow($candidate['objectives'][0], 2) + pow($candidate['objectives'][1], 2));
89
+ if ($distance < $minDistance) {
90
+ $minDistance = $distance;
91
+ $bestSolution = $candidate;
92
+ }
93
+ }
94
+
95
+ $newConfig = $bestSolution['solution'];
96
+ $maxChangeVelocity = 0.15; // Inertie de 15% maximum par cycle
97
+
98
+ $applyInertialUpdate = function (array &$current, array $target) use ($maxChangeVelocity) {
99
+ $sumCurrent = array_sum($current);
100
+ if ($sumCurrent === 0) return;
101
+ $sumTarget = 0;
102
+ foreach ($current as $k => $v) {
103
+ if (isset($target[$k])) $sumTarget += $target[$k];
104
+ }
105
+ $ratio = ($sumTarget - $sumCurrent) / $sumCurrent;
106
+ $factor = 1 + max(-$maxChangeVelocity, min($maxChangeVelocity, $ratio));
107
+ foreach ($current as $k => &$v) {
108
+ if (isset($target[$k])) $v *= $factor;
109
+ }
110
+ };
111
+
112
+ $applyInertialUpdate($config['thresholds'], $newConfig['thresholds']);
113
+ $applyInertialUpdate($config['weights'], $newConfig['weights']);
114
+ $applyInertialUpdate($config['patterns'], $newConfig['patterns']);
115
+
116
+ // 6. Écrasement propre du fichier de configuration original
117
+ file_put_contents($configPath, json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
118
+ echo "[AutoTuning] Succès : Fichier {$configPath} mis à jour avec les paramètres optimisés.\n";