ntminer 5 years ago
parent
commit
089254e15b

+ 14 - 36
src/WebApiServer/Controllers/ApiControllerBase.cs

@@ -5,48 +5,32 @@ using System.Web.Http;
 
 namespace NTMiner.Controllers {
     public abstract class ApiControllerBase : ApiController {
-        public class ClientSignData {
-            public ClientSignData(string loginName, string sign, long timestamp) {
-                this.LoginName = loginName;
-                this.Sign = sign;
-                this.Timestamp = timestamp;
-            }
-
-            public string LoginName { get; private set; }
-            public string Sign { get; private set; }
-            public long Timestamp { get; private set; }
-
-            private UserId _userId;
-            public UserId UserId {
-                get {
-                    if (_userId == null) {
-                        _userId = UserId.Create(this.LoginName);
-                    }
-                    return _userId;
-                }
+        protected new UserData User {
+            get {
+                // 在UserAttribute ActionFilter里赋值的
+                return (UserData)ControllerContext.RouteData.Values["_user"];
             }
         }
 
-        private string _minerIp = null;
-        protected string MinerIp {
+        private string _remoteIp = null;
+        protected string RemoteIp {
             get {
-                if (_minerIp == null) {
-                    _minerIp = GetWebClientIp();
-                    if (_minerIp == null) {
-                        _minerIp = string.Empty;
+                if (_remoteIp == null) {
+                    _remoteIp = GetWebClientIp();
+                    if (_remoteIp == null) {
+                        _remoteIp = string.Empty;
                     }
                 }
-                return _minerIp;
+                return _remoteIp;
             }
         }
 
         private string GetWebClientIp() {
             string ip;
-            if (Request.Properties.ContainsKey("MS_HttpContext")) {
-                ip = ((HttpContextWrapper)Request.Properties["MS_HttpContext"]).Request.UserHostAddress;
+            if (Request.Properties.TryGetValue("MS_HttpContext", out object obj) && obj is HttpContextWrapper context) {
+                ip = context.Request.UserHostAddress;
             }
-            else if (Request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name)) {
-                RemoteEndpointMessageProperty prop = (RemoteEndpointMessageProperty)Request.Properties[RemoteEndpointMessageProperty.Name];
+            else if (Request.Properties.TryGetValue(RemoteEndpointMessageProperty.Name, out obj) && obj is RemoteEndpointMessageProperty prop) {
                 ip = prop.Address;
             }
             else if (HttpContext.Current != null) {
@@ -60,11 +44,5 @@ namespace NTMiner.Controllers {
             }
             return ip;
         }
-
-        protected new UserData User {
-            get {
-                return (UserData)ControllerContext.RouteData.Values["_user"];
-            }
-        }
     }
 }

+ 2 - 2
src/WebApiServer/Controllers/CaptchaController.cs

@@ -27,7 +27,7 @@ namespace NTMiner.Controllers {
             if (id == Guid.Empty) {
                 bytes = CreateValidateGraphic("无效");
             }
-            else if (WebApiRoot.CaptchaSet.CountByIp(base.MinerIp) > 100) {
+            else if (WebApiRoot.CaptchaSet.CountByIp(base.RemoteIp) > 100) {
                 bytes = CreateValidateGraphic("拒绝");
             }
             else {
@@ -36,7 +36,7 @@ namespace NTMiner.Controllers {
                     Id = id,
                     Code = code,
                     CreatedOn = DateTime.Now,
-                    Ip = base.MinerIp
+                    Ip = base.RemoteIp
                 })) {
                     bytes = CreateValidateGraphic("无效");
                 }

+ 1 - 1
src/WebApiServer/Controllers/ReportBinaryController.cs

@@ -7,7 +7,7 @@ namespace NTMiner.Controllers {
         public ReportResponse ReportSpeed() {
             byte[] bytes = Request.Content.ReadAsByteArrayAsync().Result;
             SpeedDto speedDto = VirtualRoot.BinarySerializer.Deserialize<SpeedDto>(bytes);
-            return ReportController.DoReportSpeed(speedDto, MinerIp);
+            return ReportController.DoReportSpeed(speedDto, RemoteIp);
         }
     }
 }

+ 2 - 2
src/WebApiServer/Controllers/ReportController.cs

@@ -32,13 +32,13 @@ namespace NTMiner.Controllers {
 
         [HttpPost]
         public ReportResponse ReportSpeed([FromBody]SpeedDto speedDto) {
-            return DoReportSpeed(speedDto, MinerIp);
+            return DoReportSpeed(speedDto, RemoteIp);
         }
 
         [HttpPost]
         public void ReportState([FromBody]ReportState request) {
             try {
-                WebApiRoot.ClientDataSet.ReportState(request, MinerIp, isFromWsServerNode: false);
+                WebApiRoot.ClientDataSet.ReportState(request, RemoteIp, isFromWsServerNode: false);
             }
             catch (Exception e) {
                 Logger.ErrorDebugLine(e);

+ 2 - 2
src/WebApiServer/Controllers/UserController.cs

@@ -195,7 +195,7 @@ namespace NTMiner.Controllers {
             }
             if (request.ActionCaptchaId == Guid.Empty
                 || string.IsNullOrEmpty(request.ActionCaptcha)
-                || !WebApiRoot.CaptchaSet.IsValid(request.ActionCaptchaId, base.MinerIp, request.ActionCaptcha)) {
+                || !WebApiRoot.CaptchaSet.IsValid(request.ActionCaptchaId, base.RemoteIp, request.ActionCaptcha)) {
                 return ResponseBase.InvalidInput("验证码错误");
             }
             if (!WebApiRoot.UserSet.IsReadied) {
@@ -230,7 +230,7 @@ namespace NTMiner.Controllers {
             }
             if (request.Data.ActionCaptchaId == Guid.Empty
                 || string.IsNullOrEmpty(request.Data.ActionCaptcha)
-                || !WebApiRoot.CaptchaSet.IsValid(request.Data.ActionCaptchaId, base.MinerIp, request.Data.ActionCaptcha)) {
+                || !WebApiRoot.CaptchaSet.IsValid(request.Data.ActionCaptchaId, base.RemoteIp, request.Data.ActionCaptcha)) {
                 return ResponseBase.InvalidInput("验证码错误");
             }
 

+ 25 - 0
src/WebApiServer/Role/ClientSignData.cs

@@ -0,0 +1,25 @@
+using NTMiner.User;
+
+namespace NTMiner.Role {
+    public class ClientSignData {
+        public ClientSignData(string loginName, string sign, long timestamp) {
+            this.LoginName = loginName;
+            this.Sign = sign;
+            this.Timestamp = timestamp;
+        }
+
+        public string LoginName { get; private set; }
+        public string Sign { get; private set; }
+        public long Timestamp { get; private set; }
+
+        private UserId _userId;
+        public UserId UserId {
+            get {
+                if (_userId == null) {
+                    _userId = UserId.Create(this.LoginName);
+                }
+                return _userId;
+            }
+        }
+    }
+}

+ 0 - 2
src/WebApiServer/Role/UserAttribute.cs

@@ -10,8 +10,6 @@ using System.Text;
 using System.Web;
 using System.Web.Http.Controllers;
 using System.Web.Http.Filters;
-using System.Web.Http.Routing;
-using static NTMiner.Controllers.ApiControllerBase;
 
 namespace NTMiner.Role {
     [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]

+ 1 - 0
src/WebApiServer/WebApiServer.csproj

@@ -100,6 +100,7 @@
     <Compile Include="Core\IGpuNameSet.cs" />
     <Compile Include="Core\Impl\GpuNameSet.cs" />
     <Compile Include="Role\AdminAttribute.cs" />
+    <Compile Include="Role\ClientSignData.cs" />
     <Compile Include="Role\UserAttribute.cs" />
     <Compile Include="Controllers\ApiControllerBase.cs" />
     <Compile Include="Controllers\AppSettingController.cs" />