ip limit & tool selected
This commit is contained in:
parent
bc9783b865
commit
5ab20df487
|
@ -139,7 +139,8 @@ class Client
|
||||||
*/
|
*/
|
||||||
protected function id()
|
protected function id()
|
||||||
{
|
{
|
||||||
return $this->id.'-'.time();
|
// return $this->id.'-'.time();
|
||||||
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
57
src/Middleware/Security.php
Normal file
57
src/Middleware/Security.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ namespace JsonRpc\Providers;
|
||||||
|
|
||||||
use App\Http\Middleware\JsonRpc;
|
use App\Http\Middleware\JsonRpc;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use JsonRpc\Middleware\Security;
|
||||||
use JsonRpc\Server\JsonRpcDoc;
|
use JsonRpc\Server\JsonRpcDoc;
|
||||||
use JsonRpc\Server\JsonRpcServer;
|
use JsonRpc\Server\JsonRpcServer;
|
||||||
use JsonRpc\Server\JsonRpcTool;
|
use JsonRpc\Server\JsonRpcTool;
|
||||||
|
@ -24,9 +25,10 @@ class LumenServerServiceProvider extends ServiceProvider
|
||||||
|
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
|
$this->app->routeMiddleware(['rpc.security' => Security::class]);
|
||||||
$this->app->router->group([
|
$this->app->router->group([
|
||||||
'prefix' => 'rpc'
|
'prefix' => 'rpc',
|
||||||
// 'middleware' => 'rpc',
|
'middleware' => 'rpc.security',
|
||||||
], function () {
|
], function () {
|
||||||
|
|
||||||
$this->app->configure('rpc');
|
$this->app->configure('rpc');
|
||||||
|
|
|
@ -50,7 +50,7 @@ class JsonRpcServer
|
||||||
} else {
|
} else {
|
||||||
list($method, $params, $id) = $this->parseJson($this->request->getContent());
|
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);
|
list($class, $function) = $this->parseMethodWithMap($method);
|
||||||
// dump($class,$function);exit;
|
// dump($class,$function);exit;
|
||||||
|
|
||||||
|
|
|
@ -35,10 +35,10 @@ class JsonRpcTool
|
||||||
$view = view();
|
$view = view();
|
||||||
|
|
||||||
$params = json_decode($request->input('params'), true);
|
$params = json_decode($request->input('params'), true);
|
||||||
|
$method = $request->input('method');
|
||||||
if ($request->method() == Request::METHOD_POST) {
|
if ($request->method() == Request::METHOD_POST) {
|
||||||
|
|
||||||
$method = $request->input('method');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$result = app('rpc.'.$this->config['name'])->call($method, $params);
|
$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('error', ['code' => $exception->getCode(), 'message' => $exception->getMessage()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$view->share('method',$method);
|
||||||
$view->share('endpoint', $this->getEndpoint());
|
$view->share('endpoint', $this->getEndpoint());
|
||||||
$view->share('methods', $this->getMethods());
|
$view->share('methods', $this->getMethods());
|
||||||
$view->share('params', json_encode($params));
|
$view->share('params', json_encode($params));
|
||||||
|
|
|
@ -60,7 +60,7 @@
|
||||||
<label for="inputAddress">Method</label>
|
<label for="inputAddress">Method</label>
|
||||||
<select class="form-control" id="method" name="method">
|
<select class="form-control" id="method" name="method">
|
||||||
@foreach($methods as $k => $v)
|
@foreach($methods as $k => $v)
|
||||||
<option>{{$k}}</option>
|
<option @if($method == $k) selected @endif>{{$k}}</option>
|
||||||
@endforeach
|
@endforeach
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user