Bug fixes for custom filter list
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package customlists
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/safing/portbase/config"
|
||||
)
|
||||
|
||||
@@ -18,28 +16,28 @@ var (
|
||||
)
|
||||
|
||||
func registerConfig() error {
|
||||
help := strings.ReplaceAll(`Put all domains, Ip addresses, country codes and autonomous system that you want to block in a file in which each entry is on a new line.
|
||||
Lines that start with a '#' symbol are ignored.
|
||||
Everything after the first space/tab is ignored.
|
||||
Example:
|
||||
"""
|
||||
# Domains:
|
||||
help := `Put all domains, Ip addresses, country codes and autonomous system that you want to block in a file in where each entry is on a new line.
|
||||
Lines that start with a '#' symbol are ignored.
|
||||
Everything after the first space/tab is ignored.
|
||||
Example:
|
||||
#############
|
||||
\# Domains:
|
||||
example.com
|
||||
google.com
|
||||
|
||||
# IP addresses
|
||||
\# IP addresses
|
||||
1.2.3.4
|
||||
4.3.2.1
|
||||
|
||||
# Countries
|
||||
\# Countries
|
||||
AU
|
||||
BG
|
||||
|
||||
# Autonomous Systems
|
||||
\# Autonomous Systems
|
||||
AS123
|
||||
"""
|
||||
#############
|
||||
> * All the records are stored in RAM, careful with large block lists.
|
||||
> * Hosts files are not supported.`, "\"", "`")
|
||||
> * Hosts files are not supported.`
|
||||
|
||||
// register a setting for the file path in the ui
|
||||
err := config.Register(&config.Option{
|
||||
|
||||
@@ -23,6 +23,12 @@ var (
|
||||
const numberOfZeroIPsUntilWarning = 100
|
||||
|
||||
func parseFile(filePath string) error {
|
||||
// reset all maps, previous (if any) settings will be lost
|
||||
countryCodesFilterList = make(map[string]struct{})
|
||||
ipAddressesFilterList = make(map[string]struct{})
|
||||
autonomousSystemsFilterList = make(map[uint]struct{})
|
||||
domainsFilterList = make(map[string]struct{})
|
||||
|
||||
// ignore empty file path
|
||||
if filePath == "" {
|
||||
return nil
|
||||
@@ -36,12 +42,6 @@ func parseFile(filePath string) error {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// initialize maps to hold data from the file
|
||||
countryCodesFilterList = make(map[string]struct{})
|
||||
ipAddressesFilterList = make(map[string]struct{})
|
||||
autonomousSystemsFilterList = make(map[uint]struct{})
|
||||
domainsFilterList = make(map[string]struct{})
|
||||
|
||||
var numberOfZeroIPs uint64
|
||||
|
||||
// read filter file line by line
|
||||
|
||||
@@ -62,7 +62,7 @@ func prep() error {
|
||||
|
||||
func start() error {
|
||||
// register timer to run every periodically and check for file updates
|
||||
module.NewTask("intel/customlists list file update check", func(context.Context, *modules.Task) error {
|
||||
module.NewTask("intel/customlists file update check", func(context.Context, *modules.Task) error {
|
||||
_ = checkAndUpdateFilterList()
|
||||
return nil
|
||||
}).Repeat(10 * time.Minute)
|
||||
@@ -78,11 +78,10 @@ func checkAndUpdateFilterList() error {
|
||||
|
||||
// get path and try to get its info
|
||||
filePath := getFilePath()
|
||||
fileInfo, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
return nil
|
||||
modifiedTime := time.Now()
|
||||
if fileInfo, err := os.Stat(filePath); err == nil {
|
||||
modifiedTime = fileInfo.ModTime()
|
||||
}
|
||||
modifiedTime := fileInfo.ModTime()
|
||||
|
||||
// check if file path has changed or if modified time has changed
|
||||
if filterListFilePath != filePath || !filterListFileModifiedTime.Equal(modifiedTime) {
|
||||
@@ -105,6 +104,7 @@ func LookupIP(ip *net.IP) bool {
|
||||
// LookupDomain checks if the Domain is in a custom filter list
|
||||
func LookupDomain(fullDomain string, filterSubdomains bool) bool {
|
||||
if filterSubdomains {
|
||||
// check if domain is in the list and all its subdomains
|
||||
listOfDomains := splitDomain(fullDomain)
|
||||
for _, domain := range listOfDomains {
|
||||
_, ok := domainsFilterList[domain]
|
||||
@@ -113,6 +113,7 @@ func LookupDomain(fullDomain string, filterSubdomains bool) bool {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//check only if the domain is in the list
|
||||
_, ok := domainsFilterList[fullDomain]
|
||||
return ok
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user