accessRule.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package access
  2. import (
  3. "encoding/json"
  4. "net"
  5. "os"
  6. "path/filepath"
  7. )
  8. // Check both blacklist and whitelist for access for both geoIP and ip / CIDR ranges
  9. func (s *AccessRule) AllowIpAccess(ipaddr string) bool {
  10. if s.IsBlacklisted(ipaddr) {
  11. return false
  12. }
  13. return s.IsWhitelisted(ipaddr)
  14. }
  15. // Check both blacklist and whitelist for access using net.Conn
  16. func (s *AccessRule) AllowConnectionAccess(conn net.Conn) bool {
  17. if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
  18. return s.AllowIpAccess(addr.IP.String())
  19. }
  20. return true
  21. }
  22. // Toggle black list
  23. func (s *AccessRule) ToggleBlacklist(enabled bool) {
  24. s.parent.Options.Database.Write("blackwhitelist", "blacklistEnabled", enabled)
  25. s.BlacklistEnabled = enabled
  26. }
  27. // Toggel white list
  28. func (s *AccessRule) ToggleWhitelist(enabled bool) {
  29. s.parent.Options.Database.Write("blackwhitelist", "whitelistEnabled", enabled)
  30. s.WhitelistEnabled = enabled
  31. }
  32. /*
  33. Check if a IP address is blacklisted, in either country or IP blacklist
  34. IsBlacklisted default return is false (allow access)
  35. */
  36. func (s *AccessRule) IsBlacklisted(ipAddr string) bool {
  37. if !s.BlacklistEnabled {
  38. //Blacklist not enabled. Always return false
  39. return false
  40. }
  41. if ipAddr == "" {
  42. //Unable to get the target IP address
  43. return false
  44. }
  45. countryCode, err := s.parent.Options.GeoDB.ResolveCountryCodeFromIP(ipAddr)
  46. if err != nil {
  47. return false
  48. }
  49. if s.IsCountryCodeBlacklisted(countryCode.CountryIsoCode) {
  50. return true
  51. }
  52. if s.IsIPBlacklisted(ipAddr) {
  53. return true
  54. }
  55. return false
  56. }
  57. /*
  58. IsWhitelisted check if a given IP address is in the current
  59. server's white list.
  60. Note that the Whitelist default result is true even
  61. when encountered error
  62. */
  63. func (s *AccessRule) IsWhitelisted(ipAddr string) bool {
  64. if !s.WhitelistEnabled {
  65. //Whitelist not enabled. Always return true (allow access)
  66. return true
  67. }
  68. if ipAddr == "" {
  69. //Unable to get the target IP address, assume ok
  70. return true
  71. }
  72. countryCode, err := s.parent.Options.GeoDB.ResolveCountryCodeFromIP(ipAddr)
  73. if err != nil {
  74. return true
  75. }
  76. if s.IsCountryCodeWhitelisted(countryCode.CountryIsoCode) {
  77. return true
  78. }
  79. if s.IsIPWhitelisted(ipAddr) {
  80. return true
  81. }
  82. return false
  83. }
  84. /* Utilities function */
  85. // Update the current access rule to json file
  86. func (s *AccessRule) SaveChanges() error {
  87. saveTarget := filepath.Join(s.parent.Options.ConfigFolder, s.ID+".json")
  88. js, err := json.MarshalIndent(s, "", " ")
  89. if err != nil {
  90. return err
  91. }
  92. err = os.WriteFile(saveTarget, js, 0775)
  93. return err
  94. }
  95. // Create a deep copy object of the access rule list
  96. func deepCopy(valueList map[string]string) map[string]string {
  97. result := map[string]string{}
  98. js, _ := json.Marshal(valueList)
  99. json.Unmarshal(js, &result)
  100. return result
  101. }