Created
October 19, 2015 08:26
-
-
Save foxted/7a3de20169f8548a1a23 to your computer and use it in GitHub Desktop.
Cloudflare flexible SSL detection in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
class RedirectIfInsecure | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
// if not local environment AND the request isn't safe | |
if (!app()->isLocal() && !$request->secure()) { | |
// redirect to the mathching secure url | |
return redirect()->secure($request->path()); | |
} | |
return $next($request); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
class TrustCloudflare | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
$request->setTrustedProxies([ | |
'199.27.128.0/21', | |
'173.245.48.0/20', | |
'103.21.244.0/22', | |
'103.22.200.0/22', | |
'103.31.4.0/22', | |
'141.101.64.0/18', | |
'108.162.192.0/18', | |
'190.93.240.0/20', | |
'188.114.96.0/20', | |
'197.234.240.0/22', | |
'198.41.128.0/17', | |
'162.158.0.0/15', | |
'104.16.0.0/12', | |
]); | |
return $next($request); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment