1
0

blacklist.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package access
  2. import (
  3. "strings"
  4. "imuslab.com/zoraxy/mod/netutils"
  5. )
  6. /*
  7. Blacklist.go
  8. This script store the blacklist related functions
  9. */
  10. // Geo Blacklist
  11. func (s *AccessRule) AddCountryCodeToBlackList(countryCode string, comment string) {
  12. countryCode = strings.ToLower(countryCode)
  13. newBlacklistCountryCode := deepCopy(*s.BlackListContryCode)
  14. newBlacklistCountryCode[countryCode] = comment
  15. s.BlackListContryCode = &newBlacklistCountryCode
  16. s.SaveChanges()
  17. }
  18. func (s *AccessRule) RemoveCountryCodeFromBlackList(countryCode string) {
  19. countryCode = strings.ToLower(countryCode)
  20. newBlacklistCountryCode := deepCopy(*s.BlackListContryCode)
  21. delete(newBlacklistCountryCode, countryCode)
  22. s.BlackListContryCode = &newBlacklistCountryCode
  23. s.SaveChanges()
  24. }
  25. func (s *AccessRule) IsCountryCodeBlacklisted(countryCode string) bool {
  26. countryCode = strings.ToLower(countryCode)
  27. blacklistMap := *s.BlackListContryCode
  28. _, ok := blacklistMap[countryCode]
  29. return ok
  30. }
  31. func (s *AccessRule) GetAllBlacklistedCountryCode() []string {
  32. bannedCountryCodes := []string{}
  33. blacklistMap := *s.BlackListContryCode
  34. for cc, _ := range blacklistMap {
  35. bannedCountryCodes = append(bannedCountryCodes, cc)
  36. }
  37. return bannedCountryCodes
  38. }
  39. // IP Blacklsits
  40. func (s *AccessRule) AddIPToBlackList(ipAddr string, comment string) {
  41. newBlackListIP := deepCopy(*s.BlackListIP)
  42. newBlackListIP[ipAddr] = comment
  43. s.BlackListIP = &newBlackListIP
  44. s.SaveChanges()
  45. }
  46. func (s *AccessRule) RemoveIPFromBlackList(ipAddr string) {
  47. newBlackListIP := deepCopy(*s.BlackListIP)
  48. delete(newBlackListIP, ipAddr)
  49. s.BlackListIP = &newBlackListIP
  50. s.SaveChanges()
  51. }
  52. func (s *AccessRule) GetAllBlacklistedIp() []string {
  53. bannedIps := []string{}
  54. blacklistMap := *s.BlackListIP
  55. for ip, _ := range blacklistMap {
  56. bannedIps = append(bannedIps, ip)
  57. }
  58. return bannedIps
  59. }
  60. func (s *AccessRule) IsIPBlacklisted(ipAddr string) bool {
  61. IPBlacklist := *s.BlackListIP
  62. _, ok := IPBlacklist[ipAddr]
  63. if ok {
  64. return true
  65. }
  66. //Check for CIDR
  67. for ipOrCIDR, _ := range IPBlacklist {
  68. wildcardMatch := netutils.MatchIpWildcard(ipAddr, ipOrCIDR)
  69. if wildcardMatch {
  70. return true
  71. }
  72. cidrMatch := netutils.MatchIpCIDR(ipAddr, ipOrCIDR)
  73. if cidrMatch {
  74. return true
  75. }
  76. }
  77. return false
  78. }