geodb.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package geodb
  2. import (
  3. _ "embed"
  4. "log"
  5. "net"
  6. "net/http"
  7. "imuslab.com/zoraxy/mod/database"
  8. )
  9. //go:embed geoipv4.csv
  10. var geoipv4 []byte //Geodb dataset for ipv4
  11. //go:embed geoipv6.csv
  12. var geoipv6 []byte //Geodb dataset for ipv6
  13. type Store struct {
  14. BlacklistEnabled bool
  15. WhitelistEnabled bool
  16. geodb [][]string //Parsed geodb list
  17. geodbIpv6 [][]string //Parsed geodb list for ipv6
  18. geotrie *trie
  19. geotrieIpv6 *trie
  20. //geoipCache sync.Map
  21. sysdb *database.Database
  22. option *StoreOptions
  23. }
  24. type StoreOptions struct {
  25. AllowSlowIpv4LookUp bool
  26. AllowSloeIpv6Lookup bool
  27. }
  28. type CountryInfo struct {
  29. CountryIsoCode string
  30. ContinetCode string
  31. }
  32. func NewGeoDb(sysdb *database.Database, option *StoreOptions) (*Store, error) {
  33. parsedGeoData, err := parseCSV(geoipv4)
  34. if err != nil {
  35. return nil, err
  36. }
  37. parsedGeoDataIpv6, err := parseCSV(geoipv6)
  38. if err != nil {
  39. return nil, err
  40. }
  41. blacklistEnabled := false
  42. whitelistEnabled := false
  43. if sysdb != nil {
  44. err = sysdb.NewTable("blacklist-cn")
  45. if err != nil {
  46. return nil, err
  47. }
  48. err = sysdb.NewTable("blacklist-ip")
  49. if err != nil {
  50. return nil, err
  51. }
  52. err = sysdb.NewTable("whitelist-cn")
  53. if err != nil {
  54. return nil, err
  55. }
  56. err = sysdb.NewTable("whitelist-ip")
  57. if err != nil {
  58. return nil, err
  59. }
  60. err = sysdb.NewTable("blackwhitelist")
  61. if err != nil {
  62. return nil, err
  63. }
  64. sysdb.Read("blackwhitelist", "blacklistEnabled", &blacklistEnabled)
  65. sysdb.Read("blackwhitelist", "whitelistEnabled", &whitelistEnabled)
  66. } else {
  67. log.Println("Database pointer set to nil: Entering debug mode")
  68. }
  69. var ipv4Trie *trie
  70. if !option.AllowSlowIpv4LookUp {
  71. ipv4Trie = constrctTrieTree(parsedGeoData)
  72. }
  73. var ipv6Trie *trie
  74. if !option.AllowSloeIpv6Lookup {
  75. ipv6Trie = constrctTrieTree(parsedGeoDataIpv6)
  76. }
  77. return &Store{
  78. BlacklistEnabled: blacklistEnabled,
  79. WhitelistEnabled: whitelistEnabled,
  80. geodb: parsedGeoData,
  81. geotrie: ipv4Trie,
  82. geodbIpv6: parsedGeoDataIpv6,
  83. geotrieIpv6: ipv6Trie,
  84. sysdb: sysdb,
  85. option: option,
  86. }, nil
  87. }
  88. func (s *Store) ToggleBlacklist(enabled bool) {
  89. s.sysdb.Write("blackwhitelist", "blacklistEnabled", enabled)
  90. s.BlacklistEnabled = enabled
  91. }
  92. func (s *Store) ToggleWhitelist(enabled bool) {
  93. s.sysdb.Write("blackwhitelist", "whitelistEnabled", enabled)
  94. s.WhitelistEnabled = enabled
  95. }
  96. func (s *Store) ResolveCountryCodeFromIP(ipstring string) (*CountryInfo, error) {
  97. cc := s.search(ipstring)
  98. return &CountryInfo{
  99. CountryIsoCode: cc,
  100. ContinetCode: "",
  101. }, nil
  102. }
  103. func (s *Store) Close() {
  104. }
  105. /*
  106. Check if a IP address is blacklisted, in either country or IP blacklist
  107. IsBlacklisted default return is false (allow access)
  108. */
  109. func (s *Store) IsBlacklisted(ipAddr string) bool {
  110. if !s.BlacklistEnabled {
  111. //Blacklist not enabled. Always return false
  112. return false
  113. }
  114. if ipAddr == "" {
  115. //Unable to get the target IP address
  116. return false
  117. }
  118. countryCode, err := s.ResolveCountryCodeFromIP(ipAddr)
  119. if err != nil {
  120. return false
  121. }
  122. if s.IsCountryCodeBlacklisted(countryCode.CountryIsoCode) {
  123. return true
  124. }
  125. if s.IsIPBlacklisted(ipAddr) {
  126. return true
  127. }
  128. return false
  129. }
  130. /*
  131. IsWhitelisted check if a given IP address is in the current
  132. server's white list.
  133. Note that the Whitelist default result is true even
  134. when encountered error
  135. */
  136. func (s *Store) IsWhitelisted(ipAddr string) bool {
  137. if !s.WhitelistEnabled {
  138. //Whitelist not enabled. Always return true (allow access)
  139. return true
  140. }
  141. if ipAddr == "" {
  142. //Unable to get the target IP address, assume ok
  143. return true
  144. }
  145. countryCode, err := s.ResolveCountryCodeFromIP(ipAddr)
  146. if err != nil {
  147. return true
  148. }
  149. if s.IsCountryCodeWhitelisted(countryCode.CountryIsoCode) {
  150. return true
  151. }
  152. if s.IsIPWhitelisted(ipAddr) {
  153. return true
  154. }
  155. return false
  156. }
  157. // A helper function that check both blacklist and whitelist for access
  158. // for both geoIP and ip / CIDR ranges
  159. func (s *Store) AllowIpAccess(ipaddr string) bool {
  160. if s.IsBlacklisted(ipaddr) {
  161. return false
  162. }
  163. return s.IsWhitelisted(ipaddr)
  164. }
  165. func (s *Store) AllowConnectionAccess(conn net.Conn) bool {
  166. if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
  167. return s.AllowIpAccess(addr.IP.String())
  168. }
  169. return true
  170. }
  171. func (s *Store) GetRequesterCountryISOCode(r *http.Request) string {
  172. ipAddr := GetRequesterIP(r)
  173. if ipAddr == "" {
  174. return ""
  175. }
  176. countryCode, err := s.ResolveCountryCodeFromIP(ipAddr)
  177. if err != nil {
  178. return ""
  179. }
  180. return countryCode.CountryIsoCode
  181. }