Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-tharwat committed Aug 25, 2024
1 parent 5734e4d commit bfa3f48
Show file tree
Hide file tree
Showing 16 changed files with 1,680 additions and 402 deletions.
85 changes: 23 additions & 62 deletions app/Helpers/MainHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,69 +125,30 @@ public static function binaryToString($binary)
/*/src=[\"\'][^\'\']+[\"\']/*/

public static function rate_limit_insert(){

$ip=\UserSystemInfoHelper::get_ip();
$total_req_per_minute = \App\Models\RateLimitDetail::where('created_at','>=',\Carbon::parse(now())->subMinutes(1)->format('Y-m-d H:i:s'))->orderBy('id','DESC')->count();
if($total_req_per_minute>=2000){
$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();
}
}
$limit_for_ip = \App\Models\RateLimitDetail::where('ip',\UserSystemInfoHelper::get_ip())->where('created_at','>=',\Carbon::parse(now())->subMinutes(1)->format('Y-m-d H:i:s'))->orderBy('id','DESC')->count();
if($limit_for_ip>=100){
$response = (new \App\Helpers\SecurityHelper)->block_ip($ip,request()->header('User-Agent'));
abort(403);
}

$last_insert = \App\Models\RateLimit::where('ip',$ip)->where('created_at','<=',\Carbon::parse(now())->addMinutes(3))->first();

if($last_insert==null){
$prev_url="";
$prev_url="";
$prev_domain="";
if(filter_var(url()->previous(), FILTER_VALIDATE_URL))
{
$parsex= parse_url(url()->previous());
$prev_domain=$parsex['host'];
$prev_domain="";
if(filter_var(url()->previous(), FILTER_VALIDATE_URL)) // is a valid url
{
$parsex= parse_url(url()->previous());
$prev_domain=$parsex['host'];
$prev_domain="";
try{
$prev_url= url()->previous();
$prev_domain=$parsex['host'];
}catch(\Exception $e){

}
}
$country=(new UserSystemInfoHelper)->get_country_from_ip($ip);
$traffic= \App\Models\RateLimit::create([
'traffic_landing'=>\Request::fullUrl(),
'domain'=>$prev_domain,
'prev_link'=>$prev_url,
'ip'=>$ip,
//'country_code'=>$country['country_code'],
//'country_name'=>$country['country'],
'agent_name'=>request()->header('User-Agent'),
'user_id'=>auth()->check() ? auth()->user()->id : null ,
'browser'=>UserSystemInfoHelper::get_browsers(),
'device'=>UserSystemInfoHelper::get_device(),
'operating_system'=>UserSystemInfoHelper::get_os()
]);
\App\Models\RateLimitDetail::create([
'url'=>request()->fullUrl(),
'user_id'=> auth()->check() ? auth()->user()->id : null,
'rate_limit_id'=>$traffic->id,
'ip'=>$ip
]);
return $traffic;
}else{
\App\Models\RateLimitDetail::create([
'url'=>request()->fullUrl(),
'user_id'=> auth()->check() ? auth()->user()->id : null,
'rate_limit_id'=>$last_insert->id,
'ip'=>$ip
]);
}
return $last_insert;
try{
$prev_url= url()->previous();
$prev_domain=$parsex['host'];
}catch(\Exception $e){}
}
$data=[
'traffic_landing'=>request()->fullUrl(),
'ip'=>\UserSystemInfoHelper::get_ip(),
'prev_url'=>$prev_url,
'prev_domain'=>$prev_domain,
'agent_name'=>request()->header('User-Agent'),
'user_id'=>auth()->check() ? auth()->user()->id : null ,
'browser'=>\UserSystemInfoHelper::get_browsers(),
'device'=>\UserSystemInfoHelper::get_device(),
'operating_system'=>\UserSystemInfoHelper::get_os()
];
\App\Jobs\RateLimitInsertJob::dispatch($data);
}

public static function focus_urls($string)
Expand Down
23 changes: 9 additions & 14 deletions app/Http/Controllers/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,15 @@ public function blog(Request $request)
}
public function views_increase_article(Article $article)
{
$counter = $article->item_seens()->where('type',"ARTICLE")->where('ip',\UserSystemInfoHelper::get_ip())->whereDate('created_at', \Carbon::today())->count();
if (!$counter) {
\App\Models\ItemSeen::create([
'type_id'=>$article->id,
'type'=>"ARTICLE",
'ip'=>\UserSystemInfoHelper::get_ip(),
'prev_link'=>\UserSystemInfoHelper::prev_url(),
'agent_name'=>request()->header('User-Agent'),
'browser'=>\UserSystemInfoHelper::get_browsers(),
'device'=>\UserSystemInfoHelper::get_device(),
'operating_system'=>\UserSystemInfoHelper::get_os()
]);
$article->update(['views' => $article->views + 1]);
}
$data= [
'ip'=>\UserSystemInfoHelper::get_ip(),
'prev_link'=>\UserSystemInfoHelper::prev_url(),
'agent_name'=>request()->header('User-Agent'),
'browser'=>\UserSystemInfoHelper::get_browsers(),
'device'=>\UserSystemInfoHelper::get_device(),
'operating_system'=>\UserSystemInfoHelper::get_os()
];
\App\Jobs\ItemSeenInsertJob::dispatch("\App\Models\Article",$article->id,$data);
}
}

