Add Logs for Caller and modify resources

This commit is contained in:
Jan_Hill 2024-12-23 07:02:54 +01:00
parent 00035956d9
commit 1d80e682dc
9 changed files with 483 additions and 33 deletions

View File

@ -0,0 +1,61 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Caller_DB;
use App\Models\CallerLog_DB;
class ApiController extends Controller
{
public function checker(Request $request)
{
try {
$data = $request->validate([
"token" => "required",
]);
$caller_by = new Caller_DB();
$caller_by = $caller_by->where('token', $data['token'])->first();
if (!$caller_by) {
$this->logRequest($request, null, 'failed', '401 Unauthorized', null);
return response()->json(['error' => 'Unauthorized'], 401);
}
$this->logRequest($request, $caller_by, 'success', 'accepted command');
return response()->json(['success' => true], 200);
} catch (\Illuminate\Validation\ValidationException $exception) {
$this->logRequest($request, null, 'failed', '401 Unauthorized');
return response()->json(['error' => 'Unauthorized'], 401);
} catch (\Exception $exception) {
$this->logRequest($request, null, 'failed', $exception->getMessage());
return response()->json(['error' => 'An unexpected error occurred.' . $exception->getMessage()], 500);
}
}
private function logRequest($request, $caller_by = null, $status, $error = null)
{
$logger = new CallerLog_DB();
if ($caller_by) {
$logger->caller_id = $caller_by->id;
$logger->caller_name = $caller_by->node_name;
$logger->token_by = $caller_by->token;
$manager = $caller_by->manager;
} else {
$logger->caller_id = "not responsible";
$logger->caller_name = "not responsible";
$logger->token_by = "not responsible";
}
$logger->req_ip = $request->ip();
$logger->req_command = $request->path();
$logger->status = $status;
$logger->error_exception = $error;
$logger->save();
}
}

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Caller_DB;
use App\Models\CallerLog_DB;
class SmartController extends Controller
{
@ -21,8 +22,19 @@ class SmartController extends Controller
{
$callers = Caller_DB::all();
$callers_count = [$callers->count()];
return view('smart.caller', compact('callers', 'callers_count'));
return view('smart.caller', compact('callers'));
}
public function caller_log()
{
$caller_logs = CallerLog_DB::paginate(15);
$caller_logs->getCollection()->transform(function ($log) {
$caller = Caller_DB::where('created_by', $log->created_by)->first();
$log->token_by = $caller ? $caller->created_by : null;
return $log;
});
return view('smart.caller_log', compact('caller_logs'));
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CallerLog_DB extends Model
{
protected $table = 'caller_log';
protected $fillable = ['caller_id', 'caller_name', 'req_ip', 'req_command', 'token_by', 'status', 'error_exception'];
protected $hidden = ['created_at', 'updated_at'];
}

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('caller_log', function (Blueprint $table) {
$table->id()->autoIncrement();
$table->string('caller_id');
$table->string('caller_name');
$table->string('req_ip');
$table->string('req_command');
$table->string('token_by');
$table->enum('status', ['success', 'failed']);
$table->longtext('error_exception')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};

View File

@ -160,7 +160,7 @@
</a>
</li>
<li>
<a href="#" class="flex items-center p-2 pl-11 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700">
<a href="{{ route('caller_log') }}" class="flex items-center p-2 pl-11 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700">
<i class="bi bi-list flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 16px;"></i>
Logs
</a>
@ -270,33 +270,33 @@
</thead>
<tbody>
<tr>
<td class="px-6 py-4 whitespace-nowrap">
@php
$counter = 1;
foreach($callers_count as $caller_id) {
echo $counter++;
}
@endphp
</td>
@foreach($callers as $caller)
<td class="px-6 py-4 whitespace-nowrap">
<div class="flex items-center">
<div>
{{ $caller->node_name }}
</div>
<td class="px-6 py-4 whitespace-nowrap">
@php
$counter = 1;
foreach($callers as $caller_id) {
echo $counter++;
}
@endphp
</td>
@foreach($callers as $caller)
<td class="px-6 py-4 whitespace-nowrap">
<div class="flex items-center">
<div>
{{ $caller->node_name }}
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300">
{{ $caller->status }}
</span>
</td>
@endforeach
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
<button class="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300">Edit</button>
</td>
</tr>
</tbody>
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300">
{{ $caller->status }}
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
<button class="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300">Edit</button>
</td>
@endforeach
</tbody>
</tr>
</table>
</div>
</div>

View File

@ -0,0 +1,327 @@
<!DOCTYPE html>
<html lang="en" class="bg-slate-900">
<head>
@vite(['resources/css/app.css','resources/js/app.js'])
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NCORE - Smart Caller Logging Request</title>
</head>
<style>
/* Base styles for all devices */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
width: 100%;
margin: 0 auto;
padding: 20px;
}
.header, .footer {
background-color: #333;
color: white;
text-align: center;
padding: 10px 0;
}
.content {
padding: 20px;
}
/* Responsive styles */
@media (max-width: 1200px) {
.container {
width: 90%;
}
}
@media (max-width: 992px) {
.container {
width: 80%;
}
}
@media (max-width: 768px) {
.container {
width: 100%;
padding: 10px;
}
.header, .footer {
padding: 5px 0;
}
.content {
padding: 10px;
}
}
@media (max-width: 576px) {
.header, .footer {
font-size: 14px;
}
.content {
font-size: 14px;
}
}
</style>
</head>
<button data-drawer-target="default-sidebar" data-drawer-toggle="default-sidebar" aria-controls="default-sidebar" type="button" class="inline-flex items-center p-2 mt-2 ml-3 text-sm text-gray-500 rounded-lg sm:hidden hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-gray-200 dark:text-gray-400 dark:hover:bg-gray-700 dark:focus:ring-gray-600">
<span class="sr-only">Open sidebar</span>
<svg class="w-6 h-6" aria-hidden="true" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
<path clip-rule="evenodd" fill-rule="evenodd" d="M2 4.75A.75.75 0 012.75 4h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 4.75zm0 10.5a.75.75 0 01.75-.75h7.5a.75.75 0 010 1.5h-7.5a.75.75 0 01-.75-.75zM2 10a.75.75 0 01.75-.75h14.5a.75.75 0 010 1.5H2.75A.75.75 0 012 10z"></path>
</svg>
</button>
<body class="bg-light text-dark">
<aside id="default-sidebar" class="fixed top-0 left-0 z-50 w-64 h-screen transition-transform -translate-x-full sm:translate-x-0" aria-label="Sidenav">
<div class="overflow-y-auto py-5 px-3 h-full bg-slate-800 text-white border-r border-gray-200 dark:bg-slate-700 dark:border-slate-700">
<div class="flex items-center justify-between mb-6">
<div class="col">
<i class="bi bi-fire flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 32px;"></i>
<span class="text-2xl font-semibold">NCORE</span>
</div>
<button type="button" data-drawer-toggle="default-sidebar" class="sm:hidden text-gray-400 hover:text-white">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
</svg>
</button>
</div>
<ul class="space-y-2 rounded-lg text-light bg-white">
<li>
<a href="{{route('dashboard')}}" class="flex items-center p-2 text-base font-normal text-gray-900 rounded-lg dark:text-white hover:bg-gray-100 dark:hover:bg-gray-700 group">
<i class="bi bi-house-check-fill flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 20px;"></i>
<span class="ml-3">Dashboard</span>
</a>
</li>
<li>
<button type="button" class="flex items-center p-2 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-slate-100 dark:text-white dark:hover:bg-slate-700" aria-controls="dropdown-pages" data-collapse-toggle="dropdown-pages">
<i class="bi bi-fire flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 20px;"></i>
<span class="flex-1 ml-3 text-left whitespace-nowrap">Firewall</span>
<svg aria-hidden="true" class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
</button>
<ul id="dropdown-pages" class="hidden py-2 space-y-2">
<li>
<a href="#" class="flex items-center p-2 pl-11 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700">
<i class="bi bi-ethernet flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 16px;"></i>
Traffic Rules
</a>
</li>
<li>
<a href="#" class="flex items-center p-2 pl-11 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700">
<i class="bi bi-table flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 16px;"></i>
Routing Table
</a>
</li>
</ul>
</li>
<li>
<button type="button" class="flex items-center p-2 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700" aria-controls="dropdown-authentication" data-collapse-toggle="dropdown-authentication">
<i class="bi bi-router flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 20px;"></i>
<span class="flex-1 ml-3 text-left whitespace-nowrap">Protocols</span>
<svg aria-hidden="true" class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
</button>
<ul id="dropdown-authentication" class="hidden py-2 space-y-2">
<li>
<a href="#" class="flex items-center p-2 pl-11 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700">
<i class="bi bi-building-add flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 16px;"></i>
GRE
</a>
</li>
<li>
<a href="#" class="flex items-center p-2 pl-11 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700">
<i class="bi bi-building-add flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 16px;"></i>
VXLAN
</a>
</li>
<li>
<a href="#" class="flex items-center p-2 pl-11 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700">
<i class="bi bi-building-add flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 16px;"></i>
WireGuard
</a>
</li>
</ul>
</li>
</ul>
<ul class="pt-5 mt-5 space-y-2 rounded-lg text-light bg-white border-t border-gray-200 dark:border-gray-700">
<li>
<button type="button" class="flex items-center p-2 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-slate-100 dark:text-white dark:hover:bg-slate-700" aria-controls="dropdown-administration" data-collapse-toggle="dropdown-administration">
<i class="bi bi-server flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 20px;"></i>
<span class="flex-1 ml-3 text-left whitespace-nowrap">Nodes</span>
<svg aria-hidden="true" class="w-6 h-6" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
</button>
<ul id="dropdown-administration" class="hidden py-2 space-y-2">
<li>
<a href="{{ route('caller') }}" class="flex items-center p-2 pl-11 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700">
<i class="bi bi-phone-vibrate flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 16px;"></i>
Smart Call API
</a>
</li>
<li>
<a href="{{ route('caller_log') }}" class="flex items-center p-2 pl-11 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700">
<i class="bi bi-list flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 16px;"></i>
Logs
</a>
</li>
</ul>
</li>
</ul>
</div>
<div class="hidden absolute bottom-0 left-0 justify-center p-4 space-x-4 w-full lg:flex bg-white dark:bg-gray-800 z-20 border-r border-gray-200 dark:border-gray-700">
<a href="https://repo.ncore-solution.de/Jan_Hill/NCORE_Smart_Firewall" target="_blank" class="text-gray-900 dark:text-white hover:text-gray-900 dark:hover:text-white">
<i class="bi bi-git" style="font-size: 24px;"></i>
Visit Repository
</a>
</div>
</aside>
<main class="p-4 md:ml-64 h-auto pt-20 bg-gradient-to-br from-slate-800 to-slate-900 relative">
<div class="absolute inset-0" style="background-image: radial-gradient(circle at 1px 1px, rgba(255,255,255,0.05) 1px, transparent 0); background-size: 40px 40px;"></div>
<div class="w-full bg-white rounded-lg shadow dark:bg-gray-800">
<div class="flex items-center justify-between p-4 border-b dark:border-gray-700">
<h5 class="text-xl font-semibold text-gray-900 dark:text-white">Smart Call API Logs</h5>
</div>
<div class="p-4">
<div class="relative overflow-x-auto">
<table id="logsTable" class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
<th scope="col" class="px-6 py-3">ID</th>
<th scope="col" class="px-6 py-3">Node</th>
<th scope="col" class="px-6 py-3">Status</th>
<th scope="col" class="px-6 py-3">Requested IPv4</th>
<th scope="col" class="px-6 py-3">Requested Url</th>
<th scope="col" class="px-6 py-3">Token Managed By</th>
<th scope="col" class="px-6 py-3">Exception</th>
<th scope="col" class="px-6 py-3">Date</th>
</tr>
</thead>
<tbody>
@php
$counter = 1;
@endphp
@foreach($caller_logs as $caller)
<tr>
<td class="px-6 py-4 whitespace-nowrap">{{ $counter++ }}</td>
<td class="px-6 py-4 whitespace-nowrap">{{ $caller->caller_name }}</td>
<td class="px-6 py-4 whitespace-nowrap">
@if ( $caller->status === "success" )
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300">
{{ $caller->status }}
</span>
@else
<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-300">
{{ $caller->status }}
</span>
@endif
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
{{ $caller->req_ip }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
{{ $caller->req_command }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
{{ $caller->token_by }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
{{ $caller->error_exception }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
{{ $caller->created_at }}
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</main>
<script>
document.addEventListener('DOMContentLoaded', () => {
$('#logsTable').DataTable({
"searching": true,
"paging": true,
"info": true,
"autoWidth": false,
"responsive": true
});
});
</script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('addNode');
if (form) {
form.addEventListener('submit', (event) => {
event.preventDefault();
const node_name = document.getElementById('node_name').value.trim();
const whitelist_ip_input = document.getElementById('whitelist_ip').value.trim();
const access_token = document.getElementById('access_token').value.trim();
const whitelist_ip = document.getElementById('whitelist_ip').value.trim();
if (!node_name) {
Swal.fire({
title: "Validation Error",
text: "Node name is required",
icon: "error"
});
return;
}
axios.post("{{ route('caller_add') }}", {
node_name: node_name,
whitelist_ip: whitelist_ip,
token: access_token
})
.then(function (response) {
Swal.fire({
title: "Success!",
text: response.data.message,
icon: "success",
timer: response.data.redirect
}).then(() => {
window.location.href = "{{ route('caller') }}";
});
console.log(response);
})
.catch(function (error) {
Swal.fire({
title: "Error!",
text: error.response?.data?.message || "An error occurred.",
icon: "error",
showClass: {
popup: `
animate__animated
animate__fadeInUp
animate__faster
`
},
hideClass: {
popup: `
animate__animated
animate__fadeOutDown
animate__faster
`
}
});
console.log(error);
});
});
}
});
</script>
</body>
</html>

View File

@ -160,7 +160,7 @@
</a>
</li>
<li>
<a href="#" class="flex items-center p-2 pl-11 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700">
<a href="{{ route('caller_log') }}" class="flex items-center p-2 pl-11 w-full text-base font-normal text-gray-900 rounded-lg transition duration-75 group hover:bg-gray-100 dark:text-white dark:hover:bg-gray-700">
<i class="bi bi-list flex-shrink-0 w-6 h-6 text-gray-400 transition duration-75 group-hover:text-gray-900 dark:text-gray-400 dark:group-hover:text-white" style="font-size: 16px;"></i>
Logs
</a>

View File

@ -3,6 +3,8 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum');
// The Smart API Caller
Route::group(['prefix' => 'caller'], function () {
Route::post('register', [\App\Http\Controllers\ApiController::class, 'checker'])->name('checker');
});

View File

@ -31,6 +31,8 @@ Route::Group(['prefix' => 'call', 'middleware' => 'web'], function () {
Route::Group(['prefix' => 'internal', 'middleware' => 'auth'], function () {
Route::get('dashboard/', [\App\Http\Controllers\SmartController::class, 'dashboard'])->name('dashboard');
Route::get('caller/', [\App\Http\Controllers\SmartController::class, 'caller'])->name('caller');
Route::get('caller/logs', [\App\Http\Controllers\SmartController::class, 'caller_log'])->name('caller_log');
// Post Request for Internal Handling