|
@@ -0,0 +1,94 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Threading;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using JsonRpcLite.Network;
|
|
|
+using JsonRpcLite.Services;
|
|
|
+using JsonRpcLite.Utilities;
|
|
|
+using Newtonsoft.Json;
|
|
|
+using WingInterfaceLibrary.DTO.User;
|
|
|
+using WingInterfaceLibrary.Enum;
|
|
|
+using WingInterfaceLibrary.Interface;
|
|
|
+using WingInterfaceLibrary.Request.Authentication;
|
|
|
+using WingInterfaceLibrary.Result.Device;
|
|
|
+using WingServerCommon.Log;
|
|
|
+using WingServerCommon.Service;
|
|
|
+
|
|
|
+namespace WingCloudServer.Plugin
|
|
|
+{
|
|
|
+ public interface IIPAddressPlugin : IJsonRpcHttpServerEnginePlugin
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public class IPAddressPlugin : JsonRpcService, IIPAddressPlugin
|
|
|
+ {
|
|
|
+ protected IAuthenticationService _authenticationService;
|
|
|
+ private string _apiName = string.Empty;
|
|
|
+
|
|
|
+ public override void Load(JsonRpcClientPool jsonRpcClientPool)
|
|
|
+ {
|
|
|
+ base.Load(jsonRpcClientPool);
|
|
|
+ _authenticationService = GetProxy<IAuthenticationService>();
|
|
|
+ }
|
|
|
+
|
|
|
+ public PluginProcessResult PreProcess(IJsonRpcHttpContext context, byte[] requestData)
|
|
|
+ {
|
|
|
+ var dataLength = (int)context.GetRequestContentLength();
|
|
|
+ var requests = JsonRpcCodec.DecodeRequestsAsync(requestData, new System.Threading.CancellationToken(), dataLength).Result;
|
|
|
+ _apiName = $"{context.GetRequestPath().Trim('/')}/{requests[0].Method.Trim('/')}";
|
|
|
+ return new PluginProcessResult(requestData, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public PluginProcessResult PostProcess(IJsonRpcHttpContext context, byte[] responseData)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var ipAddress = context.RemoteEndPoint?.Address.ToString();
|
|
|
+ var responseDatas = JsonRpcCodec.DecodeResponsesAsync(responseData, new System.Threading.CancellationToken()).Result;
|
|
|
+ var resultString = responseDatas[0].Result.ToString();
|
|
|
+ if (!string.IsNullOrWhiteSpace(ipAddress) && !string.IsNullOrWhiteSpace(resultString))
|
|
|
+ {
|
|
|
+ var token = string.Empty;
|
|
|
+ switch (_apiName)
|
|
|
+ {
|
|
|
+ case "IManagementService/AdminLogin":
|
|
|
+ token = JsonConvert.DeserializeObject<string>(resultString);
|
|
|
+ break;
|
|
|
+ case "ILoginService/CommonLoginAsync":
|
|
|
+ token = JsonConvert.DeserializeObject<UserTokenDTO>(resultString).Token;
|
|
|
+ break;
|
|
|
+ case "IConnectService/ConnectAsync":
|
|
|
+ token = JsonConvert.DeserializeObject<ConnectResult>(resultString).Token;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (!string.IsNullOrWhiteSpace(token))
|
|
|
+ {
|
|
|
+ Logger.WriteLineInfo($"IPAddressPlugin api name:{_apiName}, ip address:{ipAddress}, token:{token}");
|
|
|
+ // Task.Run(async () =>
|
|
|
+ // {
|
|
|
+ // await Task.Delay(100);
|
|
|
+ // await _authenticationService.SetIPAddressAsync(new SetIPAddressRequest
|
|
|
+ // {
|
|
|
+ // Token = token,
|
|
|
+ // IPAddress = ipAddress,
|
|
|
+ // });
|
|
|
+ // });
|
|
|
+ Thread.Sleep(100);
|
|
|
+ var setIpAddressResult = _authenticationService.SetIPAddressAsync(new SetIPAddressRequest
|
|
|
+ {
|
|
|
+ Token = token,
|
|
|
+ IPAddress = ipAddress,
|
|
|
+ }).Result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineInfo($"IPAddressPlugin failed, api name:{_apiName}, ex:{ex}");
|
|
|
+ }
|
|
|
+ return new PluginProcessResult(responseData, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|