Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
1aedddc66e | |||
![]() |
445f9dd037 | ||
![]() |
436a084581 | ||
![]() |
6c54737d13 |
|
@ -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
1194
composer.lock
generated
File diff suppressed because it is too large
Load Diff
|
@ -5,25 +5,15 @@ namespace PdInternalApi;
|
||||||
class Client
|
class Client
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $currentApp;
|
protected $service_name;
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
public function __construct($config)
|
public function __construct($service_name, $config)
|
||||||
{
|
{
|
||||||
|
$this->service_name = $service_name;
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $app
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function app($app)
|
|
||||||
{
|
|
||||||
if (isset($this->config[$app]))
|
|
||||||
$this->currentApp = $app;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调用api,如果状态码不为200则抛出异常
|
* 调用api,如果状态码不为200则抛出异常
|
||||||
* @param $uri
|
* @param $uri
|
||||||
|
@ -33,8 +23,7 @@ class Client
|
||||||
*/
|
*/
|
||||||
public function call($uri, $params)
|
public function call($uri, $params)
|
||||||
{
|
{
|
||||||
$config = array_merge(['timeout' => 3],
|
$config = array_merge(['timeout' => 3], $this->config);
|
||||||
$this->config[$this->currentApp]);
|
|
||||||
$secret = $config['secret'];
|
$secret = $config['secret'];
|
||||||
unset($config['secret']);
|
unset($config['secret']);
|
||||||
$client = new \GuzzleHttp\Client($config);
|
$client = new \GuzzleHttp\Client($config);
|
||||||
|
@ -42,10 +31,10 @@ class Client
|
||||||
$params['timestamp'] = time();
|
$params['timestamp'] = time();
|
||||||
$params['sign'] = sign($params, $secret);
|
$params['sign'] = sign($params, $secret);
|
||||||
$resp = $client->post($uri, ['form_params' => $params]);
|
$resp = $client->post($uri, ['form_params' => $params]);
|
||||||
if ($resp->getStatusCode() == 200) {
|
if ($resp->getStatusCode() != 200) {
|
||||||
return \GuzzleHttp\json_decode($resp->getBody(), true);
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
return \GuzzleHttp\json_decode($resp->getBody(), true);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -15,6 +15,27 @@ class InternalApi
|
||||||
app()->configure('internal_api');
|
app()->configure('internal_api');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function isClientIPPermitted($ip)
|
||||||
|
{
|
||||||
|
if (!app()->environment('production', 'staging')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (Str::startsWith($ip, [
|
||||||
|
'127.0.0.1',
|
||||||
|
//局域网
|
||||||
|
'192.168.',
|
||||||
|
//vpc
|
||||||
|
'10.0.',
|
||||||
|
//pod network
|
||||||
|
'172.20.',
|
||||||
|
//北京办公区
|
||||||
|
'172.16.'
|
||||||
|
])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle an incoming request.
|
* Handle an incoming request.
|
||||||
*
|
*
|
||||||
|
@ -25,11 +46,8 @@ class InternalApi
|
||||||
public function handle($request, Closure $next)
|
public function handle($request, Closure $next)
|
||||||
{
|
{
|
||||||
$ip = $request->getClientIp();
|
$ip = $request->getClientIp();
|
||||||
|
if (!$this->isClientIPPermitted($ip)) {
|
||||||
if (!Str::startsWith($ip, [
|
return new JsonResponse("$ip is forbidden", 403);
|
||||||
'127.0.0.', '192.168.', '10.0.'
|
|
||||||
])) {
|
|
||||||
return new JsonResponse('', 404);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$params = $request->all();
|
$params = $request->all();
|
||||||
|
@ -42,7 +60,7 @@ class InternalApi
|
||||||
if (empty($params['timestamp'])) {
|
if (empty($params['timestamp'])) {
|
||||||
$data = ['error' => 'require time',];
|
$data = ['error' => 'require time',];
|
||||||
return new JsonResponse($data, 403);
|
return new JsonResponse($data, 403);
|
||||||
} elseif (intval($params['timestamp']) + 60 < time()) {
|
} else if (intval($params['timestamp']) + 60 < time()) {
|
||||||
$data = ['error' => 'sign expired',];
|
$data = ['error' => 'sign expired',];
|
||||||
return new JsonResponse($data, 403);
|
return new JsonResponse($data, 403);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
||||||
*
|
*
|
||||||
|
@ -12,12 +30,9 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->app->configure('internal_api');
|
$this->app->configure('internal_api');
|
||||||
$this->app->singleton('internal.api', function () {
|
foreach (config('internal_api.client') as $service_name => $config) {
|
||||||
return new Client(config('internal_api.client'));
|
$this->app->singleton('internal.api.' . $service_name, function () use ($service_name, $config) {
|
||||||
});
|
return new Client($service_name, $config);
|
||||||
foreach (config('internal_api.client') as $key => $config) {
|
|
||||||
$this->app->singleton('internal.api.' . $key, function () use ($key) {
|
|
||||||
return $this->app['internal.api']->app($key);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user