0
0
mirror of https://gitlab.metager.de/open-source/MetaGer.git synced 2025-10-06 00:32:55 +02:00

remove spam middleware

This commit is contained in:
Dominik Hebeler
2025-07-23 12:44:13 +02:00
parent bcf1df82d6
commit b17c5da5a5
9 changed files with 0 additions and 449 deletions

View File

@@ -25,7 +25,6 @@ php artisan migrate --force
php artisan optimize
php artisan route:clear # Do not cache routes; Interferes with Localization
php artisan spam:load
php artisan load:affiliate-blacklist
docker-php-entrypoint php-fpm &

View File

@@ -1,82 +0,0 @@
<?php
namespace App\Console\Commands;
use Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
class LoadSpam extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'spam:load';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Loads a list of current Spams into redis';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// Redis might not be available now
for ($count = 0; $count < 60; $count++) {
try {
$this->loadSpam();
return 0;
} catch (\Exception $e) {
if ($count >= 59) {
// If its not available after 10 seconds we will exit
return 1;
}
sleep(1);
}
}
}
private function loadSpam()
{
$filePath = \storage_path('metager/ban.txt');
$bans = [];
if (\file_exists($filePath)) {
$bans = json_decode(file_get_contents($filePath), true);
}
$bansToLoad = [];
foreach ($bans as $ban) {
$bannedUntil = Carbon::createFromFormat("Y-m-d H:i:s", $ban["banned-until"]);
if ($bannedUntil->isAfter(Carbon::now())) {
$bansToLoad[] = $ban["regexp"];
}
}
Redis::pipeline(function ($redis) use ($bansToLoad) {
$redis->del("spam");
foreach ($bansToLoad as $ban) {
$redis->rpush("spam", $ban);
}
});
}
}

View File

@@ -1,186 +0,0 @@
<?php
namespace App\Http\Controllers;
use App\QueryLogger;
use Carbon\Carbon;
use Exception;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Redis;
use Log;
use PDO;
class AdminSpamController extends Controller
{
public function index()
{
$since = now()->subMinutes(3);
$queries = $this->getQueries($since);
$latest = now();
if (sizeof($queries) > 0) {
$latest = clone $queries[sizeof($queries) - 1]->time;
}
$currentBans = $this->getBans();
$loadedBans = Redis::lrange("spam", 0, -1);
return view("admin.spam")
->with('title', "Spam Konfiguration - MetaGer")
->with('queries', $queries)
->with('latest', $latest)
->with('bans', $currentBans)
->with('loadedBans', $loadedBans)
->with('js', [mix('js/admin/spam.js')])
->with('css', [
mix('/css/admin/spam/style.css')
])
->with('darkcss', [mix('/css/admin/spam/dark.css')]);
}
public function ban(Request $request)
{
$banTime = $request->input('ban-time');
$banRegexp = $request->input('regexp');
$file = storage_path('metager/ban.txt');
$bans = [];
if (file_exists($file)) {
$bans = json_decode(file_get_contents($file), true);
}
$bans[] = ["banned-until" => $banTime . " 00:00:00", "regexp" => $banRegexp];
\file_put_contents($file, json_encode($bans));
return redirect(url('admin/spam'));
}
public function jsonQueries(Request $request)
{
if (!$request->filled("since")) {
abort(404);
} else {
$since = Carbon::createFromFormat("Y-m-d H:i:s", $request->input("since"));
}
$queries = $this->getQueries($since);
$latest = now();
if (sizeof($queries) > 0) {
$latest = clone $queries[sizeof($queries) - 1]->time;
}
$result = [
"latest" => $latest->format("Y-m-d H:i:s"),
"queries" => $queries,
];
# JSON encoding will fail if invalid UTF-8 Characters are in this string
# mb_convert_encoding will remove thise invalid characters for us
return response()->json($result);
}
public function queryregexp(Request $request)
{
$data = json_decode($request->getContent(), true);
$queries = $data["queries"];
$regexps = [$data["regexp"]];
$bans = $this->getBans();
foreach ($bans as $ban) {
$regexps[] = $ban["regexp"];
}
$resultData = [];
foreach ($queries as $query) {
$matches = false;
foreach ($regexps as $regexp) {
try {
if (preg_match($regexp, $query)) {
$matches = true;
}
} catch (\Exception $e) {
// Exceptions are expected when no valid regexp is given
}
}
$resultData[] = [
"query" => $query,
"matches" => $matches,
];
}
# JSON encoding will fail if invalid UTF-8 Characters are in this string
# mb_convert_encoding will remove thise invalid characters for us
$resultData = mb_convert_encoding($resultData, "UTF-8", "UTF-8");
return response()->json($resultData);
}
private function getQueries(Carbon $since)
{
$query_logger = \app()->make(QueryLogger::class);
$queries = $query_logger->getLogsSince($since);
# Parse the Time
foreach ($queries as $index => $query) {
if (strpos($query->time, ".") === false)
$query->time .= ".000";
$time = Carbon::createFromFormat("Y-m-d H:i:s.u", $query->time, "UTC");
$time->setTimezone(config("app.timezone"));
$queries[$index]->time = $time;
$queries[$index]->time_string = $time->isToday() ? $time->format("H:i:s") : $time->format("d.m.Y H:i:s");
$expiration = clone $time;
$expiration->addMinutes(3);
$queries[$index]->expiration = $expiration;
$queries[$index]->expiration_timestamp = $expiration->timestamp;
}
return $queries;
}
public function getBans()
{
$file = \storage_path('metager/ban.txt');
$bans = [];
if (file_exists($file)) {
$tmpBans = json_decode(file_get_contents($file), true);
if (!empty($tmpBans) && is_array($tmpBans)) {
foreach ($tmpBans as $ban) {
#dd($ban["banned-until"]);
$bannedUntil = Carbon::createFromFormat('Y-m-d H:i:s', $ban["banned-until"]);
if ($bannedUntil->isAfter(Carbon::now())) {
$bans[] = $ban;
}
}
}
}
return $bans;
}
public function deleteRegexp(Request $request)
{
$file = \storage_path('metager/ban.txt');
$bans = [];
if (file_exists($file)) {
$bans = json_decode(file_get_contents($file), true);
}
$regexpToDelete = $request->input('regexp');
$newBans = [];
foreach ($bans as $ban) {
if ($ban["regexp"] !== $regexpToDelete) {
$newBans[] = $ban;
}
}
file_put_contents($file, json_encode($newBans));
return redirect(url('admin/spam'));
}
}

