diff --git a/app/Http/Controllers/API/Settings/LogRotationController.php b/app/Http/Controllers/API/Settings/LogRotationController.php index 289cc97c..bf498196 100644 --- a/app/Http/Controllers/API/Settings/LogRotationController.php +++ b/app/Http/Controllers/API/Settings/LogRotationController.php @@ -5,9 +5,45 @@ use App\Http\Controllers\Controller; use App\System\Command; use Illuminate\Http\Request; +use Illuminate\Http\Response; class LogRotationController extends Controller { + /** + * Get log rotation configuration + * + * @return JsonResponse + */ + public function getConfiguration() + { + try { + $config = Command::runSystem('cat /etc/rsyslog.d/liman.conf'); + + $config = explode("\n", $config); + + $type = explode('protocol="', $config[2]); + $type = explode('"', $type[1])[0]; + + $ip = explode('target="', $config[2]); + $ip = explode('"', $ip[1])[0]; + + $port = explode('port="', $config[2]); + $port = explode('"', $port[1])[0]; + + return response()->json([ + 'type' => $type, + 'ip_address' => $ip, + 'port' => $port, + ]); + } catch (\Throwable $e) { + return response()->json([ + 'type' => 'tcp', + 'ip_address' => '', + 'port' => '', + ]); + } + } + /** * Set log rotation configuration * @@ -23,6 +59,26 @@ public function saveConfiguration(Request $request) 'port' => 'required|numeric|between:1,65535' ]); + // Check if the port is open + // Disable fsockopen error reporting + error_reporting(0); + try { + $connection = @fsockopen($request->ip_address, $request->port, $errno, $errstr, 0.1); + if (! $connection) { + return response()->json([ + 'ip_address' => ['Sunucuya erişim sağlanamadı.'], + ], Response::HTTP_UNPROCESSABLE_ENTITY); + } else { + fclose($connection); + } + } catch (\Throwable $e) { + return response()->json([ + 'ip_address' => ['Sunucuya erişim sağlanamadı.'], + ], Response::HTTP_UNPROCESSABLE_ENTITY); + } + // Restore error reporting + error_reporting(E_ALL); + $template = 'module(load="imfile") input(type="imfile" File="/liman/logs/liman_new.log" Tag="engine" ruleset="remote") ruleset(name="remote"){action(type="omfwd" target="" port="" protocol="")} diff --git a/routes/api.php b/routes/api.php index 74d48383..b9c68350 100644 --- a/routes/api.php +++ b/routes/api.php @@ -342,6 +342,7 @@ // Log Rotation Route::group(['prefix' => 'log_rotation'], function () { + Route::get('/', [Settings\LogRotationController::class, 'getConfiguration']); Route::post('/', [Settings\LogRotationController::class, 'saveConfiguration']); }); });