Skip to content

Commit

Permalink
Merge branch "development"
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R committed Jun 7, 2022
2 parents 5878314 + 1ddf397 commit 97aa99d
Show file tree
Hide file tree
Showing 109 changed files with 5,131 additions and 2,904 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.17
- name: Set up Go 1.18
uses: actions/setup-go@v1
with:
go-version: 1.17
go-version: 1.18
id: go

- name: Check out code into the Go module directory
Expand All @@ -30,6 +30,9 @@ jobs:
- name: Test
run: make test

- name: Race detection
run: make race

- name: Upload results to codecov
run: bash <(curl -s https://codecov.io/bash) -t 48d6a1a8-a66e-4f27-9cc1-a7b91c4209b2

Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.18'
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: v1.43.0
version: v1.46.2
args: --timeout 5m0s
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Set up Go 1.17
- name: Set up Go 1.18
uses: actions/setup-go@v1
with:
go-version: 1.17
go-version: 1.18
id: go

- name: Checkout
Expand Down
75 changes: 51 additions & 24 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,63 @@
linters:
enable:
- govet
- errcheck
- staticcheck
- unused
- gosimple
- structcheck
- varcheck
- ineffassign
- deadcode
- typecheck
- asciicheck
- bidichk
- bodyclose
- revive
- stylecheck
- gosec
- unconvert
- dupl
- goconst
- gocyclo
- gocognit
- gofmt
- goimports
- deadcode
- depguard
- misspell
- lll
- unparam
- dogsled
- dupl
- durationcheck
- errcheck
- errchkjson
- errorlint
- exhaustive
- exportloopref
- funlen
- gochecknoglobals
- gochecknoinits
- gocognit
- goconst
- gocritic
- gocyclo
- godox
- gofmt
- goimports
- gomnd
- gomoddirectives
- gomodguard
- gosec
- gosimple
- govet
- grouper
- ifshort
- importas
- ineffassign
- lll
- makezero
- misspell
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- nolintlint
- prealloc
- predeclared
- revive
- sqlclosecheck
- staticcheck
- structcheck
- stylecheck
- tenv
- typecheck
- unconvert
- unparam
- unused
- varcheck
- wastedassign
- whitespace
- wsl
- exportloopref
disable:
- noctx
- scopelint
Expand All @@ -46,6 +68,11 @@ linters:
- unused
fast: false

linters-settings:
gomnd:
ignored-numbers:
- '0666'

issues:
exclude-rules:
# Exclude some linters from running on tests files.
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# build stage
FROM golang:1.17-alpine AS build-env
FROM golang:1.18-alpine AS build-env
RUN apk add --no-cache \
git \
make \
Expand All @@ -23,7 +23,7 @@ ARG opts
RUN env ${opts} make build

# final stage
FROM alpine:3.15
FROM alpine:3.16

LABEL org.opencontainers.image.source="https://github.com/0xERR0R/blocky" \
org.opencontainers.image.url="https://github.com/0xERR0R/blocky" \
Expand All @@ -40,4 +40,4 @@ USER blocky
WORKDIR /app

ENTRYPOINT ["/sbin/tini", "--"]
CMD ["/app/blocky","--config","/app/config.yml"]
CMD ["sh", "-c", "/app/blocky --config ${CONFIG_FILE:-/app/config.yml}"]
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ serve_docs: ## serves online docs
mkdocs serve

build: ## Build binary
go install github.com/abice/go-enum@v0.3.8
go install github.com/abice/go-enum@v0.4.0
go generate ./...
go build -v -ldflags="-w -s -X github.com/0xERR0R/blocky/util.Version=${VERSION} -X github.com/0xERR0R/blocky/util.BuildTime=${BUILD_TIME}" -o $(BIN_OUT_DIR)/$(BINARY_NAME)$(BINARY_SUFFIX)

test: ## run tests
go test -v -coverprofile=coverage.txt -covermode=atomic -cover ./...

race: ## run tests with race detector
go test -race -short ./...

lint: build ## run golangcli-lint checks
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.43.0
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.46.2
$(shell go env GOPATH)/bin/golangci-lint run

run: build ## Build and run binary
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Blocky is a DNS proxy and ad-blocker for the local network written in Go with fo
* Supports modern DNS extensions: DNSSEC, eDNS, ...
* Free configurable blocking lists - no hidden filtering etc.
* Provides DoH Endpoint
* Uses random upstream resolvers from the configuration - increases you privacy though the distribution of your DNS
* Uses random upstream resolvers from the configuration - increases your privacy through the distribution of your DNS
traffic over multiple provider
* Blocky does **NOT** collect any user data, telemetry, statistics etc.

Expand All @@ -65,7 +65,7 @@ Blocky is a DNS proxy and ad-blocker for the local network written in Go with fo
* Various REST API endpoints
* CLI tool

- **Simple configuration** - single configuration file in YAML format
- **Simple configuration** - single or multiple configuration files in YAML format

* Simple to maintain
* Simple to backup
Expand Down
5 changes: 3 additions & 2 deletions api/api_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ func (s *BlockingEndpoint) apiBlockingDisable(rw http.ResponseWriter, req *http.
func (s *BlockingEndpoint) apiBlockingStatus(rw http.ResponseWriter, _ *http.Request) {
status := s.control.BlockingStatus()

response, _ := json.Marshal(status)
_, err := rw.Write(response)
response, err := json.Marshal(status)
util.LogOnError("unable to marshal response ", err)

_, err = rw.Write(response)
util.LogOnError("unable to write response ", err)
}
3 changes: 2 additions & 1 deletion api/api_endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

. "github.com/0xERR0R/blocky/helpertest"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/go-chi/chi/v5"
Expand All @@ -30,6 +30,7 @@ func (b *BlockingControlMock) EnableBlocking() {
}
func (b *BlockingControlMock) DisableBlocking(_ time.Duration, disableGroups []string) error {
b.enabled = false

return nil
}

Expand Down
6 changes: 3 additions & 3 deletions api/api_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package api_test
import (
"testing"

. "github.com/0xERR0R/blocky/log"
. "github.com/onsi/ginkgo"
"github.com/0xERR0R/blocky/log"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestResolver(t *testing.T) {
ConfigureLogger(LevelFatal, FormatTypeText, true)
log.Silence()
RegisterFailHandler(Fail)
RunSpecs(t, "API Suite")
}
32 changes: 8 additions & 24 deletions cache/expirationcache/expiration_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (e *ExpiringLRUCache) cleanUp() {

// check for expired items and collect expired keys
for _, k := range e.lru.Keys() {
if v, ok := e.lru.Get(k); ok {
if v, ok := e.lru.Peek(k); ok {
if isExpired(v.(*element)) {
expiredKeys = append(expiredKeys, k.(string))
}
Expand Down Expand Up @@ -117,18 +117,11 @@ func (e *ExpiringLRUCache) Put(key string, val interface{}, ttl time.Duration) {

expiresEpochMs := time.Now().UnixMilli() + ttl.Milliseconds()

el, found := e.lru.Get(key)
if found {
// update existing item
el.(*element).val = val
el.(*element).expiresEpochMs = expiresEpochMs
} else {
// add new item
e.lru.Add(key, &element{
val: val,
expiresEpochMs: expiresEpochMs,
})
}
// add new item
e.lru.Add(key, &element{
val: val,
expiresEpochMs: expiresEpochMs,
})
}

func (e *ExpiringLRUCache) Get(key string) (val interface{}, ttl time.Duration) {
Expand All @@ -146,24 +139,15 @@ func isExpired(el *element) bool {
}

func calculateRemainTTL(expiresEpoch int64) time.Duration {
now := time.Now().UnixMilli()
if now < expiresEpoch {
if now := time.Now().UnixMilli(); now < expiresEpoch {
return time.Duration(expiresEpoch-now) * time.Millisecond
}

return 0
}

func (e *ExpiringLRUCache) TotalCount() (count int) {
for _, k := range e.lru.Keys() {
if v, ok := e.lru.Get(k); ok {
if !isExpired(v.(*element)) {
count++
}
}
}

return count
return e.lru.Len()
}

func (e *ExpiringLRUCache) Clear() {
Expand Down
6 changes: 3 additions & 3 deletions cache/expirationcache/expiration_cache_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package expirationcache_test
import (
"testing"

. "github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/log"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestCache(t *testing.T) {
ConfigureLogger(LevelFatal, FormatTypeText, true)
log.Silence()
RegisterFailHandler(Fail)
RunSpecs(t, "Expiration cache suite")
}
8 changes: 2 additions & 6 deletions cache/expirationcache/expiration_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package expirationcache
import (
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -41,11 +41,7 @@ var _ = Describe("Expiration cache", func() {
val, ttl := cache.Get("key1")
g.Expect(val).Should(Equal("val1"))
g.Expect(ttl.Milliseconds()).Should(BeNumerically("==", 0))
}, "60ms").Should(Succeed())

Expect(cache.TotalCount()).Should(Equal(0))
// internal map has still the expired item
Expect(cache.lru.Len()).Should(Equal(1))
}, "100ms").Should(Succeed())

// wait for cleanup run
Eventually(func() int {
Expand Down
6 changes: 3 additions & 3 deletions cache/stringcache/string_cache_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package stringcache_test
import (
"testing"

. "github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/log"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestCache(t *testing.T) {
ConfigureLogger(LevelFatal, FormatTypeText, true)
log.Silence()
RegisterFailHandler(Fail)
RunSpecs(t, "String cache suite")
}
Loading

0 comments on commit 97aa99d

Please sign in to comment.