View File

@@ -1,65 +0,0 @@
<?php
namespace App\Http\Middleware;
use App\Models\Authorization\Authorization;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Jenssegers\Agent\Agent;
class Spam
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if (app(Authorization::class)->canDoAuthenticatedSearch()) {
return $next($request);
}
# Check for recent Spams
$eingabe = $request->input('eingabe');
$spams = Redis::lrange("spam", 0, -1);
$spam = false;
foreach ($spams as $spam) {
if (\preg_match("/" . $spam . "/", $eingabe)) {
$spam = true;
break;
}
}
if ($spam === true) {
$browser = new Agent();
$browser->setUserAgent($_SERVER["AGENT"]);
if ($browser->browser() === "Chrome" && $browser->version($browser->browser()) === "91.0.4472.77") {
abort(404);
}
// ToDo Remove Log
$file_path = \storage_path("metager/spam.csv");
$fh = fopen($file_path, "a");
try {
$data = [
now()->format("Y-m-d H:i:s"),
$request->input("eingabe", ""),
];
foreach ($request->header() as $key => $value) {
$data[] = $key . ":" . json_encode($value);
}
\fputcsv($fh, $data);
} finally {
fclose($fh);
}
}
return $next($request);
}
}

View File

@@ -78,7 +78,6 @@ return Application::configure(basePath: dirname(__DIR__))
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'useragentmaster' => \App\Http\Middleware\UserAgentMaster::class,
'spam' => \App\Http\Middleware\Spam::class,
'allow-local-only' => AllowLocalOnly::class,
'httpcache' => HttpCache::class,
'externalimagesearch' => ExternalImagesearch::class,

View File

