fly 2 years ago
parent
commit
b0a166eb69
3 changed files with 96 additions and 16 deletions
  1. 51 0
      src/Plugin/TokenVerifyPluginService.cs
  2. 1 0
      src/WingCloudServer.csproj
  3. 44 16
      src/WingServer.cs

+ 51 - 0
src/Plugin/TokenVerifyPluginService.cs

@@ -0,0 +1,51 @@
+using System.Collections.Generic;
+using JsonRpcLite.Network;
+using JsonRpcLite.Services;
+using JsonRpcLite.Utilities;
+using Newtonsoft.Json;
+using WingInterfaceLibrary.Enum;
+using WingInterfaceLibrary.Interface;
+using WingInterfaceLibrary.Request.Authentication;
+using WingServerCommon.Service;
+
+namespace WingCloudServer.Plugin
+{
+    public interface ITokenVerifyPlugin : IJsonRpcHttpServerEnginePlugin
+    {
+
+    }
+
+    public class TokenVerifyPlugin : JsonRpcService, ITokenVerifyPlugin
+    {
+
+        protected IAuthenticationService _authenticationService;
+
+        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;
+            if (context.GetRequestPath() == "/IUserService" && requests[0].Method == "GetUserInfoAsync")
+            {
+                var tokenRequest = JsonConvert.DeserializeObject<List<ValidateTokenRequest>>(requests[0].Params.Value.ToString().Replace("\r", "").Replace("\n", "").Replace("\t", ""));
+                var result = _authenticationService.ValidateTokenAsync(tokenRequest[0]).Result;
+                if (result.Code != CustomerRpcCode.Ok)
+                {
+                    ThrowRpcException((int)result.Code, "Permission validation error");
+                }
+            }
+            return new PluginProcessResult(requestData, false);
+        }
+
+        public PluginProcessResult PostProcess(IJsonRpcHttpContext context, byte[] responseData)
+        {
+            return new PluginProcessResult(responseData, false);
+        }
+    }
+
+}

+ 1 - 0
src/WingCloudServer.csproj

@@ -18,6 +18,7 @@
     <PackageReference Include="Emgu.CV" Version="4.2.0.3662" />
     <PackageReference Include="MailKit" Version="3.3.0" />
     <PackageReference Include="MimeKit" Version="3.3.0" />
+    <PackageReference Include="JsonRpcLite" Version="1.1.0.4" />
     
     <Reference Include="AI.Common">
       <HintPath>..\AIDiagnosis\AI.Common.dll</HintPath>

+ 44 - 16
src/WingServer.cs

@@ -12,6 +12,15 @@ using WingServerCommon.Config.Parameters;
 using WingInterfaceLibrary.OpLog;
 using WingCloudServer.InteractionCenter;
 using System.Runtime.Loader;
