Compare commits

..

3 Commits
v0.6.1 ... main

Author SHA1 Message Date
1aedddc66e feat: update package name 2024-06-12 22:02:42 +00:00
dongwei
445f9dd037 add docker ip 2019-02-28 14:20:42 +08:00
George Xie
436a084581 非生产环境的 server 不对检查客户端 ip 白名单 2018-06-26 10:50:49 +08:00
4 changed files with 93 additions and 1251 deletions

View File

@ -1,5 +1,5 @@
{ {
"name": "arch/php-internal-api-client", "name": "paidian/php-internal-api-client",
"type": "library", "type": "library",
"require": { "require": {
"guzzlehttp/guzzle": "^6.3", "guzzlehttp/guzzle": "^6.3",

1194
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -9,60 +9,78 @@ use function PdInternalApi\sign;
class InternalApi class InternalApi
{ {
public function __construct() public function __construct()
{ {
app()->configure('internal_api'); app()->configure('internal_api');
} }
/** private function isClientIPPermitted($ip)
* Handle an incoming request. {
* if (!app()->environment('production', 'staging')) {
* @param \Illuminate\Http\Request $request return true;
* @param \Closure $next }
* @return mixed if (Str::startsWith($ip, [
*/ '127.0.0.1',
public function handle($request, Closure $next) //局域网
{ '192.168.',
$ip = $request->getClientIp(); //vpc
'10.0.',
if (!Str::startsWith($ip, [ //pod network
'127.0.0.', '192.168.', '10.0.' '172.20.',
])) { //北京办公区
return new JsonResponse('', 404); '172.16.'
} ])) {
return true;
$params = $request->all(); }
return false;
if (empty($params['appid'])) { }
$data = ['error' => 'require appid',];
return new JsonResponse($data, 403); /**
} * Handle an incoming request.
*
if (empty($params['timestamp'])) { * @param \Illuminate\Http\Request $request
$data = ['error' => 'require time',]; * @param \Closure $next
return new JsonResponse($data, 403); * @return mixed
} elseif (intval($params['timestamp']) + 60 < time()) { */
$data = ['error' => 'sign expired',]; public function handle($request, Closure $next)
return new JsonResponse($data, 403); {
} $ip = $request->getClientIp();
if (!$this->isClientIPPermitted($ip)) {
$key = config('internal_api.server.' . $params['appid']); return new JsonResponse("$ip is forbidden", 403);
}
if (empty($key)) {
$data = ['error' => 'config error',]; $params = $request->all();
return new JsonResponse($data, 403);
} if (empty($params['appid'])) {
$data = ['error' => 'require appid',];
$sign = sign($params, $key); return new JsonResponse($data, 403);
if ($sign != $params['sign']) { }
$data = [
'error' => 'sign error', if (empty($params['timestamp'])) {
]; $data = ['error' => 'require time',];
return new JsonResponse($data, 403); return new JsonResponse($data, 403);
} } else if (intval($params['timestamp']) + 60 < time()) {
$data = ['error' => 'sign expired',];
return $next($request); return new JsonResponse($data, 403);
} }
$key = config('internal_api.server.' . $params['appid']);
if (empty($key)) {
$data = ['error' => 'config error',];
return new JsonResponse($data, 403);
}
$sign = sign($params, $key);
if ($sign != $params['sign']) {
$data = [
'error' => 'sign error',
];
return new JsonResponse($data, 403);
}
return $next($request);
}
} }

View File

@ -2,8 +2,26 @@
namespace PdInternalApi; namespace PdInternalApi;
use Illuminate\Http\Request;
class ServiceProvider extends \Illuminate\Support\ServiceProvider class ServiceProvider extends \Illuminate\Support\ServiceProvider
{ {
public function boot(){
Request::setTrustedProxies([
//pod network
'172.20.0.0/16',
//vpc
'10.0.0.0/16',
//local
'127.0.0.1',
//北京办公区
'172.16.0.0/16',
//aliyun slb
'100.116.0.0/16',
], Request::HEADER_X_FORWARDED_ALL);
}
/** /**
* Register any application services. * Register any application services.
* *