@@ -1,94 +0,0 @@
@extends('layouts.subPages')
@section('title', $title )
@section('content')
<div id="block-requests">
<form method="post">
<input class="form-control" type="text" name="regexp" id="regexp" placeholder="Type in regexp to match queries...">
<div id="ban-until">
<label for="ban-time">Ban Until</label>
<input type="date" name="ban-time" min="{{now()->format("Y-m-d")}}" id="ban-time">
</div>
<button type="submit" class="btn btn-default btn-sm">Sperren</button>
</form>
</div>
<div id="bans">
<h1>Current Bans</h1>
<table class="table table-striped">
<thead>
<tr>
<td>Regexp</td>
<td>Banned until</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
@foreach($bans as $ban)
<tr>
<td>{{ $ban["regexp"] }}</td>
<td>{{ Carbon::createFromFormat("Y-m-d H:i:s", $ban["banned-until"])->format("d.m.Y H:i:s")}} ({{ Carbon::createFromFormat("Y-m-d H:i:s", $ban["banned-until"])->diffInDays(Carbon::now(), true) }} Days)</td>
<td>
<form action="{{ url("admin/spam/deleteRegexp") }}" method="post">
<input type="hidden" name="regexp" value="{{ $ban["regexp"] }}">
<button type="submit">&#128465;</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div id="loadedbans">
<h1>Loaded Bans</h1>
<table class="table table-striped">
<thead>
<tr>
<td>Regexp</td>
</tr>
</thead>
<tbody>
@foreach($loadedBans as $ban)
<tr>
<td>{{ $ban }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div id="head">
<h1>Letzte Suchanfragen</h1>
<button type="button" class="btn btn-success btn-sm">Alte Abfragen entfernen</button>
</div>
<input class="form-control" type="text" name="" id="check-against" placeholder="Match against...">
<table id="queries" class="table table-striped" data-latest="{{$latest->format("Y-m-d H:i:s")}}" data-api="{{ url('admin/spam/jsonQueries') }}">
<thead>
<tr>
<td>Zeit</td>
<td>Referer</td>
<td>Abfragezeit</td>
<td>Fokus</td>
<td>Locale</td>
<td>Abfrage</td>
</tr>
</thead>
<tbody>
@foreach($queries as $index => $query)
<tr data-expiration="{{$query->expiration->timestamp}}" @if($index % 2 === 0) class="dark" @endif>
<td>
@if($query->time->isToday())
{{$query->time->format("H:i:s")}}
@else
{{$query->time->format("d.m.Y H:i:s")}}
@endif
</td>
<td class="referer" title="{{$query->referer}}">{{$query->referer}}</td>
<td>{{$query->request_time}}</td>
<td>{{$query->focus}}</td>
<td>{{$query->locale}}</td>
<td>{{$query->query}}</td>
</tr>
@endforeach
</tbody>
</table>
@endsection

View File

@@ -18,7 +18,6 @@ Schedule::command("requests:gather")->everyFifteenMinutes();
Schedule::command("requests:useragents")->everyFiveMinutes();
Schedule::command("logs:gather")->everyMinute();
Schedule::command("logs:truncate")->daily()->onOneServer();
Schedule::command("spam:load")->everyMinute();
Schedule::command("load:affiliate-blacklist")->everyMinute();
Schedule::command("affilliates:store")->everyMinute()->onOneServer();
Schedule::call(function () {

View File

@@ -41,16 +41,6 @@ Route::group(['middleware' => $auth_middleware, 'prefix' => 'admin'], function (
dd($request->ip(), $_SERVER["AGENT"], $request->headers);
}
);
Route::group(
['prefix' => 'spam'],
function () {
Route::get('/', 'AdminSpamController@index');
Route::post('/', 'AdminSpamController@ban');
Route::get('jsonQueries', 'AdminSpamController@jsonQueries');
Route::post('queryregexp', 'AdminSpamController@queryregexp');
Route::post('deleteRegexp', 'AdminSpamController@deleteRegexp');
}
);
Route::get('stress', 'Stresstest@index');
Route::get('stress/verify', 'Stresstest@index');
Route::get('adgoal', 'AdgoalTestController@index')->name("adgoal-index");

View File

@@ -26,15 +26,7 @@ mix
"resources/less/metager/pages/startpage/dark.less",
"public/css/themes/startpage/dark.css"
)
.less(
"resources/less/metager/pages/admin/spam/style.less",
"public/css/admin/spam/style.css"
)
.less("resources/less/metager/pages/admin/membership.less", "public/css/admin/membership.css")
.less(
"resources/less/metager/pages/admin/spam/dark.less",
"public/css/admin/spam/dark.css"
)
.less(
"resources/less/metager/metager-dark.less",
"public/css/themes/metager-dark.css"
@@ -169,7 +161,6 @@ mix
"resources/js/admin/affilliates/index.js",
"public/js/admin/affilliates.js"
)
.js("resources/js/admin/spam.js", "public/js/admin/spam.js")
.js("resources/js/admin/bot.js", "public/js/admin/bot.js")
.js("resources/js/verify.js", "public/js/index.js")
.js("resources/js/membership.js", "public/js/membership.js")