Compare commits

..

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

5 changed files with 1277 additions and 105 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

@ -5,15 +5,25 @@ namespace PdInternalApi;
class Client class Client
{ {
protected $service_name; protected $currentApp;
protected $config; protected $config;
public function __construct($service_name, $config) public function __construct($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
@ -23,7 +33,8 @@ class Client
*/ */
public function call($uri, $params) public function call($uri, $params)
{ {
$config = array_merge(['timeout' => 3], $this->config); $config = array_merge(['timeout' => 3],
$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);
@ -31,10 +42,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 false; return \GuzzleHttp\json_decode($resp->getBody(), true);
} }
return \GuzzleHttp\json_decode($resp->getBody(), true); return false;
} }
} }

View File

@ -10,77 +10,59 @@ 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
'172.20.',
//北京办公区
'172.16.'
])) {
return true;
}
return false;
}
/** if (!Str::startsWith($ip, [
* Handle an incoming request. '127.0.0.', '192.168.', '10.0.'
* ])) {
* @param \Illuminate\Http\Request $request return new JsonResponse('', 404);
* @param \Closure $next }
* @return mixed
*/
public function handle($request, Closure $next)
{
$ip = $request->getClientIp();
if (!$this->isClientIPPermitted($ip)) {
return new JsonResponse("$ip is forbidden", 403);
}
$params = $request->all(); $params = $request->all();
if (empty($params['appid'])) { if (empty($params['appid'])) {
$data = ['error' => 'require appid',]; $data = ['error' => 'require appid',];
return new JsonResponse($data, 403); return new JsonResponse($data, 403);
} }
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);
} else if (intval($params['timestamp']) + 60 < time()) { } elseif (intval($params['timestamp']) + 60 < time()) {
$data = ['error' => 'sign expired',]; $data = ['error' => 'sign expired',];
return new JsonResponse($data, 403); return new JsonResponse($data, 403);
} }
$key = config('internal_api.server.' . $params['appid']); $key = config('internal_api.server.' . $params['appid']);
if (empty($key)) { if (empty($key)) {
$data = ['error' => 'config error',]; $data = ['error' => 'config error',];
return new JsonResponse($data, 403); return new JsonResponse($data, 403);
} }
$sign = sign($params, $key); $sign = sign($params, $key);
if ($sign != $params['sign']) { if ($sign != $params['sign']) {
$data = [ $data = [
'error' => 'sign error', 'error' => 'sign error',
]; ];
return new JsonResponse($data, 403); return new JsonResponse($data, 403);
} }
return $next($request); 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.
* *
@ -30,9 +12,12 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
public function register() public function register()
{ {
$this->app->configure('internal_api'); $this->app->configure('internal_api');
foreach (config('internal_api.client') as $service_name => $config) { $this->app->singleton('internal.api', function () {
$this->app->singleton('internal.api.' . $service_name, function () use ($service_name, $config) { return new Client(config('internal_api.client'));
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);
}); });
} }