@@ -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; | |||
@@ -20,7 +20,7 @@ | |||
<PackageReleaseNotes>基于DotNetty实现的JT808DotNetty的抽象库</PackageReleaseNotes> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="JT808" Version="2.1.7" /> | |||
<PackageReference Include="JT808" Version="2.1.8" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<None Include="..\..\LICENSE" Pack="true" PackagePath="" /> | |||
@@ -11,7 +11,7 @@ | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" /> | |||
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.0" /> | |||
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
@@ -24,7 +24,7 @@ | |||
<PackageReference Include="DotNetty.Handlers" Version="0.6.0" /> | |||
<PackageReference Include="DotNetty.Transport.Libuv" Version="0.6.0" /> | |||
<PackageReference Include="DotNetty.Codecs" Version="0.6.0" /> | |||
<PackageReference Include="JT808" Version="2.1.7" /> | |||
<PackageReference Include="JT808" Version="2.1.8" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.0" /> | |||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> | |||
@@ -34,6 +34,11 @@ namespace JT808.DotNetty.Core.Configurations | |||
/// </summary> | |||
public int WebApiPort { get; set; } = 828; | |||
/// <summary> | |||
/// WebApi 默认token 123456 | |||
/// </summary> | |||
public string WebApiToken { get; set; } = "123456"; | |||
/// <summary> | |||
/// 转发远程地址 (可选项)知道转发的地址有利于提升性能 | |||
/// 按照808的消息,有些请求必须要应答,但是转发可以不需要有应答可以节省部分资源包括: | |||
@@ -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<string>() | |||
{ | |||
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<string>() | |||
@@ -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); | |||
} | |||
} |
@@ -11,5 +11,6 @@ namespace JT808.DotNetty.Core.Interfaces | |||
IJT808NettyBuilder Instance { get; } | |||
IJT808NettyBuilder Builder(); | |||
IJT808WebApiNettyBuilder ReplaceMsgIdHandler<T>() where T : JT808MsgIdHttpHandlerBase; | |||
IJT808WebApiNettyBuilder ReplaceAuthorization<T>() where T : IJT808WebApiAuthorization; | |||
} | |||
} |
@@ -23,6 +23,7 @@ | |||
<ItemGroup> | |||
<PackageReference Include="DotNetty.Handlers" Version="0.6.0" /> | |||
<PackageReference Include="DotNetty.Transport.Libuv" Version="0.6.0" /> | |||
<PackageReference Include="DotNetty.Codecs.Http" Version="0.6.0" /> | |||
<PackageReference Include="DotNetty.Codecs" Version="0.6.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.0" /> | |||
@@ -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")] | |||
@@ -7,7 +7,7 @@ | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" /> | |||
</ItemGroup> | |||
@@ -23,7 +23,7 @@ | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\..\JT808.DotNetty.Abstractions\JT808.DotNetty.Abstractions.csproj" /> | |||
@@ -22,9 +22,9 @@ | |||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<None Include="..\..\..\LICENSE" Pack="true" PackagePath="" /> | |||
@@ -21,9 +21,9 @@ | |||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\..\JT808.DotNetty.Abstractions\JT808.DotNetty.Abstractions.csproj" /> | |||
@@ -22,9 +22,9 @@ | |||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<None Include="..\..\..\LICENSE" Pack="true" PackagePath="" /> | |||
@@ -21,10 +21,10 @@ | |||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" /> | |||
<PackageReference Include="CSRedisCore" Version="3.1.5" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.0" /> | |||
<PackageReference Include="CSRedisCore" Version="3.1.10" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\..\JT808.DotNetty.Abstractions\JT808.DotNetty.Abstractions.csproj" /> | |||
@@ -25,10 +25,10 @@ | |||
<PackageReference Include="DotNetty.Buffers" Version="0.6.0" /> | |||
<PackageReference Include="DotNetty.Handlers" Version="0.6.0" /> | |||
<PackageReference Include="DotNetty.Transport" Version="0.6.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" /> | |||
<PackageReference Include="Polly" Version="7.1.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.0" /> | |||
<PackageReference Include="Polly" Version="7.1.1" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\..\JT808.DotNetty.Abstractions\JT808.DotNetty.Abstractions.csproj" /> | |||
@@ -1,4 +1,4 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<PropertyGroup> | |||
<TargetFramework>netcoreapp3.0</TargetFramework> | |||
@@ -7,12 +7,12 @@ | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> | |||
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" /> | |||
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" /> | |||
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" /> | |||
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
@@ -8,13 +8,14 @@ | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" /> | |||
<PackageReference Include="NLog" Version="4.6.4" /> | |||
<PackageReference Include="NLog.Extensions.Logging" Version="1.5.0" /> | |||
<PackageReference Include="WebApiClient.Extensions.DependencyInjection" Version="1.2.3" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Http" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" /> | |||
<PackageReference Include="NLog" Version="4.6.7" /> | |||
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
@@ -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<JT808MsgIdHttpCustomHandler>() | |||
.Builder(); | |||
//添加kafka插件 | |||
//.AddJT808ServerKafkaMsgProducer(hostContext.Configuration) | |||
//.AddJT808ServerKafkaMsgReplyConsumer(hostContext.Configuration) | |||
//.AddJT808ServerKafkaSessionProducer(hostContext.Configuration) | |||
//.Builder(); | |||
//webapi客户端调用 | |||
//services.AddHttpApi<IJT808DotNettyWebApi>().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<ILoggerFactory>(); | |||
//}); | |||
//var client = services.BuildServiceProvider().GetRequiredService<IJT808DotNettyWebApi>(); | |||
//var result = client.GetTcpAtomicCounter().InvokeAsync().Result; | |||
// c.BaseAddress = new Uri("http://localhost:828/"); | |||
// c.DefaultRequestHeaders.Add("token", "123456); | |||
//}) | |||
//.AddTypedClient<JT808HttpClient>(); | |||
//var client = services.BuildServiceProvider().GetRequiredService<JT808HttpClient>(); | |||
//var result = client.GetTcpAtomicCounter(); | |||
}); | |||
await serverHostBuilder.RunConsoleAsync(); | |||
@@ -7,12 +7,12 @@ | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> | |||
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" /> | |||
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" /> | |||
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" /> | |||
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
@@ -7,12 +7,12 @@ | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" /> | |||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> | |||
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" /> | |||
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" /> | |||
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" /> | |||
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
@@ -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<JT808Configuration>((options)=> { }); | |||
var options=serviceDescriptors.BuildServiceProvider().GetRequiredService<IOptionsMonitor<JT808Configuration>>(); | |||
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<JT808Configuration>((options) => { }); | |||
var options = serviceDescriptors.BuildServiceProvider().GetRequiredService<IOptionsMonitor<JT808Configuration>>(); | |||
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<JT808Configuration>((options) => { }); | |||
var options = serviceDescriptors.BuildServiceProvider().GetRequiredService<IOptionsMonitor<JT808Configuration>>(); | |||
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<JT808Configuration>((options) => { }); | |||
var options = serviceDescriptors.BuildServiceProvider().GetRequiredService<IOptionsMonitor<JT808Configuration>>(); | |||
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)); | |||
} | |||
} | |||
} |
@@ -7,26 +7,17 @@ | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> | |||
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" /> | |||
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" /> | |||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /> | |||
<PackageReference Include="xunit" Version="2.4.0" /> | |||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> | |||
<PackageReference Include="coverlet.collector" Version="1.0.1" /> | |||
<PackageReference Include="RichardSzalay.MockHttp" Version="5.0.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\..\JT808.DotNetty.Tcp\JT808.DotNetty.Tcp.csproj" /> | |||
<ProjectReference Include="..\..\JT808.DotNetty.Udp\JT808.DotNetty.Udp.csproj" /> | |||
<ProjectReference Include="..\..\JT808.DotNetty.WebApiClientTool\JT808.DotNetty.WebApiClientTool.csproj" /> | |||
<ProjectReference Include="..\..\JT808.DotNetty.WebApi\JT808.DotNetty.WebApi.csproj" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<None Update="appsettings.json"> | |||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | |||
</None> | |||
</ItemGroup> | |||
</Project> |
@@ -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<IJT808DotNettyWebApi>().ConfigureHttpApiConfig(c => | |||
{ | |||
c.HttpHost = new Uri("http://127.0.0.1:12828" + JT808NettyConstants.JT808WebApiRouteTable.RouteTablePrefix + "/"); | |||
c.LoggerFactory = new LoggerFactory(); | |||
}); | |||
var api = HttpApi.Resolve<IJT808DotNettyWebApi>(); | |||
} | |||
[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(); | |||
} | |||
} | |||
} |
@@ -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<List<JT808TcpSessionInfoDto>> jT808ResultDto = new JT808ResultDto<List<JT808TcpSessionInfoDto>>(); | |||
jT808ResultDto.Data = new List<JT808TcpSessionInfoDto>(); | |||
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<List<JT808TcpSessionInfoDto>> jT808ResultDto = new JT808ResultDto<List<JT808TcpSessionInfoDto>>(); | |||
jT808ResultDto.Data = new List<JT808TcpSessionInfoDto>(); | |||
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<bool> jT808ResultDto = new JT808ResultDto<bool>(); | |||
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<bool> jT808ResultDto = new JT808ResultDto<bool>(); | |||
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<JT808AtomicCounterDto> jT808ResultDto = new JT808ResultDto<JT808AtomicCounterDto>(); | |||
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<JT808AtomicCounterDto> jT808ResultDto = new JT808ResultDto<JT808AtomicCounterDto>(); | |||
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<List<JT808UdpSessionInfoDto>> jT808ResultDto = new JT808ResultDto<List<JT808UdpSessionInfoDto>>(); | |||
jT808ResultDto.Data = new List<JT808UdpSessionInfoDto>(); | |||
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); | |||
} | |||
} | |||
} |
@@ -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<JT808DotNettyWebApiOptions>(hostContext.Configuration.GetSection("JT808DotNettyWebApiOptions")); | |||
services.AddSingleton<ILoggerFactory, LoggerFactory>(); | |||
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<IJT808Config>().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(); | |||
} | |||
} | |||
} |
@@ -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" ] | |||
} | |||
} |
@@ -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<JT808Configuration> optionsMonitor; | |||
public JT808AuthorizationDefault(IOptionsMonitor<JT808Configuration> 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; | |||
} | |||
} | |||
} | |||
} |
@@ -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<JT808WebAPIServerHandler> logger; | |||
private readonly IJT808WebApiAuthorization jT808WebApiAuthorization; | |||
public JT808WebAPIServerHandler( | |||
IJT808WebApiAuthorization jT808WebApiAuthorization, | |||
JT808MsgIdHttpHandlerBase jT808MsgIdHttpHandlerBase, | |||
ILoggerFactory loggerFactory) | |||
{ | |||
this.jT808WebApiAuthorization = jT808WebApiAuthorization; | |||
this.jT808MsgIdHttpHandlerBase = jT808MsgIdHttpHandlerBase; | |||
logger = loggerFactory.CreateLogger<JT808WebAPIServerHandler>(); | |||
} | |||
@@ -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 | |||
{ | |||
@@ -27,6 +27,7 @@ | |||
<PackageReference Include="DotNetty.Transport.Libuv" Version="0.6.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options" Version="3.0.0" /> | |||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.0.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\JT808.DotNetty.Core\JT808.DotNetty.Core.csproj" /> | |||
@@ -22,6 +22,12 @@ namespace JT808.DotNetty.WebApi | |||
return Instance; | |||
} | |||
public IJT808WebApiNettyBuilder ReplaceAuthorization<T>() where T : IJT808WebApiAuthorization | |||
{ | |||
Instance.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(IJT808WebApiAuthorization), typeof(T), ServiceLifetime.Singleton)); | |||
return this; | |||
} | |||
public IJT808WebApiNettyBuilder ReplaceMsgIdHandler<T>() where T : JT808MsgIdHttpHandlerBase | |||
{ | |||
Instance.JT808Builder.Services.Replace(new ServiceDescriptor(typeof(JT808MsgIdHttpHandlerBase), typeof(T), ServiceLifetime.Singleton)); | |||
@@ -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<JT808MsgIdHttpHandlerBase, JT808MsgIdDefaultWebApiHandler>(); | |||
jT808NettyBuilder.JT808Builder.Services.TryAddSingleton<IJT808WebApiAuthorization, JT808AuthorizationDefault>(); | |||
jT808NettyBuilder.JT808Builder.Services.TryAddScoped<JT808WebAPIServerHandler>(); | |||
jT808NettyBuilder.JT808Builder.Services.AddHostedService<JT808WebAPIServerHost>(); | |||
return new JT808WebApiBuilderDefault(jT808NettyBuilder); | |||
@@ -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 | |||
/// <summary> | |||
/// 会话服务集合 | |||
/// </summary> | |||
/// <returns></returns> | |||
[HttpGet("Tcp/Session/GetAll")] | |||
ITask<JT808ResultDto<List<JT808TcpSessionInfoDto>>> GetTcpSessionAll(); | |||
/// <summary> | |||
/// 会话服务-通过设备终端号移除对应会话 | |||
/// </summary> | |||
/// <param name="terminalPhoneNo"></param> | |||
/// <returns></returns> | |||
[HttpPost("Tcp/Session/RemoveByTerminalPhoneNo")] | |||
ITask<JT808ResultDto<bool>> RemoveTcpSessionByTerminalPhoneNo([JsonContent] string terminalPhoneNo); | |||
/// <summary> | |||
/// 统一下发信息 | |||
/// </summary> | |||
/// <param name="jT808UnificationSendRequestDto"></param> | |||
/// <returns></returns> | |||
[HttpPost("/UnificationSend")] | |||
ITask<JT808ResultDto<bool>> UnificationSend([JsonContent]JT808UnificationSendRequestDto jT808UnificationSendRequestDto); | |||
/// <summary> | |||
/// 获取Tcp包计数器 | |||
/// </summary> | |||
/// <param name="uri"></param> | |||
/// <returns></returns> | |||
[HttpGet("Tcp/GetAtomicCounter")] | |||
ITask<JT808ResultDto<JT808AtomicCounterDto>> GetTcpAtomicCounter(); | |||
#endregion | |||
#region 基于Udp WebApi | |||
/// <summary> | |||
/// 会话服务集合 | |||
/// </summary> | |||
/// <param name="uri"></param> | |||
/// <returns></returns> | |||
[HttpGet("Udp/Session/GetAll")] | |||
ITask<JT808ResultDto<List<JT808UdpSessionInfoDto>>> GetUdpSessionAll(); | |||
/// <summary> | |||
/// 会话服务-通过设备终端号移除对应会话 | |||
/// </summary> | |||
/// <param name="terminalPhoneNo"></param> | |||
/// <returns></returns> | |||
[HttpPost("Udp/Session/RemoveByTerminalPhoneNo")] | |||
ITask<JT808ResultDto<bool>> RemoveUdpSessionByTerminalPhoneNo([JsonContent] string terminalPhoneNo); | |||
/// <summary> | |||
/// 获取Udp包计数器 | |||
/// </summary> | |||
/// <returns></returns> | |||
[HttpGet("Udp/GetAtomicCounter")] | |||
ITask<JT808ResultDto<JT808AtomicCounterDto>> GetUdpAtomicCounter(); | |||
#endregion | |||
} | |||
} |
@@ -19,9 +19,12 @@ | |||
<Description>JT808DotNetty的WebApiClient客户端调用工具</Description> | |||
<PackageReleaseNotes>JT808DotNetty的WebApiClient客户端调用工具</PackageReleaseNotes> | |||
</PropertyGroup> | |||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> | |||
<DocumentationFile>JT808.DotNetty.WebApiClientTool.xml</DocumentationFile> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<PackageReference Include="WebApiClient.JIT" Version="1.1.0" /> | |||
<PackageReference Include="System.Text.Json" Version="4.6.0" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<None Include="..\..\LICENSE" Pack="true" PackagePath="" /> | |||
@@ -1,15 +0,0 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
namespace JT808.DotNetty.WebApiClientTool | |||
{ | |||
public class JT808DotNettyWebApiOptions | |||
{ | |||
/// <summary> | |||
/// 接口服务地址 | |||
/// </summary> | |||
public List<string> WebApiHosts { get; set; } | |||
} | |||
} |
@@ -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; | |||
} | |||
/// <summary> | |||
/// 会话服务集合 | |||
/// </summary> | |||
/// <returns></returns> | |||
public JT808ResultDto<List<JT808TcpSessionInfoDto>> GetTcpSessionAll() | |||
{ | |||
var request = new HttpRequestMessage(HttpMethod.Get, JT808NettyConstants.JT808WebApiRouteTable.SessionTcpGetAll); | |||
var response = HttpClient.SendAsync(request).Result; | |||
response.EnsureSuccessStatusCode(); | |||
var value = JsonSerializer.Deserialize<JT808ResultDto<List<JT808TcpSessionInfoDto>>>(response.Content.ReadAsByteArrayAsync().Result); | |||
return value; | |||
} | |||
/// <summary> | |||
/// 会话服务-通过设备终端号移除对应会话 | |||
/// </summary> | |||
/// <param name="terminalPhoneNo"></param> | |||
/// <returns></returns> | |||
public JT808ResultDto<bool> 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<JT808ResultDto<bool>>(response.Content.ReadAsByteArrayAsync().Result); | |||
return value; | |||
} | |||
/// <summary> | |||
/// 统一下发信息 | |||
/// </summary> | |||
/// <param name="jT808UnificationSendRequestDto"></param> | |||
/// <returns></returns> | |||
public JT808ResultDto<bool> 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<JT808ResultDto<bool>>(response.Content.ReadAsByteArrayAsync().Result); | |||
return value; | |||
} | |||
/// <summary> | |||
/// 获取Tcp包计数器 | |||
/// </summary> | |||
/// <returns></returns> | |||
public JT808ResultDto<JT808AtomicCounterDto> GetTcpAtomicCounter() | |||
{ | |||
var request = new HttpRequestMessage(HttpMethod.Get, JT808NettyConstants.JT808WebApiRouteTable.GetTcpAtomicCounter); | |||
var response = HttpClient.SendAsync(request).Result; | |||
response.EnsureSuccessStatusCode(); | |||
var value = JsonSerializer.Deserialize<JT808ResultDto<JT808AtomicCounterDto>>(response.Content.ReadAsByteArrayAsync().Result); | |||
return value; | |||
} | |||
/// <summary> | |||
/// 会话服务集合 | |||
/// </summary> | |||
/// <returns></returns> | |||
public JT808ResultDto<List<JT808UdpSessionInfoDto>> GetUdpSessionAll() | |||
{ | |||
var request = new HttpRequestMessage(HttpMethod.Get, JT808NettyConstants.JT808WebApiRouteTable.SessionUdpGetAll); | |||
var response = HttpClient.SendAsync(request).Result; | |||
response.EnsureSuccessStatusCode(); | |||
var value = JsonSerializer.Deserialize<JT808ResultDto<List<JT808UdpSessionInfoDto>>>(response.Content.ReadAsByteArrayAsync().Result); | |||
return value; | |||
} | |||
/// <summary> | |||
/// 获取Udp包计数器 | |||
/// </summary> | |||
/// <returns></returns> | |||
public JT808ResultDto<JT808AtomicCounterDto> GetUdpAtomicCounter() | |||
{ | |||
var request = new HttpRequestMessage(HttpMethod.Get, JT808NettyConstants.JT808WebApiRouteTable.GetUdpAtomicCounter); | |||
var response = HttpClient.SendAsync(request).Result; | |||
response.EnsureSuccessStatusCode(); | |||
var value = JsonSerializer.Deserialize<JT808ResultDto<JT808AtomicCounterDto>>(response.Content.ReadAsByteArrayAsync().Result); | |||
return value; | |||
} | |||
} | |||
} |
@@ -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} | |||