diff --git a/.golangci.yml b/.golangci.yml index 8a8a6301a..281961c57 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -39,6 +39,7 @@ linters: - nilerr - nilnil - nlreturn + - nolintlint - nosprintfhostport - prealloc - predeclared @@ -51,6 +52,7 @@ linters: - unconvert - unparam - unused + - usestdlibvars - wastedassign - whitespace - wsl @@ -61,6 +63,7 @@ linters: - structcheck - deadcode - varcheck + - forbidigo disable-all: false presets: diff --git a/cache/stringcache/string_caches_benchmark_test.go b/cache/stringcache/string_caches_benchmark_test.go index f87d087b1..b83b6a2ac 100644 --- a/cache/stringcache/string_caches_benchmark_test.go +++ b/cache/stringcache/string_caches_benchmark_test.go @@ -27,7 +27,7 @@ func randString(n int) string { b := make([]byte, n) for i := range b { - b[i] = charPool[rand.Intn(len(charPool))] // nolint:gosec + b[i] = charPool[rand.Intn(len(charPool))] } return string(b) @@ -37,7 +37,7 @@ func createTestdata(count int) []string { var result []string for i := 0; i < count; i++ { - result = append(result, randString(8+rand.Intn(20))) // nolint:gosec + result = append(result, randString(8+rand.Intn(20))) } return result diff --git a/cmd/root.go b/cmd/root.go index 3dd31efab..d7948c826 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -45,8 +45,8 @@ Complete documentation is available at https://github.com/0xERR0R/blocky`, } c.PersistentFlags().StringVarP(&configPath, "config", "c", defaultConfigPath, "path to config file or folder") - c.PersistentFlags().StringVar(&apiHost, "apiHost", defaultHost, "host of blocky (API). Default overridden by config and CLI.") // nolint:lll - c.PersistentFlags().Uint16Var(&apiPort, "apiPort", defaultPort, "port of blocky (API). Default overridden by config and CLI.") // nolint:lll + c.PersistentFlags().StringVar(&apiHost, "apiHost", defaultHost, "host of blocky (API). Default overridden by config and CLI.") //nolint:lll + c.PersistentFlags().Uint16Var(&apiPort, "apiPort", defaultPort, "port of blocky (API). Default overridden by config and CLI.") //nolint:lll c.AddCommand(newRefreshCommand(), NewQueryCommand(), diff --git a/config/config.go b/config/config.go index 6cfecfe6f..93bf4f458 100644 --- a/config/config.go +++ b/config/config.go @@ -128,7 +128,7 @@ func (c *Duration) String() string { return durafmt.Parse(time.Duration(*c)).String() } -// nolint:gochecknoglobals +//nolint:gochecknoglobals var netDefaultPort = map[NetProtocol]uint16{ NetProtocolTcpUdp: udpPort, NetProtocolTcpTls: tlsPort, @@ -451,7 +451,8 @@ func extractNet(upstream string) (NetProtocol, string) { } // Config main configuration -// nolint:maligned +// +//nolint:maligned type Config struct { Upstream UpstreamConfig `yaml:"upstream"` UpstreamTimeout Duration `yaml:"upstreamTimeout" default:"2s"` @@ -608,7 +609,7 @@ type EdeConfig struct { Enable bool `yaml:"enable" default:"false"` } -// nolint:gochecknoglobals +//nolint:gochecknoglobals var ( config = &Config{} cfgLock sync.RWMutex diff --git a/evt/events.go b/evt/events.go index 12774623f..7d67f4532 100644 --- a/evt/events.go +++ b/evt/events.go @@ -36,7 +36,7 @@ const ( ApplicationStarted = "application:started" ) -// nolint +//nolint:gochecknoglobals var evtBus = EventBus.New() // Bus returns the global bus instance diff --git a/helpertest/helper.go b/helpertest/helper.go index 4adf33e64..81357f428 100644 --- a/helpertest/helper.go +++ b/helpertest/helper.go @@ -41,7 +41,7 @@ func TestServer(data string) *httptest.Server { // DoGetRequest performs a GET request func DoGetRequest(url string, fn func(w http.ResponseWriter, r *http.Request)) (*httptest.ResponseRecorder, *bytes.Buffer) { - r, _ := http.NewRequest("GET", url, nil) + r, _ := http.NewRequest(http.MethodGet, url, nil) rr := httptest.NewRecorder() handler := http.HandlerFunc(fn) diff --git a/lists/list_cache_test.go b/lists/list_cache_test.go index 3f496420b..4d82bc1dc 100644 --- a/lists/list_cache_test.go +++ b/lists/list_cache_test.go @@ -81,15 +81,15 @@ var _ = Describe("ListCache", func() { // should produce a transient error on second and third attempt data := make(chan func() (io.ReadCloser, error), 3) mockDownloader := &MockDownloader{data: data} - // nolint:unparam + //nolint:unparam data <- func() (io.ReadCloser, error) { return io.NopCloser(strings.NewReader("blocked1.com")), nil } - // nolint:unparam + //nolint:unparam data <- func() (io.ReadCloser, error) { return nil, &TransientError{inner: errors.New("boom")} } - // nolint:unparam + //nolint:unparam data <- func() (io.ReadCloser, error) { return nil, &TransientError{inner: errors.New("boom")} } @@ -401,7 +401,7 @@ func createTestListFile(dir string, totalLines int) string { w := bufio.NewWriter(file) for i := 0; i < totalLines; i++ { - fmt.Fprintln(w, RandStringBytes(8+rand.Intn(10))+".com") // nolint:gosec + fmt.Fprintln(w, RandStringBytes(8+rand.Intn(10))+".com") } w.Flush() @@ -413,7 +413,7 @@ const charpool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 func RandStringBytes(n int) string { b := make([]byte, n) for i := range b { - b[i] = charpool[rand.Intn(len(charpool))] // nolint:gosec + b[i] = charpool[rand.Intn(len(charpool))] } return string(b) diff --git a/log/logger.go b/log/logger.go index 8f3223f0d..7b5e7a661 100644 --- a/log/logger.go +++ b/log/logger.go @@ -11,7 +11,8 @@ import ( ) // Logger is the global logging instance -// nolint:gochecknoglobals +// +//nolint:gochecknoglobals var logger *logrus.Logger // FormatType format for logging ENUM( @@ -30,7 +31,7 @@ type FormatType int // ) type Level int -// nolint:gochecknoinits +//nolint:gochecknoinits func init() { logger = logrus.New() diff --git a/metrics/metrics.go b/metrics/metrics.go index 1ec99803a..b0057fc7d 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -9,7 +9,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) -// nolint +//nolint:gochecknoglobals var reg = prometheus.NewRegistry() // RegisterMetric registers prometheus collector diff --git a/redis/redis.go b/redis/redis.go index 62f795911..a40557389 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -69,7 +69,7 @@ type Client struct { func New(cfg *config.RedisConfig) (*Client, error) { // disable redis if no address is provided if cfg == nil || len(cfg.Address) == 0 { - return nil, nil // nolint:nilnil + return nil, nil //nolint:nilnil } var rdb *redis.Client diff --git a/resolver/blocking_resolver.go b/resolver/blocking_resolver.go index 0b69e16e0..e6f70c5af 100644 --- a/resolver/blocking_resolver.go +++ b/resolver/blocking_resolver.go @@ -188,7 +188,7 @@ func (r *BlockingResolver) RefreshLists() { r.whitelistMatcher.Refresh() } -// nolint:prealloc +//nolint:prealloc func (r *BlockingResolver) retrieveAllBlockingGroups() []string { groups := make(map[string]bool, len(r.cfg.BlackLists)) diff --git a/resolver/bootstrap.go b/resolver/bootstrap.go index 14f13d97e..b2661cfca 100644 --- a/resolver/bootstrap.go +++ b/resolver/bootstrap.go @@ -19,7 +19,7 @@ import ( "github.com/sirupsen/logrus" ) -// nolint:gochecknoglobals +//nolint:gochecknoglobals var ( v4v6QTypes = []dns.Type{dns.Type(dns.TypeA), dns.Type(dns.TypeAAAA)} ) @@ -162,7 +162,7 @@ func (b *Bootstrap) NewHTTPTransport() *http.Transport { return nil, err } - ip := ips[rand.Intn(len(ips))] // nolint:gosec + ip := ips[rand.Intn(len(ips))] //nolint:gosec log.WithField("ip", ip).Tracef("dialing %s", host) diff --git a/resolver/bootstrap_test.go b/resolver/bootstrap_test.go index bd3f1eccd..e2d10f200 100644 --- a/resolver/bootstrap_test.go +++ b/resolver/bootstrap_test.go @@ -73,7 +73,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { transport := sut.NewHTTPTransport() Expect(transport).ShouldNot(BeNil()) - Expect(*transport).Should(BeZero()) // nolint:govet + Expect(*transport).Should(BeZero()) //nolint:govet }) }) }) @@ -255,7 +255,7 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { Describe("HTTP Transport", func() { It("uses the bootstrap upstream", func() { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(200) + w.WriteHeader(http.StatusOK) })) DeferCleanup(server.Close) diff --git a/resolver/hosts_file_resolver.go b/resolver/hosts_file_resolver.go index 88e09242c..0c5571f6c 100644 --- a/resolver/hosts_file_resolver.go +++ b/resolver/hosts_file_resolver.go @@ -159,7 +159,7 @@ type host struct { Aliases []string } -// nolint:funlen +//nolint:funlen func (r *HostsFileResolver) parseHostsFile() error { const minColumnCount = 2 diff --git a/resolver/hosts_file_resolver_test.go b/resolver/hosts_file_resolver_test.go index bfb1c7072..60954f180 100644 --- a/resolver/hosts_file_resolver_test.go +++ b/resolver/hosts_file_resolver_test.go @@ -50,7 +50,6 @@ var _ = Describe("HostsFileResolver", func() { When("Hosts file cannot be located", func() { BeforeEach(func() { sut = NewHostsFileResolver(config.HostsFileConfig{ - //nolint:gosec Filepath: fmt.Sprintf("/tmp/blocky/file-%d", rand.Uint64()), HostsTTL: config.Duration(time.Duration(TTL) * time.Second), }).(*HostsFileResolver) diff --git a/resolver/noop_resolver.go b/resolver/noop_resolver.go index fd0ffcdaa..89335ae32 100644 --- a/resolver/noop_resolver.go +++ b/resolver/noop_resolver.go @@ -4,7 +4,7 @@ import ( "github.com/0xERR0R/blocky/model" ) -var NoResponse = &model.Response{} // nolint:gochecknoglobals +var NoResponse = &model.Response{} //nolint:gochecknoglobals // NoOpResolver is used to finish a resolver branch as created in RewriterResolver type NoOpResolver struct{} diff --git a/resolver/query_logging_resolver.go b/resolver/query_logging_resolver.go index 649d7d30c..512b2e211 100644 --- a/resolver/query_logging_resolver.go +++ b/resolver/query_logging_resolver.go @@ -186,7 +186,7 @@ func (r *QueryLoggingResolver) writeLog() { r.writer.Write(logEntry) - halfCap := cap(r.logChan) / 2 // nolint:gomnd + halfCap := cap(r.logChan) / 2 //nolint:gomnd // if log channel is > 50% full, this could be a problem with slow writer (external storage over network etc.) if len(r.logChan) > halfCap { diff --git a/resolver/rewriter_resolver.go b/resolver/rewriter_resolver.go index b8425818c..57f37b5c1 100644 --- a/resolver/rewriter_resolver.go +++ b/resolver/rewriter_resolver.go @@ -110,7 +110,7 @@ func (r *RewriterResolver) Resolve(request *model.Request) (*model.Response, err return response, nil } -func (r *RewriterResolver) rewriteRequest(logger *logrus.Entry, request *dns.Msg) (rewritten *dns.Msg, originalNames []string) { // nolint: lll +func (r *RewriterResolver) rewriteRequest(logger *logrus.Entry, request *dns.Msg) (rewritten *dns.Msg, originalNames []string) { //nolint: lll originalNames = make([]string, len(request.Question)) for i := range request.Question { diff --git a/resolver/upstream_resolver.go b/resolver/upstream_resolver.go index 1635f0c5d..1fab5158f 100644 --- a/resolver/upstream_resolver.go +++ b/resolver/upstream_resolver.go @@ -28,7 +28,7 @@ const ( retryAttempts = 3 ) -// nolint:gochecknoglobals +//nolint:gochecknoglobals var ( // This is only set during tests (see upstream_resolver_test.go) skipUpstreamCheck *Bootstrap @@ -126,7 +126,7 @@ func (r *httpUpstreamClient) callExternal(msg *dns.Msg, return nil, 0, fmt.Errorf("can't pack message: %w", err) } - req, err := http.NewRequest("POST", upstreamURL, bytes.NewReader(rawDNSMessage)) + req, err := http.NewRequest(http.MethodPost, upstreamURL, bytes.NewReader(rawDNSMessage)) if err != nil { return nil, 0, fmt.Errorf("can't create the new request %w", err) diff --git a/resolver/upstream_resolver_test.go b/resolver/upstream_resolver_test.go index 49b22b203..40f81f1b8 100644 --- a/resolver/upstream_resolver_test.go +++ b/resolver/upstream_resolver_test.go @@ -16,7 +16,7 @@ import ( . "github.com/onsi/gomega" ) -// nolint:gochecknoinits +//nolint:gochecknoinits func init() { // Skips the constructor's check // Resolves hostnames using system resolver @@ -141,7 +141,6 @@ var _ = Describe("UpstreamResolver", Label("upstreamResolver"), func() { sut, _ = NewUpstreamResolver(upstream, skipUpstreamCheck) // use insecure certificates for test doh upstream - // nolint:gosec sut.upstreamClient.(*httpUpstreamClient).client.Transport = &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, @@ -161,7 +160,7 @@ var _ = Describe("UpstreamResolver", Label("upstreamResolver"), func() { When("Configured DOH resolver returns wrong http status code", func() { BeforeEach(func() { modifyHTTPRespFn = func(w http.ResponseWriter) { - w.WriteHeader(500) + w.WriteHeader(http.StatusInternalServerError) } }) It("should return error", func() { diff --git a/server/server.go b/server/server.go index df46f1ba5..e49044af9 100644 --- a/server/server.go +++ b/server/server.go @@ -112,7 +112,8 @@ func retrieveCertificate(cfg *config.Config) (cert tls.Certificate, err error) { } // NewServer creates new server instance with passed config -// nolint:funlen +// +//nolint:funlen func NewServer(cfg *config.Config) (server *Server, err error) { log.ConfigureLogger(cfg.LogLevel, cfg.LogFormat, cfg.LogTimestamp) @@ -292,11 +293,11 @@ func createUDPServer(address string) (*dns.Server, error) { }, nil } -// nolint:funlen +//nolint:funlen func createSelfSignedCert() (tls.Certificate, error) { // Create CA ca := &x509.Certificate{ - SerialNumber: big.NewInt(int64(mrand.Intn(math.MaxInt))), //nolint:gosec + SerialNumber: big.NewInt(int64(mrand.Intn(math.MaxInt))), NotBefore: time.Now(), NotAfter: time.Now().AddDate(caExpiryYears, 0, 0), IsCA: true, @@ -339,7 +340,7 @@ func createSelfSignedCert() (tls.Certificate, error) { // Create certificate cert := &x509.Certificate{ - SerialNumber: big.NewInt(int64(mrand.Intn(math.MaxInt))), //nolint:gosec + SerialNumber: big.NewInt(int64(mrand.Intn(math.MaxInt))), DNSNames: []string{"*"}, NotBefore: time.Now(), NotAfter: time.Now().AddDate(certExpiryYears, 0, 0),