فهرست منبع

Plugin-tokenIp

Jeremy 2 سال پیش
والد
کامیت
3ec76e624b
2فایلهای تغییر یافته به همراه99 افزوده شده و 0 حذف شده
  1. 94 0
      src/Plugin/IPAddressPlugin.cs
  2. 5 0
      src/WingServer.cs

+ 94 - 0
src/Plugin/IPAddressPlugin.cs

@@ -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);
+        }
+    }
+
+}

+ 5 - 0
src/WingServer.cs

@@ -44,6 +44,7 @@ namespace WingCloudServer
        
 
         private TokenVerifyPlugin tokenVerifyPlugin;
+        private IPAddressPlugin ipAddressPlugin;
 
     
         
@@ -58,6 +59,7 @@ namespace WingCloudServer
             InitializeServices();
             //按注册顺序执行
             jsonRpcHttpServerEngine.RegisterPlugin(tokenVerifyPlugin);
+            jsonRpcHttpServerEngine.RegisterPlugin(ipAddressPlugin);
             _rpcInProcessServer.Start();
             _rpcHttpServer.Start();
         }
@@ -187,6 +189,9 @@ namespace WingCloudServer
             tokenVerifyPlugin = new TokenVerifyPlugin();
             _rpcHttpServer.RegisterService(typeof(ITokenVerifyPlugin), tokenVerifyPlugin);
             tokenVerifyPlugin.Load(rpcClientPool);
+            ipAddressPlugin = new IPAddressPlugin();
+            _rpcHttpServer.RegisterService(typeof(IIPAddressPlugin), ipAddressPlugin);
+            ipAddressPlugin.Load(rpcClientPool);
  
         }