blacklist.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package geodb
  2. import "strings"
  3. /*
  4. Blacklist.go
  5. This script store the blacklist related functions
  6. */
  7. //Geo Blacklist
  8. func (s *Store) AddCountryCodeToBlackList(countryCode string) {
  9. countryCode = strings.ToLower(countryCode)
  10. s.sysdb.Write("blacklist-cn", countryCode, true)
  11. }
  12. func (s *Store) RemoveCountryCodeFromBlackList(countryCode string) {
  13. countryCode = strings.ToLower(countryCode)
  14. s.sysdb.Delete("blacklist-cn", countryCode)
  15. }
  16. func (s *Store) IsCountryCodeBlacklisted(countryCode string) bool {
  17. countryCode = strings.ToLower(countryCode)
  18. var isBlacklisted bool = false
  19. s.sysdb.Read("blacklist-cn", countryCode, &isBlacklisted)
  20. return isBlacklisted
  21. }
  22. func (s *Store) GetAllBlacklistedCountryCode() []string {
  23. bannedCountryCodes := []string{}
  24. entries, err := s.sysdb.ListTable("blacklist-cn")
  25. if err != nil {
  26. return bannedCountryCodes
  27. }
  28. for _, keypairs := range entries {
  29. ip := string(keypairs[0])
  30. bannedCountryCodes = append(bannedCountryCodes, ip)
  31. }
  32. return bannedCountryCodes
  33. }
  34. //IP Blacklsits
  35. func (s *Store) AddIPToBlackList(ipAddr string) {
  36. s.sysdb.Write("blacklist-ip", ipAddr, true)
  37. }
  38. func (s *Store) RemoveIPFromBlackList(ipAddr string) {
  39. s.sysdb.Delete("blacklist-ip", ipAddr)
  40. }
  41. func (s *Store) GetAllBlacklistedIp() []string {
  42. bannedIps := []string{}
  43. entries, err := s.sysdb.ListTable("blacklist-ip")
  44. if err != nil {
  45. return bannedIps
  46. }
  47. for _, keypairs := range entries {
  48. ip := string(keypairs[0])
  49. bannedIps = append(bannedIps, ip)
  50. }
  51. return bannedIps
  52. }
  53. func (s *Store) IsIPBlacklisted(ipAddr string) bool {
  54. var isBlacklisted bool = false
  55. s.sysdb.Read("blacklist-ip", ipAddr, &isBlacklisted)
  56. if isBlacklisted {
  57. return true
  58. }
  59. //Check for IP wildcard and CIRD rules
  60. AllBlacklistedIps := s.GetAllBlacklistedIp()
  61. for _, blacklistRule := range AllBlacklistedIps {
  62. wildcardMatch := MatchIpWildcard(ipAddr, blacklistRule)
  63. if wildcardMatch {
  64. return true
  65. }
  66. cidrMatch := MatchIpCIDR(ipAddr, blacklistRule)
  67. if cidrMatch {
  68. return true
  69. }
  70. }
  71. return false
  72. }