From e391cac286a4c6a2f0666fa8623b44a5fc9a103c Mon Sep 17 00:00:00 2001 From: smallchi <564952747@qq.com> Date: Tue, 29 Oct 2019 18:34:47 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9webapi=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E8=B0=83=E7=94=A8=E5=B7=A5=E5=85=B7=E4=B8=BA=E5=BE=AE?= =?UTF-8?q?=E8=BD=AF=E8=87=AA=E5=B8=A6=E7=9A=84httpclient=202.=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0WebApi=E7=9A=84Token=E8=AE=A4=E8=AF=81=E5=8F=8A?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E6=B5=8B=E8=AF=95=203.=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E5=BA=93=E4=B8=BAcore3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dtos/JT808ResultDto.cs | 1 + .../JT808.DotNetty.Abstractions.csproj | 2 +- .../JT808.DotNetty.CleintBenchmark.csproj | 2 +- .../JT808.DotNetty.Client.csproj | 2 +- .../Configurations/JT808Configuration.cs | 5 + .../Handlers/JT808MsgIdHttpHandlerBase.cs | 11 ++ .../Interfaces/IJT808WebApiAuthorization.cs | 13 ++ .../Interfaces/IJT808WebApiNettyBuilder.cs | 1 + .../JT808.DotNetty.Core.csproj | 1 + .../JT808CoreDotnettyExtensions.cs | 2 +- .../JT808.DotNetty.Transmit.Test.csproj | 2 +- .../JT808.DotNetty.MsgIdHandler.csproj | 2 +- .../JT808.DotNetty.MsgLogging.csproj | 6 +- .../JT808.DotNetty.ReplyMessage.csproj | 6 +- .../JT808.DotNetty.SessionNotice.csproj | 6 +- .../JT808.DotNetty.Traffic.csproj | 8 +- .../JT808.DotNetty.Transmit.csproj | 8 +- .../JT808.DotNetty.Core.Test.csproj | 14 +- .../JT808.DotNetty.Hosting.csproj | 15 +- .../JT808.DotNetty.Hosting/Program.cs | 27 ++- .../JT808.DotNetty.Tcp.Test.csproj | 12 +- .../JT808.DotNetty.Udp.Test.csproj | 12 +- .../JT808AuthorizationDefaultTest.cs | 62 +++++++ .../JT808.DotNetty.WebApi.Test.csproj | 19 +-- .../JT808DotNettyWebApiTest.cs | 81 --------- .../JT808HttpClientTest.cs | 159 ++++++++++++++++++ .../JT808.DotNetty.WebApi.Test/TestBase.cs | 140 --------------- .../appsettings.json | 27 --- .../JT808AuthorizationDefault.cs | 51 ++++++ .../Handlers/JT808WebAPIServerHandler.cs | 11 +- .../JT808.DotNetty.WebApi.csproj | 1 + .../JT808WebApiBuilderDefault.cs | 6 + .../JT808WebApiDotnettyExtensions.cs | 2 + .../IJT808DotNettyWebApi.cs | 64 ------- .../JT808.DotNetty.WebApiClientTool.csproj | 5 +- .../JT808DotNettyWebApiOptions.cs | 15 -- .../JT808HttpClient.cs | 97 +++++++++++ src/JT808.DotNetty.sln | 22 +-- 38 files changed, 502 insertions(+), 418 deletions(-) create mode 100644 src/JT808.DotNetty.Core/Interfaces/IJT808WebApiAuthorization.cs create mode 100644 src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/Authorization/JT808AuthorizationDefaultTest.cs delete mode 100644 src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808DotNettyWebApiTest.cs create mode 100644 src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808HttpClientTest.cs delete mode 100644 src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/TestBase.cs delete mode 100644 src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/appsettings.json create mode 100644 src/JT808.DotNetty.WebApi/Authorization/JT808AuthorizationDefault.cs delete mode 100644 src/JT808.DotNetty.WebApiClientTool/IJT808DotNettyWebApi.cs delete mode 100644 src/JT808.DotNetty.WebApiClientTool/JT808DotNettyWebApiOptions.cs create mode 100644 src/JT808.DotNetty.WebApiClientTool/JT808HttpClient.cs diff --git a/src/JT808.DotNetty.Abstractions/Dtos/JT808ResultDto.cs b/src/JT808.DotNetty.Abstractions/Dtos/JT808ResultDto.cs index c6e8970..6376adb 100644 --- a/src/JT808.DotNetty.Abstractions/Dtos/JT808ResultDto.cs +++ b/src/JT808.DotNetty.Abstractions/Dtos/JT808ResultDto.cs @@ -22,6 +22,7 @@ namespace JT808.DotNetty.Abstractions.Dtos { public const int Ok = 200; public const int Empty = 201; + public const int AuthFail = 401; public const int NotFound = 404; public const int Fail = 400; public const int Error = 500; diff --git a/src/JT808.DotNetty.Abstractions/JT808.DotNetty.Abstractions.csproj b/src/JT808.DotNetty.Abstractions/JT808.DotNetty.Abstractions.csproj index e1990f1..e345a04 100644 --- a/src/JT808.DotNetty.Abstractions/JT808.DotNetty.Abstractions.csproj +++ b/src/JT808.DotNetty.Abstractions/JT808.DotNetty.Abstractions.csproj @@ -20,7 +20,7 @@ 基于DotNetty实现的JT808DotNetty的抽象库 - + diff --git a/src/JT808.DotNetty.CleintBenchmark/JT808.DotNetty.CleintBenchmark.csproj b/src/JT808.DotNetty.CleintBenchmark/JT808.DotNetty.CleintBenchmark.csproj index 5d63a63..8b5a8a0 100644 --- a/src/JT808.DotNetty.CleintBenchmark/JT808.DotNetty.CleintBenchmark.csproj +++ b/src/JT808.DotNetty.CleintBenchmark/JT808.DotNetty.CleintBenchmark.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/JT808.DotNetty.Client/JT808.DotNetty.Client.csproj b/src/JT808.DotNetty.Client/JT808.DotNetty.Client.csproj index 2cb8832..6d68cc9 100644 --- a/src/JT808.DotNetty.Client/JT808.DotNetty.Client.csproj +++ b/src/JT808.DotNetty.Client/JT808.DotNetty.Client.csproj @@ -24,7 +24,7 @@ - + diff --git a/src/JT808.DotNetty.Core/Configurations/JT808Configuration.cs b/src/JT808.DotNetty.Core/Configurations/JT808Configuration.cs index 8375363..636952f 100644 --- a/src/JT808.DotNetty.Core/Configurations/JT808Configuration.cs +++ b/src/JT808.DotNetty.Core/Configurations/JT808Configuration.cs @@ -34,6 +34,11 @@ namespace JT808.DotNetty.Core.Configurations /// public int WebApiPort { get; set; } = 828; + /// + /// WebApi 默认token 123456 + /// + public string WebApiToken { get; set; } = "123456"; + /// /// 转发远程地址 (可选项)知道转发的地址有利于提升性能 /// 按照808的消息,有些请求必须要应答,但是转发可以不需要有应答可以节省部分资源包括: diff --git a/src/JT808.DotNetty.Core/Handlers/JT808MsgIdHttpHandlerBase.cs b/src/JT808.DotNetty.Core/Handlers/JT808MsgIdHttpHandlerBase.cs index fc7ef74..34be164 100644 --- a/src/JT808.DotNetty.Core/Handlers/JT808MsgIdHttpHandlerBase.cs +++ b/src/JT808.DotNetty.Core/Handlers/JT808MsgIdHttpHandlerBase.cs @@ -77,6 +77,17 @@ namespace JT808.DotNetty.Core.Handlers return new JT808HttpResponse(json); } + public JT808HttpResponse AuthFailHttpResponse() + { + byte[] json = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new JT808ResultDto() + { + Code = JT808ResultCode.AuthFail, + Message = "token认证失败", + Data = "token认证失败" + })); + return new JT808HttpResponse(json); + } + public JT808HttpResponse ErrorHttpResponse(Exception ex) { byte[] json = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new JT808ResultDto() diff --git a/src/JT808.DotNetty.Core/Interfaces/IJT808WebApiAuthorization.cs b/src/JT808.DotNetty.Core/Interfaces/IJT808WebApiAuthorization.cs new file mode 100644 index 0000000..24f33e0 --- /dev/null +++ b/src/JT808.DotNetty.Core/Interfaces/IJT808WebApiAuthorization.cs @@ -0,0 +1,13 @@ +using DotNetty.Codecs.Http; +using System; +using System.Collections.Generic; +using System.Security.Principal; +using System.Text; + +namespace JT808.DotNetty.Core.Interfaces +{ + public interface IJT808WebApiAuthorization + { + bool Authorization(IFullHttpRequest request, out IPrincipal principal); + } +} diff --git a/src/JT808.DotNetty.Core/Interfaces/IJT808WebApiNettyBuilder.cs b/src/JT808.DotNetty.Core/Interfaces/IJT808WebApiNettyBuilder.cs index 02dae2b..5c9c343 100644 --- a/src/JT808.DotNetty.Core/Interfaces/IJT808WebApiNettyBuilder.cs +++ b/src/JT808.DotNetty.Core/Interfaces/IJT808WebApiNettyBuilder.cs @@ -11,5 +11,6 @@ namespace JT808.DotNetty.Core.Interfaces IJT808NettyBuilder Instance { get; } IJT808NettyBuilder Builder(); IJT808WebApiNettyBuilder ReplaceMsgIdHandler() where T : JT808MsgIdHttpHandlerBase; + IJT808WebApiNettyBuilder ReplaceAuthorization() where T : IJT808WebApiAuthorization; } } diff --git a/src/JT808.DotNetty.Core/JT808.DotNetty.Core.csproj b/src/JT808.DotNetty.Core/JT808.DotNetty.Core.csproj index 84f8bf6..f96989c 100644 --- a/src/JT808.DotNetty.Core/JT808.DotNetty.Core.csproj +++ b/src/JT808.DotNetty.Core/JT808.DotNetty.Core.csproj @@ -23,6 +23,7 @@ + diff --git a/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs b/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs index f568f14..042dba7 100644 --- a/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs +++ b/src/JT808.DotNetty.Core/JT808CoreDotnettyExtensions.cs @@ -18,7 +18,7 @@ using System.Runtime.CompilerServices; [assembly: InternalsVisibleTo("JT808.DotNetty.Core.Test")] [assembly: InternalsVisibleTo("JT808.DotNetty.Tcp.Test")] [assembly: InternalsVisibleTo("JT808.DotNetty.Udp.Test")] -[assembly: InternalsVisibleTo("JT808.DotNetty.WebApi.Test")] +[assembly: InternalsVisibleTo("JT808.DotNetty.WebApiTest")] [assembly: InternalsVisibleTo("JT808.DotNetty.Tcp")] [assembly: InternalsVisibleTo("JT808.DotNetty.Udp")] [assembly: InternalsVisibleTo("JT808.DotNetty.WebApi")] diff --git a/src/JT808.DotNetty.Services.Tests/JT808.DotNetty.Transmit.Test/JT808.DotNetty.Transmit.Test.csproj b/src/JT808.DotNetty.Services.Tests/JT808.DotNetty.Transmit.Test/JT808.DotNetty.Transmit.Test.csproj index b6870ed..285c128 100644 --- a/src/JT808.DotNetty.Services.Tests/JT808.DotNetty.Transmit.Test/JT808.DotNetty.Transmit.Test.csproj +++ b/src/JT808.DotNetty.Services.Tests/JT808.DotNetty.Transmit.Test/JT808.DotNetty.Transmit.Test.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/JT808.DotNetty.Services/JT808.DotNetty.MsgIdHandler/JT808.DotNetty.MsgIdHandler.csproj b/src/JT808.DotNetty.Services/JT808.DotNetty.MsgIdHandler/JT808.DotNetty.MsgIdHandler.csproj index 1fabe39..5dc7827 100644 --- a/src/JT808.DotNetty.Services/JT808.DotNetty.MsgIdHandler/JT808.DotNetty.MsgIdHandler.csproj +++ b/src/JT808.DotNetty.Services/JT808.DotNetty.MsgIdHandler/JT808.DotNetty.MsgIdHandler.csproj @@ -23,7 +23,7 @@ - + diff --git a/src/JT808.DotNetty.Services/JT808.DotNetty.MsgLogging/JT808.DotNetty.MsgLogging.csproj b/src/JT808.DotNetty.Services/JT808.DotNetty.MsgLogging/JT808.DotNetty.MsgLogging.csproj index 84eaeec..f32b7ae 100644 --- a/src/JT808.DotNetty.Services/JT808.DotNetty.MsgLogging/JT808.DotNetty.MsgLogging.csproj +++ b/src/JT808.DotNetty.Services/JT808.DotNetty.MsgLogging/JT808.DotNetty.MsgLogging.csproj @@ -22,9 +22,9 @@ LICENSE - - - + + + diff --git a/src/JT808.DotNetty.Services/JT808.DotNetty.ReplyMessage/JT808.DotNetty.ReplyMessage.csproj b/src/JT808.DotNetty.Services/JT808.DotNetty.ReplyMessage/JT808.DotNetty.ReplyMessage.csproj index af1c2fc..ad69ed2 100644 --- a/src/JT808.DotNetty.Services/JT808.DotNetty.ReplyMessage/JT808.DotNetty.ReplyMessage.csproj +++ b/src/JT808.DotNetty.Services/JT808.DotNetty.ReplyMessage/JT808.DotNetty.ReplyMessage.csproj @@ -21,9 +21,9 @@ LICENSE - - - + + + diff --git a/src/JT808.DotNetty.Services/JT808.DotNetty.SessionNotice/JT808.DotNetty.SessionNotice.csproj b/src/JT808.DotNetty.Services/JT808.DotNetty.SessionNotice/JT808.DotNetty.SessionNotice.csproj index 53326fb..a07d6f1 100644 --- a/src/JT808.DotNetty.Services/JT808.DotNetty.SessionNotice/JT808.DotNetty.SessionNotice.csproj +++ b/src/JT808.DotNetty.Services/JT808.DotNetty.SessionNotice/JT808.DotNetty.SessionNotice.csproj @@ -22,9 +22,9 @@ LICENSE - - - + + + diff --git a/src/JT808.DotNetty.Services/JT808.DotNetty.Traffic/JT808.DotNetty.Traffic.csproj b/src/JT808.DotNetty.Services/JT808.DotNetty.Traffic/JT808.DotNetty.Traffic.csproj index 8dc6d4b..5119ab1 100644 --- a/src/JT808.DotNetty.Services/JT808.DotNetty.Traffic/JT808.DotNetty.Traffic.csproj +++ b/src/JT808.DotNetty.Services/JT808.DotNetty.Traffic/JT808.DotNetty.Traffic.csproj @@ -21,10 +21,10 @@ LICENSE - - - - + + + + diff --git a/src/JT808.DotNetty.Services/JT808.DotNetty.Transmit/JT808.DotNetty.Transmit.csproj b/src/JT808.DotNetty.Services/JT808.DotNetty.Transmit/JT808.DotNetty.Transmit.csproj index 03c2456..14f3a8b 100644 --- a/src/JT808.DotNetty.Services/JT808.DotNetty.Transmit/JT808.DotNetty.Transmit.csproj +++ b/src/JT808.DotNetty.Services/JT808.DotNetty.Transmit/JT808.DotNetty.Transmit.csproj @@ -25,10 +25,10 @@ - - - - + + + + diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Core.Test/JT808.DotNetty.Core.Test.csproj b/src/JT808.DotNetty.Tests/JT808.DotNetty.Core.Test/JT808.DotNetty.Core.Test.csproj index 9e7deb6..5a3b7a2 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Core.Test/JT808.DotNetty.Core.Test.csproj +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Core.Test/JT808.DotNetty.Core.Test.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.0 @@ -7,12 +7,12 @@ - - - - - - + + + + + + diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/JT808.DotNetty.Hosting.csproj b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/JT808.DotNetty.Hosting.csproj index dfeec32..7191ebf 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/JT808.DotNetty.Hosting.csproj +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/JT808.DotNetty.Hosting.csproj @@ -8,13 +8,14 @@ - - - - - - - + + + + + + + + diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs index 1625ff3..fc97731 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs @@ -18,7 +18,6 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; -using WebApiClient.Extensions.DependencyInjection; using JT808.DotNetty.Kafka; namespace JT808.DotNetty.Hosting @@ -59,20 +58,20 @@ namespace JT808.DotNetty.Hosting //扩展webapi JT808MsgIdHttpHandlerBase //.ReplaceMsgIdHandler() .Builder(); - //添加kafka插件 - //.AddJT808ServerKafkaMsgProducer(hostContext.Configuration) - //.AddJT808ServerKafkaMsgReplyConsumer(hostContext.Configuration) - //.AddJT808ServerKafkaSessionProducer(hostContext.Configuration) - //.Builder(); - //webapi客户端调用 - //services.AddHttpApi().ConfigureHttpApiConfig((c, p) => + //添加kafka插件 + //.AddJT808ServerKafkaMsgProducer(hostContext.Configuration) + //.AddJT808ServerKafkaMsgReplyConsumer(hostContext.Configuration) + //.AddJT808ServerKafkaSessionProducer(hostContext.Configuration) + //.Builder(); + //使用微软自带的webapi客户端 + //services.AddHttpClient("jt808webapi", c => //{ - // c.HttpHost = new Uri("http://localhost:828/jt808api/"); - // c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"; - // c.LoggerFactory = p.GetRequiredService(); - //}); - //var client = services.BuildServiceProvider().GetRequiredService(); - //var result = client.GetTcpAtomicCounter().InvokeAsync().Result; + // c.BaseAddress = new Uri("http://localhost:828/"); + // c.DefaultRequestHeaders.Add("token", "123456); + //}) + //.AddTypedClient(); + //var client = services.BuildServiceProvider().GetRequiredService(); + //var result = client.GetTcpAtomicCounter(); }); await serverHostBuilder.RunConsoleAsync(); diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808.DotNetty.Tcp.Test.csproj b/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808.DotNetty.Tcp.Test.csproj index d060ea2..a8af071 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808.DotNetty.Tcp.Test.csproj +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Tcp.Test/JT808.DotNetty.Tcp.Test.csproj @@ -7,12 +7,12 @@ - - - - - - + + + + + + diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808.DotNetty.Udp.Test.csproj b/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808.DotNetty.Udp.Test.csproj index e165bf1..316722e 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808.DotNetty.Udp.Test.csproj +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Udp.Test/JT808.DotNetty.Udp.Test.csproj @@ -7,12 +7,12 @@ - - - - - - + + + + + + diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/Authorization/JT808AuthorizationDefaultTest.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/Authorization/JT808AuthorizationDefaultTest.cs new file mode 100644 index 0000000..6c36d04 --- /dev/null +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/Authorization/JT808AuthorizationDefaultTest.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Text; +using DotNetty.Codecs.Http; +using DotNetty.Common.Utilities; +using JT808.DotNetty.Core.Configurations; +using JT808.DotNetty.WebApi.Authorization; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Xunit; + +namespace JT808.DotNetty.WebApi.Test.Authorization +{ + public class JT808AuthorizationDefaultTest + { + [Fact] + public void AuthorizationQuertStringTest() + { + IServiceCollection serviceDescriptors = new ServiceCollection(); + serviceDescriptors.Configure((options)=> { }); + var options=serviceDescriptors.BuildServiceProvider().GetRequiredService>(); + JT808AuthorizationDefault jT808AuthorizationDefault = new JT808AuthorizationDefault(options); + var m = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Get, "/demo?token=123456"); + Assert.True(jT808AuthorizationDefault.Authorization(m, out var principal)); + } + + [Fact] + public void AuthorizationQuertStringFailTest() + { + IServiceCollection serviceDescriptors = new ServiceCollection(); + serviceDescriptors.Configure((options) => { }); + var options = serviceDescriptors.BuildServiceProvider().GetRequiredService>(); + JT808AuthorizationDefault jT808AuthorizationDefault = new JT808AuthorizationDefault(options); + var m = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Get, "/demo?token=12345"); + Assert.False(jT808AuthorizationDefault.Authorization(m, out var principal)); + } + + [Fact] + public void AuthorizationHeaderTest() + { + IServiceCollection serviceDescriptors = new ServiceCollection(); + serviceDescriptors.Configure((options) => { }); + var options = serviceDescriptors.BuildServiceProvider().GetRequiredService>(); + JT808AuthorizationDefault jT808AuthorizationDefault = new JT808AuthorizationDefault(options); + var m = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Get, "/"); + m.Headers.Add((AsciiString)"token", "123456"); + Assert.True(jT808AuthorizationDefault.Authorization(m, out var principal)); + } + + [Fact] + public void AuthorizationHeaderFailTest() + { + IServiceCollection serviceDescriptors = new ServiceCollection(); + serviceDescriptors.Configure((options) => { }); + var options = serviceDescriptors.BuildServiceProvider().GetRequiredService>(); + JT808AuthorizationDefault jT808AuthorizationDefault = new JT808AuthorizationDefault(options); + var m = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Get, "/"); + m.Headers.Add((AsciiString)"token", "12345"); + Assert.False(jT808AuthorizationDefault.Authorization(m, out var principal)); + } + } +} diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808.DotNetty.WebApi.Test.csproj b/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808.DotNetty.WebApi.Test.csproj index ef2534f..150f9e4 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808.DotNetty.WebApi.Test.csproj +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808.DotNetty.WebApi.Test.csproj @@ -7,26 +7,17 @@ - - - - - - + + + + + - - - - - Always - - - diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808DotNettyWebApiTest.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808DotNettyWebApiTest.cs deleted file mode 100644 index d4ea7f9..0000000 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808DotNettyWebApiTest.cs +++ /dev/null @@ -1,81 +0,0 @@ -using JT808.DotNetty.Abstractions; -using JT808.DotNetty.WebApiClientTool; -using Microsoft.Extensions.Logging; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; -using WebApiClient; - -namespace JT808.DotNetty.WebApi.Test -{ - [TestClass] - public class JT808DotNettyWebApiTest: TestBase - { - IJT808DotNettyWebApi jT808DotNettyWebApi; - - public JT808DotNettyWebApiTest() - { - HttpApi.Register().ConfigureHttpApiConfig(c => - { - c.HttpHost = new Uri("http://127.0.0.1:12828" + JT808NettyConstants.JT808WebApiRouteTable.RouteTablePrefix + "/"); - c.LoggerFactory = new LoggerFactory(); - }); - var api = HttpApi.Resolve(); - } - - [TestMethod] - public void GetUdpAtomicCounterTest() - { - var result = jT808DotNettyWebApi.GetUdpAtomicCounter().GetAwaiter().GetResult(); - } - - [TestMethod] - public void UnificationUdpSendTest() - { - var result = jT808DotNettyWebApi.UnificationSend(new Abstractions.Dtos.JT808UnificationSendRequestDto { - TerminalPhoneNo= "123456789014", - Data=new byte[] {1,2,3,4} - }).GetAwaiter().GetResult(); - } - - [TestMethod] - public void RemoveUdpSessionByTerminalPhoneNoTest() - { - var result = jT808DotNettyWebApi.RemoveUdpSessionByTerminalPhoneNo("123456789014").GetAwaiter().GetResult(); - } - - [TestMethod] - public void GetUdpSessionAllTest() - { - var result = jT808DotNettyWebApi.GetUdpSessionAll().GetAwaiter().GetResult(); - } - - [TestMethod] - public void GetTcpAtomicCounterTest() - { - var result = jT808DotNettyWebApi.GetTcpAtomicCounter().GetAwaiter().GetResult(); - } - - [TestMethod] - public void UnificationTcpSendTest() - { - var result = jT808DotNettyWebApi.UnificationSend(new Abstractions.Dtos.JT808UnificationSendRequestDto - { - TerminalPhoneNo = "123456789002", - Data = new byte[] { 1, 2, 3, 4 } - }).GetAwaiter().GetResult(); - } - - [TestMethod] - public void RemoveTcpSessionByTerminalPhoneNoTest() - { - var result = jT808DotNettyWebApi.RemoveTcpSessionByTerminalPhoneNo("123456789002").GetAwaiter().GetResult(); - } - - [TestMethod] - public void GetTcpSessionAllTest() - { - var result = jT808DotNettyWebApi.GetTcpSessionAll().GetAwaiter().GetResult(); - - } - } -} diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808HttpClientTest.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808HttpClientTest.cs new file mode 100644 index 0000000..b3d8c03 --- /dev/null +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/JT808HttpClientTest.cs @@ -0,0 +1,159 @@ +using JT808.DotNetty.Abstractions; +using JT808.DotNetty.Abstractions.Dtos; +using JT808.DotNetty.WebApiClientTool; +using RichardSzalay.MockHttp; +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using Xunit; + +namespace JT808.DotNetty.WebApi.Test +{ + public class JT808HttpClientTest + { + public static HttpClient CreateHttpClient(string uri,string requestjson,string responseJson) + { + string baseUrl = "http://localhost"; + var mockHttp = new MockHttpMessageHandler(); + var request = mockHttp.When($"{baseUrl}{uri}") + .Respond("application/json", responseJson); + if (!string.IsNullOrEmpty(requestjson)) + { + request.WithContent(requestjson); + } + var client = mockHttp.ToHttpClient(); + + client.BaseAddress = new Uri(baseUrl); + return client; + } + + [Fact] + public void GetTcpSessionAllTest() + { + JT808ResultDto> jT808ResultDto = new JT808ResultDto>(); + jT808ResultDto.Data = new List(); + jT808ResultDto.Code = 200; + jT808ResultDto.Data.Add(new JT808TcpSessionInfoDto { + LastActiveTime=DateTime.Parse("2019-10-29 23:23:23"), + StartTime=DateTime.Parse("2019-10-29 23:23:23"), + RemoteAddressIP="127.0.0.1:555", + TerminalPhoneNo="123456789" + }); + JT808HttpClient jT808HttpClient = new JT808HttpClient(CreateHttpClient(JT808NettyConstants.JT808WebApiRouteTable.SessionTcpGetAll,"", JsonSerializer.Serialize(jT808ResultDto))); + var result = jT808HttpClient.GetTcpSessionAll(); + Assert.Equal(jT808ResultDto.Code, result.Code); + Assert.Equal(jT808ResultDto.Data[0].TerminalPhoneNo, result.Data[0].TerminalPhoneNo); + Assert.Equal(jT808ResultDto.Data[0].StartTime, result.Data[0].StartTime); + Assert.Equal(jT808ResultDto.Data[0].LastActiveTime, result.Data[0].LastActiveTime); + Assert.Equal(jT808ResultDto.Data[0].RemoteAddressIP, result.Data[0].RemoteAddressIP); + } + + [Fact] + public void GetTcpSessionAllLargeTest() + { + JT808ResultDto> jT808ResultDto = new JT808ResultDto>(); + jT808ResultDto.Data = new List(); + jT808ResultDto.Code = 200; + for(var i = 0; i < 50000; i++) + { + jT808ResultDto.Data.Add(new JT808TcpSessionInfoDto + { + LastActiveTime = DateTime.Parse("2019-10-29 23:23:23"), + StartTime = DateTime.Parse("2019-10-29 23:23:23"), + RemoteAddressIP = "127.0.0.1:555", + TerminalPhoneNo = (i+1).ToString() + }); + } + JT808HttpClient jT808HttpClient = new JT808HttpClient(CreateHttpClient(JT808NettyConstants.JT808WebApiRouteTable.SessionTcpGetAll, "", JsonSerializer.Serialize(jT808ResultDto))); + var result = jT808HttpClient.GetTcpSessionAll(); + Assert.Equal(jT808ResultDto.Code, result.Code); + Assert.Equal(50000, result.Data.Count); + } + + [Fact] + public void RemoveSessionByTerminalPhoneNoTest() + { + JT808ResultDto jT808ResultDto = new JT808ResultDto(); + jT808ResultDto.Data = true; + jT808ResultDto.Code = 200; + JT808HttpClient jT808HttpClient = new JT808HttpClient(CreateHttpClient(JT808NettyConstants.JT808WebApiRouteTable.SessionRemoveByTerminalPhoneNo, "123456789", JsonSerializer.Serialize(jT808ResultDto))); + var result = jT808HttpClient.RemoveByTerminalPhoneNo("123456789"); + Assert.Equal(jT808ResultDto.Code, result.Code); + Assert.Equal(jT808ResultDto.Data, result.Data); + } + + [Fact] + public void UnificationSendTest() + { + JT808ResultDto jT808ResultDto = new JT808ResultDto(); + jT808ResultDto.Data = true; + jT808ResultDto.Code = 200; + JT808UnificationSendRequestDto jT808UnificationSendRequestDto = new JT808UnificationSendRequestDto + { + TerminalPhoneNo = "123456789", + Data = new byte[] { 1, 2, 3, 4 } + }; + JT808HttpClient jT808HttpClient = new JT808HttpClient(CreateHttpClient(JT808NettyConstants.JT808WebApiRouteTable.UnificationSend, JsonSerializer.Serialize(jT808UnificationSendRequestDto), JsonSerializer.Serialize(jT808ResultDto))); + var result = jT808HttpClient.UnificationSend(jT808UnificationSendRequestDto); + Assert.Equal(jT808ResultDto.Code, result.Code); + Assert.Equal(jT808ResultDto.Data, result.Data); + } + + [Fact] + public void GetTcpAtomicCounterTest() + { + JT808ResultDto jT808ResultDto = new JT808ResultDto(); + jT808ResultDto.Data = new JT808AtomicCounterDto { + MsgFailCount=9, + MsgSuccessCount=10 + }; + jT808ResultDto.Code = 200; + JT808HttpClient jT808HttpClient = new JT808HttpClient(CreateHttpClient(JT808NettyConstants.JT808WebApiRouteTable.GetTcpAtomicCounter, "",JsonSerializer.Serialize(jT808ResultDto))); + var result = jT808HttpClient.GetTcpAtomicCounter(); + Assert.Equal(jT808ResultDto.Code, result.Code); + Assert.Equal(jT808ResultDto.Data.MsgFailCount, result.Data.MsgFailCount); + Assert.Equal(jT808ResultDto.Data.MsgSuccessCount, result.Data.MsgSuccessCount); + } + + [Fact] + public void GetUdpAtomicCounterTest() + { + JT808ResultDto jT808ResultDto = new JT808ResultDto(); + jT808ResultDto.Data = new JT808AtomicCounterDto + { + MsgFailCount = 19, + MsgSuccessCount = 110 + }; + jT808ResultDto.Code = 200; + JT808HttpClient jT808HttpClient = new JT808HttpClient(CreateHttpClient(JT808NettyConstants.JT808WebApiRouteTable.GetUdpAtomicCounter, "", JsonSerializer.Serialize(jT808ResultDto))); + var result = jT808HttpClient.GetUdpAtomicCounter(); + Assert.Equal(jT808ResultDto.Code, result.Code); + Assert.Equal(jT808ResultDto.Data.MsgFailCount, result.Data.MsgFailCount); + Assert.Equal(jT808ResultDto.Data.MsgSuccessCount, result.Data.MsgSuccessCount); + } + + [Fact] + public void GetUdpSessionAllTest() + { + JT808ResultDto> jT808ResultDto = new JT808ResultDto>(); + jT808ResultDto.Data = new List(); + jT808ResultDto.Data.Add(new JT808UdpSessionInfoDto + { + LastActiveTime = DateTime.Parse("2019-10-29 21:21:21"), + StartTime = DateTime.Parse("2019-10-29 21:21:21"), + RemoteAddressIP = "127.0.0.1:666", + TerminalPhoneNo = "123456789" + }); + jT808ResultDto.Code = 200; + JT808HttpClient jT808HttpClient = new JT808HttpClient(CreateHttpClient(JT808NettyConstants.JT808WebApiRouteTable.SessionUdpGetAll, "", JsonSerializer.Serialize(jT808ResultDto))); + var result = jT808HttpClient.GetUdpSessionAll(); + Assert.Equal(jT808ResultDto.Code, result.Code); + Assert.Equal(jT808ResultDto.Data[0].TerminalPhoneNo, result.Data[0].TerminalPhoneNo); + Assert.Equal(jT808ResultDto.Data[0].StartTime, result.Data[0].StartTime); + Assert.Equal(jT808ResultDto.Data[0].LastActiveTime, result.Data[0].LastActiveTime); + Assert.Equal(jT808ResultDto.Data[0].RemoteAddressIP, result.Data[0].RemoteAddressIP); + } + } +} diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/TestBase.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/TestBase.cs deleted file mode 100644 index 6970417..0000000 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/TestBase.cs +++ /dev/null @@ -1,140 +0,0 @@ -using JT808.DotNetty.Core; -using JT808.DotNetty.Udp; -using JT808.DotNetty.Tcp; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Options; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Text; -using JT808.DotNetty.WebApiClientTool; -using System.Net; -using JT808.Protocol; -using JT808.Protocol.Extensions; -using System.Threading; -using JT808.Protocol.Interfaces; - -namespace JT808.DotNetty.WebApi.Test -{ - public class TestBase - { - public static IServiceProvider ServiceProvider; - public static JT808Serializer JT808Serializer; - static TestBase() - { - var serverHostBuilder = new HostBuilder() - .ConfigureAppConfiguration((hostingContext, config) => - { - config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory); - config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); - }) - .ConfigureServices((hostContext, services) => - { - services.Configure(hostContext.Configuration.GetSection("JT808DotNettyWebApiOptions")); - services.AddSingleton(); - services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); - services.AddJT808Configure() - .AddJT808NettyCore(hostContext.Configuration) - .AddJT808TcpNettyHost() - .AddJT808UdpNettyHost() - .AddJT808WebApiNettyHost(); - }); - var build = serverHostBuilder.Build(); - build.Start(); - ServiceProvider = build.Services; - JT808Serializer = ServiceProvider.GetRequiredService().GetSerializer(); - } - - static IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12808); - - static IPEndPoint endPoint1 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12818); - - JT808SimpleTcpClient SimpleTcpClient1; - JT808SimpleTcpClient SimpleTcpClient2; - JT808SimpleTcpClient SimpleTcpClient3; - JT808SimpleTcpClient SimpleTcpClient4; - JT808SimpleTcpClient SimpleTcpClient5; - - - JT808SimpleUdpClient SimpleUdpClient1; - JT808SimpleUdpClient SimpleUdpClient2; - JT808SimpleUdpClient SimpleUdpClient3; - JT808SimpleUdpClient SimpleUdpClient4; - JT808SimpleUdpClient SimpleUdpClient5; - - - - - public TestBase() - { - SimpleTcpClient1 = new JT808SimpleTcpClient(endPoint); - SimpleTcpClient2 = new JT808SimpleTcpClient(endPoint); - SimpleTcpClient3 = new JT808SimpleTcpClient(endPoint); - SimpleTcpClient4 = new JT808SimpleTcpClient(endPoint); - SimpleTcpClient5 = new JT808SimpleTcpClient(endPoint); - // 心跳会话包 - JT808Package jT808Package1 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789001"); - SimpleTcpClient1.WriteAsync(JT808Serializer.Serialize(jT808Package1)); - - // 心跳会话包 - JT808Package jT808Package2 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789002"); - SimpleTcpClient2.WriteAsync(JT808Serializer.Serialize(jT808Package2)); - - // 心跳会话包 - JT808Package jT808Package3 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789003"); - SimpleTcpClient3.WriteAsync(JT808Serializer.Serialize(jT808Package3)); - - // 心跳会话包 - JT808Package jT808Package4 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789004"); - SimpleTcpClient4.WriteAsync(JT808Serializer.Serialize(jT808Package4)); - - // 心跳会话包 - JT808Package jT808Package5 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789005"); - SimpleTcpClient5.WriteAsync(JT808Serializer.Serialize(jT808Package5)); - - SimpleUdpClient1 = new JT808SimpleUdpClient(endPoint1); - SimpleUdpClient2 = new JT808SimpleUdpClient(endPoint1); - SimpleUdpClient3 = new JT808SimpleUdpClient(endPoint1); - SimpleUdpClient4 = new JT808SimpleUdpClient(endPoint1); - SimpleUdpClient5 = new JT808SimpleUdpClient(endPoint1); - // 心跳会话包 - JT808Package jT808Package12 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789011"); - SimpleUdpClient1.WriteAsync(JT808Serializer.Serialize(jT808Package12)); - Thread.Sleep(300); - // 心跳会话包 - JT808Package jT808Package23 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789012"); - SimpleUdpClient2.WriteAsync(JT808Serializer.Serialize(jT808Package23)); - Thread.Sleep(300); - // 心跳会话包 - JT808Package jT808Package34 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789013"); - SimpleUdpClient3.WriteAsync(JT808Serializer.Serialize(jT808Package34)); - Thread.Sleep(300); - // 心跳会话包 - JT808Package jT808Package45 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789014"); - SimpleUdpClient4.WriteAsync(JT808Serializer.Serialize(jT808Package45)); - Thread.Sleep(300); - // 心跳会话包 - JT808Package jT808Package56 = JT808.Protocol.Enums.JT808MsgId.终端心跳.Create("123456789015"); - SimpleUdpClient5.WriteAsync(JT808Serializer.Serialize(jT808Package56)); - - Thread.Sleep(300); - } - - public void Dispose() - { - SimpleTcpClient1.Down(); - SimpleTcpClient2.Down(); - SimpleTcpClient3.Down(); - SimpleTcpClient4.Down(); - SimpleTcpClient5.Down(); - - SimpleUdpClient1.Down(); - SimpleUdpClient2.Down(); - SimpleUdpClient3.Down(); - SimpleUdpClient4.Down(); - SimpleUdpClient5.Down(); - } - } -} diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/appsettings.json b/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/appsettings.json deleted file mode 100644 index 66be72e..0000000 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.WebApi.Test/appsettings.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "Logging": { - "IncludeScopes": false, - "Debug": { - "LogLevel": { - "Default": "Trace" - } - }, - "Console": { - "LogLevel": { - "Default": "Trace" - } - } - }, - - "JT808Configuration": { - "TcpPort": 12808, - "UdpPort": 12818, - "WebApiPort": 12828, - "ForwardingRemoteIPAddress": [ - "127.0.0.1" - ] - }, - "JT808DotNettyWebApiOptions": { - "WebApiHosts": [ "127.0.0.1:12828" ] - } -} diff --git a/src/JT808.DotNetty.WebApi/Authorization/JT808AuthorizationDefault.cs b/src/JT808.DotNetty.WebApi/Authorization/JT808AuthorizationDefault.cs new file mode 100644 index 0000000..cb319ee --- /dev/null +++ b/src/JT808.DotNetty.WebApi/Authorization/JT808AuthorizationDefault.cs @@ -0,0 +1,51 @@ +using DotNetty.Codecs.Http; +using DotNetty.Common.Utilities; +using JT808.DotNetty.Core.Configurations; +using JT808.DotNetty.Core.Interfaces; +using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using System.Security.Principal; +using System.Text; + +namespace JT808.DotNetty.WebApi.Authorization +{ + class JT808AuthorizationDefault : IJT808WebApiAuthorization + { + private IOptionsMonitor optionsMonitor; + public JT808AuthorizationDefault(IOptionsMonitor optionsMonitor) + { + this.optionsMonitor = optionsMonitor; + } + public bool Authorization(IFullHttpRequest request, out IPrincipal principal) + { + var uriSpan = request.Uri.AsSpan(); + var uriParamStr = uriSpan.Slice(uriSpan.IndexOf('?')+1).ToString().ToLower(); + var uriParams = uriParamStr.Split('&'); + var tokenParam = uriParams.FirstOrDefault(m => m.Contains("token")); + string tokenValue = string.Empty; + if (!string.IsNullOrEmpty(tokenParam)) + { + tokenValue = tokenParam.Split('=')[1]; + } + else + { + if (request.Headers.TryGetAsString((AsciiString)"token", out tokenValue)) + { + } + } + if (optionsMonitor.CurrentValue.WebApiToken == tokenValue) + { + principal = new ClaimsPrincipal(new GenericIdentity(tokenValue)); + return true; + } + else + { + principal = null; + return false; + } + } + } +} diff --git a/src/JT808.DotNetty.WebApi/Handlers/JT808WebAPIServerHandler.cs b/src/JT808.DotNetty.WebApi/Handlers/JT808WebAPIServerHandler.cs index 159625d..ca530eb 100644 --- a/src/JT808.DotNetty.WebApi/Handlers/JT808WebAPIServerHandler.cs +++ b/src/JT808.DotNetty.WebApi/Handlers/JT808WebAPIServerHandler.cs @@ -3,6 +3,7 @@ using DotNetty.Codecs.Http; using DotNetty.Common.Utilities; using DotNetty.Transport.Channels; using JT808.DotNetty.Core.Handlers; +using JT808.DotNetty.Core.Interfaces; using JT808.DotNetty.Core.Metadata; using Microsoft.Extensions.Logging; using System; @@ -25,11 +26,13 @@ namespace JT808.DotNetty.WebApi.Handlers private static readonly AsciiString ServerEntity = HttpHeaderNames.Server; private readonly JT808MsgIdHttpHandlerBase jT808MsgIdHttpHandlerBase; private readonly ILogger logger; - + private readonly IJT808WebApiAuthorization jT808WebApiAuthorization; public JT808WebAPIServerHandler( + IJT808WebApiAuthorization jT808WebApiAuthorization, JT808MsgIdHttpHandlerBase jT808MsgIdHttpHandlerBase, ILoggerFactory loggerFactory) { + this.jT808WebApiAuthorization = jT808WebApiAuthorization; this.jT808MsgIdHttpHandlerBase = jT808MsgIdHttpHandlerBase; logger = loggerFactory.CreateLogger(); } @@ -42,9 +45,13 @@ namespace JT808.DotNetty.WebApi.Handlers logger.LogDebug($"Content:{msg.Content.ToString(Encoding.UTF8)}"); } JT808HttpResponse jT808HttpResponse = null; + if (!jT808WebApiAuthorization.Authorization(msg, out var principal)) + { + jT808HttpResponse = jT808MsgIdHttpHandlerBase.AuthFailHttpResponse(); + } if (jT808MsgIdHttpHandlerBase.HandlerDict.TryGetValue(msg.Uri,out var funcHandler)) { - jT808HttpResponse = funcHandler( new JT808HttpRequest() { Json = msg.Content.ToString(Encoding.UTF8)}); + jT808HttpResponse = funcHandler(new JT808HttpRequest(){ Json = msg.Content.ToString(Encoding.UTF8)}); } else { diff --git a/src/JT808.DotNetty.WebApi/JT808.DotNetty.WebApi.csproj b/src/JT808.DotNetty.WebApi/JT808.DotNetty.WebApi.csproj index 5ee5765..52f3103 100644 --- a/src/JT808.DotNetty.WebApi/JT808.DotNetty.WebApi.csproj +++ b/src/JT808.DotNetty.WebApi/JT808.DotNetty.WebApi.csproj @@ -27,6 +27,7 @@ + diff --git a/src/JT808.DotNetty.WebApi/JT808WebApiBuilderDefault.cs b/src/JT808.DotNetty.WebApi/JT808WebApiBuilderDefault.cs index b64c68f..a91a83d 100644 --- a/src/JT808.DotNetty.WebApi/JT808WebApiBuilderDefault.cs +++ b/src/JT808.DotNetty.WebApi/JT808WebApiBuilderDefault.cs @@ -22,6 +22,12 @@ namespace JT808.DotNetty.WebApi return Instance; } + public IJT808WebApiNettyBuilder ReplaceAuthorization() where T : IJT808WebApiAuthorization + { + Instance.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(IJT808WebApiAuthorization), typeof(T), ServiceLifetime.Singleton)); + return this; + } + public IJT808WebApiNettyBuilder ReplaceMsgIdHandler() where T : JT808MsgIdHttpHandlerBase { Instance.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(JT808MsgIdHttpHandlerBase), typeof(T), ServiceLifetime.Singleton)); diff --git a/src/JT808.DotNetty.WebApi/JT808WebApiDotnettyExtensions.cs b/src/JT808.DotNetty.WebApi/JT808WebApiDotnettyExtensions.cs index 1d4fec3..21db7b1 100644 --- a/src/JT808.DotNetty.WebApi/JT808WebApiDotnettyExtensions.cs +++ b/src/JT808.DotNetty.WebApi/JT808WebApiDotnettyExtensions.cs @@ -1,6 +1,7 @@ using JT808.DotNetty.Abstractions; using JT808.DotNetty.Core.Handlers; using JT808.DotNetty.Core.Interfaces; +using JT808.DotNetty.WebApi.Authorization; using JT808.DotNetty.WebApi.Handlers; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -15,6 +16,7 @@ namespace JT808.DotNetty.WebApi public static IJT808WebApiNettyBuilder AddJT808WebApiNettyHost(this IJT808NettyBuilder jT808NettyBuilder) { jT808NettyBuilder.JT808Builder.Services.TryAddSingleton(); + jT808NettyBuilder.JT808Builder.Services.TryAddSingleton(); jT808NettyBuilder.JT808Builder.Services.TryAddScoped(); jT808NettyBuilder.JT808Builder.Services.AddHostedService(); return new JT808WebApiBuilderDefault(jT808NettyBuilder); diff --git a/src/JT808.DotNetty.WebApiClientTool/IJT808DotNettyWebApi.cs b/src/JT808.DotNetty.WebApiClientTool/IJT808DotNettyWebApi.cs deleted file mode 100644 index 55c3495..0000000 --- a/src/JT808.DotNetty.WebApiClientTool/IJT808DotNettyWebApi.cs +++ /dev/null @@ -1,64 +0,0 @@ -using JT808.DotNetty.Abstractions.Dtos; -using System.Collections.Generic; -using WebApiClient; -using WebApiClient.Attributes; - -namespace JT808.DotNetty.WebApiClientTool -{ - public interface IJT808DotNettyWebApi : IHttpApi - { - #region 基于Tcp WebApi - /// - /// 会话服务集合 - /// - /// - [HttpGet("Tcp/Session/GetAll")] - ITask>> GetTcpSessionAll(); - /// - /// 会话服务-通过设备终端号移除对应会话 - /// - /// - /// - [HttpPost("Tcp/Session/RemoveByTerminalPhoneNo")] - ITask> RemoveTcpSessionByTerminalPhoneNo([JsonContent] string terminalPhoneNo); - /// - /// 统一下发信息 - /// - /// - /// - [HttpPost("/UnificationSend")] - ITask> UnificationSend([JsonContent]JT808UnificationSendRequestDto jT808UnificationSendRequestDto); - /// - /// 获取Tcp包计数器 - /// - /// - /// - [HttpGet("Tcp/GetAtomicCounter")] - ITask> GetTcpAtomicCounter(); - - #endregion - - #region 基于Udp WebApi - /// - /// 会话服务集合 - /// - /// - /// - [HttpGet("Udp/Session/GetAll")] - ITask>> GetUdpSessionAll(); - /// - /// 会话服务-通过设备终端号移除对应会话 - /// - /// - /// - [HttpPost("Udp/Session/RemoveByTerminalPhoneNo")] - ITask> RemoveUdpSessionByTerminalPhoneNo([JsonContent] string terminalPhoneNo); - /// - /// 获取Udp包计数器 - /// - /// - [HttpGet("Udp/GetAtomicCounter")] - ITask> GetUdpAtomicCounter(); - #endregion - } -} diff --git a/src/JT808.DotNetty.WebApiClientTool/JT808.DotNetty.WebApiClientTool.csproj b/src/JT808.DotNetty.WebApiClientTool/JT808.DotNetty.WebApiClientTool.csproj index 569e7ca..06f0d78 100644 --- a/src/JT808.DotNetty.WebApiClientTool/JT808.DotNetty.WebApiClientTool.csproj +++ b/src/JT808.DotNetty.WebApiClientTool/JT808.DotNetty.WebApiClientTool.csproj @@ -19,9 +19,12 @@ JT808DotNetty的WebApiClient客户端调用工具 JT808DotNetty的WebApiClient客户端调用工具 + + JT808.DotNetty.WebApiClientTool.xml + - + diff --git a/src/JT808.DotNetty.WebApiClientTool/JT808DotNettyWebApiOptions.cs b/src/JT808.DotNetty.WebApiClientTool/JT808DotNettyWebApiOptions.cs deleted file mode 100644 index 8286ba3..0000000 --- a/src/JT808.DotNetty.WebApiClientTool/JT808DotNettyWebApiOptions.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace JT808.DotNetty.WebApiClientTool -{ - public class JT808DotNettyWebApiOptions - { - /// - /// 接口服务地址 - /// - public List WebApiHosts { get; set; } - } -} diff --git a/src/JT808.DotNetty.WebApiClientTool/JT808HttpClient.cs b/src/JT808.DotNetty.WebApiClientTool/JT808HttpClient.cs new file mode 100644 index 0000000..804fc27 --- /dev/null +++ b/src/JT808.DotNetty.WebApiClientTool/JT808HttpClient.cs @@ -0,0 +1,97 @@ +using JT808.DotNetty.Abstractions; +using JT808.DotNetty.Abstractions.Dtos; +using System; +using System.Buffers.Text; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace JT808.DotNetty.WebApiClientTool +{ + public class JT808HttpClient + { + public HttpClient HttpClient { get; } + public JT808HttpClient(HttpClient httpClient) + { + HttpClient = httpClient; + } + /// + /// 会话服务集合 + /// + /// + public JT808ResultDto> GetTcpSessionAll() + { + var request = new HttpRequestMessage(HttpMethod.Get, JT808NettyConstants.JT808WebApiRouteTable.SessionTcpGetAll); + var response = HttpClient.SendAsync(request).Result; + response.EnsureSuccessStatusCode(); + var value = JsonSerializer.Deserialize>>(response.Content.ReadAsByteArrayAsync().Result); + return value; + } + /// + /// 会话服务-通过设备终端号移除对应会话 + /// + /// + /// + public JT808ResultDto RemoveByTerminalPhoneNo(string terminalPhoneNo) + { + var request = new HttpRequestMessage(HttpMethod.Post, JT808NettyConstants.JT808WebApiRouteTable.SessionRemoveByTerminalPhoneNo); + request.Content = new StringContent(terminalPhoneNo); + var response = HttpClient.SendAsync(request).Result; + response.EnsureSuccessStatusCode(); + var value = JsonSerializer.Deserialize>(response.Content.ReadAsByteArrayAsync().Result); + return value; + } + /// + /// 统一下发信息 + /// + /// + /// + public JT808ResultDto UnificationSend(JT808UnificationSendRequestDto jT808UnificationSendRequestDto) + { + var request = new HttpRequestMessage(HttpMethod.Post, JT808NettyConstants.JT808WebApiRouteTable.UnificationSend); + request.Content = new StringContent(JsonSerializer.Serialize(jT808UnificationSendRequestDto)); + var response = HttpClient.SendAsync(request).Result; + response.EnsureSuccessStatusCode(); + var value = JsonSerializer.Deserialize>(response.Content.ReadAsByteArrayAsync().Result); + return value; + } + /// + /// 获取Tcp包计数器 + /// + /// + public JT808ResultDto GetTcpAtomicCounter() + { + var request = new HttpRequestMessage(HttpMethod.Get, JT808NettyConstants.JT808WebApiRouteTable.GetTcpAtomicCounter); + var response = HttpClient.SendAsync(request).Result; + response.EnsureSuccessStatusCode(); + var value = JsonSerializer.Deserialize>(response.Content.ReadAsByteArrayAsync().Result); + return value; + } + /// + /// 会话服务集合 + /// + /// + public JT808ResultDto> GetUdpSessionAll() + { + var request = new HttpRequestMessage(HttpMethod.Get, JT808NettyConstants.JT808WebApiRouteTable.SessionUdpGetAll); + var response = HttpClient.SendAsync(request).Result; + response.EnsureSuccessStatusCode(); + var value = JsonSerializer.Deserialize>>(response.Content.ReadAsByteArrayAsync().Result); + return value; + } + /// + /// 获取Udp包计数器 + /// + /// + public JT808ResultDto GetUdpAtomicCounter() + { + var request = new HttpRequestMessage(HttpMethod.Get, JT808NettyConstants.JT808WebApiRouteTable.GetUdpAtomicCounter); + var response = HttpClient.SendAsync(request).Result; + response.EnsureSuccessStatusCode(); + var value = JsonSerializer.Deserialize>(response.Content.ReadAsByteArrayAsync().Result); + return value; + } + } +} diff --git a/src/JT808.DotNetty.sln b/src/JT808.DotNetty.sln index e704dbd..13806b4 100644 --- a/src/JT808.DotNetty.sln +++ b/src/JT808.DotNetty.sln @@ -21,8 +21,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Tcp.Test", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Udp.Test", "JT808.DotNetty.Tests\JT808.DotNetty.Udp.Test\JT808.DotNetty.Udp.Test.csproj", "{E503BFD8-D90A-4610-97C7-5B9A0497303B}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.WebApi.Test", "JT808.DotNetty.Tests\JT808.DotNetty.WebApi.Test\JT808.DotNetty.WebApi.Test.csproj", "{EDE77A29-0840-450C-8B08-2D3388845AE5}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Hosting", "JT808.DotNetty.Tests\JT808.DotNetty.Hosting\JT808.DotNetty.Hosting.csproj", "{A0F2F006-5AEB-454E-83C5-ABFB58DE17A9}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.WebApiClientTool", "JT808.DotNetty.WebApiClientTool\JT808.DotNetty.WebApiClientTool.csproj", "{9D86C951-94F2-4CBD-B177-8AF31DDB05D8}" @@ -61,13 +59,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.MsgIdHandler EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.MsgIdHandler", "JT808.DotNetty.Services\JT808.DotNetty.MsgIdHandler\JT808.DotNetty.MsgIdHandler.csproj", "{081E805B-CDB7-48AB-89D7-136D94A9A413}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT808.DotNetty.MsgLogging", "JT808.DotNetty.Services\JT808.DotNetty.MsgLogging\JT808.DotNetty.MsgLogging.csproj", "{8F84D633-DE73-4267-B05F-06740B9C1EA6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.MsgLogging", "JT808.DotNetty.Services\JT808.DotNetty.MsgLogging\JT808.DotNetty.MsgLogging.csproj", "{8F84D633-DE73-4267-B05F-06740B9C1EA6}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.ReplyMessage", "JT808.DotNetty.Services\JT808.DotNetty.ReplyMessage\JT808.DotNetty.ReplyMessage.csproj", "{DDB299EC-6817-495F-8CF0-FBB9408AF6A4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT808.DotNetty.ReplyMessage", "JT808.DotNetty.Services\JT808.DotNetty.ReplyMessage\JT808.DotNetty.ReplyMessage.csproj", "{DDB299EC-6817-495F-8CF0-FBB9408AF6A4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.SessionNotice", "JT808.DotNetty.Services\JT808.DotNetty.SessionNotice\JT808.DotNetty.SessionNotice.csproj", "{A30A0671-FC46-4BEC-A6F9-DC2400CA7F3A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT808.DotNetty.SessionNotice", "JT808.DotNetty.Services\JT808.DotNetty.SessionNotice\JT808.DotNetty.SessionNotice.csproj", "{A30A0671-FC46-4BEC-A6F9-DC2400CA7F3A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Traffic", "JT808.DotNetty.Services\JT808.DotNetty.Traffic\JT808.DotNetty.Traffic.csproj", "{60B5BD57-1529-4D06-8FAD-0C6427AAF3CA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT808.DotNetty.Traffic", "JT808.DotNetty.Services\JT808.DotNetty.Traffic\JT808.DotNetty.Traffic.csproj", "{60B5BD57-1529-4D06-8FAD-0C6427AAF3CA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.WebApi.Test", "JT808.DotNetty.Tests\JT808.DotNetty.WebApi.Test\JT808.DotNetty.WebApi.Test.csproj", "{864A21E9-39F1-4B6C-9F95-3347454A7198}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -107,10 +107,6 @@ Global {E503BFD8-D90A-4610-97C7-5B9A0497303B}.Debug|Any CPU.Build.0 = Debug|Any CPU {E503BFD8-D90A-4610-97C7-5B9A0497303B}.Release|Any CPU.ActiveCfg = Release|Any CPU {E503BFD8-D90A-4610-97C7-5B9A0497303B}.Release|Any CPU.Build.0 = Release|Any CPU - {EDE77A29-0840-450C-8B08-2D3388845AE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EDE77A29-0840-450C-8B08-2D3388845AE5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EDE77A29-0840-450C-8B08-2D3388845AE5}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EDE77A29-0840-450C-8B08-2D3388845AE5}.Release|Any CPU.Build.0 = Release|Any CPU {A0F2F006-5AEB-454E-83C5-ABFB58DE17A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A0F2F006-5AEB-454E-83C5-ABFB58DE17A9}.Debug|Any CPU.Build.0 = Debug|Any CPU {A0F2F006-5AEB-454E-83C5-ABFB58DE17A9}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -195,6 +191,10 @@ Global {60B5BD57-1529-4D06-8FAD-0C6427AAF3CA}.Debug|Any CPU.Build.0 = Debug|Any CPU {60B5BD57-1529-4D06-8FAD-0C6427AAF3CA}.Release|Any CPU.ActiveCfg = Release|Any CPU {60B5BD57-1529-4D06-8FAD-0C6427AAF3CA}.Release|Any CPU.Build.0 = Release|Any CPU + {864A21E9-39F1-4B6C-9F95-3347454A7198}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {864A21E9-39F1-4B6C-9F95-3347454A7198}.Debug|Any CPU.Build.0 = Debug|Any CPU + {864A21E9-39F1-4B6C-9F95-3347454A7198}.Release|Any CPU.ActiveCfg = Release|Any CPU + {864A21E9-39F1-4B6C-9F95-3347454A7198}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -203,7 +203,6 @@ Global {1C4CCE9B-761B-4581-B5DA-5B6D83572D56} = {3BD7FF02-8516-4A77-A385-9FDCDD792E22} {AEF1E1E2-C861-4268-86F6-6F376FAF79A7} = {3BD7FF02-8516-4A77-A385-9FDCDD792E22} {E503BFD8-D90A-4610-97C7-5B9A0497303B} = {3BD7FF02-8516-4A77-A385-9FDCDD792E22} - {EDE77A29-0840-450C-8B08-2D3388845AE5} = {3BD7FF02-8516-4A77-A385-9FDCDD792E22} {A0F2F006-5AEB-454E-83C5-ABFB58DE17A9} = {3BD7FF02-8516-4A77-A385-9FDCDD792E22} {E6F61CE8-BFB4-4946-A0D3-AECCE77824E5} = {2459FB59-8A33-49A4-ADBC-A0B12C5886A6} {CCE6AEFB-1AB0-4BD9-8EA2-8B4CDD097E88} = {2459FB59-8A33-49A4-ADBC-A0B12C5886A6} @@ -221,6 +220,7 @@ Global {DDB299EC-6817-495F-8CF0-FBB9408AF6A4} = {7F077BD5-8E4C-402A-9E24-DECAF251A420} {A30A0671-FC46-4BEC-A6F9-DC2400CA7F3A} = {7F077BD5-8E4C-402A-9E24-DECAF251A420} {60B5BD57-1529-4D06-8FAD-0C6427AAF3CA} = {7F077BD5-8E4C-402A-9E24-DECAF251A420} + {864A21E9-39F1-4B6C-9F95-3347454A7198} = {3BD7FF02-8516-4A77-A385-9FDCDD792E22} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {FC0FFCEA-E1EF-4C97-A1C5-F89418B6834B}