From b835baf013d428cc41709c672dc92379e11e88b8 Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Wed, 2 Oct 2019 15:41:14 +0300 Subject: [PATCH 1/7] *(global): CI scripts --- .gitignore | 3 ++- .golangci.yml | 3 +++ .travis.yml | 9 ++------- ci.sh | 32 ++++++++++++++++++++++++++++++++ querylog/querylog_file.go | 4 ++-- 5 files changed, 41 insertions(+), 10 deletions(-) create mode 100755 ci.sh 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..df2714e1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,7 @@ os: before_install: - nvm install node - npm install -g npm + - go install github.com/golangci/golangci-lint/cmd/golangci-lint install: - npm --prefix client install @@ -22,13 +23,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..63e5c0c5 --- /dev/null +++ b/ci.sh @@ -0,0 +1,32 @@ +#!/bin/bash +set -e +set -x + +# Check versions and current directory +node -v +npm -v +go version +golangci-lint --version + +# Print the current directory contents +ls -la + +# 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 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 { From 846f5033370cde9b8ce2718548b2861c5f88bdcf Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Wed, 2 Oct 2019 16:23:17 +0300 Subject: [PATCH 2/7] *(global): fix CI scripts --- .travis.yml | 2 +- ci.sh | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index df2714e1..533881cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ os: before_install: - nvm install node - npm install -g npm - - go install github.com/golangci/golangci-lint/cmd/golangci-lint + - curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b /usr/local/bin v1.19.1 install: - npm --prefix client install diff --git a/ci.sh b/ci.sh index 63e5c0c5..a747b330 100755 --- a/ci.sh +++ b/ci.sh @@ -2,15 +2,17 @@ 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 -# Print the current directory contents -ls -la - # Run linter golangci-lint run @@ -30,3 +32,5 @@ else echo "Please make sure there are no changes" exit 1 fi + +echo "AdGuard Home CI script finished successfully" \ No newline at end of file From e4fba69f9de2c7a4df9d75d4f40b0bb1ceae6ac5 Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Wed, 2 Oct 2019 16:39:32 +0300 Subject: [PATCH 3/7] *(global): fix travis config --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 533881cc..dcf23a77 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ sudo: false go: - 1.12.x - - 1.x os: - linux - osx @@ -11,7 +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 /usr/local/bin v1.19.1 + - 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 From 9b23acf6da369e3a6b57ab4ca588dff3f1b7d2d1 Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Wed, 2 Oct 2019 16:40:26 +0300 Subject: [PATCH 4/7] *: fix linter warnings --- home/dns.go | 1 + 1 file changed, 1 insertion(+) diff --git a/home/dns.go b/home/dns.go index c9c8c003..9ea4db47 100644 --- a/home/dns.go +++ b/home/dns.go @@ -65,6 +65,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() From 33ae359cc1d892d82f4e8fc84b92e1d47e8361d2 Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Wed, 2 Oct 2019 16:53:23 +0300 Subject: [PATCH 5/7] *: change initDNSServer method, add getDataDir --- home/config.go | 5 +++++ home/control_install.go | 6 ++---- home/dns.go | 6 ++++-- home/dns_test.go | 2 +- home/filter.go | 2 +- home/home.go | 3 +-- 6 files changed, 14 insertions(+), 10 deletions(-) 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 9ea4db47..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 diff --git a/home/dns_test.go b/home/dns_test.go index 67d7b3ed..49ff3212 100644 --- a/home/dns_test.go +++ b/home/dns_test.go @@ -6,7 +6,7 @@ import ( func TestResolveRDNS(t *testing.T) { 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 { From ed0c5ee525d1823c31ced32089ffc15593f42655 Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Wed, 2 Oct 2019 16:59:34 +0300 Subject: [PATCH 6/7] *: tests mustn't leave repo dirty --- home/dns_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/home/dns_test.go b/home/dns_test.go index 49ff3212..52344279 100644 --- a/home/dns_test.go +++ b/home/dns_test.go @@ -1,10 +1,14 @@ 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() if r := config.dnsctx.rdns.resolve("1.1.1.1"); r != "one.one.one.one" { From 8a05e61feb2a8b7c73c6f4c8f11c5a076c43170b Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Wed, 2 Oct 2019 17:02:16 +0300 Subject: [PATCH 7/7] *: fix auth tests - don't leave repo dirty --- home/auth_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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)