geodb.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package geodb
  2. import (
  3. "net"
  4. "net/http"
  5. "strings"
  6. "github.com/oschwald/geoip2-golang"
  7. "imuslab.com/zoraxy/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. countryCode = strings.ToLower(countryCode)
  64. s.sysdb.Write("blacklist-cn", countryCode, true)
  65. }
  66. func (s *Store) RemoveCountryCodeFromBlackList(countryCode string) {
  67. countryCode = strings.ToLower(countryCode)
  68. s.sysdb.Delete("blacklist-cn", countryCode)
  69. }
  70. func (s *Store) IsCountryCodeBlacklisted(countryCode string) bool {
  71. countryCode = strings.ToLower(countryCode)
  72. var isBlacklisted bool = false
  73. s.sysdb.Read("blacklist-cn", countryCode, &isBlacklisted)
  74. return isBlacklisted
  75. }
  76. func (s *Store) GetAllBlacklistedCountryCode() []string {
  77. bannedCountryCodes := []string{}
  78. entries, err := s.sysdb.ListTable("blacklist-cn")
  79. if err != nil {
  80. return bannedCountryCodes
  81. }
  82. for _, keypairs := range entries {
  83. ip := string(keypairs[0])
  84. bannedCountryCodes = append(bannedCountryCodes, ip)
  85. }
  86. return bannedCountryCodes
  87. }
  88. func (s *Store) AddIPToBlackList(ipAddr string) {
  89. s.sysdb.Write("blacklist-ip", ipAddr, true)
  90. }
  91. func (s *Store) RemoveIPFromBlackList(ipAddr string) {
  92. s.sysdb.Delete("blacklist-ip", ipAddr)
  93. }
  94. func (s *Store) IsIPBlacklisted(ipAddr string) bool {
  95. var isBlacklisted bool = false
  96. s.sysdb.Read("blacklist-ip", ipAddr, &isBlacklisted)
  97. if isBlacklisted {
  98. return true
  99. }
  100. //Check for IP wildcard and CIRD rules
  101. AllBlacklistedIps := s.GetAllBlacklistedIp()
  102. for _, blacklistRule := range AllBlacklistedIps {
  103. wildcardMatch := MatchIpWildcard(ipAddr, blacklistRule)
  104. if wildcardMatch {
  105. return true
  106. }
  107. cidrMatch := MatchIpCIDR(ipAddr, blacklistRule)
  108. if cidrMatch {
  109. return true
  110. }
  111. }
  112. return false
  113. }
  114. func (s *Store) GetAllBlacklistedIp() []string {
  115. bannedIps := []string{}
  116. entries, err := s.sysdb.ListTable("blacklist-ip")
  117. if err != nil {
  118. return bannedIps
  119. }
  120. for _, keypairs := range entries {
  121. ip := string(keypairs[0])
  122. bannedIps = append(bannedIps, ip)
  123. }
  124. return bannedIps
  125. }
  126. //Check if a IP address is blacklisted, in either country or IP blacklist
  127. func (s *Store) IsBlacklisted(ipAddr string) bool {
  128. if !s.Enabled {
  129. //Blacklist not enabled. Always return false
  130. return false
  131. }
  132. if ipAddr == "" {
  133. //Unable to get the target IP address
  134. return false
  135. }
  136. countryCode, err := s.ResolveCountryCodeFromIP(ipAddr)
  137. if err != nil {
  138. return false
  139. }
  140. if s.IsCountryCodeBlacklisted(countryCode.CountryIsoCode) {
  141. return true
  142. }
  143. if s.IsIPBlacklisted(ipAddr) {
  144. return true
  145. }
  146. return false
  147. }
  148. func (s *Store) GetRequesterCountryISOCode(r *http.Request) string {
  149. ipAddr := GetRequesterIP(r)
  150. if ipAddr == "" {
  151. return ""
  152. }
  153. countryCode, err := s.ResolveCountryCodeFromIP(ipAddr)
  154. if err != nil {
  155. return ""
  156. }
  157. return countryCode.CountryIsoCode
  158. }
  159. //Utilities function
  160. func GetRequesterIP(r *http.Request) string {
  161. ip := r.Header.Get("X-Forwarded-For")
  162. if ip == "" {
  163. ip = r.Header.Get("X-Real-IP")
  164. if ip == "" {
  165. ip = strings.Split(r.RemoteAddr, ":")[0]
  166. }
  167. }
  168. return ip
  169. }
  170. //Match the IP address with a wildcard string
  171. func MatchIpWildcard(ipAddress, wildcard string) bool {
  172. // Split IP address and wildcard into octets
  173. ipOctets := strings.Split(ipAddress, ".")
  174. wildcardOctets := strings.Split(wildcard, ".")
  175. // Check that both have 4 octets
  176. if len(ipOctets) != 4 || len(wildcardOctets) != 4 {
  177. return false
  178. }
  179. // Check each octet to see if it matches the wildcard or is an exact match
  180. for i := 0; i < 4; i++ {
  181. if wildcardOctets[i] == "*" {
  182. continue
  183. }
  184. if ipOctets[i] != wildcardOctets[i] {
  185. return false
  186. }
  187. }
  188. return true
  189. }
  190. //Match ip address with CIDR
  191. func MatchIpCIDR(ip string, cidr string) bool {
  192. // parse the CIDR string
  193. _, cidrnet, err := net.ParseCIDR(cidr)
  194. if err != nil {
  195. return false
  196. }
  197. // parse the IP address
  198. ipAddr := net.ParseIP(ip)
  199. // check if the IP address is within the CIDR range
  200. return cidrnet.Contains(ipAddr)
  201. }