1
0

whitelist.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package geodb
  2. import (
  3. "encoding/json"
  4. "strings"
  5. )
  6. /*
  7. Whitelist.go
  8. This script handles whitelist related functions
  9. */
  10. const (
  11. EntryType_CountryCode int = 0
  12. EntryType_IP int = 1
  13. )
  14. type WhitelistEntry struct {
  15. EntryType int //Entry type of whitelist, Country Code or IP
  16. CC string //ISO Country Code
  17. IP string //IP address or range
  18. Comment string //Comment for this entry
  19. }
  20. //Geo Whitelist
  21. func (s *Store) AddCountryCodeToWhitelist(countryCode string, comment string) {
  22. countryCode = strings.ToLower(countryCode)
  23. entry := WhitelistEntry{
  24. EntryType: EntryType_CountryCode,
  25. CC: countryCode,
  26. Comment: comment,
  27. }
  28. s.sysdb.Write("whitelist-cn", countryCode, entry)
  29. }
  30. func (s *Store) RemoveCountryCodeFromWhitelist(countryCode string) {
  31. countryCode = strings.ToLower(countryCode)
  32. s.sysdb.Delete("whitelist-cn", countryCode)
  33. }
  34. func (s *Store) IsCountryCodeWhitelisted(countryCode string) bool {
  35. countryCode = strings.ToLower(countryCode)
  36. return s.sysdb.KeyExists("whitelist-cn", countryCode)
  37. }
  38. func (s *Store) GetAllWhitelistedCountryCode() []*WhitelistEntry {
  39. whitelistedCountryCode := []*WhitelistEntry{}
  40. entries, err := s.sysdb.ListTable("whitelist-cn")
  41. if err != nil {
  42. return whitelistedCountryCode
  43. }
  44. for _, keypairs := range entries {
  45. thisWhitelistEntry := WhitelistEntry{}
  46. json.Unmarshal(keypairs[1], &thisWhitelistEntry)
  47. whitelistedCountryCode = append(whitelistedCountryCode, &thisWhitelistEntry)
  48. }
  49. return whitelistedCountryCode
  50. }
  51. //IP Whitelist
  52. func (s *Store) AddIPToWhiteList(ipAddr string, comment string) {
  53. thisIpEntry := WhitelistEntry{
  54. EntryType: EntryType_IP,
  55. IP: ipAddr,
  56. Comment: comment,
  57. }
  58. s.sysdb.Write("whitelist-ip", ipAddr, thisIpEntry)
  59. }
  60. func (s *Store) RemoveIPFromWhiteList(ipAddr string) {
  61. s.sysdb.Delete("whitelist-ip", ipAddr)
  62. }
  63. func (s *Store) IsIPWhitelisted(ipAddr string) bool {
  64. isWhitelisted := s.sysdb.KeyExists("whitelist-ip", ipAddr)
  65. if isWhitelisted {
  66. //single IP whitelist entry
  67. return true
  68. }
  69. //Check for IP wildcard and CIRD rules
  70. AllWhitelistedIps := s.GetAllWhitelistedIpAsStringSlice()
  71. for _, whitelistRules := range AllWhitelistedIps {
  72. wildcardMatch := MatchIpWildcard(ipAddr, whitelistRules)
  73. if wildcardMatch {
  74. return true
  75. }
  76. cidrMatch := MatchIpCIDR(ipAddr, whitelistRules)
  77. if cidrMatch {
  78. return true
  79. }
  80. }
  81. return false
  82. }
  83. func (s *Store) GetAllWhitelistedIp() []*WhitelistEntry {
  84. whitelistedIp := []*WhitelistEntry{}
  85. entries, err := s.sysdb.ListTable("whitelist-ip")
  86. if err != nil {
  87. return whitelistedIp
  88. }
  89. for _, keypairs := range entries {
  90. //ip := string(keypairs[0])
  91. thisEntry := WhitelistEntry{}
  92. json.Unmarshal(keypairs[1], &thisEntry)
  93. whitelistedIp = append(whitelistedIp, &thisEntry)
  94. }
  95. return whitelistedIp
  96. }
  97. func (s *Store) GetAllWhitelistedIpAsStringSlice() []string {
  98. allWhitelistedIPs := []string{}
  99. entries := s.GetAllWhitelistedIp()
  100. for _, entry := range entries {
  101. allWhitelistedIPs = append(allWhitelistedIPs, entry.IP)
  102. }
  103. return allWhitelistedIPs
  104. }