Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-tharwat committed Aug 26, 2024
1 parent cfc3969 commit 07a931b
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 126 deletions.
14 changes: 8 additions & 6 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
$schedule->call('\App\Http\Controllers\Backend\BackendScheduleController@update_traffics_country')->everyMinute();
$schedule->call('\App\Http\Controllers\Backend\BackendScheduleController@update_under_attack_limits')->everyFiveMinutes();
$schedule->call('\App\Http\Controllers\Backend\BackendScheduleController@clean_items_seens')->daily();
$schedule->call('\App\Http\Controllers\Backend\BackendScheduleController@clean_dashboard_logs')->daily();
$schedule->call('\App\Http\Controllers\Backend\BackendScheduleController@clean_sessions_rate_limits')->daily();


$schedule_controller = "\App\Http\Controllers\Backend\BackendScheduleController";

$schedule->call("$schedule_controller@update_traffics_country")->name("update_traffics_country")->withoutOverlapping()->everyMinute();
$schedule->call("$schedule_controller@update_under_attack")->name("update_under_attack")->withoutOverlapping()->everyFiveMinutes();

$schedule->call("$schedule_controller@clean_system")->daily();


// $schedule->command('inspire')->hourly();
}
Expand Down
41 changes: 28 additions & 13 deletions app/Http/Controllers/Backend/BackendScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@

class BackendScheduleController extends Controller
{
public function clean_items_seens(){
\App\Models\ItemSeen::where('created_at','<',\Carbon::parse(now())->subDays(8)->format('Y-m-d H:i:s'))->delete();
}
public function clean_dashboard_logs(){
\App\Models\RateLimit::where('created_at','<',\Carbon::parse(now())->subDays(8)->format('Y-m-d H:i:s'))->delete();
\App\Models\RateLimitDetail::where('created_at','<',\Carbon::parse(now())->subDays(8)->format('Y-m-d H:i:s'))->delete();
}


public function update_traffics_country(){
$rate_limits = \App\Models\RateLimit::whereNull('country_code')->get();
foreach($rate_limits as $rate_limit){
Expand All @@ -29,7 +24,21 @@ public function update_traffics_country(){
]);
}
}
public function update_under_attack_limits(){
public function update_under_attack(){

//enable under attack
$ip=\UserSystemInfoHelper::get_ip();
$total_req_per_minute = \App\Models\RateLimitDetail::where('created_at','>=',\Carbon::parse(now())->subMinutes(2)->format('Y-m-d H:i:s'))->orderBy('id','DESC')->count();

if($total_req_per_minute>=10000){
$attacks=\App\Models\UnderAttack::where('status','UNDER_ATTACK')->where('release_at','>',\Carbon::parse(now())->format('Y-m-d H:i:s'))->count();
if($attacks==0){
\App\Models\UnderAttack::create(['status'=>"UNDER_ATTACK",'release_at'=>\Carbon::parse(now())->addMinutes(30)->format('Y-m-d H:i:s')]);
(new \App\Helpers\SecurityHelper)->enable_under_attack_mode();
}
}

//disable under attack
$under_attacks=\App\Models\UnderAttack::where('status','UNDER_ATTACK')->where('created_at','<',\Carbon::parse(now())->addMinutes(15)->format('Y-m-d H:i:s'))->count();
if($under_attacks){
(new \App\Helpers\SecurityHelper)->disable_under_attack_mode();
Expand All @@ -39,20 +48,26 @@ public function update_under_attack_limits(){
foreach($blocked_ips as $blocked_ip){
$response = (new \App\Helpers\SecurityHelper)->unblock_ip($blocked_ip->state_id);
}

}
public function clean_sessions_rate_limits(){
public function clean_system(){
//general users
$delete_sessions = \App\Session::whereNull('user_id')->delete();
$delete_sessions = \App\Models\Session::whereNull('user_id')->delete();

//loged in users
$delete_old_sessions = \App\Session::whereNull('user_id')->where('last_activity','<',\Carbon::now()->subDays(30)->valueOf()/1000)->delete();
$delete_old_sessions = \App\Models\Session::whereNull('user_id')->where('last_activity','<',\Carbon::now()->subDays(30)->valueOf()/1000)->delete();

//delete rate limits
$delete_old_rate_limits = \App\RateLimit::whereDate('created_at', '<',\Carbon::now()->subDays(3))->delete();
$delete_rate_limits = \App\Models\RateLimit::where('created_at','<',\Carbon::parse(now())->subDays(8)->format('Y-m-d H:i:s'))->delete();

//delete rate limit details
$delete_rate_limit_details = \App\Models\RateLimitDetail::where('created_at','<',\Carbon::parse(now())->subDays(8)->format('Y-m-d H:i:s'))->delete();

//delete item seens
$delete_old_rate_limits = \App\ItemSeen::whereDate('created_at', '<',\Carbon::now()->subDays(1))->delete();
$delete_old_rate_limits = \App\Models\ItemSeen::whereDate('created_at', '<',\Carbon::now()->subDays(1))->delete();

//old_error_reports
$delete_old_error_reports = \App\Models\ReportError::whereDate('created_at', '<',\Carbon::now()->subDays(3))->delete();

}

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit 07a931b

Please sign in to comment.