瀏覽代碼

1.增加注入访问ip作为传入参数给业务后台
2.增加文件访问权限判断与文件缓存更新机制

linhong 3 年之前
父節點
當前提交
aac5c16c30
共有 6 個文件被更改,包括 86 次插入22 次删除
  1. 21 0
      Controllers/L1SvrController.cs
  2. 3 0
      LJLib.Net.SPI.Com/LJResponse.cs
  3. 3 0
      Models/GetFile.cs
  4. 49 19
      Providers/L1SvrFileInfo.cs
  5. 7 2
      Providers/L1SvrFileProvider.cs
  6. 3 1
      Startup.cs

+ 21 - 0
Controllers/L1SvrController.cs

@@ -31,11 +31,26 @@ namespace LJProxy.Controllers
             _ljClient = ljClient;
         }
 
+        private string getIpAddress()
+        {
+            var ipAddress = Request.HttpContext.GetServerVariable("HTTP_X_FORWARDED_FOR");
+            if (!string.IsNullOrEmpty(ipAddress))
+            {
+                string[] addresses = ipAddress.Split(',');
+                if (addresses.Length > 0)
+                {
+                    return addresses[0];
+                }
+            }
+            return Request.HttpContext.GetServerVariable("REMOTE_ADDR");
+        }
+
         [Route("svr/{apiName}")]
         [HttpPost]
         [HttpGet]
         public async Task<IActionResult> Svr(string apiName)
         {
+            var ipAddress = getIpAddress();
             string requestBody;
             using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
             {
@@ -51,6 +66,10 @@ namespace LJProxy.Controllers
             {
                 var reqObj = JObject.Parse(requestBody);
                 reqObj["gateway"] = gateway;
+                if (!string.IsNullOrEmpty(ipAddress))
+                {
+                    reqObj["ip"] = ipAddress;
+                }
                 requestBody = JsonConvert.SerializeObject(reqObj);
             }
             var rslt = _ljClient.Pool.DoExcute(apiName, requestBody);
@@ -96,5 +115,7 @@ namespace LJProxy.Controllers
             }
             
         }
+
+
     }
 }

+ 3 - 0
LJLib.Net.SPI.Com/LJResponse.cs

@@ -16,5 +16,8 @@ namespace LJLib.Net.SPI.Com
         /// </summary>
         public string DebugMsg { get; set; }
 
+        public string AppErrCode { get; set; }
+        public string AppErrMsg { get; set; }
+
     }
 }

+ 3 - 0
Models/GetFile.cs

@@ -9,6 +9,9 @@ namespace LJProxy.Models
     public class GetFileRequest : LJRequest
     {
         public string Filepath { get; set; }
+        public string token { get; set; }
+        public bool checkAuthorization { get; set; }
+        public DateTime? lastWriteTime { get; set; }
     }
 
     public class GetFileResponse : LJResponse

+ 49 - 19
Providers/L1SvrFileInfo.cs

@@ -15,18 +15,22 @@ namespace LJProxy.Providers
         private string _fullFilepath;
         private string _subpath;
         private FileInfo _file;
-        private Stream _stream;
+        private FileInfo _tempFile;
+        private MemoryStream _stream;
         private ILJClient _ljPool;
         private static object _syncRoot = new object();
