mirror of
https://github.com/safing/portmaster
synced 2025-09-15 17:29:42 +00:00
Refactoring
This commit is contained in:
parent
93367b64df
commit
3d11a1029b
2 changed files with 45 additions and 41 deletions
|
@ -11,9 +11,7 @@ var (
|
||||||
cfgOptionCustomListCategoryAnnotation = "Filter Lists"
|
cfgOptionCustomListCategoryAnnotation = "Filter Lists"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var getFilePath config.StringOption
|
||||||
getFilePath func() string
|
|
||||||
)
|
|
||||||
|
|
||||||
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 := `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.
|
||||||
|
|
|
@ -2,6 +2,7 @@ package customlists
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -22,9 +23,9 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
numberOfZeroIPsUntilWarning = 100
|
numberOfZeroIPsUntilWarning = 100
|
||||||
customFilterListStatusNotificationID = "intel/customlists_status"
|
parseStatusNotificationID = "customlists:parse-status"
|
||||||
customFilterListZeroIPNotificationID = "intel/customlists_zeroip"
|
zeroIPNotificationID = "customlists:too-many-zero-ips"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initFilterLists() {
|
func initFilterLists() {
|
||||||
|
@ -35,7 +36,7 @@ func initFilterLists() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseFile(filePath string) error {
|
func parseFile(filePath string) error {
|
||||||
// reset all maps, previous (if any) settings will be lost
|
// reset all maps, previous (if any) settings will be lost.
|
||||||
for key := range countryCodesFilterList {
|
for key := range countryCodesFilterList {
|
||||||
delete(countryCodesFilterList, key)
|
delete(countryCodesFilterList, key)
|
||||||
}
|
}
|
||||||
|
@ -49,7 +50,7 @@ func parseFile(filePath string) error {
|
||||||
delete(domainsFilterList, key)
|
delete(domainsFilterList, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ignore empty file path
|
// ignore empty file path.
|
||||||
if filePath == "" {
|
if filePath == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -58,46 +59,35 @@ func parseFile(filePath string) error {
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("intel/customlists: failed to parse file %q ", err)
|
log.Warningf("intel/customlists: failed to parse file %q ", err)
|
||||||
// notifications.NotifyWarn("intel/customlists parse failed", "Failed to open custom filter list")
|
module.Warning(parseStatusNotificationID, "Failed to open custom filter list", err.Error())
|
||||||
notifications.Notify(¬ifications.Notification{
|
|
||||||
EventID: customFilterListStatusNotificationID,
|
|
||||||
Type: notifications.Warning,
|
|
||||||
Title: "Failed to open custom filter list",
|
|
||||||
Message: err.Error(),
|
|
||||||
ShowOnSystem: false,
|
|
||||||
AvailableActions: []*notifications.Action{
|
|
||||||
{
|
|
||||||
ID: "ack",
|
|
||||||
Text: "OK",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
var numberOfZeroIPs uint64
|
var numberOfZeroIPs 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)
|
parseLine(scanner.Text(), &numberOfZeroIPs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for scanner error
|
// check for scanner error.
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if numberOfZeroIPs >= numberOfZeroIPsUntilWarning {
|
if numberOfZeroIPs >= numberOfZeroIPsUntilWarning {
|
||||||
log.Warning("intel/customlists: Too many zero IP addresses.")
|
log.Warning("intel/customlists: Too many zero IP addresses.")
|
||||||
notifications.NotifyWarn(customFilterListZeroIPNotificationID, "Too many zero IP addresses. Check your custom filter list.", "Hosts file format is not spported.")
|
module.Warning(zeroIPNotificationID, "Too many zero IP addresses. Check your custom filter list.", "Hosts file format is not spported.")
|
||||||
|
} else {
|
||||||
|
module.Resolve(zeroIPNotificationID)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("intel/customlists: list loaded successful: %s", filePath)
|
log.Infof("intel/customlists: list loaded successful: %s", filePath)
|
||||||
|
|
||||||
notifications.NotifyInfo(customFilterListStatusNotificationID,
|
notifications.NotifyInfo(parseStatusNotificationID,
|
||||||
"Custom filter list loaded successfully.",
|
"Custom filter list loaded successfully.",
|
||||||
fmt.Sprintf(`Custom filter list loaded successfully from file %s
|
fmt.Sprintf(`Custom filter list loaded successfully from file %s
|
||||||
%d domains
|
%d domains
|
||||||
|
@ -110,48 +100,64 @@ func parseFile(filePath string) error {
|
||||||
len(autonomousSystemsFilterList),
|
len(autonomousSystemsFilterList),
|
||||||
len(domainsFilterList)))
|
len(domainsFilterList)))
|
||||||
|
|
||||||
|
module.Resolve(parseStatusNotificationID)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseLine(line string, numberOfZeroIPs *uint64) {
|
func parseLine(line string, numberOfZeroIPs *uint64) {
|
||||||
// ignore empty lines and comment lines
|
// everything after the first field will be ignored.
|
||||||
if len(line) == 0 || line[0] == '#' {
|
fields := strings.Fields(line)
|
||||||
|
|
||||||
|
// ignore empty lines.
|
||||||
|
if len(fields) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// everything after the first field will be ignored
|
field := fields[0]
|
||||||
field := strings.Fields(line)[0]
|
|
||||||
|
|
||||||
// check if it'a a country code
|
// ignore comments
|
||||||
if isCountryCode(field) {
|
if field[0] == '#' {
|
||||||
countryCodesFilterList[field] = struct{}{}
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to parse IP address
|
// check if it'a a country code.
|
||||||
|
if isCountryCode(field) {
|
||||||
|
countryCodesFilterList[field] = struct{}{}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to parse IP address.
|
||||||
ip := net.ParseIP(field)
|
ip := net.ParseIP(field)
|
||||||
if ip != nil {
|
if ip != nil {
|
||||||
ipAddressesFilterList[ip.String()] = struct{}{}
|
ipAddressesFilterList[ip.String()] = struct{}{}
|
||||||
|
|
||||||
// check if its zero ip
|
// check for zero ip.
|
||||||
for i := 0; i < len(ip); i++ {
|
if bytes.Compare(ip.To4(), net.IPv4zero) == 0 || bytes.Compare(ip.To16(), net.IPv6zero) == 0 {
|
||||||
if ip[i] != 0 {
|
// check if its zero ip.
|
||||||
*numberOfZeroIPs++
|
for i := 0; i < len(ip); i++ {
|
||||||
|
if ip[i] != 0 {
|
||||||
|
*numberOfZeroIPs++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
}
|
}
|
||||||
autonomousSystemsFilterList[uint(asNumber)] = struct{}{}
|
autonomousSystemsFilterList[uint(asNumber)] = struct{}{}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue