ip limit & tool selected

This commit is contained in:
dongwei 2019-01-14 12:03:47 +08:00
parent bc9783b865
commit 5ab20df487
6 changed files with 68 additions and 8 deletions

View File

@ -139,7 +139,8 @@ class Client
*/
protected function id()
{
return $this->id.'-'.time();
// return $this->id.'-'.time();
return $this->id;
}
}

View File

@ -0,0 +1,57 @@
<?php
/**
* Created by PhpStorm.
* User: dongwei
* Date: 2019/1/14
* Time: 11:45 AM
*/
namespace JsonRpc\Middleware;
use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Str;
class Security
{
public function __construct()
{
}
/**
* @param \Illuminate\Http\Request $request
* @param Closure $next
* @return JsonResponse|mixed
*/
public function handle($request, Closure $next)
{
app('log')->info("rpc security middleware call here");
$ip = $request->getClientIp();
if ($this->isClientIPPermitted($ip) == false) {
return new JsonResponse("$ip is forbidden", 403);
}
return $next($request);
}
/**
* 内网ip判断
* @param $ip
* @return bool
*/
private function isClientIPPermitted($ip)
{
if (!app()->environment('production', 'staging')) {
return true;
}
if (Str::startsWith($ip, [
'127.0.0.',
'192.168.',
'10.0.',
])) {
return true;
}
return false;
}
}

View File

@ -4,6 +4,7 @@ namespace JsonRpc\Providers;
use App\Http\Middleware\JsonRpc;
use Illuminate\Support\ServiceProvider;
use JsonRpc\Middleware\Security;
use JsonRpc\Server\JsonRpcDoc;
use JsonRpc\Server\JsonRpcServer;
use JsonRpc\Server\JsonRpcTool;
@ -24,9 +25,10 @@ class LumenServerServiceProvider extends ServiceProvider
public function boot()
{
$this->app->routeMiddleware(['rpc.security' => Security::class]);
$this->app->router->group([
'prefix' => 'rpc'
// 'middleware' => 'rpc',
'prefix' => 'rpc',
'middleware' => 'rpc.security',
], function () {
$this->app->configure('rpc');

View File

@ -50,7 +50,7 @@ class JsonRpcServer
} else {
list($method, $params, $id) = $this->parseJson($this->request->getContent());
}
app('log')->info('rpc ser', [$method, $params, $id, $this->request->header('client_app')]);
list($class, $function) = $this->parseMethodWithMap($method);
// dump($class,$function);exit;

View File

@ -35,10 +35,10 @@ class JsonRpcTool
$view = view();
$params = json_decode($request->input('params'), true);
$method = $request->input('method');
if ($request->method() == Request::METHOD_POST) {
$method = $request->input('method');
try {
$result = app('rpc.'.$this->config['name'])->call($method, $params);
@ -47,7 +47,7 @@ class JsonRpcTool
$view->share('error', ['code' => $exception->getCode(), 'message' => $exception->getMessage()]);
}
}
$view->share('method',$method);
$view->share('endpoint', $this->getEndpoint());
$view->share('methods', $this->getMethods());
$view->share('params', json_encode($params));

View File

@ -60,7 +60,7 @@
<label for="inputAddress">Method</label>
<select class="form-control" id="method" name="method">
@foreach($methods as $k => $v)
<option>{{$k}}</option>
<option @if($method == $k) selected @endif>{{$k}}</option>
@endforeach
</select>
</div>