Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
open-source
MetaGer
Commits
c5c5c346
Commit
c5c5c346
authored
Oct 15, 2019
by
Dominik Hebeler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Users will now get a random user agent string instead of a modified one
parent
a8d83d91
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
179 additions
and
12 deletions
+179
-12
app/Console/Commands/SaveUseragents.php
app/Console/Commands/SaveUseragents.php
+59
-0
app/Console/Kernel.php
app/Console/Kernel.php
+1
-0
app/Http/Kernel.php
app/Http/Kernel.php
+1
-0
app/Http/Middleware/UserAgentMaster.php
app/Http/Middleware/UserAgentMaster.php
+67
-0
app/UserAgent.php
app/UserAgent.php
+10
-0
config/database.php
config/database.php
+5
-1
database/migrations/2019_10_15_103139_create_user_agents_table.php
...migrations/2019_10_15_103139_create_user_agents_table.php
+35
-0
public/index.php
public/index.php
+0
-10
routes/web.php
routes/web.php
+1
-1
No files found.
app/Console/Commands/SaveUseragents.php
0 → 100644
View file @
c5c5c346
<?php
namespace
App\Console\Commands
;
use
Carbon\Carbon
;
use
Illuminate\Console\Command
;
use
Illuminate\Support\Facades\Redis
;
class
SaveUseragents
extends
Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected
$signature
=
'requests:useragents'
;
/**
* The console command description.
*
* @var string
*/
protected
$description
=
'Saves a list of recent User-Agents from Redis into a sqlite database'
;
/**
* Create a new command instance.
*
* @return void
*/
public
function
__construct
()
{
parent
::
__construct
();
}
/**
* Execute the console command.
*
* @return mixed
*/
public
function
handle
()
{
$agents
=
[];
$agent
=
null
;
$now
=
Carbon
::
now
(
'utc'
)
->
toDateTimeString
();
while
((
$agent
=
Redis
::
lpop
(
"useragents"
))
!==
null
)
{
$newEntry
=
json_decode
(
$agent
,
true
);
$newEntry
[
"created_at"
]
=
$now
;
$newEntry
[
"updated_at"
]
=
$now
;
$agents
[]
=
$newEntry
;
}
\
App\UserAgent
::
insert
(
$agents
);
// Delete old entries (older than 24h)
$expiration
=
Carbon
::
now
(
'utc'
)
->
subDays
(
1
);
\
App\UserAgent
::
where
(
'created_at'
,
'<'
,
$expiration
)
->
delete
();
}
}
app/Console/Kernel.php
View file @
c5c5c346
...
@@ -26,6 +26,7 @@ class Kernel extends ConsoleKernel
...
@@ -26,6 +26,7 @@ class Kernel extends ConsoleKernel
protected
function
schedule
(
Schedule
$schedule
)
protected
function
schedule
(
Schedule
$schedule
)
{
{
$schedule
->
command
(
'requests:gather'
)
->
everyFifteenMinutes
();
$schedule
->
command
(
'requests:gather'
)
->
everyFifteenMinutes
();
$schedule
->
command
(
'requests:useragents'
)
->
everyFiveMinutes
();
$schedule
->
call
(
function
()
{
$schedule
->
call
(
function
()
{
DB
::
table
(
'monthlyrequests'
)
->
truncate
();
DB
::
table
(
'monthlyrequests'
)
->
truncate
();
...
...
app/Http/Kernel.php
View file @
c5c5c346
...
@@ -60,5 +60,6 @@ class Kernel extends HttpKernel
...
@@ -60,5 +60,6 @@ class Kernel extends HttpKernel
'throttle'
=>
\
Illuminate\Routing\Middleware\ThrottleRequests
::
class
,
'throttle'
=>
\
Illuminate\Routing\Middleware\ThrottleRequests
::
class
,
'referer.check'
=>
\
App\Http\Middleware\RefererCheck
::
class
,
'referer.check'
=>
\
App\Http\Middleware\RefererCheck
::
class
,
'humanverification'
=>
\
App\Http\Middleware\HumanVerification
::
class
,
'humanverification'
=>
\
App\Http\Middleware\HumanVerification
::
class
,
'useragentmaster'
=>
\
App\Http\Middleware\UserAgentMaster
::
class
,
];
];
}
}
app/Http/Middleware/UserAgentMaster.php
0 → 100644
View file @
c5c5c346
<?php
namespace
App\Http\Middleware
;
use
Closure
;
use
Illuminate\Support\Facades\Redis
;
use
Jenssegers\Agent\Agent
;
class
UserAgentMaster
{
/**
* This Middleware takes the User-Agent of the user and saves it into a Database
* It will also take a random User Agent of a matching device and replace the current User-Agent with it.
*/
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public
function
handle
(
$request
,
Closure
$next
)
{
/**
* Categorize the User-Agents by
* 1. Platform (i.e. Ubuntu)
* 2. Browser (i.e. Firefox)
* 3. Device (desktop|tablet|mobile)
*/
$agent
=
new
Agent
();
$device
=
"desktop"
;
if
(
$agent
->
isTablet
())
{
$device
=
"tablet"
;
}
else
if
(
$agent
->
isPhone
())
{
$device
=
"mobile"
;
}
// Push an entry to a list in Redis
// App\Console\Commands\SaveUseragents.php is called regulary to save the list into a sqlite database
Redis
::
rpush
(
'useragents'
,
json_encode
([
"platform"
=>
$agent
->
platform
(),
"browser"
=>
$agent
->
browser
(),
"device"
=>
$device
,
"useragent"
=>
$_SERVER
[
'HTTP_USER_AGENT'
]]));
Redis
::
expire
(
'useragents'
,
301
);
// Try to retrieve a random User-Agent of the same category from the sqlite database
$newAgent
=
\
App\UserAgent
::
where
(
"platform"
,
$agent
->
platform
())
->
where
(
"browser"
,
$agent
->
browser
())
->
where
(
"device"
,
$device
)
->
inRandomOrder
()
->
limit
(
1
)
->
get
();
if
(
sizeof
(
$newAgent
)
>=
1
)
{
// If there is an Entry in the database use it
$newAgent
=
$newAgent
[
0
]
->
useragent
;
}
else
{
// Else anonymize the version of the current User-Agent
$agentPieces
=
explode
(
" "
,
$_SERVER
[
'HTTP_USER_AGENT'
]);
for
(
$i
=
0
;
$i
<
count
(
$agentPieces
);
$i
++
)
{
$agentPieces
[
$i
]
=
preg_replace
(
"/(\d+\.\d+)/s"
,
"0.0"
,
$agentPieces
[
$i
]);
$agentPieces
[
$i
]
=
preg_replace
(
"/([^\/]*)\/\w+/s"
,
"$1/0.0"
,
$agentPieces
[
$i
]);
}
$newAgent
=
implode
(
" "
,
$agentPieces
);
}
// Replace the User-Agent
$_SERVER
[
'HTTP_USER_AGENT'
]
=
$newAgent
;
return
$next
(
$request
);
}
}
app/UserAgent.php
0 → 100644
View file @
c5c5c346
<?php
namespace
App
;
use
Illuminate\Database\Eloquent\Model
;
class
UserAgent
extends
Model
{
protected
$connection
=
'useragents'
;
}
config/database.php
View file @
c5c5c346
...
@@ -51,7 +51,11 @@ return [
...
@@ -51,7 +51,11 @@ return [
'database'
=>
database_path
(
env
(
'SQLITE_DATABASE'
,
'database.sqlite'
)),
'database'
=>
database_path
(
env
(
'SQLITE_DATABASE'
,
'database.sqlite'
)),
'prefix'
=>
''
,
'prefix'
=>
''
,
],
],
'useragents'
=>
[
'driver'
=>
'sqlite'
,
'database'
=>
database_path
(
'useragents.sqlite'
),
'prefix'
=>
''
,
],
'mysql'
=>
[
'mysql'
=>
[
'driver'
=>
'mysql'
,
'driver'
=>
'mysql'
,
'host'
=>
env
(
'DB_HOST'
,
'localhost'
),
'host'
=>
env
(
'DB_HOST'
,
'localhost'
),
...
...
database/migrations/2019_10_15_103139_create_user_agents_table.php
0 → 100644
View file @
c5c5c346
<?php
use
Illuminate\Database\Migrations\Migration
;
use
Illuminate\Database\Schema\Blueprint
;
use
Illuminate\Support\Facades\Schema
;
class
CreateUserAgentsTable
extends
Migration
{
/**
* Run the migrations.
*
* @return void
*/
public
function
up
()
{
Schema
::
connection
(
'useragents'
)
->
create
(
'user_agents'
,
function
(
Blueprint
$table
)
{
$table
->
increments
(
'id'
);
$table
->
string
(
'platform'
);
$table
->
string
(
'browser'
);
$table
->
enum
(
'device'
,
[
"desktop"
,
"tablet"
,
"mobile"
]);
$table
->
string
(
'useragent'
,
300
);
$table
->
timestamps
();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public
function
down
()
{
Schema
::
connection
(
'useragents'
)
->
dropIfExists
(
'user_agents'
);
}
}
public/index.php
View file @
c5c5c346
...
@@ -15,16 +15,6 @@ if (isset($_SERVER["HTTP_FORWARDED"]) && isset($_SERVER["HTTP_X_FORWARDED_FOR"])
...
@@ -15,16 +15,6 @@ if (isset($_SERVER["HTTP_FORWARDED"]) && isset($_SERVER["HTTP_X_FORWARDED_FOR"])
$_SERVER
[
"AGENT"
]
=
$_SERVER
[
"HTTP_USER_AGENT"
];
$_SERVER
[
"AGENT"
]
=
$_SERVER
[
"HTTP_USER_AGENT"
];
if
(
isset
(
$_SERVER
[
'HTTP_USER_AGENT'
]))
{
$agentPieces
=
explode
(
" "
,
$_SERVER
[
'HTTP_USER_AGENT'
]);
for
(
$i
=
0
;
$i
<
count
(
$agentPieces
);
$i
++
)
{
$agentPieces
[
$i
]
=
preg_replace
(
"/(\d+\.\d+)/s"
,
"0.0"
,
$agentPieces
[
$i
]);
$agentPieces
[
$i
]
=
preg_replace
(
"/([^\/]*)\/\w+/s"
,
"$1/0.0"
,
$agentPieces
[
$i
]);
}
$_SERVER
[
'HTTP_USER_AGENT'
]
=
implode
(
" "
,
$agentPieces
);
}
/*
/*
|--------------------------------------------------------------------------
|--------------------------------------------------------------------------
| Register The Auto Loader
| Register The Auto Loader
...
...
routes/web.php
View file @
c5c5c346
...
@@ -180,7 +180,7 @@ Route::group(
...
@@ -180,7 +180,7 @@ Route::group(
return
redirect
(
LaravelLocalization
::
getLocalizedURL
(
LaravelLocalization
::
getCurrentLocale
(),
'/'
));
return
redirect
(
LaravelLocalization
::
getLocalizedURL
(
LaravelLocalization
::
getCurrentLocale
(),
'/'
));
});
});
Route
::
match
([
'get'
,
'post'
],
'meta/meta.ger3'
,
'MetaGerSearch@search'
)
->
middleware
(
'humanverification'
);
Route
::
match
([
'get'
,
'post'
],
'meta/meta.ger3'
,
'MetaGerSearch@search'
)
->
middleware
(
'humanverification'
,
'useragentmaster'
);
Route
::
get
(
'meta/loadMore'
,
'MetaGerSearch@loadMore'
);
Route
::
get
(
'meta/loadMore'
,
'MetaGerSearch@loadMore'
);
Route
::
post
(
'img/cat.jpg'
,
'HumanVerification@remove'
);
Route
::
post
(
'img/cat.jpg'
,
'HumanVerification@remove'
);
Route
::
get
(
'r/metager/{mm}/{pw}/{url}'
,
[
'as'
=>
'humanverification'
,
'uses'
=>
'HumanVerification@removeGet'
]);
Route
::
get
(
'r/metager/{mm}/{pw}/{url}'
,
[
'as'
=>
'humanverification'
,
'uses'
=>
'HumanVerification@removeGet'
]);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment