1
0

whitelist.go 2.1 KB

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