(filePath string)
| 57 | } |
| 58 | |
| 59 | func parseFile(filePath string) error { |
| 60 | // Reset all maps, previous (if any) settings will be lost. |
| 61 | for key := range countryCodesFilterList { |
| 62 | delete(countryCodesFilterList, key) |
| 63 | } |
| 64 | for key := range ipAddressesFilterList { |
| 65 | delete(ipAddressesFilterList, key) |
| 66 | } |
| 67 | for key := range autonomousSystemsFilterList { |
| 68 | delete(autonomousSystemsFilterList, key) |
| 69 | } |
| 70 | for key := range domainsFilterList { |
| 71 | delete(domainsFilterList, key) |
| 72 | } |
| 73 | |
| 74 | // Ignore empty file path. |
| 75 | if filePath == "" { |
| 76 | return nil |
| 77 | } |
| 78 | |
| 79 | // Open the file if possible |
| 80 | file, err := os.Open(filePath) |
| 81 | if err != nil { |
| 82 | log.Warningf("intel/customlists: failed to parse file %s", err) |
| 83 | module.states.Add(mgr.State{ |
| 84 | ID: parseWarningNotificationID, |
| 85 | Name: "Failed to open custom filter list", |
| 86 | Message: err.Error(), |
| 87 | Type: mgr.StateTypeWarning, |
| 88 | }) |
| 89 | return err |
| 90 | } else { |
| 91 | module.states.Remove(parseWarningNotificationID) |
| 92 | } |
| 93 | defer func() { _ = file.Close() }() |
| 94 | |
| 95 | var allLinesCount uint64 |
| 96 | var invalidLinesCount uint64 |
| 97 | |
| 98 | // Read filter file line by line. |
| 99 | scanner := bufio.NewScanner(file) |
| 100 | // The scanner will error out if the line is greater than 64K, in this case it is enough. |
| 101 | for scanner.Scan() { |
| 102 | allLinesCount++ |
| 103 | // Parse and count invalid lines (comment, empty lines, zero IPs...) |
| 104 | if !parseLine(scanner.Text()) { |
| 105 | invalidLinesCount++ |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Check for scanner error. |
| 110 | if err := scanner.Err(); err != nil { |
| 111 | return err |
| 112 | } |
| 113 | |
| 114 | invalidLinesRation := float32(invalidLinesCount) / float32(allLinesCount) |
| 115 | |
| 116 | if invalidLinesRation > rationForInvalidLinesUntilWarning { |
no test coverage detected