From cf30014e9da8ac9a6d75966cb4cfa9e7acab2d88 Mon Sep 17 00:00:00 2001 From: Vladimir Stoilov Date: Wed, 27 Jul 2022 07:47:48 +0200 Subject: [PATCH] Bug fixes for custom filter list --- intel/customlists/config.go | 24 +++++++++++------------- intel/customlists/lists.go | 12 ++++++------ intel/customlists/module.go | 11 ++++++----- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/intel/customlists/config.go b/intel/customlists/config.go index 08a947b6..9b8e0ba7 100644 --- a/intel/customlists/config.go +++ b/intel/customlists/config.go @@ -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{ diff --git a/intel/customlists/lists.go b/intel/customlists/lists.go index 753541fe..ad066d0d 100644 --- a/intel/customlists/lists.go +++ b/intel/customlists/lists.go @@ -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 diff --git a/intel/customlists/module.go b/intel/customlists/module.go index 18c90c8f..03d87f67 100644 --- a/intel/customlists/module.go +++ b/intel/customlists/module.go @@ -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 }