diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ErrorExtensions.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ErrorExtensions.cs new file mode 100644 index 0000000..69d94e5 --- /dev/null +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ErrorExtensions.cs @@ -0,0 +1,15 @@ +using Grpc.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace JT808.DotNetty.Dashbord.GrpcProtocol.Extensions +{ + public static class ErrorExtensions + { + public static void Unauthenticated(string msg= "Invalid Token") + { + throw new Grpc.Core.RpcException(new Status(StatusCode.Unauthenticated, msg)); + } + } +} diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ServerCallContextExtensions.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ServerCallContextExtensions.cs new file mode 100644 index 0000000..27ad2bf --- /dev/null +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ServerCallContextExtensions.cs @@ -0,0 +1,30 @@ +using Grpc.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace JT808.DotNetty.Dashbord.GrpcProtocol.Extensions +{ + public static class ServerCallContextExtensions + { + public static void SetResultStatus(this ServerCallContext serverCallContext, StatusCode statusCode, string detail) + { + serverCallContext.Status = new Status(statusCode, detail); + } + + public static void Ok(this ServerCallContext serverCallContext,string detail="") + { + serverCallContext.Status = new Status(StatusCode.OK, detail); + } + + public static void Auth(this ServerCallContext serverCallContext, string detail = "") + { + serverCallContext.Status = new Status(StatusCode.Unauthenticated, detail); + } + + public static void InternalError(this ServerCallContext serverCallContext, string detail = "") + { + serverCallContext.Status = new Status(StatusCode.Internal, detail); + } + } +} diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/JT808AtomicCounterService.proto b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/JT808AtomicCounterService.proto index d093018..f840985 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/JT808AtomicCounterService.proto +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/JT808AtomicCounterService.proto @@ -2,7 +2,6 @@ package JT808.GrpcDashbord.AtomicCounterGrpcService; -import "ResultReply.proto"; import "EmptyRequest.proto"; service AtomicCounterService{ @@ -11,11 +10,6 @@ service AtomicCounterService{ } message AtomicCounterReply{ - AtomicCounterInfo AtomicCounterInfo = 1; - JT808.GrpcDashbord.ServiceGrpcBase.ResultReply ResultReply = 2; -} - -message AtomicCounterInfo{ int32 MsgSuccessCount = 1; int32 MsgFailCount = 2; } \ No newline at end of file diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/csharp/JT808AtomicCounterService.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/csharp/JT808AtomicCounterService.cs index b0f3cd2..84216f3 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/csharp/JT808AtomicCounterService.cs +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/csharp/JT808AtomicCounterService.cs @@ -25,26 +25,20 @@ namespace JT808.GrpcDashbord.AtomicCounterGrpcService { byte[] descriptorData = global::System.Convert.FromBase64String( string.Concat( "Ch9KVDgwOEF0b21pY0NvdW50ZXJTZXJ2aWNlLnByb3RvEitKVDgwOC5HcnBj", - "RGFzaGJvcmQuQXRvbWljQ291bnRlckdycGNTZXJ2aWNlGhFSZXN1bHRSZXBs", - "eS5wcm90bxoSRW1wdHlSZXF1ZXN0LnByb3RvIrUBChJBdG9taWNDb3VudGVy", - "UmVwbHkSWQoRQXRvbWljQ291bnRlckluZm8YASABKAsyPi5KVDgwOC5HcnBj", - "RGFzaG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ZXJ2aWNlLkF0", + "b21pY0NvdW50ZXJSZXBseSIAEooBChNHZXRVZHBBdG9taWNDb3VudGVyEjAu", + "SlQ4MDguR3JwY0Rhc2hib3JkLlNlcnZpY2VHcnBjQmFzZS5FbXB0eVJlcXVl", + "c3QaPy5KVDgwOC5HcnBjRGFzaGJvcmQuQXRvbWljQ291bnRlckdycGNTZXJ2", + "aWNlLkF0b21pY0NvdW50ZXJSZXBseSIAYgZwcm90bzM=")); descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, - new pbr::FileDescriptor[] { global::JT808.GrpcDashbord.ServiceGrpcBase.ResultReplyReflection.Descriptor, global::JT808.GrpcDashbord.ServiceGrpcBase.EmptyRequestReflection.Descriptor, }, + new pbr::FileDescriptor[] { global::JT808.GrpcDashbord.ServiceGrpcBase.EmptyRequestReflection.Descriptor, }, new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { - new pbr::GeneratedClrTypeInfo(typeof(global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterReply), global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterReply.Parser, new[]{ "AtomicCounterInfo", "ResultReply" }, null, null, null), - new pbr::GeneratedClrTypeInfo(typeof(global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo), global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo.Parser, new[]{ "MsgSuccessCount", "MsgFailCount" }, null, null, null) + new pbr::GeneratedClrTypeInfo(typeof(global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterReply), global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterReply.Parser, new[]{ "MsgSuccessCount", "MsgFailCount" }, null, null, null) })); } #endregion @@ -76,183 +70,14 @@ namespace JT808.GrpcDashbord.AtomicCounterGrpcService { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public AtomicCounterReply(AtomicCounterReply other) : this() { - atomicCounterInfo_ = other.atomicCounterInfo_ != null ? other.atomicCounterInfo_.Clone() : null; - resultReply_ = other.resultReply_ != null ? other.resultReply_.Clone() : null; - _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public AtomicCounterReply Clone() { - return new AtomicCounterReply(this); - } - - /// Field number for the "AtomicCounterInfo" field. - public const int AtomicCounterInfoFieldNumber = 1; - private global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo atomicCounterInfo_; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo AtomicCounterInfo { - get { return atomicCounterInfo_; } - set { - atomicCounterInfo_ = value; - } - } - - /// Field number for the "ResultReply" field. - public const int ResultReplyFieldNumber = 2; - private global::JT808.GrpcDashbord.ServiceGrpcBase.ResultReply resultReply_; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public global::JT808.GrpcDashbord.ServiceGrpcBase.ResultReply ResultReply { - get { return resultReply_; } - set { - resultReply_ = value; - } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override bool Equals(object other) { - return Equals(other as AtomicCounterReply); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public bool Equals(AtomicCounterReply other) { - if (ReferenceEquals(other, null)) { - return false; - } - if (ReferenceEquals(other, this)) { - return true; - } - if (!object.Equals(AtomicCounterInfo, other.AtomicCounterInfo)) return false; - if (!object.Equals(ResultReply, other.ResultReply)) return false; - return Equals(_unknownFields, other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override int GetHashCode() { - int hash = 1; - if (atomicCounterInfo_ != null) hash ^= AtomicCounterInfo.GetHashCode(); - if (resultReply_ != null) hash ^= ResultReply.GetHashCode(); - if (_unknownFields != null) { - hash ^= _unknownFields.GetHashCode(); - } - return hash; - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public override string ToString() { - return pb::JsonFormatter.ToDiagnosticString(this); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void WriteTo(pb::CodedOutputStream output) { - if (atomicCounterInfo_ != null) { - output.WriteRawTag(10); - output.WriteMessage(AtomicCounterInfo); - } - if (resultReply_ != null) { - output.WriteRawTag(18); - output.WriteMessage(ResultReply); - } - if (_unknownFields != null) { - _unknownFields.WriteTo(output); - } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public int CalculateSize() { - int size = 0; - if (atomicCounterInfo_ != null) { - size += 1 + pb::CodedOutputStream.ComputeMessageSize(AtomicCounterInfo); - } - if (resultReply_ != null) { - size += 1 + pb::CodedOutputStream.ComputeMessageSize(ResultReply); - } - if (_unknownFields != null) { - size += _unknownFields.CalculateSize(); - } - return size; - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(AtomicCounterReply other) { - if (other == null) { - return; - } - if (other.atomicCounterInfo_ != null) { - if (atomicCounterInfo_ == null) { - atomicCounterInfo_ = new global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo(); - } - AtomicCounterInfo.MergeFrom(other.AtomicCounterInfo); - } - if (other.resultReply_ != null) { - if (resultReply_ == null) { - resultReply_ = new global::JT808.GrpcDashbord.ServiceGrpcBase.ResultReply(); - } - ResultReply.MergeFrom(other.ResultReply); - } - _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(pb::CodedInputStream input) { - uint tag; - while ((tag = input.ReadTag()) != 0) { - switch(tag) { - default: - _unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input); - break; - case 10: { - if (atomicCounterInfo_ == null) { - atomicCounterInfo_ = new global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo(); - } - input.ReadMessage(atomicCounterInfo_); - break; - } - case 18: { - if (resultReply_ == null) { - resultReply_ = new global::JT808.GrpcDashbord.ServiceGrpcBase.ResultReply(); - } - input.ReadMessage(resultReply_); - break; - } - } - } - } - - } - - public sealed partial class AtomicCounterInfo : pb::IMessage { - private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new AtomicCounterInfo()); - private pb::UnknownFieldSet _unknownFields; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pb::MessageParser Parser { get { return _parser; } } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pbr::MessageDescriptor Descriptor { - get { return global::JT808.GrpcDashbord.AtomicCounterGrpcService.JT808AtomicCounterServiceReflection.Descriptor.MessageTypes[1]; } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - pbr::MessageDescriptor pb::IMessage.Descriptor { - get { return Descriptor; } - } - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public AtomicCounterInfo() { - OnConstruction(); - } - - partial void OnConstruction(); - - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public AtomicCounterInfo(AtomicCounterInfo other) : this() { msgSuccessCount_ = other.msgSuccessCount_; msgFailCount_ = other.msgFailCount_; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public AtomicCounterInfo Clone() { - return new AtomicCounterInfo(this); + public AtomicCounterReply Clone() { + return new AtomicCounterReply(this); } /// Field number for the "MsgSuccessCount" field. @@ -279,11 +104,11 @@ namespace JT808.GrpcDashbord.AtomicCounterGrpcService { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { - return Equals(other as AtomicCounterInfo); + return Equals(other as AtomicCounterReply); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public bool Equals(AtomicCounterInfo other) { + public bool Equals(AtomicCounterReply other) { if (ReferenceEquals(other, null)) { return false; } @@ -342,7 +167,7 @@ namespace JT808.GrpcDashbord.AtomicCounterGrpcService { } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(AtomicCounterInfo other) { + public void MergeFrom(AtomicCounterReply other) { if (other == null) { return; } diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/GrpcImpls/JT808AtomicCounterServiceGrpcImpl.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/GrpcImpls/JT808AtomicCounterServiceGrpcImpl.cs index 4e42c6e..e4ed823 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/GrpcImpls/JT808AtomicCounterServiceGrpcImpl.cs +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/GrpcImpls/JT808AtomicCounterServiceGrpcImpl.cs @@ -14,30 +14,16 @@ namespace JT808.DotNetty.Dashbord.GrpcServer.GrpcImpls public override Task GetTcpAtomicCounter(EmptyRequest request, ServerCallContext context) { AtomicCounterReply atomicCounterReply = new AtomicCounterReply(); - atomicCounterReply.AtomicCounterInfo = new AtomicCounterInfo - { - MsgFailCount = 10, - MsgSuccessCount = 11111 - }; - atomicCounterReply.ResultReply = new ResultReply - { - Code = ResultReply.Types.StatusCode.Success, - }; + atomicCounterReply.MsgFailCount = 10; + atomicCounterReply.MsgSuccessCount = 1111; return Task.FromResult(atomicCounterReply); } public override Task GetUdpAtomicCounter(EmptyRequest request, ServerCallContext context) { AtomicCounterReply atomicCounterReply = new AtomicCounterReply(); - atomicCounterReply.AtomicCounterInfo = new AtomicCounterInfo - { - MsgFailCount = 50, - MsgSuccessCount = 10000 - }; - atomicCounterReply.ResultReply = new ResultReply - { - Code = ResultReply.Types.StatusCode.Success, - }; + atomicCounterReply.MsgFailCount = 50; + atomicCounterReply.MsgSuccessCount = 1111; return Task.FromResult(atomicCounterReply); } } 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 index 8bf0c51..dd7d3d9 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs @@ -1,5 +1,7 @@ using Grpc.Core; using Grpc.Core.Interceptors; +using JT808.DotNetty.Dashbord.GrpcProtocol.Extensions; +using JT808.GrpcDashbord.ServiceGrpcBase; using System; using System.Collections.Generic; using System.Text; @@ -11,17 +13,21 @@ namespace JT808.DotNetty.Dashbord.GrpcServer.Interceptors /// /// https://github.com/grpc/grpc/blob/master/doc/server_side_auth.md /// https://github.com/Falco20019/grpc-opentracing + /// https://github.com/grpc/proposal/blob/master/L12-csharp-interceptors.md + /// https://stackoverflow.com/questions/52950210/populate-authcontext-in-grpc-c-sharp-from-jwt-authentication + /// https://github.com/grpc/grpc/tree/master/doc /// class DemoInterceptor : Interceptor { - public override Task UnaryServerHandler(TRequest request, ServerCallContext context, UnaryServerMethod continuation) + 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); + //ErrorExtensions.Unauthenticated(); } return continuation(request, context); + //return Task.FromResult(default(TResponse)); + //return await continuation(request, context); } private bool TryGetValue(Metadata metadata,string key,out string value) 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 508a1c0..f5cc394 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs @@ -33,7 +33,15 @@ namespace JT808.DotNetty.Dashbord.GrpcServer Metadata metadata = new Metadata(); metadata.Add("token", "test"); metadata.Add("request", "web"); - var result=client.GetTcpAtomicCounter(new GrpcDashbord.ServiceGrpcBase.EmptyRequest(), metadata); + + try + { + var result = client.GetTcpAtomicCounter(new GrpcDashbord.ServiceGrpcBase.EmptyRequest(), metadata); + } + catch (RpcException ex) + { + + } Console.ReadKey(); server.ShutdownAsync().Wait(); } diff --git a/src/JT808.DotNetty.Dashbord.sln b/src/JT808.DotNetty.Dashbord.sln index 191353c..0dacb2d 100644 --- a/src/JT808.DotNetty.Dashbord.sln +++ b/src/JT808.DotNetty.Dashbord.sln @@ -13,7 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Dashbord.Grp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Dashbord", "JT808.DotNetty.Admin\JT808.DotNetty.Dashbord\JT808.DotNetty.Dashbord.csproj", "{80D1505D-44E1-4128-8900-B5C0AE7C69E5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT808.DotNetty.Dashbord.GrpcServer", "JT808.DotNetty.Admin\JT808.DotNetty.Dashbord.GrpcServer\JT808.DotNetty.Dashbord.GrpcServer.csproj", "{9F143F03-D90D-477F-9CE4-0BD1E2A1E379}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Dashbord.GrpcServer", "JT808.DotNetty.Admin\JT808.DotNetty.Dashbord.GrpcServer\JT808.DotNetty.Dashbord.GrpcServer.csproj", "{9F143F03-D90D-477F-9CE4-0BD1E2A1E379}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution