banIP.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. )
  9. func initBannedIP(serverFolder string) BannedIP {
  10. jsonFile, err := os.Open(serverFolder + "/banned-ips.json")
  11. if err != nil {
  12. fmt.Println(err)
  13. }
  14. byte, _ := ioutil.ReadAll(jsonFile)
  15. var dataArray BannedIP
  16. json.Unmarshal(byte, &dataArray)
  17. return dataArray
  18. }
  19. //ReadAllBannedIPs is exported function
  20. func (mch *Handler) ReadAllBannedIPs() BannedIP {
  21. return mch.bannedIPs
  22. }
  23. //WriteBannedIP is exported function
  24. func (mch *Handler) WriteBannedIP(IP string, Created string, Source string, Expires string, Reason string) bool {
  25. mch.reloadBanIP() //ensure that everything updated.
  26. newItem := BannedIP{}
  27. newItem = append(newItem, struct {
  28. IP string `json:"ip"`
  29. Created string `json:"created"`
  30. Source string `json:"source"`
  31. Expires string `json:"expires"`
  32. Reason string `json:"reason"`
  33. }{IP, Created, Source, Expires, Reason})
  34. mch.bannedIPs = append(mch.bannedIPs, newItem...)
  35. mch.SaveAllBannedIPs() //save it
  36. return true
  37. }
  38. //ReadBannedIP is exported function
  39. func (mch *Handler) ReadBannedIP(search string, field string) BannedIP {
  40. var list BannedIP
  41. for _, item := range mch.bannedIPs {
  42. fieldValue := ""
  43. switch strings.ToLower(field) {
  44. case "ip":
  45. fieldValue = item.IP
  46. case "created":
  47. fieldValue = item.Created
  48. case "source":
  49. fieldValue = item.Source
  50. case "expires":
  51. fieldValue = item.Expires
  52. case "reason":
  53. fieldValue = item.Reason
  54. default:
  55. fieldValue = ""
  56. }
  57. if fieldValue == search {
  58. list = append(list, item)
  59. }
  60. }
  61. return list
  62. }
  63. //RemoveBannedIP is exported function
  64. func (mch *Handler) RemoveBannedIP(search string, field string) bool {
  65. mch.reloadBanIP() //ensure that everything updated.
  66. for i, item := range mch.bannedIPs {
  67. fieldValue := ""
  68. switch strings.ToLower(field) {
  69. case "ip":
  70. fieldValue = item.IP
  71. case "created":
  72. fieldValue = item.Created
  73. case "source":
  74. fieldValue = item.Source
  75. case "expires":
  76. fieldValue = item.Expires
  77. case "reason":
  78. fieldValue = item.Reason
  79. default:
  80. fieldValue = ""
  81. }
  82. if fieldValue == search {
  83. if len(mch.bannedIPs)-1 != i {
  84. mch.bannedIPs = append(mch.bannedIPs[:i], mch.bannedIPs[i+1:]...)
  85. } else {
  86. // if it is the last item, just remove it
  87. mch.bannedIPs = mch.bannedIPs[:i]
  88. }
  89. }
  90. }
  91. mch.SaveAllBannedIPs()
  92. return true
  93. }
  94. //SaveAllBannedIPs is exported function
  95. func (mch *Handler) SaveAllBannedIPs() bool {
  96. JSON, _ := json.Marshal(mch.bannedIPs)
  97. err := ioutil.WriteFile(mch.serverFolder+"/banned-ips.json", JSON, 0777)
  98. if err != nil {
  99. fmt.Println(err)
  100. }
  101. return true
  102. }