accessRule.go 3.5 KB

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