Compare commits

..

No commits in common. "main" and "v0.6.1" have entirely different histories.
main ... v0.6.1

4 changed files with 1251 additions and 93 deletions

View File

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

1194
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

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