diff --git a/.gitignore b/.gitignore index f8564fd9..7ed45af2 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,8 @@ /querylog.json /querylog.json.1 /a_main-packr.go +coverage.txt # Test output dnsfilter/tests/top-1m.csv -dnsfilter/tests/dnsfilter.TestLotsOfRules*.pprof \ No newline at end of file +dnsfilter/tests/dnsfilter.TestLotsOfRules*.pprof diff --git a/.golangci.yml b/.golangci.yml index ee952d7b..013b9000 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -38,6 +38,9 @@ linters: - gochecknoinits - prealloc - maligned + - godox + - funlen + - whitespace - goconst # disabled until it's possible to configure fast: true diff --git a/.travis.yml b/.travis.yml index 972fabd3..dcf23a77 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ sudo: false go: - 1.12.x - - 1.x os: - linux - osx @@ -11,6 +10,7 @@ os: before_install: - nvm install node - npm install -g npm + - curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $(go env GOPATH)/bin v1.19.1 install: - npm --prefix client install @@ -22,13 +22,7 @@ cache: - $HOME/Library/Caches/go-build script: - - node -v - - npm -v - # Run tests - - go test -race -v -bench=. -coverprofile=coverage.txt -covermode=atomic ./... - # Make - - make build/static/index.html - - make + - /bin/bash ci.sh after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/ci.sh b/ci.sh new file mode 100755 index 00000000..a747b330 --- /dev/null +++ b/ci.sh @@ -0,0 +1,36 @@ +#!/bin/bash +set -e +set -x + +echo "Starting AdGuard Home CI script" + +# Print the current directory contents +ls -la + +# Check versions and current directory +node -v +npm -v +go version +golangci-lint --version + +# Run linter +golangci-lint run + +# Run tests +go test -race -v -bench=. -coverprofile=coverage.txt -covermode=atomic ./... + +# Make +make clean +make build/static/index.html +make + +if [[ -z "$(git status --porcelain)" ]]; then + # Working directory clean + echo "Git status is clean" +else + echo "Git status is not clean and contains uncommited changes" + echo "Please make sure there are no changes" + exit 1 +fi + +echo "AdGuard Home CI script finished successfully" \ No newline at end of file diff --git a/home/auth_test.go b/home/auth_test.go index 61bf4b23..abb6dfc4 100644 --- a/home/auth_test.go +++ b/home/auth_test.go @@ -3,6 +3,7 @@ package home import ( "encoding/hex" "os" + "path/filepath" "testing" "time" @@ -10,13 +11,17 @@ import ( ) func TestAuth(t *testing.T) { - fn := "./sessions.db" + config.ourWorkingDir = "." + fn := filepath.Join(config.getDataDir(), "sessions.db") + + _ = os.RemoveAll(config.getDataDir()) + defer func() { _ = os.RemoveAll(config.getDataDir()) }() + users := []User{ User{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"}, } - os.Remove(fn) - config.ourWorkingDir = "." + os.MkdirAll(config.getDataDir(), 0755) a := InitAuth(fn, users) assert.True(t, a.CheckSession("notfound") == -1) diff --git a/home/config.go b/home/config.go index e2c52302..68e5fc00 100644 --- a/home/config.go +++ b/home/config.go @@ -239,6 +239,11 @@ func (c *configuration) getConfigFilename() string { return configFile } +// getDataDir returns path to the directory where we store databases and filters +func (c *configuration) getDataDir() string { + return filepath.Join(c.ourWorkingDir, dataDir) +} + // getLogSettings reads logging settings from the config file. // we do it in a separate method in order to configure logger before the actual configuration is parsed and applied. func getLogSettings() logSettings { diff --git a/home/control_install.go b/home/control_install.go index 822f0c0b..7e9e315f 100644 --- a/home/control_install.go +++ b/home/control_install.go @@ -7,7 +7,6 @@ import ( "net" "net/http" "os/exec" - "path/filepath" "strconv" "github.com/AdguardTeam/golibs/log" @@ -236,8 +235,7 @@ func handleInstallConfigure(w http.ResponseWriter, r *http.Request) { config.DNS.BindHost = newSettings.DNS.IP config.DNS.Port = newSettings.DNS.Port - dnsBaseDir := filepath.Join(config.ourWorkingDir, dataDir) - initDNSServer(dnsBaseDir) + initDNSServer() err = startDNSServer() if err != nil { @@ -263,7 +261,7 @@ func handleInstallConfigure(w http.ResponseWriter, r *http.Request) { // until all requests are finished, and _we_ are inside a request right now, so it will block indefinitely if restartHTTP { go func() { - config.httpServer.Shutdown(context.TODO()) + _ = config.httpServer.Shutdown(context.TODO()) }() } diff --git a/home/dns.go b/home/dns.go index c9c8c003..2a08db3b 100644 --- a/home/dns.go +++ b/home/dns.go @@ -29,7 +29,9 @@ func onConfigModified() { // initDNSServer creates an instance of the dnsforward.Server // Please note that we must do it even if we don't start it // so that we had access to the query log and the stats -func initDNSServer(baseDir string) { +func initDNSServer() { + baseDir := config.getDataDir() + err := os.MkdirAll(baseDir, 0755) if err != nil { log.Fatalf("Cannot create DNS data dir at %s: %s", baseDir, err) @@ -52,7 +54,7 @@ func initDNSServer(baseDir string) { config.queryLog = querylog.New(conf) config.dnsServer = dnsforward.NewServer(config.stats, config.queryLog) - sessFilename := filepath.Join(config.ourWorkingDir, "data/sessions.db") + sessFilename := filepath.Join(baseDir, "sessions.db") config.auth = InitAuth(sessFilename, config.Users) config.Users = nil @@ -65,6 +67,7 @@ func isRunning() bool { return config.dnsServer != nil && config.dnsServer.IsRunning() } +// nolint (gocyclo) // Return TRUE if IP is within public Internet IP range func isPublicIP(ip net.IP) bool { ip4 := ip.To4() diff --git a/home/dns_test.go b/home/dns_test.go index 67d7b3ed..52344279 100644 --- a/home/dns_test.go +++ b/home/dns_test.go @@ -1,12 +1,16 @@ package home import ( + "os" "testing" ) func TestResolveRDNS(t *testing.T) { + _ = os.RemoveAll(config.getDataDir()) + defer func() { _ = os.RemoveAll(config.getDataDir()) }() + config.DNS.BindHost = "1.1.1.1" - initDNSServer(".") + initDNSServer() if r := config.dnsctx.rdns.resolve("1.1.1.1"); r != "one.one.one.one" { t.Errorf("resolveRDNS(): %s", r) } diff --git a/home/filter.go b/home/filter.go index 8bf902a2..80e448ea 100644 --- a/home/filter.go +++ b/home/filter.go @@ -448,7 +448,7 @@ func (filter *filter) unload() { // Path to the filter contents func (filter *filter) Path() string { - return filepath.Join(config.ourWorkingDir, dataDir, filterDir, strconv.FormatInt(filter.ID, 10)+".txt") + return filepath.Join(config.getDataDir(), filterDir, strconv.FormatInt(filter.ID, 10)+".txt") } // LastTimeUpdated returns the time when the filter was last time updated diff --git a/home/home.go b/home/home.go index 5f2d4fc0..80f9e860 100644 --- a/home/home.go +++ b/home/home.go @@ -142,8 +142,7 @@ func run(args options) { log.Fatal(err) } - dnsBaseDir := filepath.Join(config.ourWorkingDir, dataDir) - initDNSServer(dnsBaseDir) + initDNSServer() err = startDNSServer() if err != nil { diff --git a/querylog/querylog_file.go b/querylog/querylog_file.go index 4f63a247..8f613012 100644 --- a/querylog/querylog_file.go +++ b/querylog/querylog_file.go @@ -271,7 +271,7 @@ func (r *Reader) BeginReadPrev(olderThan time.Time, count uint64) { if int64(off) < maxEntrySize { off = 0 } - r.fpos = uint64(off) + r.fpos = off log.Debug("QueryLog: seek: %x", off) _, err := r.f.Seek(int64(off), io.SeekStart) if err != nil { @@ -376,7 +376,7 @@ func (r *Reader) prepareRead() bool { if int64(off) < maxEntrySize { off = 0 } - r.fpos = uint64(off) + r.fpos = off log.Debug("QueryLog: seek: %x", off) _, err = r.f.Seek(int64(off), io.SeekStart) if err != nil {