mirror of
https://github.com/safing/portmaster
synced 2025-09-04 19:49:15 +00:00
Change worning for invalid lines in filter list
This commit is contained in:
parent
3d11a1029b
commit
fa1480d4c1
3 changed files with 37 additions and 31 deletions
|
@ -14,7 +14,7 @@ var (
|
||||||
var getFilePath config.StringOption
|
var getFilePath config.StringOption
|
||||||
|
|
||||||
func registerConfig() error {
|
func registerConfig() error {
|
||||||
help := `File that contains list of all domains, Ip addresses, country codes and autonomous system that you want to block, where each entry is on a new line.
|
help := `The file should contain list of all domains, Ip addresses, country codes and autonomous system that you want to block, where each entry is on a new line.
|
||||||
Lines that start with a '#' symbol are ignored.
|
Lines that start with a '#' symbol are ignored.
|
||||||
Everything after the first space/tab is ignored.
|
Everything after the first space/tab is ignored.
|
||||||
Example:
|
Example:
|
||||||
|
|
|
@ -23,9 +23,9 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
numberOfZeroIPsUntilWarning = 100
|
rationForInvalidLinesUntilWarning = 0.1
|
||||||
parseStatusNotificationID = "customlists:parse-status"
|
parseStatusNotificationID = "customlists:parse-status"
|
||||||
zeroIPNotificationID = "customlists:too-many-zero-ips"
|
zeroIPNotificationID = "customlists:too-many-zero-ips"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initFilterLists() {
|
func initFilterLists() {
|
||||||
|
@ -62,15 +62,20 @@ func parseFile(filePath string) error {
|
||||||
module.Warning(parseStatusNotificationID, "Failed to open custom filter list", err.Error())
|
module.Warning(parseStatusNotificationID, "Failed to open custom filter list", err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer func() { _ = file.Close() }()
|
||||||
|
|
||||||
var numberOfZeroIPs uint64
|
var allLinesCount uint64
|
||||||
|
var invalidLinesCount uint64
|
||||||
|
|
||||||
// read filter file line by line.
|
// read filter file line by line.
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
// the scanner will error out if the line is greater than 64K, in this case it is enough.
|
// the scanner will error out if the line is greater than 64K, in this case it is enough.
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
parseLine(scanner.Text(), &numberOfZeroIPs)
|
allLinesCount++
|
||||||
|
// parse and count invalid lines (comment, empty lines, zero IPs...)
|
||||||
|
if !parseLine(scanner.Text()) {
|
||||||
|
invalidLinesCount++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for scanner error.
|
// check for scanner error.
|
||||||
|
@ -78,9 +83,13 @@ func parseFile(filePath string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if numberOfZeroIPs >= numberOfZeroIPsUntilWarning {
|
var invalidLinesRation float32 = float32(invalidLinesCount) / float32(allLinesCount)
|
||||||
log.Warning("intel/customlists: Too many zero IP addresses.")
|
|
||||||
module.Warning(zeroIPNotificationID, "Too many zero IP addresses. Check your custom filter list.", "Hosts file format is not spported.")
|
if invalidLinesRation > rationForInvalidLinesUntilWarning {
|
||||||
|
log.Warning("intel/customlists: Too many invalid lines")
|
||||||
|
module.Warning(zeroIPNotificationID, "Check your custom filter list, there is too many invalid lines",
|
||||||
|
fmt.Sprintf(`There are %d from total %d lines that we flagged as invalid.
|
||||||
|
Check if you are using the correct file format or if the path to the custom filter list is correct.`, invalidLinesCount, allLinesCount))
|
||||||
} else {
|
} else {
|
||||||
module.Resolve(zeroIPNotificationID)
|
module.Resolve(zeroIPNotificationID)
|
||||||
}
|
}
|
||||||
|
@ -105,59 +114,56 @@ func parseFile(filePath string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseLine(line string, numberOfZeroIPs *uint64) {
|
func parseLine(line string) bool {
|
||||||
// everything after the first field will be ignored.
|
// everything after the first field will be ignored.
|
||||||
fields := strings.Fields(line)
|
fields := strings.Fields(line)
|
||||||
|
|
||||||
// ignore empty lines.
|
// ignore empty lines.
|
||||||
if len(fields) == 0 {
|
if len(fields) == 0 {
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
field := fields[0]
|
field := fields[0]
|
||||||
|
|
||||||
// ignore comments
|
// ignore comments
|
||||||
if field[0] == '#' {
|
if field[0] == '#' {
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if it'a a country code.
|
// check if it'a a country code.
|
||||||
if isCountryCode(field) {
|
if isCountryCode(field) {
|
||||||
countryCodesFilterList[field] = struct{}{}
|
countryCodesFilterList[field] = struct{}{}
|
||||||
return
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to parse IP address.
|
// try to parse IP address.
|
||||||
ip := net.ParseIP(field)
|
ip := net.ParseIP(field)
|
||||||
if ip != nil {
|
if ip != nil {
|
||||||
ipAddressesFilterList[ip.String()] = struct{}{}
|
|
||||||
|
|
||||||
// check for zero ip.
|
// check for zero ip.
|
||||||
if bytes.Compare(ip.To4(), net.IPv4zero) == 0 || bytes.Compare(ip.To16(), net.IPv6zero) == 0 {
|
if bytes.Compare(ip, net.IPv4zero) == 0 || bytes.Compare(ip, net.IPv6zero) == 0 {
|
||||||
// check if its zero ip.
|
return false
|
||||||
for i := 0; i < len(ip); i++ {
|
|
||||||
if ip[i] != 0 {
|
|
||||||
*numberOfZeroIPs++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
|
||||||
|
ipAddressesFilterList[ip.String()] = struct{}{}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if it's a Autonomous system (example AS123).
|
// check if it's a Autonomous system (example AS123).
|
||||||
if isAutonomousSystem(field) {
|
if isAutonomousSystem(field) {
|
||||||
asNumber, err := strconv.ParseUint(field[2:], 10, 32)
|
asNumber, err := strconv.ParseUint(field[2:], 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
autonomousSystemsFilterList[uint(asNumber)] = struct{}{}
|
autonomousSystemsFilterList[uint(asNumber)] = struct{}{}
|
||||||
return
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if it's a domain.
|
// check if it's a domain.
|
||||||
domain := dns.Fqdn(field)
|
domain := dns.Fqdn(field)
|
||||||
if netutils.IsValidFqdn(domain) {
|
if netutils.IsValidFqdn(domain) {
|
||||||
domainsFilterList[domain] = struct{}{}
|
domainsFilterList[domain] = struct{}{}
|
||||||
return
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ func init() {
|
||||||
func prep() error {
|
func prep() error {
|
||||||
initFilterLists()
|
initFilterLists()
|
||||||
|
|
||||||
// register the config in the ui
|
// register the config in the ui.
|
||||||
err := registerConfig()
|
err := registerConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -64,8 +64,8 @@ func start() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// create parser task and enqueue for execution. "checkAndUpdateFilterList" will schedule the next execution
|
// create parser task and enqueue for execution. "checkAndUpdateFilterList" will schedule the next execution.
|
||||||
parserTask = module.NewTask("intel/customlists file update check", func(context.Context, *modules.Task) error {
|
parserTask = module.NewTask("intel/customlists:file-update-check", func(context.Context, *modules.Task) error {
|
||||||
_ = checkAndUpdateFilterList()
|
_ = checkAndUpdateFilterList()
|
||||||
return nil
|
return nil
|
||||||
}).Schedule(time.Now().Add(20 * time.Second))
|
}).Schedule(time.Now().Add(20 * time.Second))
|
||||||
|
@ -119,7 +119,7 @@ func LookupDomain(fullDomain string, filterSubdomains bool) (bool, string) {
|
||||||
defer filterListLock.RUnlock()
|
defer filterListLock.RUnlock()
|
||||||
|
|
||||||
if filterSubdomains {
|
if filterSubdomains {
|
||||||
// check if domain is in the list and all its subdomains
|
// check if domain is in the list and all its subdomains.
|
||||||
listOfDomains := splitDomain(fullDomain)
|
listOfDomains := splitDomain(fullDomain)
|
||||||
for _, domain := range listOfDomains {
|
for _, domain := range listOfDomains {
|
||||||
_, ok := domainsFilterList[domain]
|
_, ok := domainsFilterList[domain]
|
||||||
|
|
Loading…
Add table
Reference in a new issue