-        public L1SvrFileInfo(string fullFilepath, string subpath, ILJClient ljPool)
+        private string _token;
+        public L1SvrFileInfo(string fullFilepath, string subpath, ILJClient ljPool,string token)
         {
             _fullFilepath = fullFilepath;
             _subpath = subpath;
             _ljPool = ljPool;
+            _token = token;
+            _stream = new MemoryStream(); ;
             if (File.Exists(_fullFilepath))
             {
-                _file = new FileInfo(_fullFilepath);
-                _stream = _file.OpenRead();
+                _tempFile = new FileInfo(_fullFilepath);
+                CheckAuthorizationOrGetRemoteFile(_tempFile.LastWriteTime, true);
             }
             else
             {
@@ -34,17 +38,12 @@ namespace LJProxy.Providers
                 {
                     if (File.Exists(_fullFilepath))
                     {
-                        _file = new FileInfo(_fullFilepath);
-                        _stream = _file.OpenRead();
+                        _tempFile = new FileInfo(_fullFilepath);
+                        CheckAuthorizationOrGetRemoteFile(_tempFile.LastWriteTime,true);
                     }
                     else
                     {
-                        GetRemoteFile();
-                        if (File.Exists(_fullFilepath))
-                        {
-                            _file = new FileInfo(_fullFilepath);
-                            _stream = _file.OpenRead();
-                        }
+                        CheckAuthorizationOrGetRemoteFile(null);
                     }
 
                 }
@@ -67,21 +66,52 @@ namespace LJProxy.Providers
             return _stream;
         }
 
-        public void GetRemoteFile()
+        public void CheckAuthorizationOrGetRemoteFile(DateTime? lastWriteTime, bool checkAuthorization=false)
         {
             var getFileReq = new GetFileRequest() {
-                Filepath = _subpath
+                Filepath = _subpath,
+                token = _token,
+                checkAuthorization = checkAuthorization,
+                lastWriteTime = lastWriteTime
             };
             var responseStr = _ljPool.DoExcute(getFileReq.GetApiName(),JsonConvert.SerializeObject(getFileReq));
             var response = JsonConvert.DeserializeObject<GetFileResponse>(responseStr);
-            if (string.IsNullOrEmpty(response.ErrMsg))
+            if (response.AppErrCode == "900")
             {
-                var fileDir = Path.GetDirectoryName(_fullFilepath);
-                if (!Directory.Exists(fileDir)){
-                    Directory.CreateDirectory(fileDir);
+                if (!checkAuthorization)
+                {
+                    var fileDir = Path.GetDirectoryName(_fullFilepath);
+                    if (!Directory.Exists(fileDir))
+                    {
+                        Directory.CreateDirectory(fileDir);
+                    }
+                    File.WriteAllBytes(_fullFilepath, response.FileData);
                 }
-                File.WriteAllBytes(_fullFilepath, response.FileData);
+                else
+                {
+                    if(response.FileData!=null && response.FileData.Length > 0)
+                    {
+                        var fileDir = Path.GetDirectoryName(_fullFilepath);
+                        if (!Directory.Exists(fileDir))
+                        {
+                            Directory.CreateDirectory(fileDir);
+                        }
+                        File.WriteAllBytes(_fullFilepath, response.FileData);
+                    }
+                }
+                _file = new FileInfo(_fullFilepath);
+                byte[] buff = new byte[1024];
+                using(var fs = _file.OpenRead())
+                {
+                    int readCount = fs.Read(buff, 0, buff.Length);
+                    while (readCount > 0)
+                    {
+                        _stream.Write(buff, 0, readCount);
+                        readCount = fs.Read(buff, 0, buff.Length);
+                    }
+                }                
             }
         }
+
     }
 }

+ 7 - 2
Providers/L1SvrFileProvider.cs

@@ -1,4 +1,5 @@
 using LJProxy.Services;
+using Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.FileProviders;
 using Microsoft.Extensions.Primitives;
 using System;
@@ -12,10 +13,13 @@ namespace LJProxy.Providers
     {
         private string _contentRoot;
         private LJClientPoolService _ljClient;
-        public L1SvrFileProvider(string contentRoot, LJClientPoolService ljClient)
+        private string _token;
+        IHttpContextAccessor _accessor;
+        public L1SvrFileProvider(string contentRoot, LJClientPoolService ljClient,IHttpContextAccessor accessor)
         {
             _contentRoot = contentRoot;
             _ljClient = ljClient;
+            _accessor = accessor;
         }
 
         public IDirectoryContents GetDirectoryContents(string subpath)
@@ -28,7 +32,8 @@ namespace LJProxy.Providers
             string physicSubpath = subpath.Replace("/","\\");
             string directory = @$"{_contentRoot}\wwwroot";
             var fullFilepath = @$"{directory}{physicSubpath}";
-            var result = new L1SvrFileInfo(fullFilepath, physicSubpath,_ljClient.Pool);
+            _token = _accessor.HttpContext.Request.Headers["Authorization"];
+            var result = new L1SvrFileInfo(fullFilepath, physicSubpath,_ljClient.Pool,_token);
             return result;
         }
 

+ 3 - 1
Startup.cs

@@ -3,6 +3,7 @@ using LJProxy.Services;
 using LJProxy.Settings;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Http.Features;
 using Microsoft.AspNetCore.HttpsPolicy;
 using Microsoft.AspNetCore.Mvc;
@@ -43,6 +44,7 @@ namespace LJProxy
             //    opt.MultipartBodyLengthLimit = 20* 1024 * 1024;
             //    opt.MultipartHeadersCountLimit = 5;
             });
+            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -68,7 +70,7 @@ namespace LJProxy
             var provider = new FileExtensionContentTypeProvider();
             provider.Mappings[".apk"] = "application/vnd.android.package-archive";
             app.UseStaticFiles(new StaticFileOptions { 
-                FileProvider = new L1SvrFileProvider(env.ContentRootPath,app.ApplicationServices.GetService<LJClientPoolService>()),
+                FileProvider = new L1SvrFileProvider(env.ContentRootPath,app.ApplicationServices.GetService<LJClientPoolService>(),app.ApplicationServices.GetService<IHttpContextAccessor>()),
                 ContentTypeProvider = provider
             });
         }