Created
February 9, 2020 22:17
-
-
Save qdm12/bcee2161b58c9410095cb25e5d701194 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func buildBlockedIPs(client network.Client, blockMalicious, blockAds, blockSurveillance bool, | |
privateAddresses []string) (lines []string, errs []error) { | |
chResults := make(chan []string) | |
chError := make(chan error) | |
listsLeftToFetch := 0 | |
if blockMalicious { | |
listsLeftToFetch++ | |
go func() { | |
results, err := getList(client, string(constants.MaliciousBlockListIPsURL)) | |
chResults <- results | |
chError <- err | |
}() | |
} | |
if blockAds { | |
listsLeftToFetch++ | |
go func() { | |
results, err := getList(client, string(constants.AdsBlockListIPsURL)) | |
chResults <- results | |
chError <- err | |
}() | |
} | |
if blockSurveillance { | |
listsLeftToFetch++ | |
go func() { | |
results, err := getList(client, string(constants.SurveillanceBlockListIPsURL)) | |
chResults <- results | |
chError <- err | |
}() | |
} | |
uniqueResults := make(map[string]struct{}) | |
for listsLeftToFetch > 0 { | |
select { | |
case results := <-chResults: | |
for _, result := range results { | |
uniqueResults[result] = struct{}{} | |
} | |
case err := <-chError: | |
listsLeftToFetch-- | |
if err != nil { | |
errs = append(errs, err) | |
} | |
} | |
} | |
for _, privateAddress := range privateAddresses { | |
uniqueResults[privateAddress] = struct{}{} | |
} | |
for result := range uniqueResults { | |
lines = append(lines, " private-address: "+result) | |
} | |
return lines, errs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment