Quellcode durchsuchen

The send_user_ip function has been added to the API, which allows you to create an ip record with a comment, a mac, and a hostname.

Roman Dmitriev vor 2 Monaten
Ursprung
Commit
4a405d25b8
2 geänderte Dateien mit 76 neuen und 2 gelöschten Zeilen
  1. 66 0
      html/api.php
  2. 10 2
      html/inc/common.php

+ 66 - 0
html/api.php

@@ -14,6 +14,7 @@ $ip          = getParam('ip', null, null, FILTER_VALIDATE_IP, ['flags' => FILTER
 $mac_raw     = getParam('mac', null, null);
 $rec_id      = getParam('id', null, null, FILTER_VALIDATE_INT);
 $f_subnet    = getParam('subnet', null, null);
+$description = getParam('description', null, null);
 
 // Преобразуем IP в BIGINT
 $ip_aton = null;
@@ -558,6 +559,71 @@ if (!empty($action)) {
         do_exit();
     }
 
+    // === send_user_ip ===
+    if ($action === 'send_user_ip') {
+
+        log_api_call($action, [
+            'ip' => $ip,
+            'mac' => $mac,
+            'action' => $dhcp_action,
+	    'description' => $description
+            ]);
+
+        if ($ip) {
+            $faction = $dhcp_action !== null ? (int)$dhcp_action : 1;
+            $action_str = ($faction === 0) ? 'del' : 'add';
+
+            LOG_VERBOSE($db_link, "API: external add auth request for $ip [$mac] $action_str");
+
+            if (is_our_network($db_link, $ip)) {
+                $ip_record = [
+                    'action' => $action_str,
+                    'mac' => $mac,
+                    'ip' => $ip,
+		    'description' => $description,
+                    'dhcp_hostname' => $dhcp_hostname
+                ];
+		$auth_id = resurrection_auth($db_link, $ip_record);
+                if ($auth_id !== false && $auth_id > 0) {
+                    http_response_code(201);
+                    echo json_encode([
+                        'status' => 'queued',
+                        'id' => (int)$auth_id,
+                        'ip' => $ip,
+                        'action' => $action_str
+                    ], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
+        	} else {
+                    $error_msg = "Failed to add ip record";
+                    LOG_ERROR($db_link, "API: $error_msg. IP: $ip, MAC: $mac, Action: $action_str");
+                    http_response_code(500);
+                    echo json_encode([
+                        'error' => $error_msg,
+                        'ip' => $ip,
+                        'mac' => $mac
+                    ], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
+                }
+            } else {
+                $error_msg = "IP not in allowed network";
+                LOG_ERROR($db_link, "API: $error_msg - $ip [$mac]");
+                http_response_code(400);
+                echo json_encode([
+                    'error' => $error_msg,
+                    'ip' => $ip
+                ], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
+            }
+        } else {
+            $missing_params = [];
+            if (!$ip) $missing_params[] = 'ip';
+            $error_msg = 'Missing required parameters: ' . implode(', ', $missing_params);
+            LOG_WARNING($db_link, "API: send_auth called with missing parameters. Missing: " . implode(', ', $missing_params));
+            http_response_code(400);
+            echo json_encode([
+                'error' => $error_msg
+            ], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
+        }
+        do_exit();
+    }
+
 } else {
     LOG_WARNING($db_link, "API: Unknown request");
     http_response_code(400);

+ 10 - 2
html/inc/common.php

@@ -3345,7 +3345,8 @@ function resurrection_auth($db, $ip_record)
     $action = $ip_record['type'] ?? '';
     $dhcp_hostname = $ip_record['hostname'] ?? '';
     $hotspot_found = !empty($ip_record['hotspot']);
-    
+    $description = $ip_record['description'] ?? '';
+
     $ip_aton = ip2long($ip);
     if ($ip_aton === false) {
         return null;
@@ -3460,6 +3461,9 @@ function resurrection_auth($db, $ip_record)
             'save_traf' => $save_traf
         ];
         
+	if (!empty($description)) { 
+            $auth['description'] = $description;
+	    }
         if (!empty($dhcp_hostname)) {
             $auth['dhcp_hostname'] = $dhcp_hostname;
         }
@@ -3481,7 +3485,11 @@ function resurrection_auth($db, $ip_record)
             'dhcp_time' => GetNowTimeString(),
             'save_traf' => $save_traf
         ];
-        
+
+	if (!empty($description)) { 
+            $auth['description'] = $description;
+	    }
+
         if (!empty($dhcp_hostname)) {
             $auth['dhcp_hostname'] = $dhcp_hostname;
         }