Commit 0cca30a6 authored by Chris Coley's avatar Chris Coley
Browse files

Initial Commit

parents
{
"name": "coding-all-night/laravel-trusted-proxies",
"type": "library",
"description": "A ServiceProvider for setting trusted proxies in Laravel applications",
"keywords": ["laravel", "laravel 5", "proxy", "proxies", "trusted proxies", "load balancing"],
"homepage": "https://git.codingallnight.com/coding-all-night/laravel-trusted-proxies",
"license": "MIT",
"authors": [
{
"name": "Chris Coley",
"email": "chris@codingallnight.com",
"homepage": "http://codingallnight.com"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/contracts": "~5.0"
},
"autoload": {
"psr-4": {
"CodingAllNight\\Proxy\\": "src/"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
<?php
return [
/*
* Set the trusted proxy IP addresses.
*
* Supports IPv4 and IPv6 addresses, and CIDR notation.
*/
'proxies' => [
'192.168.1.1',
],
/*
* Trust all proxies.
*
* This is useful if you cannot know the IP address of your proxy.
*/
// 'proxies' => '*',
/*
* Default Header Names, pulled from Symphony.
*
* Change these to match the headers sent by your proxy.
*
* @see \Symfony\Component\HttpFoundation\Request::$trustedHeaders
*/
'headers' => [
\Illuminate\Http\Request::HEADER_CLIENT_PROTO => 'X_FORWARDED_PROTO',
\Illuminate\Http\Request::HEADER_CLIENT_HOST => 'X_FORWARDED_HOST',
\Illuminate\Http\Request::HEADER_CLIENT_PORT => 'X_FORWARDED_PORT',
\Illuminate\Http\Request::HEADER_CLIENT_IP => 'X_FORWARDED_FOR',
],
];
<?php
namespace CodingAllNight\Proxy;
use Closure;
use Illuminate\Contracts\Config\Repository;
class TrustedProxies
{
/**
* The Config repository instance.
*
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Config\Repository $config
*/
public function __construction(Repository $config) {
$this->config = $config;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next) {
// Set trusted header names
foreach ($this->getTrustedHeaders() as $headerKey => $headerName) {
$request->setTrustedHeaderName($headerKey, $headerName);
}
$request->setTrustedProxies($this->getTrustedProxies($request->getClientIps()));
return $next($request);
}
/**
* Get the array of IP addresses for trusted proxies.
*
* @param array $clientIpAddresses Array of client IP addresses.
* @return array An array of IP addresses of the trusted proxies.
*/
protected function getTrustedProxies(array $clientIpAddress = []) {
$trustedProxies = $this->config->get('trustedproxies.proxies');
// Trust all proxies
if ($trustedProxies === '*') {
return $clientIpAddresses;
}
return (array) $trustedProxies;
}
/**
* Get the trusted header names.
*
* @return array An array of the trusted headers.
*/
protected function getTrustedHeaders() {
$trustedHeaderNames = $this->config->get('trustedproxies.headers');
/*
* If trustedproxies.headers is blank or empty, the defaults from
* Symphony will be used.
*
* @see \Symfony\Component\HttpFoundation\Request::$trustedHeaders
*/
return is_array($trustedHeaderNames) ? $trustedHeaderNames : [];
}
}
<?php
namespace CodingAllNight\Proxy;
use Illuminate\Support\ServiceProvider;
class TrustedProxiesServiceProvider extends ServiceProvider
{
/**
* Boot the service provider.
*
* @return void
*/
public function boot() {
$config = realpath(__DIR__ . '/../config/trustedproxies.php');
// Publish the config to the application's config directory
$this->publishes([$config => config_path('trustedproxies.php')]);
// Merge the config
$this->mergeConfigFrom($config, 'trustedproxies');
}
/**
* Register the service provider.
*
* @return void
*/
public function register() {
// Nothing to do here...
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment