1
0

geodb.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package geodb
  2. import (
  3. _ "embed"
  4. "log"
  5. "net/http"
  6. "strings"
  7. "imuslab.com/zoraxy/mod/database"
  8. )
  9. //go:embed geoipv4.csv
  10. var geoipv4 []byte //Original embedded csv file
  11. type Store struct {
  12. BlacklistEnabled bool
  13. WhitelistEnabled bool
  14. geodb [][]string //Parsed geodb list
  15. //geoipCache sync.Map
  16. geotrie *trie
  17. sysdb *database.Database
  18. }
  19. type CountryInfo struct {
  20. CountryIsoCode string
  21. ContinetCode string
  22. }
  23. func NewGeoDb(sysdb *database.Database) (*Store, error) {
  24. parsedGeoData, err := parseCSV(geoipv4)
  25. if err != nil {
  26. return nil, err
  27. }
  28. blacklistEnabled := false
  29. whitelistEnabled := false
  30. if sysdb != nil {
  31. err = sysdb.NewTable("blacklist-cn")
  32. if err != nil {
  33. return nil, err
  34. }
  35. err = sysdb.NewTable("blacklist-ip")
  36. if err != nil {
  37. return nil, err
  38. }
  39. err = sysdb.NewTable("whitelist-cn")
  40. if err != nil {
  41. return nil, err
  42. }
  43. err = sysdb.NewTable("whitelist-ip")
  44. if err != nil {
  45. return nil, err
  46. }
  47. err = sysdb.NewTable("blackwhitelist")
  48. if err != nil {
  49. return nil, err
  50. }
  51. sysdb.Read("blackwhitelist", "blacklistEnabled", &blacklistEnabled)
  52. sysdb.Read("blackwhitelist", "whitelistEnabled", &whitelistEnabled)
  53. } else {
  54. log.Println("Database pointer set to nil: Entering debug mode")
  55. }
  56. return &Store{
  57. BlacklistEnabled: blacklistEnabled,
  58. WhitelistEnabled: whitelistEnabled,
  59. geodb: parsedGeoData,
  60. //geoipCache: sync.Map{},
  61. geotrie: constrctTrieTree(parsedGeoData),
  62. sysdb: sysdb,
  63. }, nil
  64. }
  65. func (s *Store) ToggleBlacklist(enabled bool) {
  66. s.sysdb.Write("blackwhitelist", "blacklistEnabled", enabled)
  67. s.BlacklistEnabled = enabled
  68. }
  69. func (s *Store) ToggleWhitelist(enabled bool) {
  70. s.sysdb.Write("blackwhitelist", "whitelistEnabled", enabled)
  71. s.WhitelistEnabled = enabled
  72. }
  73. func (s *Store) ResolveCountryCodeFromIP(ipstring string) (*CountryInfo, error) {
  74. cc := s.search(ipstring)
  75. return &CountryInfo{
  76. CountryIsoCode: cc,
  77. ContinetCode: "",
  78. }, nil
  79. }
  80. func (s *Store) Close() {
  81. }
  82. /*
  83. Country code based black / white list
  84. */
  85. func (s *Store) AddCountryCodeToBlackList(countryCode string) {
  86. countryCode = strings.ToLower(countryCode)
  87. s.sysdb.Write("blacklist-cn", countryCode, true)
  88. }
  89. func (s *Store) RemoveCountryCodeFromBlackList(countryCode string) {
  90. countryCode = strings.ToLower(countryCode)
  91. s.sysdb.Delete("blacklist-cn", countryCode)
  92. }
  93. func (s *Store) AddCountryCodeToWhitelist(countryCode string) {
  94. countryCode = strings.ToLower(countryCode)
  95. s.sysdb.Write("whitelist-cn", countryCode, true)
  96. }
  97. func (s *Store) RemoveCountryCodeFromWhitelist(countryCode string) {
  98. countryCode = strings.ToLower(countryCode)
  99. s.sysdb.Delete("whitelist-cn", countryCode)
  100. }
  101. func (s *Store) IsCountryCodeBlacklisted(countryCode string) bool {
  102. countryCode = strings.ToLower(countryCode)
  103. var isBlacklisted bool = false
  104. s.sysdb.Read("blacklist-cn", countryCode, &isBlacklisted)
  105. return isBlacklisted
  106. }
  107. func (s *Store) IsCountryCodeWhitelisted(countryCode string) bool {
  108. countryCode = strings.ToLower(countryCode)
  109. var isWhitelisted bool = false
  110. s.sysdb.Read("whitelist-cn", countryCode, &isWhitelisted)
  111. return isWhitelisted
  112. }
  113. func (s *Store) GetAllBlacklistedCountryCode() []string {
  114. bannedCountryCodes := []string{}
  115. entries, err := s.sysdb.ListTable("blacklist-cn")
  116. if err != nil {
  117. return bannedCountryCodes
  118. }
  119. for _, keypairs := range entries {
  120. ip := string(keypairs[0])
  121. bannedCountryCodes = append(bannedCountryCodes, ip)
  122. }
  123. return bannedCountryCodes
  124. }
  125. func (s *Store) GetAllWhitelistedCountryCode() []string {
  126. whitelistedCountryCode := []string{}
  127. entries, err := s.sysdb.ListTable("whitelist-cn")
  128. if err != nil {
  129. return whitelistedCountryCode
  130. }
  131. for _, keypairs := range entries {
  132. ip := string(keypairs[0])
  133. whitelistedCountryCode = append(whitelistedCountryCode, ip)
  134. }
  135. return whitelistedCountryCode
  136. }
  137. /*
  138. IP based black / whitelist
  139. */
  140. func (s *Store) AddIPToBlackList(ipAddr string) {
  141. s.sysdb.Write("blacklist-ip", ipAddr, true)
  142. }
  143. func (s *Store) RemoveIPFromBlackList(ipAddr string) {
  144. s.sysdb.Delete("blacklist-ip", ipAddr)
  145. }
  146. func (s *Store) AddIPToWhiteList(ipAddr string) {
  147. s.sysdb.Write("whitelist-ip", ipAddr, true)
  148. }
  149. func (s *Store) RemoveIPFromWhiteList(ipAddr string) {
  150. s.sysdb.Delete("whitelist-ip", ipAddr)
  151. }
  152. func (s *Store) IsIPBlacklisted(ipAddr string) bool {
  153. var isBlacklisted bool = false
  154. s.sysdb.Read("blacklist-ip", ipAddr, &isBlacklisted)
  155. if isBlacklisted {
  156. return true
  157. }
  158. //Check for IP wildcard and CIRD rules
  159. AllBlacklistedIps := s.GetAllBlacklistedIp()
  160. for _, blacklistRule := range AllBlacklistedIps {
  161. wildcardMatch := MatchIpWildcard(ipAddr, blacklistRule)
  162. if wildcardMatch {
  163. return true
  164. }
  165. cidrMatch := MatchIpCIDR(ipAddr, blacklistRule)
  166. if cidrMatch {
  167. return true
  168. }
  169. }
  170. return false
  171. }
  172. func (s *Store) IsIPWhitelisted(ipAddr string) bool {
  173. var isBlacklisted bool = false
  174. s.sysdb.Read("whitelist-ip", ipAddr, &isBlacklisted)
  175. if isBlacklisted {
  176. return true
  177. }
  178. //Check for IP wildcard and CIRD rules
  179. AllBlacklistedIps := s.GetAllBlacklistedIp()
  180. for _, blacklistRule := range AllBlacklistedIps {
  181. wildcardMatch := MatchIpWildcard(ipAddr, blacklistRule)
  182. if wildcardMatch {
  183. return true
  184. }
  185. cidrMatch := MatchIpCIDR(ipAddr, blacklistRule)
  186. if cidrMatch {
  187. return true
  188. }
  189. }
  190. return false
  191. }
  192. func (s *Store) GetAllBlacklistedIp() []string {
  193. bannedIps := []string{}
  194. entries, err := s.sysdb.ListTable("blacklist-ip")
  195. if err != nil {
  196. return bannedIps
  197. }
  198. for _, keypairs := range entries {
  199. ip := string(keypairs[0])
  200. bannedIps = append(bannedIps, ip)
  201. }
  202. return bannedIps
  203. }
  204. func (s *Store) GetAllWhitelistedIp() []string {
  205. whitelistedIp := []string{}
  206. entries, err := s.sysdb.ListTable("whitelist-ip")
  207. if err != nil {
  208. return whitelistedIp
  209. }
  210. for _, keypairs := range entries {
  211. ip := string(keypairs[0])
  212. whitelistedIp = append(whitelistedIp, ip)
  213. }
  214. return whitelistedIp
  215. }
  216. /*
  217. Check if a IP address is blacklisted, in either country or IP blacklist
  218. IsBlacklisted default return is false (allow access)
  219. */
  220. func (s *Store) IsBlacklisted(ipAddr string) bool {
  221. if !s.BlacklistEnabled {
  222. //Blacklist not enabled. Always return false
  223. return false
  224. }
  225. if ipAddr == "" {
  226. //Unable to get the target IP address
  227. return false
  228. }
  229. countryCode, err := s.ResolveCountryCodeFromIP(ipAddr)
  230. if err != nil {
  231. return false
  232. }
  233. if s.IsCountryCodeBlacklisted(countryCode.CountryIsoCode) {
  234. return true
  235. }
  236. if s.IsIPBlacklisted(ipAddr) {
  237. return true
  238. }
  239. return false
  240. }
  241. /*
  242. IsWhitelisted check if a given IP address is in the current
  243. server's white list.
  244. Note that the Whitelist default result is true even
  245. when encountered error
  246. */
  247. func (s *Store) IsWhitelisted(ipAddr string) bool {
  248. if !s.WhitelistEnabled {
  249. //Whitelist not enabled. Always return true (allow access)
  250. return true
  251. }
  252. if ipAddr == "" {
  253. //Unable to get the target IP address, assume ok
  254. return true
  255. }
  256. countryCode, err := s.ResolveCountryCodeFromIP(ipAddr)
  257. if err != nil {
  258. return true
  259. }
  260. if s.IsCountryCodeWhitelisted(countryCode.CountryIsoCode) {
  261. return true
  262. }
  263. if s.IsIPWhitelisted(ipAddr) {
  264. return true
  265. }
  266. return false
  267. }
  268. func (s *Store) GetRequesterCountryISOCode(r *http.Request) string {
  269. ipAddr := GetRequesterIP(r)
  270. if ipAddr == "" {
  271. return ""
  272. }
  273. countryCode, err := s.ResolveCountryCodeFromIP(ipAddr)
  274. if err != nil {
  275. return ""
  276. }
  277. return countryCode.CountryIsoCode
  278. }