From ceb41a541ca5964dab4197b004a20895c1905773 Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Tue, 15 Jan 2019 23:40:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=99=A8=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Handlers/DemoServerHandler.cs | 41 +++++++++++++++++++ .../Interceptors/DemoInterceptor.cs | 41 +++++++++++++++++++ .../Program.cs | 11 +++-- src/JT808.Protocol | 2 +- 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Handlers/DemoServerHandler.cs create mode 100644 src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Handlers/DemoServerHandler.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Handlers/DemoServerHandler.cs new file mode 100644 index 0000000..a11d92e --- /dev/null +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Handlers/DemoServerHandler.cs @@ -0,0 +1,41 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Grpc.Core; + +namespace JT808.DotNetty.Dashbord.GrpcServer +{ + public class DemoServerHandler + where TRequest : class + where TResponse : class + { + private readonly ServerCallContext _context; + + public DemoServerHandler(ServerCallContext context) + { + _context = context; + } + + public async Task UnaryServerHandler(TRequest request, UnaryServerMethod continuation) + { + var response = await continuation(request, _context).ConfigureAwait(false); + return response; + } + + public async Task ClientStreamingServerHandler(IAsyncStreamReader requestStream, ClientStreamingServerMethod continuation) + { + var response = await continuation(requestStream, _context).ConfigureAwait(false); + return response; + } + + public async Task ServerStreamingServerHandler(TRequest request, IServerStreamWriter responseStream, ServerStreamingServerMethod continuation) + { + await continuation(request, responseStream, _context).ConfigureAwait(false); + } + + public async Task DuplexStreamingServerHandler(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, DuplexStreamingServerMethod continuation) + { + await continuation(requestStream, responseStream, _context).ConfigureAwait(false); + } + } +} \ No newline at end of file diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs new file mode 100644 index 0000000..8bf0c51 --- /dev/null +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs @@ -0,0 +1,41 @@ +using Grpc.Core; +using Grpc.Core.Interceptors; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace JT808.DotNetty.Dashbord.GrpcServer.Interceptors +{ + /// + /// + /// https://github.com/grpc/grpc/blob/master/doc/server_side_auth.md + /// https://github.com/Falco20019/grpc-opentracing + /// + class DemoInterceptor : Interceptor + { + public override Task UnaryServerHandler(TRequest request, ServerCallContext context, UnaryServerMethod continuation) + { + if(TryGetValue(context.RequestHeaders,"token",out var str)) + { + //context.Status = new Status(StatusCode.Unauthenticated, "Invalid token"); + return default(Task); + } + return continuation(request, context); + } + + private bool TryGetValue(Metadata metadata,string key,out string value) + { + foreach(var item in metadata) + { + if(item.Key== key) + { + value = item.Value; + return true; + } + } + value = ""; + return false; + } + } +} diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs index a571b5d..508a1c0 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs @@ -1,5 +1,7 @@ using Grpc.Core; +using Grpc.Core.Interceptors; using JT808.DotNetty.Dashbord.GrpcServer.GrpcImpls; +using JT808.DotNetty.Dashbord.GrpcServer.Interceptors; using JT808.GrpcDashbord.AtomicCounterGrpcService; using System; using System.Threading; @@ -14,7 +16,8 @@ namespace JT808.DotNetty.Dashbord.GrpcServer var server = new Server { Services = { - BindService(new JT808AtomicCounterServiceGrpcImpl()), + BindService(new JT808AtomicCounterServiceGrpcImpl()) + .Intercept(new DemoInterceptor()), }, Ports = { new ServerPort("0.0.0.0", 14000,ServerCredentials.Insecure) @@ -26,9 +29,11 @@ namespace JT808.DotNetty.Dashbord.GrpcServer Console.WriteLine(string.Format("RPC server {0} listening on port {1}", item.Host, item.Port)); } server.Start(); - AtomicCounterServiceClient client = new AtomicCounterServiceClient(new Channel("127.0.0.1:14000", ChannelCredentials.Insecure)); - var result=client.GetTcpAtomicCounter(new GrpcDashbord.ServiceGrpcBase.EmptyRequest()); + Metadata metadata = new Metadata(); + metadata.Add("token", "test"); + metadata.Add("request", "web"); + var result=client.GetTcpAtomicCounter(new GrpcDashbord.ServiceGrpcBase.EmptyRequest(), metadata); Console.ReadKey(); server.ShutdownAsync().Wait(); } diff --git a/src/JT808.Protocol b/src/JT808.Protocol index 641907a..16b8bfc 160000 --- a/src/JT808.Protocol +++ b/src/JT808.Protocol @@ -1 +1 @@ -Subproject commit 641907ad8373cf62d8973dedf76b84aaa894e52f +Subproject commit 16b8bfcd87686c809912b3ed722c5c035e1121cf