geodb.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package geodb
  2. import (
  3. "net"
  4. "net/http"
  5. "strings"
  6. "github.com/oschwald/geoip2-golang"
  7. "imuslab.com/Zoarxy/mod/database"
  8. )
  9. type Store struct {
  10. Enabled bool
  11. geodb *geoip2.Reader
  12. sysdb *database.Database
  13. }
  14. type CountryInfo struct {
  15. CountryIsoCode string
  16. ContinetCode string
  17. }
  18. func NewGeoDb(sysdb *database.Database, dbfile string) (*Store, error) {
  19. db, err := geoip2.Open(dbfile)
  20. if err != nil {
  21. return nil, err
  22. }
  23. err = sysdb.NewTable("blacklist-cn")
  24. if err != nil {
  25. return nil, err
  26. }
  27. err = sysdb.NewTable("blacklist-ip")
  28. if err != nil {
  29. return nil, err
  30. }
  31. err = sysdb.NewTable("blacklist")
  32. if err != nil {
  33. return nil, err
  34. }
  35. blacklistEnabled := false
  36. sysdb.Read("blacklist", "enabled", &blacklistEnabled)
  37. return &Store{
  38. Enabled: blacklistEnabled,
  39. geodb: db,
  40. sysdb: sysdb,
  41. }, nil
  42. }
  43. func (s *Store) ToggleBlacklist(enabled bool) {
  44. s.sysdb.Write("blacklist", "enabled", enabled)
  45. s.Enabled = enabled
  46. }
  47. func (s *Store) ResolveCountryCodeFromIP(ipstring string) (*CountryInfo, error) {
  48. // If you are using strings that may be invalid, check that ip is not nil
  49. ip := net.ParseIP(ipstring)
  50. record, err := s.geodb.City(ip)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return &CountryInfo{
  55. record.Country.IsoCode,
  56. record.Continent.Code,
  57. }, nil
  58. }
  59. func (s *Store) Close() {
  60. s.geodb.Close()
  61. }
  62. func (s *Store) AddCountryCodeToBlackList(countryCode string) {
  63. s.sysdb.Write("blacklist-cn", countryCode, true)
  64. }
  65. func (s *Store) RemoveCountryCodeFromBlackList(countryCode string) {
  66. s.sysdb.Delete("blacklist-cn", countryCode)
  67. }
  68. func (s *Store) IsCountryCodeBlacklisted(countryCode string) bool {
  69. var isBlacklisted bool = false
  70. s.sysdb.Read("blacklist-cn", countryCode, &isBlacklisted)
  71. return isBlacklisted
  72. }
  73. func (s *Store) GetAllBlacklistedCountryCode() []string {
  74. bannedCountryCodes := []string{}
  75. entries, err := s.sysdb.ListTable("blacklist-cn")
  76. if err != nil {
  77. return bannedCountryCodes
  78. }
  79. for _, keypairs := range entries {
  80. ip := string(keypairs[0])
  81. bannedCountryCodes = append(bannedCountryCodes, ip)
  82. }
  83. return bannedCountryCodes
  84. }
  85. func (s *Store) AddIPToBlackList(ipAddr string) {
  86. s.sysdb.Write("blacklist-ip", ipAddr, true)
  87. }
  88. func (s *Store) RemoveIPFromBlackList(ipAddr string) {
  89. s.sysdb.Delete("blacklist-ip", ipAddr)
  90. }
  91. func (s *Store) IsIPBlacklisted(ipAddr string) bool {
  92. var isBlacklisted bool = false
  93. s.sysdb.Read("blacklist-ip", ipAddr, &isBlacklisted)
  94. if isBlacklisted {
  95. return true
  96. }
  97. //Check for IP wildcard and CIRD rules
  98. AllBlacklistedIps := s.GetAllBlacklistedIp()
  99. for _, blacklistRule := range AllBlacklistedIps {
  100. wildcardMatch := MatchIpWildcard(ipAddr, blacklistRule)
  101. if wildcardMatch {
  102. return true
  103. }
  104. cidrMatch := MatchIpCIDR(ipAddr, blacklistRule)
  105. if cidrMatch {
  106. return true
  107. }
  108. }
  109. return false
  110. }
  111. func (s *Store) GetAllBlacklistedIp() []string {
  112. bannedIps := []string{}
  113. entries, err := s.sysdb.ListTable("blacklist-ip")
  114. if err != nil {
  115. return bannedIps
  116. }
  117. for _, keypairs := range entries {
  118. ip := string(keypairs[0])
  119. bannedIps = append(bannedIps, ip)
  120. }
  121. return bannedIps
  122. }
  123. //Check if a IP address is blacklisted, in either country or IP blacklist
  124. func (s *Store) IsBlacklisted(ipAddr string) bool {
  125. if !s.Enabled {
  126. //Blacklist not enabled. Always return false
  127. return false
  128. }
  129. if ipAddr == "" {
  130. //Unable to get the target IP address
  131. return false
  132. }
  133. countryCode, err := s.ResolveCountryCodeFromIP(ipAddr)
  134. if err != nil {
  135. return false
  136. }
  137. if s.IsCountryCodeBlacklisted(countryCode.CountryIsoCode) {
  138. return true
  139. }
  140. if s.IsIPBlacklisted(ipAddr) {
  141. return true
  142. }
  143. return false
  144. }
  145. func (s *Store) GetRequesterCountryISOCode(r *http.Request) string {
  146. ipAddr := GetRequesterIP(r)
  147. if ipAddr == "" {
  148. return ""
  149. }
  150. countryCode, err := s.ResolveCountryCodeFromIP(ipAddr)
  151. if err != nil {
  152. return ""
  153. }
  154. return countryCode.CountryIsoCode
  155. }
  156. //Utilities function
  157. func GetRequesterIP(r *http.Request) string {
  158. ip := r.Header.Get("X-Forwarded-For")
  159. if ip == "" {
  160. ip = r.Header.Get("X-Real-IP")
  161. if ip == "" {
  162. ip = strings.Split(r.RemoteAddr, ":")[0]
  163. }
  164. }
  165. return ip
  166. }
  167. //Match the IP address with a wildcard string
  168. func MatchIpWildcard(ipAddress, wildcard string) bool {
  169. // Split IP address and wildcard into octets
  170. ipOctets := strings.Split(ipAddress, ".")
  171. wildcardOctets := strings.Split(wildcard, ".")
  172. // Check that both have 4 octets
  173. if len(ipOctets) != 4 || len(wildcardOctets) != 4 {
  174. return false
  175. }
  176. // Check each octet to see if it matches the wildcard or is an exact match
  177. for i := 0; i < 4; i++ {
  178. if wildcardOctets[i] == "*" {
  179. continue
  180. }
  181. if ipOctets[i] != wildcardOctets[i] {
  182. return false
  183. }
  184. }
  185. return true
  186. }
  187. //Match ip address with CIDR
  188. func MatchIpCIDR(ip string, cidr string) bool {
  189. // parse the CIDR string
  190. _, cidrnet, err := net.ParseCIDR(cidr)
  191. if err != nil {
  192. return false
  193. }
  194. // parse the IP address
  195. ipAddr := net.ParseIP(ip)
  196. // check if the IP address is within the CIDR range
  197. return cidrnet.Contains(ipAddr)
  198. }