Compare commits

...

4 Commits
v0.6 ... 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
George Xie
6c54737d13 将每个服务的 client 改为单例,修正重入问题 2018-06-22 20:21:08 +08:00
5 changed files with 105 additions and 1277 deletions

View File

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

1194
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -5,25 +5,15 @@ namespace PdInternalApi;
class Client
{
protected $currentApp;
protected $service_name;
protected $config;
public function __construct($config)
public function __construct($service_name, $config)
{
$this->service_name = $service_name;
$this->config = $config;
}
/**
* @param $app
* @return $this
*/
public function app($app)
{
if (isset($this->config[$app]))
$this->currentApp = $app;
return $this;
}
/**
* 调用api如果状态码不为200则抛出异常
* @param $uri
@ -33,8 +23,7 @@ class Client
*/
public function call($uri, $params)
{
$config = array_merge(['timeout' => 3],
$this->config[$this->currentApp]);
$config = array_merge(['timeout' => 3], $this->config);
$secret = $config['secret'];
unset($config['secret']);
$client = new \GuzzleHttp\Client($config);
@ -42,10 +31,10 @@ class Client
$params['timestamp'] = time();
$params['sign'] = sign($params, $secret);
$resp = $client->post($uri, ['form_params' => $params]);
if ($resp->getStatusCode() == 200) {
return \GuzzleHttp\json_decode($resp->getBody(), true);
if ($resp->getStatusCode() != 200) {
return false;
}
return false;
return \GuzzleHttp\json_decode($resp->getBody(), true);
}
}

View File

@ -10,59 +10,77 @@ use function PdInternalApi\sign;
class InternalApi
{
public function __construct()
{
app()->configure('internal_api');
}
public function __construct()
{
app()->configure('internal_api');
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$ip = $request->getClientIp();
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;
}
if (!Str::startsWith($ip, [
'127.0.0.', '192.168.', '10.0.'
])) {
return new JsonResponse('', 404);
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @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'])) {
$data = ['error' => 'require appid',];
return new JsonResponse($data, 403);
}
if (empty($params['appid'])) {
$data = ['error' => 'require appid',];
return new JsonResponse($data, 403);
}
if (empty($params['timestamp'])) {
$data = ['error' => 'require time',];
return new JsonResponse($data, 403);
} elseif (intval($params['timestamp']) + 60 < time()) {
$data = ['error' => 'sign expired',];
return new JsonResponse($data, 403);
}
if (empty($params['timestamp'])) {
$data = ['error' => 'require time',];
return new JsonResponse($data, 403);
} else if (intval($params['timestamp']) + 60 < time()) {
$data = ['error' => 'sign expired',];
return new JsonResponse($data, 403);
}
$key = config('internal_api.server.' . $params['appid']);
$key = config('internal_api.server.' . $params['appid']);
if (empty($key)) {
$data = ['error' => 'config error',];
return new JsonResponse($data, 403);
}
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);
}
$sign = sign($params, $key);
if ($sign != $params['sign']) {
$data = [
'error' => 'sign error',
];
return new JsonResponse($data, 403);
}
return $next($request);
}
return $next($request);
}
}

View File

@ -2,8 +2,26 @@
namespace PdInternalApi;
use Illuminate\Http\Request;
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.
*
@ -12,12 +30,9 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
public function register()
{
$this->app->configure('internal_api');
$this->app->singleton('internal.api', function () {
return new Client(config('internal_api.client'));
});
foreach (config('internal_api.client') as $key => $config) {
$this->app->singleton('internal.api.' . $key, function () use ($key) {
return $this->app['internal.api']->app($key);
foreach (config('internal_api.client') as $service_name => $config) {
$this->app->singleton('internal.api.' . $service_name, function () use ($service_name, $config) {
return new Client($service_name, $config);
});
}