lock_account.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # coding:utf-8
  2. from django.core.cache import cache
  3. import common.error_info as cei
  4. MAX_ERROR_TIMES = 5
  5. MAX_ERROR_TIMES_IP = 20
  6. LOCK_IP_TMP = 'LOCK_IP_{}'
  7. LOCK_ACCOUNT_TMP = 'LOCK_ACCOUNT_{}'
  8. ACCOUNT_INCR_TMP = 'ACCOUNT_COUNT_{}'
  9. IP_INCR_TMP = 'IP_COUNT_{}'
  10. def is_lock(uid, ip):
  11. """
  12. 是否锁
  13. :param uid:
  14. :param ip:
  15. :return:
  16. """
  17. account_key = LOCK_ACCOUNT_TMP.format(uid)
  18. ip_key = LOCK_IP_TMP.format(ip)
  19. if cache.get(ip_key):
  20. return "ip_lock"
  21. if cache.get(account_key):
  22. return "account_lock"
  23. return False
  24. def is_lock_ip(ip):
  25. """是否封禁IP
  26. """
  27. ip_key = LOCK_IP_TMP.format(ip)
  28. if cache.get(ip_key):
  29. return True
  30. return False
  31. def increase_error_count(uid, ip):
  32. """
  33. 5分钟内连续输错五次
  34. :return:
  35. """
  36. ip_key = IP_INCR_TMP.format(ip)
  37. ip_count = cache.get(ip_key) or 0
  38. account_key = ACCOUNT_INCR_TMP.format(uid)
  39. account_count = cache.get(account_key) or 0
  40. if account_count + 1 >= MAX_ERROR_TIMES or ip_count + 1 >= MAX_ERROR_TIMES:
  41. lock(uid, ip)
  42. raise cei.TipException(u'密码连续输错五次,锁定ip和账户半个小时')
  43. if not account_count:
  44. cache.set(account_key, 1, 5*60)
  45. else:
  46. cache.incr(account_key)
  47. if not ip_count:
  48. cache.set(ip_count, 1, 5*60)
  49. else:
  50. cache.incr(ip_key)
  51. def increase_error_count_ip(ip):
  52. """
  53. """
  54. ip_key = IP_INCR_TMP.format(ip)
  55. ip_count = cache.get(ip_key) or 0
  56. if ip_count + 1 >= MAX_ERROR_TIMES_IP:
  57. lock(0,ip)
  58. raise cei.TipException(u'密码连续输错20次,锁定ip半个小时!')
  59. if not ip_count:
  60. cache.set(ip_key, 1, 30*60)
  61. else:
  62. cache.incr(ip_key)
  63. def increase_error_count_uid(uid):
  64. """
  65. """
  66. account_key = ACCOUNT_INCR_TMP.format(uid)
  67. account_count = cache.get(account_key) or 0
  68. if account_count + 1 >= MAX_ERROR_TIMES:
  69. lock(uid,0)
  70. raise cei.TipException(u'密码连续输错5次,锁定账户10分钟!')
  71. if not account_count:
  72. cache.set(account_key, 1, 10*60)
  73. else:
  74. cache.incr(account_key)
  75. def clear_lock_count(uid, ip):
  76. """
  77. 清除计数,清楚锁ip,锁account的key
  78. :param uid:
  79. :param ip:
  80. :return:
  81. """
  82. if uid:
  83. cache.delete(ACCOUNT_INCR_TMP.format(uid))
  84. if ip:
  85. cache.delete(IP_INCR_TMP.format(ip))
  86. def lock(uid, ip):
  87. """
  88. 锁账户,ip半个小时
  89. :param uid:
  90. :param ip:
  91. :return:
  92. """
  93. clear_lock_count(uid, ip)
  94. if uid:
  95. key = LOCK_ACCOUNT_TMP.format(uid)
  96. cache.set(key, 'lock_account', 10*60)
  97. if ip:
  98. key = LOCK_IP_TMP.format(ip)
  99. cache.set(key, 'lock_ip', 30*60)
  100. def clear_lock(uid,ip):
  101. clear_lock_count(uid, ip)
  102. if uid:
  103. key = LOCK_ACCOUNT_TMP.format(uid)
  104. cache.delete(key)
  105. if ip:
  106. key = LOCK_IP_TMP.format(ip)
  107. cache.delete(IP_INCR_TMP.format(ip))