banip.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // ui定制
  2. package models
  3. import (
  4. "errors"
  5. "github.com/astaxie/beego"
  6. "github.com/astaxie/beego/validation"
  7. "log"
  8. "sync"
  9. "fmt"
  10. "box-gm/utils"
  11. "github.com/garyburd/redigo/redis"
  12. )
  13. type BanIPInfo struct {
  14. IP string // 地址
  15. }
  16. type BanIPInfoArray []*BanIPInfo
  17. var listBanIPInfo BanIPInfoArray
  18. var bipfMutex sync.Mutex
  19. func BanIPGetMajorKey() string {
  20. return "ban:ip"
  21. }
  22. func InitBanIPInfo() {
  23. pool := utils.GetAccountRedisPool()
  24. if pool == nil {
  25. fmt.Println("get redis pool fail")
  26. return
  27. }
  28. rd := pool.Get()
  29. defer rd.Close()
  30. if rd == nil {
  31. fmt.Println("get redis conn fail")
  32. return
  33. }
  34. key := BanIPGetMajorKey()
  35. result, _ := redis.Values(rd.Do("smembers", key))
  36. for _, v := range result {
  37. info := &BanIPInfo{
  38. IP: string(v.([]byte)),
  39. }
  40. listBanIPInfo = append(listBanIPInfo, info)
  41. }
  42. beego.Info("load ban ip info", len(listBanIPInfo))
  43. }
  44. func checkBanIPInfo(hf *BanIPInfo) (err error) {
  45. valid := validation.Validation{}
  46. b, _ := valid.Valid(&hf)
  47. if !b {
  48. for _, err := range valid.Errors {
  49. log.Println(err.Key, err.Message)
  50. return errors.New(err.Message)
  51. }
  52. }
  53. return nil
  54. }
  55. func AddBanIPInfo(hf *BanIPInfo) (error) {
  56. if err := checkBanIPInfo(hf); err != nil {
  57. return err
  58. }
  59. pool := utils.GetAccountRedisPool()
  60. if pool == nil {
  61. return errors.New("get redis conn fail")
  62. }
  63. rd := pool.Get()
  64. defer rd.Close()
  65. if rd == nil {
  66. return errors.New("get redis conn fail")
  67. }
  68. key := BanIPGetMajorKey()
  69. rd.Do("sadd", key, hf.IP)
  70. bipfMutex.Lock()
  71. listBanIPInfo = append(listBanIPInfo, hf)
  72. bipfMutex.Unlock()
  73. return nil
  74. }
  75. func DelBanIPInfoByIP(ip string) (error) {
  76. pool := utils.GetAccountRedisPool()
  77. if pool == nil {
  78. return errors.New("get redis conn fail")
  79. }
  80. rd := pool.Get()
  81. defer rd.Close()
  82. if rd == nil {
  83. return errors.New("get redis conn fail")
  84. }
  85. key := BanIPGetMajorKey()
  86. rd.Do("srem", key, ip)
  87. bipfMutex.Lock()
  88. defer bipfMutex.Unlock()
  89. for i := 0; i < len(listBanIPInfo); i++ {
  90. if ip == listBanIPInfo[i].IP {
  91. ii := i + 1
  92. listBanIPInfo = append(listBanIPInfo[:i], listBanIPInfo[ii:]...)
  93. }
  94. }
  95. return nil
  96. }
  97. func GetBanIPInfoByIP(ip string) (hf *BanIPInfo) {
  98. bipfMutex.Lock()
  99. defer bipfMutex.Unlock()
  100. for i := 0; i < len(listBanIPInfo); i++ {
  101. if ip == listBanIPInfo[i].IP {
  102. return listBanIPInfo[i]
  103. }
  104. }
  105. return nil
  106. }
  107. func GetBanIPInfo() BanIPInfoArray {
  108. bipfMutex.Lock()
  109. defer bipfMutex.Unlock()
  110. return listBanIPInfo
  111. }