Sfoglia il codice sorgente

update 增加ip限制。

CrazyIter_Bin 2 anni fa
parent
commit
0fb52c2ce8

+ 1 - 0
TEAMModelOS.SDK/Models/Service/LoginService.cs

@@ -359,6 +359,7 @@ namespace TEAMModelOS.SDK.Models.Service
             }
             string ip = IpPort.Split(":")[0];
             string region = await _searcher.SearchIpAsync(ip);
+            region= region.Replace("中国·", "").Replace("中国","").Replace("台湾省", "台湾");
             return (ip, region);
         }
 

+ 31 - 192
TEAMModelOS/Controllers/System/CoreController.cs

@@ -55,14 +55,41 @@ namespace TEAMModelOS.Controllers
         [HttpPost("sendsms/pin")]
         public async Task<IActionResult> SendSmsPinCode(JsonElement request)
         {
-
+            (string ip, string region) = await LoginService.LoginIp(HttpContext, _searcher);
+            
+            //获取投票活动的选项及投票数
+            string ipkey = $"Ip:Pin:Count:{ip}";
+          
+            bool ipkeyexist = await _azureRedis.GetRedisClient(8).KeyExistsAsync(ipkey);
+            if (ipkeyexist)
+            {
+                await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync(ipkey, ip, 1);
+            }
+            else {
+                await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync(ipkey, ip, 1);
+                var Expire = DateTime.UtcNow.AddSeconds(600);
+                _azureRedis.GetRedisClient(8).KeyExpire(ipkey, Expire);
+            }
+            var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores(ipkey);
+            long sum = 0;
+            if (counts != null && counts.Length > 0)
+            {
+                foreach (var count in counts)
+                {
+                    sum += (int)count.Score;
+                }
+            }
+            int limit = 1000;
+            if (sum > limit) {
+                await  _dingDing.SendBotMsg($"{_option.Location}\nIp:{ip}\n位置:{region}\n 短信验证码10分钟内访问次数超过:{limit}次!",GroupNames.成都开发測試群組);
+                return Ok(new { send = 2 });
+            }
             if (!request.TryGetProperty("area", out JsonElement _area)) return BadRequest();
             if (!request.TryGetProperty("to", out JsonElement _to)) return BadRequest();
             if (!request.TryGetProperty("lang", out JsonElement _lang)) return BadRequest();
             request.TryGetProperty("HasUser", out JsonElement _HasUser);
             string code=$"{_area}{_to}";
             int exp = 120;
-
             string key = $"Random:Code:PinCode-{_area}{_to}";
             bool exist = await _azureRedis.GetRedisClient(8).KeyExistsAsync(key);
             if (!exist)
@@ -89,7 +116,7 @@ namespace TEAMModelOS.Controllers
                 }
                 else
                 {
-                    return BadRequest();
+                   return Ok(new {  send = 0  });
                 }
             }
             else
@@ -134,195 +161,7 @@ namespace TEAMModelOS.Controllers
             }
 
         }
-
-        [HttpPost("sendsms/pin-code")]
-        public async Task<IActionResult> SendSmsPin(JsonElement request) {
-             
-            if (!request.TryGetProperty("area", out JsonElement _area)) return BadRequest();
-            if (!request.TryGetProperty("to", out JsonElement _to)) return BadRequest();
-            if (!request.TryGetProperty("lang", out JsonElement _lang)) return BadRequest();
-            request.TryGetProperty("HasUser", out JsonElement _HasUser);
-            request.TryGetProperty("id_token", out JsonElement id_token);
-            request.TryGetProperty("random", out JsonElement _random);
-            string random;
-            int type  ;//0 idtoken   1 random
-            if (string.IsNullOrWhiteSpace($"{id_token}"))
-            {
-                if (string.IsNullOrWhiteSpace($"{_random}"))
-                {
-                    return BadRequest();
-                }
-                else
-                {
-                    random = $"{_random}";
-                    type = 1;
-                }
-            }
-            else
-            {
-                var jwt = new JwtSecurityToken(id_token.GetString());
-                random = jwt.Payload.Sub;
-                type = 0;
-            }
-
-            string key = $"Random:Code:Pin-{random}";
-            bool exist=  await _azureRedis.GetRedisClient(8).KeyExistsAsync(key);
-            if (!exist)
-            {
-                if (type == 0)
-                {
-                    //不存在则发送请求。
-                    // 如果是idtoken获取的code ,则不存在可以重新发送。
-                    Dictionary<string, object> dict = null; 
-                    if (_HasUser.ValueKind.Equals(JsonValueKind.True) || _HasUser.ValueKind.Equals(JsonValueKind.False))
-                    {
-                        dict=new Dictionary<string, object> { { "country", $"{_area}" }, { "to", $"{_to}" }, { "lang", $"{_lang}" }, { "HasUser", _HasUser } };
-                    }
-                    else
-                    {
-                        dict=new Dictionary<string, object> { { "country", $"{_area}" }, { "to", $"{_to}" }, { "lang", $"{_lang}" } };
-                    }
-                    var httpStatusCode = await _coreAPIHttpService.SendSmsPin(dict, _option.Location, _configuration, _dingDing);
-                    if (httpStatusCode.Equals(HttpStatusCode.OK))
-                    {
-                        var Expire = DateTime.UtcNow.AddSeconds(60);
-                        //send=1 表示已发送
-                        await _azureRedis.GetRedisClient(8).StringSetAsync(key, new { code = random, send = 1, Expire = Expire.Ticks }.ToJsonString());
-                        _azureRedis.GetRedisClient(8).KeyExpire(key, Expire);
-                        return Ok(new { random, send = 1, expire = 60 });
-                    }
-                    else
-                    {
-                        return BadRequest();
-                    }
-                    
-                }
-                else if (type == 1)
-                {
-                    // key不存在,且是随机码类型,则需要重新生成最新的随机码,
-                    random = $"{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
-                    key = $"Random:Code:Pin-{random}";
-                    //最新的保持5分钟
-                    var Expire = DateTime.UtcNow.AddSeconds(60);
-                    await _azureRedis.GetRedisClient(8).StringSetAsync(key, new { code = random, send = 0, Expire = 0 }.ToJsonString());
-                    _azureRedis.GetRedisClient(8).KeyExpire(key, Expire);
-                    return Ok(new { random, send = 0, expire = 0 });
-                }
-                else {
-                    return BadRequest();
-                }
-            }
-            else {
-                //如果是random获取的code ,则随机码匹配且存在才会发送短信。
-                if (type == 1)
-                {
-                    //检查当前key是否已经发送了.
-                    RedisValue value =  await _azureRedis.GetRedisClient(8).StringGetAsync(key);
-                    JsonElement element = value.ToString().ToObject<JsonElement>();
-                    int send = 0;
-                    if (element.TryGetProperty("send", out JsonElement _send)) {
-                        if (_send.ValueKind.Equals(JsonValueKind.Number)) {
-                            if (int.Parse($"{_send}") == 1)
-                            {
-                                send = 1;
-
-                            }
-                            else if (int.Parse($"{_send}") == 0)
-                            {
-                                send=0;
-                            }
-                        }
-                    }
-                    if (send == 1)
-                    {
-                        TimeSpan? timeSpan=  await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync(key);
-                        if (timeSpan != null && timeSpan.HasValue) {
-                            int seconds = timeSpan.Value.Seconds;
-                           return Ok(new { random, send = 1, expire = seconds });
-                        }
-                        else{
-                            random = $"{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
-                            key= $"Random:Code:Pin-{random}";
-                            //最新的保持5分钟
-                            var Expire = DateTime.UtcNow.AddSeconds(60);
-                            await _azureRedis.GetRedisClient(8).StringSetAsync(key, new { code = random, send = 0, Expire = 0 }.ToJsonString());
-                            _azureRedis.GetRedisClient(8).KeyExpire(key, Expire);
-                            return Ok(new { random, send = 0, expire = 0 });
-                        }
-                        
-                    }
-                    else {
-                        Dictionary<string, object> dict = null;
-                        if (_HasUser.ValueKind.Equals(JsonValueKind.True) || _HasUser.ValueKind.Equals(JsonValueKind.False))
-                        {
-                            dict = new Dictionary<string, object> { { "country", $"{_area}" }, { "to", $"{_to}" }, { "lang", $"{_lang}" }, { "HasUser", _HasUser } };
-                        }
-                        else
-                        {
-                            dict = new Dictionary<string, object> { { "country", $"{_area}" }, { "to", $"{_to}" }, { "lang", $"{_lang}" }  };
-                        }
-                        var httpStatusCode = await _coreAPIHttpService.SendSmsPin(dict, _option.Location, _configuration, _dingDing);
-                        if (httpStatusCode.Equals(HttpStatusCode.OK))
-                        {
-                            //最新的保持1分钟
-                            var Expire = DateTime.UtcNow.AddSeconds(60);
-                            await _azureRedis.GetRedisClient(8).StringSetAsync(key, new { code = random, send = 1, Expire = 60 }.ToJsonString());
-                            _azureRedis.GetRedisClient(8).KeyExpire(key, Expire);
-                            return Ok(new { random, send = 1, expire = 60 });
-                        }
-                        else
-                        {
-                            return BadRequest();
-                        }
-                    }
-                    
-                }
-                else if (type == 0)
-                {
-                    //key存在,且是idtoken类型的。
-
-                    //检查当前key是否已经发送了.
-                    RedisValue value = await _azureRedis.GetRedisClient(8).StringGetAsync(key);
-                    JsonElement element = value.ToString().ToObject<JsonElement>();
-                    int send = 0;
-                    if (element.TryGetProperty("send", out JsonElement _send))
-                    {
-                        if (_send.ValueKind.Equals(JsonValueKind.Number))
-                        {
-                            if (int.Parse($"{_send}") == 1)
-                            {
-                                send = 1;
-
-                            }
-                            else if (int.Parse($"{_send}") == 0)
-                            {
-                                send = 0;
-                            }
-                        }
-                    }
-                    if (send == 0)
-                    {
-                        await _azureRedis.GetRedisClient(8).KeyDeleteAsync(key);
-                        return Ok(new { random, send = 0, expire = 0 });
-                    }
-                    else {
-                        TimeSpan? timeSpan = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync(key);
-                        if (timeSpan != null && timeSpan.HasValue)
-                        {
-                            int seconds = timeSpan.Value.Seconds;
-                            return Ok(new { random, send = 1, expire = seconds });
-                        }
-                        else {
-                            return Ok(new { random, send = 0, expire = 0 });
-                        }
-                    }
-                }
-                else {
-                    return BadRequest();
-                }
-            }
-           
-        }
+ 
         [HttpPost("apply-school")]
         public async Task<IActionResult> ApplySchool(ApplySchool request)
         {