Skip to content

Commit

Permalink
fixed golangci-lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R committed Oct 13, 2021
1 parent e5b44f4 commit 57036aa
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
45 changes: 31 additions & 14 deletions lists/list_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 92,11 @@ func NewListCache(t ListCacheType, groupToLinks map[string][]string, refreshPeri
listType: t,
}
initError := b.refresh(true)

if len(initError) == 0 {
go periodicUpdate(b)
}

return b, initError
}

Expand Down Expand Up @@ -179,11 181,13 @@ func (b *ListCache) Refresh() {
}
func (b *ListCache) refresh(init bool) []error {
res := []error{}

for group, links := range b.groupToLinks {
cacheForGroup, errors := b.createCacheForGroup(links)
if len(errors) > 0 {
res = append(res, errors...)
}

if cacheForGroup != nil {
b.lock.Lock()
b.groupCaches[group] = cacheForGroup
Expand All @@ -196,6 200,7 @@ func (b *ListCache) refresh(init bool) []error {
logger().Warn("Populating of group cache failed, leaving items from last successful download in cache")
}
}

if b.groupCaches[group] != nil {
evt.Bus().Publish(evt.BlockingCacheGroupChanged, b.listType, group, b.groupCaches[group].elementCount())

Expand All @@ -205,6 210,7 @@ func (b *ListCache) refresh(init bool) []error {
}).Info("group import finished")
}
}

return res
}

Expand All @@ -226,25 232,28 @@ func (b *ListCache) downloadFile(link string) (io.ReadCloser, error) {
if resp, err = client.Get(link); err == nil {
if resp.StatusCode == http.StatusOK {
return resp.Body, nil
} else {
logger().WithField("link", link).WithField("attempt",
attempt).Warnf("Got status code %d", resp.StatusCode)
}

logger().WithField("link", link).WithField("attempt",
attempt).Warnf("Got status code %d", resp.StatusCode)

_ = resp.Body.Close()

err = fmt.Errorf("couldn't download url '%s', got status code %d", link, resp.StatusCode)
}

var netErr net.Error

var dnsErr *net.DNSError

if errors.As(err, &netErr) && (netErr.Timeout() || netErr.Temporary()) {
logger().WithField("link", link).WithField("attempt",
attempt).Warnf("Temporary network error / Timeout occurred, retrying... %s", netErr)
} else if errors.As(err, &dnsErr) {
logger().WithField("link", link).WithField("attempt",
attempt).Warnf("Name resolution error, retrying... %s", dnsErr.Err)
}

time.Sleep(time.Second)
attempt
}
Expand Down Expand Up @@ -272,22 281,14 @@ func (b *ListCache) processFile(link string, ch chan<- groupCache, wg *sync.Wait

var err error

switch {
// link contains a line break -> this is inline list definition in YAML (with literal style Block Scalar)
case strings.ContainsAny(link, "\n"):
r = io.NopCloser(strings.NewReader(link))
// link is http(s) -> download it
case strings.HasPrefix(link, "http"):
r, err = b.downloadFile(link)
// probably path to a local file
default:
r, err = readFile(link)
}
r, err = b.getLinkReader(link)

if err != nil {
logger().Warn("error during file processing: ", err)
result.errors = append(result.errors, err)

var netErr net.Error

if errors.As(err, &netErr) && (netErr.Timeout() || netErr.Temporary()) {
// put nil to indicate the temporary error
result.cache = nil
Expand Down Expand Up @@ -323,6 324,22 @@ func (b *ListCache) processFile(link string, ch chan<- groupCache, wg *sync.Wait
ch <- result
}

func (b *ListCache) getLinkReader(link string) (r io.ReadCloser, err error) {
switch {
// link contains a line break -> this is inline list definition in YAML (with literal style Block Scalar)
case strings.ContainsAny(link, "\n"):
r = io.NopCloser(strings.NewReader(link))
// link is http(s) -> download it
case strings.HasPrefix(link, "http"):
r, err = b.downloadFile(link)
// probably path to a local file
default:
r, err = readFile(link)
}

return
}

// return only first column (see hosts format)
func processLine(line string) string {
if strings.HasPrefix(line, "#") {
Expand Down
2 changes: 2 additions & 0 deletions resolver/blocking_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 92,11 @@ func NewBlockingResolver(cfg config.BlockingConfig) (ChainedResolver, []error) {
if len(blErr) > 0 {
initErrors = append(initErrors, blErr...)
}

if len(wlErr) > 0 {
initErrors = append(initErrors, wlErr...)
}

res := &BlockingResolver{
blockHandler: blockHandler,
cfg: cfg,
Expand Down
33 changes: 18 additions & 15 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 60,9 @@ func NewServer(cfg *config.Config) (server *Server, errors []error) {
}

var httpListener, httpsListener net.Listener

var err error

router := createRouter(cfg)

if cfg.HTTPPort != "" {
Expand All @@ -84,25 86,25 @@ func NewServer(cfg *config.Config) (server *Server, errors []error) {
queryResolver, queryErrors := createQueryResolver(cfg)
if len(queryErrors) > 0 {
return nil, queryErrors
} else {
server = &Server{
dnsServers: dnsServers,
queryResolver: queryResolver,
cfg: cfg,
httpListener: httpListener,
httpsListener: httpsListener,
httpMux: router,
}
}

server.printConfiguration()
server = &Server{
dnsServers: dnsServers,
queryResolver: queryResolver,
cfg: cfg,
httpListener: httpListener,
httpsListener: httpsListener,
httpMux: router,
}

server.registerDNSHandlers()
server.registerAPIEndpoints(router)
server.printConfiguration()

registerResolverAPIEndpoints(router, queryResolver)
server.registerDNSHandlers()
server.registerAPIEndpoints(router)

return server, errors
}
registerResolverAPIEndpoints(router, queryResolver)

return server, errors
}

func registerResolverAPIEndpoints(router chi.Router, res resolver.Resolver) {
Expand Down Expand Up @@ -159,6 161,7 @@ func createUDPServer(address string) *dns.Server {

func createQueryResolver(cfg *config.Config) (resolver.Resolver, []error) {
br, brErr := resolver.NewBlockingResolver(cfg.Blocking)

return resolver.Chain(
resolver.NewIPv6Checker(cfg.DisableIPv6),
resolver.NewClientNamesResolver(cfg.ClientLookup),
Expand Down
2 changes: 2 additions & 0 deletions util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 160,13 @@ func LogOnErrorWithEntry(logEntry *logrus.Entry, message string, err error) {
func FatalOnError(message string, errors ...error) {
if errors != nil {
var nnErrors []error

for _, r := range errors {
if r != nil {
nnErrors = append(nnErrors, r)
}
}

if len(nnErrors) == 1 {
log.Log().Fatal(message, nnErrors[0])
} else if len(nnErrors) > 1 {
Expand Down

0 comments on commit 57036aa

Please sign in to comment.