|
@@ -10,8 +10,9 @@ import (
|
|
)
|
|
)
|
|
|
|
|
|
type Store struct {
|
|
type Store struct {
|
|
- geodb *geoip2.Reader
|
|
|
|
- sysdb *database.Database
|
|
|
|
|
|
+ Enabled bool
|
|
|
|
+ geodb *geoip2.Reader
|
|
|
|
+ sysdb *database.Database
|
|
}
|
|
}
|
|
|
|
|
|
type CountryInfo struct {
|
|
type CountryInfo struct {
|
|
@@ -35,12 +36,26 @@ func NewGeoDb(sysdb *database.Database, dbfile string) (*Store, error) {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ err = sysdb.NewTable("blacklist")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ blacklistEnabled := false
|
|
|
|
+ sysdb.Read("blacklist", "enabled", &blacklistEnabled)
|
|
|
|
+
|
|
return &Store{
|
|
return &Store{
|
|
- geodb: db,
|
|
|
|
- sysdb: sysdb,
|
|
|
|
|
|
+ Enabled: blacklistEnabled,
|
|
|
|
+ geodb: db,
|
|
|
|
+ sysdb: sysdb,
|
|
}, nil
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (s *Store) ToggleBlacklist(enabled bool) {
|
|
|
|
+ s.sysdb.Write("blacklist", "enabled", enabled)
|
|
|
|
+ s.Enabled = enabled
|
|
|
|
+}
|
|
|
|
+
|
|
func (s *Store) ResolveCountryCodeFromIP(ipstring string) (*CountryInfo, error) {
|
|
func (s *Store) ResolveCountryCodeFromIP(ipstring string) (*CountryInfo, error) {
|
|
// If you are using strings that may be invalid, check that ip is not nil
|
|
// If you are using strings that may be invalid, check that ip is not nil
|
|
ip := net.ParseIP(ipstring)
|
|
ip := net.ParseIP(ipstring)
|
|
@@ -97,7 +112,25 @@ func (s *Store) RemoveIPFromBlackList(ipAddr string) {
|
|
func (s *Store) IsIPBlacklisted(ipAddr string) bool {
|
|
func (s *Store) IsIPBlacklisted(ipAddr string) bool {
|
|
var isBlacklisted bool = false
|
|
var isBlacklisted bool = false
|
|
s.sysdb.Read("blacklist-ip", ipAddr, &isBlacklisted)
|
|
s.sysdb.Read("blacklist-ip", ipAddr, &isBlacklisted)
|
|
- return isBlacklisted
|
|
|
|
|
|
+ if isBlacklisted {
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Check for IP wildcard and CIRD rules
|
|
|
|
+ AllBlacklistedIps := s.GetAllBlacklistedIp()
|
|
|
|
+ for _, blacklistRule := range AllBlacklistedIps {
|
|
|
|
+ wildcardMatch := MatchIpWildcard(ipAddr, blacklistRule)
|
|
|
|
+ if wildcardMatch {
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cidrMatch := MatchIpCIDR(ipAddr, blacklistRule)
|
|
|
|
+ if cidrMatch {
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return false
|
|
}
|
|
}
|
|
|
|
|
|
func (s *Store) GetAllBlacklistedIp() []string {
|
|
func (s *Store) GetAllBlacklistedIp() []string {
|
|
@@ -117,6 +150,11 @@ func (s *Store) GetAllBlacklistedIp() []string {
|
|
|
|
|
|
//Check if a IP address is blacklisted, in either country or IP blacklist
|
|
//Check if a IP address is blacklisted, in either country or IP blacklist
|
|
func (s *Store) IsBlacklisted(ipAddr string) bool {
|
|
func (s *Store) IsBlacklisted(ipAddr string) bool {
|
|
|
|
+ if !s.Enabled {
|
|
|
|
+ //Blacklist not enabled. Always return false
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+
|
|
if ipAddr == "" {
|
|
if ipAddr == "" {
|
|
//Unable to get the target IP address
|
|
//Unable to get the target IP address
|
|
return false
|
|
return false
|