+using JsonRpcLite.Utilities;
+using System.Threading.Tasks;
+using WingInterfaceLibrary.Interface.DBInterface;
+using WingInterfaceLibrary.Interface;
+using Newtonsoft.Json;
+using WingInterfaceLibrary.Request.Authentication;
+using JsonRpcLite.Services;
+using WingInterfaceLibrary.Enum;
+using WingCloudServer.Plugin;
 
 namespace WingCloudServer
 {
@@ -20,22 +29,35 @@ namespace WingCloudServer
         private JsonRpcServer _rpcHttpServer;
         private JsonRpcServer _rpcInProcessServer;
         private JsonRpcInProcessEngine _inProcessEngine;
+
+        private JsonRpcHttpServerEngine jsonRpcHttpServerEngine;
         public WingServer(string host)
         {
             _rpcInProcessServer = new JsonRpcServer();
             _inProcessEngine = new JsonRpcInProcessEngine();
-
             _rpcInProcessServer.UseEngine(_inProcessEngine);
             _rpcHttpServer = new JsonRpcServer();
-            var jsonRpcHttpServerEngine = new JsonRpcHttpServerEngine(host);
+            jsonRpcHttpServerEngine = new JsonRpcHttpServerEngine(host);
             _rpcHttpServer.UseEngine(jsonRpcHttpServerEngine);
         }
+
+       
+
+        private TokenVerifyPlugin tokenVerifyPlugin;
+
+    
+        
+
+ 
+
         /// <summary>
         /// Start server to load all service
         /// </summary>
         internal void Start()
         {
             InitializeServices();
+            //按注册顺序执行
+            jsonRpcHttpServerEngine.RegisterPlugin(tokenVerifyPlugin);
             _rpcInProcessServer.Start();
             _rpcHttpServer.Start();
         }
@@ -54,7 +76,7 @@ namespace WingCloudServer
         {
             //initiaize rpc client pool
             var remoteRpcHttpServices = LoadRemoteServiceConfig();//new RemoteServiceInfo[0]; 
-            var rpcClientPool = new JsonRpcClientPool(_inProcessEngine);            
+            var rpcClientPool = new JsonRpcClientPool(_inProcessEngine);
             rpcClientPool.Initialize(remoteRpcHttpServices.ToArray());
 
             var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
@@ -93,14 +115,14 @@ namespace WingCloudServer
             }
 
             //Rpc http service load and register to rpc http server
-            if(ConfigurationManager.IsDistributed)
+            if (ConfigurationManager.IsDistributed)
             {
-                if(ConfigurationManager.IsMaster)
+                if (ConfigurationManager.IsMaster)
                 {
-                   var masterInteractionCenterService = new MasterInteractionCenterService();
+                    var masterInteractionCenterService = new MasterInteractionCenterService();
                     _rpcHttpServer.RegisterService(typeof(IMasterInteractionCenterService), masterInteractionCenterService);
                     masterInteractionCenterService.Load(rpcClientPool);
-                   
+
                 }
                 else
                 {
@@ -147,7 +169,7 @@ namespace WingCloudServer
             //load service if distrubuted system
             if (ConfigurationManager.IsDistributed)
             {
-                if(ConfigurationManager.IsMaster)
+                if (ConfigurationManager.IsMaster)
                 {
                     //_rpcHttpServer.RegisterService(IMaster); TODO
                 }
@@ -160,6 +182,12 @@ namespace WingCloudServer
             var vinnoServerService = new VinnoServerService();
             _rpcHttpServer.RegisterService(typeof(WingInterfaceLibrary.Interface.IVinnoServerService), vinnoServerService);
             vinnoServerService.Load(rpcClientPool);
+
+            //plugin 
+            tokenVerifyPlugin = new TokenVerifyPlugin();
+            _rpcHttpServer.RegisterService(typeof(ITokenVerifyPlugin), tokenVerifyPlugin);
+            tokenVerifyPlugin.Load(rpcClientPool);
+ 
         }
 
         private Type LoadService(string folder, object serviceName)
@@ -169,7 +197,7 @@ namespace WingCloudServer
             {
                 throw new NotSupportedException($"File {file} not exist");
             }
-            
+
             var assembly = AssemblyLoader.LoadAssemblyDependencies(file);
             var types = assembly.GetTypes();
             var wingServiceType = types.FirstOrDefault(x => typeof(JsonRpcService).IsAssignableFrom(x) && x.IsClass && !x.IsAbstract);
@@ -198,7 +226,7 @@ namespace WingCloudServer
             {
                 throw new NotSupportedException($"File {file} not exist");
             }
-            
+
             var assembly = AssemblyLoader.LoadAssemblyDependencies(file);
             var types = assembly.GetTypes();
             var namespaceStr = $"{defaultInterfaceName}.{defaultInterfaceFolder}";
@@ -235,17 +263,17 @@ namespace WingCloudServer
         private List<RemoteServiceInfo> LoadRemoteServiceConfig()
         {
             List<RemoteServiceInfo> resList = new List<RemoteServiceInfo>();
-            if(ConfigurationManager.IsDistributed)
+            if (ConfigurationManager.IsDistributed)
             {
-                if(ConfigurationManager.IsMaster)
+                if (ConfigurationManager.IsMaster)
                 {
-                   //TODO determin if we need to assess slave server on master server
+                    //TODO determin if we need to assess slave server on master server
                 }
                 else
                 {
-                    var serviceInfo = new RemoteServiceInfo() 
+                    var serviceInfo = new RemoteServiceInfo()
                     {
-                        ServiceName="IMasterInteractionCenterService",
+                        ServiceName = "IMasterInteractionCenterService",
                         Url = ConfigurationManager.MasterUrl
                     };
                     resList.Add(serviceInfo);
@@ -283,7 +311,7 @@ namespace WingCloudServer
             return resList;
         }
 
-        
+
 
         /// <summary>
         /// 初始化AIDiagnosis