19 changes: 17 additions & 2 deletions app/Http/Middleware/RedirectLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,30 @@ public function handle(Request $request, Closure $next)
{


if(Schema::hasTable('redirections')){
/*if(Schema::hasTable('redirections')){
$url = str_replace('www.','' , preg_replace("(^https?://)", "", url()->full() ) );
$redirection = \App\Models\Redirection::where('url','LIKE','%'.$url)->first();
if($redirection !=null){
header('Location: ' . $redirection->new_url, true, $redirection->code);
die();
}
}
}*/

try{
$url = str_replace('www.','' , preg_replace("(^https?://)", "", url()->full() ) );
$redirections = cache()->remember('redirections',60,function(){
return \App\Models\Redirection::get();
});
foreach($redirections as $redirection){
if(str_contains($redirection->url, $url)){
header('Location: ' . $redirection->new_url, true, $redirection->code);
die();
}
}
}catch(\Execption $e){}

return $next($request);

return $next($request);
}
Expand Down
62 changes: 62 additions & 0 deletions app/Jobs/ItemSeenInsertJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ItemSeenInsertJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;


public $model_name;
public $model_id;
public $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($model_name,$model_id,$data)
{
$this->model_name = $model_name;
$this->model_id = $model_id;
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$classNameString = strtoupper((new \ReflectionClass($this->model_name))->getShortName());
$data = $this->data;
$model_id = $this->model_id;

$item_seen = cache()->remember('item_seen_'.\MainHelper::slug($this->model_id).\MainHelper::slug($classNameString).$this->data['ip'],60*60*24,function()use($data,$model_id,$classNameString){
return \App\Models\ItemSeen::where('type_id',$model_id)->where('type',$classNameString)->where('ip',$data['ip'])->whereDate('created_at', \Carbon::today())->first();
});


if ($item_seen==null) {
\App\Models\ItemSeen::insert([[
'type_id'=>$this->model_id,
'type'=>$classNameString,
'ip'=>$this->data['ip'],
'prev_link'=>$this->data['prev_link'],
'agent_name'=>$this->data['agent_name'],
'browser'=>$this->data['browser'],
'device'=>$this->data['device'],
'operating_system'=>$this->data['operating_system']
]]);
$className = $this->model_name;
$className::where('id',$this->model_id)->increment('views');
}
}
}
68 changes: 68 additions & 0 deletions app/Jobs/RateLimitInsertJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class RateLimitInsertJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $data;
public $tries = 2;
public $timeout = 0.1;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($data)
{
$this->data= $data;
}

public function handle()
{
$data=$this->data;
$last_insert = cache()->remember('rate_limit_'.\MainHelper::slug($data['ip']),60*20,function()use($data){
$last_insert = \App\Models\RateLimit::where('ip',$data['ip'])->where('created_at','>=',\Carbon::parse(now())->subMinutes(19))->orderBy('id','DESC')->first();

if($last_insert!=null){
return $last_insert;
}if($last_insert==null){
$country=(new \UserSystemInfoHelper)->get_country_from_ip($data['ip']);
$last_insert= \App\Models\RateLimit::create([
'traffic_landing'=>$data['traffic_landing'],
'domain'=>$data['prev_domain'],
'prev_link'=>$data['prev_url'],
'ip'=>$data['ip'],
'country_code'=>$country['country_code'],
'country_name'=>$country['country'],
'agent_name'=>$data['agent_name'],
'user_id'=>$data['user_id'],
'browser'=>$data['browser'],
'device'=>$data['device'],
'operating_system'=>$data['operating_system'],
'created_at'=>\Carbon::parse(now())->format('Y-m-d H:i:s'),
'updated_at'=>\Carbon::parse(now())->format('Y-m-d H:i:s'),
]);
return $last_insert;
}

});
$rate_limit_detail = \App\Models\RateLimitDetail::insert([[
'url'=>$data['traffic_landing'],
'user_id'=> $data['user_id'],
'rate_limit_id'=>$last_insert->id,
'ip'=>$data['ip'],
'created_at'=>\Carbon::parse(now())->format('Y-m-d H:i:s'),
'updated_at'=>\Carbon::parse(now())->format('Y-m-d H:i:s'),
]]);

}
}
Loading

0 comments on commit bfa3f48

Please sign in to comment.