From 9b963fc777e3846325c7cd0d01a261bcae7c09e3 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Mon, 7 Dec 2020 16:04:53 +0300 Subject: [PATCH] Pull request: all: fix lint and naming issues vol. 3 Merge in DNS/adguard-home from 2276-fix-lint-3 to master Updates #2276. Squashed commit of the following: commit 6ee94cc6ed2a9762b70ef395b58b496434244b80 Author: Ainar Garipov Date: Mon Dec 7 15:55:45 2020 +0300 all: fix lint and naming issues vol. 3 --- go.mod | 2 +- go.sum | 2 + internal/home/auth_test.go | 6 +- .../home/{auth_glinet.go => authglinet.go} | 0 ...auth_glinet_test.go => authglinet_test.go} | 4 +- internal/home/clients.go | 46 ++-- internal/home/clients_test.go | 237 +++++++++--------- .../home/{clients_http.go => clientshttp.go} | 18 +- .../home/{clients_tags.go => clientstags.go} | 0 internal/home/control.go | 2 +- ...ntrol_filtering.go => controlfiltering.go} | 10 +- .../{control_install.go => controlinstall.go} | 2 +- .../{control_update.go => controlupdate.go} | 0 ...l_update_test.go => controlupdate_test.go} | 4 +- internal/home/dns.go | 6 +- internal/home/filter.go | 18 +- internal/home/home.go | 11 +- internal/home/home_test.go | 2 +- internal/home/mobileconfig.go | 18 +- internal/home/mobileconfig_test.go | 8 +- internal/home/upgrade.go | 25 +- internal/home/web.go | 26 +- main.go | 13 +- 23 files changed, 234 insertions(+), 226 deletions(-) rename internal/home/{auth_glinet.go => authglinet.go} (100%) rename internal/home/{auth_glinet_test.go => authglinet_test.go} (86%) rename internal/home/{clients_http.go => clientshttp.go} (95%) rename internal/home/{clients_tags.go => clientstags.go} (100%) rename internal/home/{control_filtering.go => controlfiltering.go} (98%) rename internal/home/{control_install.go => controlinstall.go} (99%) rename internal/home/{control_update.go => controlupdate.go} (100%) rename internal/home/{control_update_test.go => controlupdate_test.go} (98%) diff --git a/go.mod b/go.mod index c20a9da1..03dd3446 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.14 require ( github.com/AdguardTeam/dnsproxy v0.33.2 - github.com/AdguardTeam/golibs v0.4.3 + github.com/AdguardTeam/golibs v0.4.4 github.com/AdguardTeam/urlfilter v0.13.0 github.com/NYTimes/gziphandler v1.1.1 github.com/beefsack/go-rate v0.0.0-20200827232406-6cde80facd47 // indirect diff --git a/go.sum b/go.sum index 9a7f298f..7a7c1876 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/AdguardTeam/golibs v0.4.2 h1:7M28oTZFoFwNmp8eGPb3ImmYbxGaJLyQXeIFVHjM github.com/AdguardTeam/golibs v0.4.2/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4= github.com/AdguardTeam/golibs v0.4.3 h1:nXTLLLlIyU4BSRF0An5azS0uimSK/YpIMOBAO0/v1RY= github.com/AdguardTeam/golibs v0.4.3/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4= +github.com/AdguardTeam/golibs v0.4.4 h1:cM9UySQiYFW79zo5XRwnaIWVzfW4eNXmZktMrWbthpw= +github.com/AdguardTeam/golibs v0.4.4/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4= github.com/AdguardTeam/gomitmproxy v0.2.0/go.mod h1:Qdv0Mktnzer5zpdpi5rAwixNJzW2FN91LjKJCkVbYGU= github.com/AdguardTeam/urlfilter v0.12.3 h1:FMjQG0eTgrr8xA3z2zaLVcCgGdpzoECPGWwgPjtwPNs= github.com/AdguardTeam/urlfilter v0.12.3/go.mod h1:1fcCQx5TGJANrQN6sHNNM9KPBl7qx7BJml45ko6vru0= diff --git a/internal/home/auth_test.go b/internal/home/auth_test.go index b88035b9..25db2dd6 100644 --- a/internal/home/auth_test.go +++ b/internal/home/auth_test.go @@ -20,7 +20,7 @@ func TestMain(m *testing.M) { func prepareTestDir() string { const dir = "./agh-test" _ = os.RemoveAll(dir) - _ = os.MkdirAll(dir, 0755) + _ = os.MkdirAll(dir, 0o755) return dir } @@ -30,7 +30,7 @@ func TestAuth(t *testing.T) { fn := filepath.Join(dir, "sessions.db") users := []User{ - User{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"}, + {Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"}, } a := InitAuth(fn, nil, 60) s := session{} @@ -106,7 +106,7 @@ func TestAuthHTTP(t *testing.T) { fn := filepath.Join(dir, "sessions.db") users := []User{ - User{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"}, + {Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"}, } Context.auth = InitAuth(fn, users, 60) diff --git a/internal/home/auth_glinet.go b/internal/home/authglinet.go similarity index 100% rename from internal/home/auth_glinet.go rename to internal/home/authglinet.go diff --git a/internal/home/auth_glinet_test.go b/internal/home/authglinet_test.go similarity index 86% rename from internal/home/auth_glinet_test.go rename to internal/home/authglinet_test.go index 171bb84e..df5e3342 100644 --- a/internal/home/auth_glinet_test.go +++ b/internal/home/authglinet_test.go @@ -25,7 +25,7 @@ func TestAuthGL(t *testing.T) { } else { binary.BigEndian.PutUint32(data, tval) } - assert.Nil(t, ioutil.WriteFile(glFilePrefix+"test", data, 0644)) + assert.Nil(t, ioutil.WriteFile(glFilePrefix+"test", data, 0o644)) assert.False(t, glCheckToken("test")) tval = uint32(time.Now().UTC().Unix() + 60) @@ -35,7 +35,7 @@ func TestAuthGL(t *testing.T) { } else { binary.BigEndian.PutUint32(data, tval) } - assert.Nil(t, ioutil.WriteFile(glFilePrefix+"test", data, 0644)) + assert.Nil(t, ioutil.WriteFile(glFilePrefix+"test", data, 0o644)) r, _ := http.NewRequest("GET", "http://localhost/", nil) r.AddCookie(&http.Cookie{Name: glCookieName, Value: "test"}) assert.True(t, glProcessCookie(r)) diff --git a/internal/home/clients.go b/internal/home/clients.go index 538bfe7e..3c6bfa48 100644 --- a/internal/home/clients.go +++ b/internal/home/clients.go @@ -570,31 +570,35 @@ func (clients *clientsContainer) SetWhoisInfo(ip string, info [][]string) { // so we overwrite existing entries with an equal or higher priority func (clients *clientsContainer) AddHost(ip, host string, source clientSource) (bool, error) { clients.lock.Lock() - b, e := clients.addHost(ip, host, source) + b := clients.addHost(ip, host, source) clients.lock.Unlock() - return b, e + return b, nil } -func (clients *clientsContainer) addHost(ip, host string, source clientSource) (bool, error) { - // check auto-clients index +func (clients *clientsContainer) addHost(ip, host string, source clientSource) (addedNew bool) { ch, ok := clients.ipHost[ip] - if ok && ch.Source > source { - return false, nil - } else if ok { + if ok { + if ch.Source > source { + return false + } + ch.Source = source } else { ch = &ClientHost{ Host: host, Source: source, } + clients.ipHost[ip] = ch } - log.Debug("Clients: added %q -> %q [%d]", ip, host, len(clients.ipHost)) - return true, nil + + log.Debug("clients: added %q -> %q [%d]", ip, host, len(clients.ipHost)) + + return true } // Remove all entries that match the specified source -func (clients *clientsContainer) rmHosts(source clientSource) int { +func (clients *clientsContainer) rmHosts(source clientSource) { n := 0 for k, v := range clients.ipHost { if v.Source == source { @@ -602,8 +606,8 @@ func (clients *clientsContainer) rmHosts(source clientSource) int { n++ } } - log.Debug("Clients: removed %d client aliases", n) - return n + + log.Debug("clients: removed %d client aliases", n) } // addFromHostsFile fills the clients hosts list from the system's hosts files. @@ -613,15 +617,12 @@ func (clients *clientsContainer) addFromHostsFile() { clients.lock.Lock() defer clients.lock.Unlock() - _ = clients.rmHosts(ClientSourceHostsFile) + clients.rmHosts(ClientSourceHostsFile) n := 0 for ip, names := range hosts { for _, name := range names { - ok, err := clients.addHost(ip, name, ClientSourceHostsFile) - if err != nil { - log.Debug("Clients: %s", err) - } + ok := clients.addHost(ip, name, ClientSourceHostsFile) if ok { n++ } @@ -650,7 +651,7 @@ func (clients *clientsContainer) addFromSystemARP() { clients.lock.Lock() defer clients.lock.Unlock() - _ = clients.rmHosts(ClientSourceARP) + clients.rmHosts(ClientSourceARP) n := 0 lines := strings.Split(string(data), "\n") @@ -668,10 +669,7 @@ func (clients *clientsContainer) addFromSystemARP() { continue } - ok, e := clients.addHost(ip, host, ClientSourceARP) - if e != nil { - log.Tracef("%s", e) - } + ok := clients.addHost(ip, host, ClientSourceARP) if ok { n++ } @@ -689,7 +687,7 @@ func (clients *clientsContainer) addFromDHCP() { clients.lock.Lock() defer clients.lock.Unlock() - _ = clients.rmHosts(ClientSourceDHCP) + clients.rmHosts(ClientSourceDHCP) leases := clients.dhcpServer.Leases(dhcpd.LeasesAll) n := 0 @@ -697,7 +695,7 @@ func (clients *clientsContainer) addFromDHCP() { if len(l.Hostname) == 0 { continue } - ok, _ := clients.addHost(l.IP.String(), l.Hostname, ClientSourceDHCP) + ok := clients.addHost(l.IP.String(), l.Hostname, ClientSourceDHCP) if ok { n++ } diff --git a/internal/home/clients_test.go b/internal/home/clients_test.go index 5d5a3c81..9268c08f 100644 --- a/internal/home/clients_test.go +++ b/internal/home/clients_test.go @@ -12,144 +12,155 @@ import ( ) func TestClients(t *testing.T) { - var c Client - var e error - var b bool clients := clientsContainer{} clients.testing = true clients.Init(nil, nil, nil) - // add - c = Client{ - IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa"}, - Name: "client1", - } - b, e = clients.Add(c) - if !b || e != nil { - t.Fatalf("Add #1") - } + t.Run("add_success", func(t *testing.T) { + c := Client{ + IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa"}, + Name: "client1", + } - // add #2 - c = Client{ - IDs: []string{"2.2.2.2"}, - Name: "client2", - } - b, e = clients.Add(c) - if !b || e != nil { - t.Fatalf("Add #2") - } + b, err := clients.Add(c) + assert.True(t, b) + assert.Nil(t, err) - c, b = clients.Find("1.1.1.1") - assert.True(t, b && c.Name == "client1") + c = Client{ + IDs: []string{"2.2.2.2"}, + Name: "client2", + } - c, b = clients.Find("1:2:3::4") - assert.True(t, b && c.Name == "client1") + b, err = clients.Add(c) + assert.True(t, b) + assert.Nil(t, err) - c, b = clients.Find("2.2.2.2") - assert.True(t, b && c.Name == "client2") + c, b = clients.Find("1.1.1.1") + assert.True(t, b && c.Name == "client1") - // failed add - name in use - c = Client{ - IDs: []string{"1.2.3.5"}, - Name: "client1", - } - b, _ = clients.Add(c) - if b { - t.Fatalf("Add - name in use") - } + c, b = clients.Find("1:2:3::4") + assert.True(t, b && c.Name == "client1") - // failed add - ip in use - c = Client{ - IDs: []string{"2.2.2.2"}, - Name: "client3", - } - b, e = clients.Add(c) - if b || e == nil { - t.Fatalf("Add - ip in use") - } + c, b = clients.Find("2.2.2.2") + assert.True(t, b && c.Name == "client2") - // get - assert.True(t, !clients.Exists("1.2.3.4", ClientSourceHostsFile)) - assert.True(t, clients.Exists("1.1.1.1", ClientSourceHostsFile)) - assert.True(t, clients.Exists("2.2.2.2", ClientSourceHostsFile)) + assert.True(t, !clients.Exists("1.2.3.4", ClientSourceHostsFile)) + assert.True(t, clients.Exists("1.1.1.1", ClientSourceHostsFile)) + assert.True(t, clients.Exists("2.2.2.2", ClientSourceHostsFile)) + }) - // failed update - no such name - c.IDs = []string{"1.2.3.0"} - c.Name = "client3" - if clients.Update("client3", c) == nil { - t.Fatalf("Update") - } + t.Run("add_fail_name", func(t *testing.T) { + c := Client{ + IDs: []string{"1.2.3.5"}, + Name: "client1", + } - // failed update - name in use - c.IDs = []string{"1.2.3.0"} - c.Name = "client2" - if clients.Update("client1", c) == nil { - t.Fatalf("Update - name in use") - } + b, err := clients.Add(c) + assert.False(t, b) + assert.Nil(t, err) + }) - // failed update - ip in use - c.IDs = []string{"2.2.2.2"} - c.Name = "client1" - if clients.Update("client1", c) == nil { - t.Fatalf("Update - ip in use") - } + t.Run("add_fail_ip", func(t *testing.T) { + c := Client{ + IDs: []string{"2.2.2.2"}, + Name: "client3", + } - // update - c.IDs = []string{"1.1.1.2"} - c.Name = "client1" - if clients.Update("client1", c) != nil { - t.Fatalf("Update") - } + b, err := clients.Add(c) + assert.False(t, b) + assert.NotNil(t, err) + }) - // get after update - assert.True(t, !clients.Exists("1.1.1.1", ClientSourceHostsFile)) - assert.True(t, clients.Exists("1.1.1.2", ClientSourceHostsFile)) + t.Run("update_fail_name", func(t *testing.T) { + c := Client{ + IDs: []string{"1.2.3.0"}, + Name: "client3", + } - // update - rename - c.IDs = []string{"1.1.1.2"} - c.Name = "client1-renamed" - c.UseOwnSettings = true - assert.True(t, clients.Update("client1", c) == nil) - c = Client{} - c, b = clients.Find("1.1.1.2") - assert.True(t, b && c.Name == "client1-renamed" && c.IDs[0] == "1.1.1.2" && c.UseOwnSettings) - assert.True(t, clients.list["client1"] == nil) + err := clients.Update("client3", c) + assert.NotNil(t, err) - // failed remove - no such name - if clients.Del("client3") { - t.Fatalf("Del - no such name") - } + c = Client{ + IDs: []string{"1.2.3.0"}, + Name: "client2", + } - // remove - assert.True(t, !(!clients.Del("client1-renamed") || clients.Exists("1.1.1.2", ClientSourceHostsFile))) + err = clients.Update("client3", c) + assert.NotNil(t, err) + }) - // add host client - b, e = clients.AddHost("1.1.1.1", "host", ClientSourceARP) - if !b || e != nil { - t.Fatalf("clientAddHost") - } + t.Run("update_fail_ip", func(t *testing.T) { + c := Client{ + IDs: []string{"2.2.2.2"}, + Name: "client1", + } - // failed add - ip exists - b, e = clients.AddHost("1.1.1.1", "host1", ClientSourceRDNS) - if b || e != nil { - t.Fatalf("clientAddHost - ip exists") - } + err := clients.Update("client1", c) + assert.NotNil(t, err) + }) - // overwrite with new data - b, e = clients.AddHost("1.1.1.1", "host2", ClientSourceARP) - if !b || e != nil { - t.Fatalf("clientAddHost - overwrite with new data") - } + t.Run("update_success", func(t *testing.T) { + c := Client{ + IDs: []string{"1.1.1.2"}, + Name: "client1", + } - // overwrite with new data (higher priority) - b, e = clients.AddHost("1.1.1.1", "host3", ClientSourceHostsFile) - if !b || e != nil { - t.Fatalf("clientAddHost - overwrite with new data (higher priority)") - } + err := clients.Update("client1", c) + assert.Nil(t, err) - // get - assert.True(t, clients.Exists("1.1.1.1", ClientSourceHostsFile)) + assert.True(t, !clients.Exists("1.1.1.1", ClientSourceHostsFile)) + assert.True(t, clients.Exists("1.1.1.2", ClientSourceHostsFile)) + + c = Client{ + IDs: []string{"1.1.1.2"}, + Name: "client1-renamed", + UseOwnSettings: true, + } + + err = clients.Update("client1", c) + assert.Nil(t, err) + + c, b := clients.Find("1.1.1.2") + assert.True(t, b) + assert.True(t, c.Name == "client1-renamed") + assert.True(t, c.IDs[0] == "1.1.1.2") + assert.True(t, c.UseOwnSettings) + assert.Nil(t, clients.list["client1"]) + }) + + t.Run("del_success", func(t *testing.T) { + b := clients.Del("client1-renamed") + assert.True(t, b) + assert.False(t, clients.Exists("1.1.1.2", ClientSourceHostsFile)) + }) + + t.Run("del_fail", func(t *testing.T) { + b := clients.Del("client3") + assert.False(t, b) + }) + + t.Run("addhost_success", func(t *testing.T) { + b, err := clients.AddHost("1.1.1.1", "host", ClientSourceARP) + assert.True(t, b) + assert.Nil(t, err) + + b, err = clients.AddHost("1.1.1.1", "host2", ClientSourceARP) + assert.True(t, b) + assert.Nil(t, err) + + b, err = clients.AddHost("1.1.1.1", "host3", ClientSourceHostsFile) + assert.True(t, b) + assert.Nil(t, err) + + assert.True(t, clients.Exists("1.1.1.1", ClientSourceHostsFile)) + }) + + t.Run("addhost_fail", func(t *testing.T) { + b, err := clients.AddHost("1.1.1.1", "host1", ClientSourceRDNS) + assert.False(t, b) + assert.Nil(t, err) + }) } func TestClientsWhois(t *testing.T) { diff --git a/internal/home/clients_http.go b/internal/home/clientshttp.go similarity index 95% rename from internal/home/clients_http.go rename to internal/home/clientshttp.go index 752b3c6f..d8cc3ee3 100644 --- a/internal/home/clients_http.go +++ b/internal/home/clientshttp.go @@ -94,8 +94,8 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, _ *http } // Convert JSON object to Client object -func jsonToClient(cj clientJSON) (*Client, error) { - c := Client{ +func jsonToClient(cj clientJSON) (c *Client) { + return &Client{ Name: cj.Name, IDs: cj.IDs, Tags: cj.Tags, @@ -110,7 +110,6 @@ func jsonToClient(cj clientJSON) (*Client, error) { Upstreams: cj.Upstreams, } - return &c, nil } // Convert Client object to JSON @@ -157,11 +156,7 @@ func (clients *clientsContainer) handleAddClient(w http.ResponseWriter, r *http. return } - c, err := jsonToClient(cj) - if err != nil { - httpError(w, http.StatusBadRequest, "%s", err) - return - } + c := jsonToClient(cj) ok, err := clients.Add(*c) if err != nil { httpError(w, http.StatusBadRequest, "%s", err) @@ -219,12 +214,7 @@ func (clients *clientsContainer) handleUpdateClient(w http.ResponseWriter, r *ht return } - c, err := jsonToClient(dj.Data) - if err != nil { - httpError(w, http.StatusBadRequest, "%s", err) - return - } - + c := jsonToClient(dj.Data) err = clients.Update(dj.Name, *c) if err != nil { httpError(w, http.StatusBadRequest, "%s", err) diff --git a/internal/home/clients_tags.go b/internal/home/clientstags.go similarity index 100% rename from internal/home/clients_tags.go rename to internal/home/clientstags.go diff --git a/internal/home/control.go b/internal/home/control.go index 732fa919..3443515a 100644 --- a/internal/home/control.go +++ b/internal/home/control.go @@ -117,7 +117,7 @@ func registerControlHandlers() { RegisterAuthHandlers() } -func httpRegister(method string, url string, handler func(http.ResponseWriter, *http.Request)) { +func httpRegister(method, url string, handler func(http.ResponseWriter, *http.Request)) { if len(method) == 0 { // "/dns-query" handler doesn't need auth, gzip and isn't restricted by 1 HTTP method Context.mux.HandleFunc(url, postInstall(handler)) diff --git a/internal/home/control_filtering.go b/internal/home/controlfiltering.go similarity index 98% rename from internal/home/control_filtering.go rename to internal/home/controlfiltering.go index 1794cce7..e7fb80ba 100644 --- a/internal/home/control_filtering.go +++ b/internal/home/controlfiltering.go @@ -196,9 +196,9 @@ func (f *Filtering) handleFilteringSetURL(w http.ResponseWriter, r *http.Request } if (status&statusUpdateRequired) != 0 && fj.Data.Enabled { // download new filter and apply its rules - flags := FilterRefreshBlocklists + flags := filterRefreshBlocklists if fj.Whitelist { - flags = FilterRefreshAllowlists + flags = filterRefreshAllowlists } nUpdated, _ := f.refreshFilters(flags, true) // if at least 1 filter has been updated, refreshFilters() restarts the filtering automatically @@ -244,11 +244,11 @@ func (f *Filtering) handleFilteringRefresh(w http.ResponseWriter, r *http.Reques } Context.controlLock.Unlock() - flags := FilterRefreshBlocklists + flags := filterRefreshBlocklists if req.White { - flags = FilterRefreshAllowlists + flags = filterRefreshAllowlists } - resp.Updated, err = f.refreshFilters(flags|FilterRefreshForce, false) + resp.Updated, err = f.refreshFilters(flags|filterRefreshForce, false) Context.controlLock.Lock() if err != nil { httpError(w, http.StatusInternalServerError, "%s", err) diff --git a/internal/home/control_install.go b/internal/home/controlinstall.go similarity index 99% rename from internal/home/control_install.go rename to internal/home/controlinstall.go index 06f3bf43..3661d300 100644 --- a/internal/home/control_install.go +++ b/internal/home/controlinstall.go @@ -273,7 +273,7 @@ type applyConfigReq struct { } // Copy installation parameters between two configuration objects -func copyInstallSettings(dst *configuration, src *configuration) { +func copyInstallSettings(dst, src *configuration) { dst.BindHost = src.BindHost dst.BindPort = src.BindPort dst.DNS.BindHost = src.DNS.BindHost diff --git a/internal/home/control_update.go b/internal/home/controlupdate.go similarity index 100% rename from internal/home/control_update.go rename to internal/home/controlupdate.go diff --git a/internal/home/control_update_test.go b/internal/home/controlupdate_test.go similarity index 98% rename from internal/home/control_update_test.go rename to internal/home/controlupdate_test.go index 82101249..45112f50 100644 --- a/internal/home/control_update_test.go +++ b/internal/home/controlupdate_test.go @@ -81,7 +81,7 @@ func TestTargzFileUnpack(t *testing.T) { fn := "../dist/AdGuardHome_linux_amd64.tar.gz" outdir := "../test-unpack" defer os.RemoveAll(outdir) - _ = os.Mkdir(outdir, 0755) + _ = os.Mkdir(outdir, 0o755) files, e := targzFileUnpack(fn, outdir) if e != nil { t.Fatalf("FAILED: %s", e) @@ -92,7 +92,7 @@ func TestTargzFileUnpack(t *testing.T) { func TestZipFileUnpack(t *testing.T) { fn := "../dist/AdGuardHome_windows_amd64.zip" outdir := "../test-unpack" - _ = os.Mkdir(outdir, 0755) + _ = os.Mkdir(outdir, 0o755) files, e := zipFileUnpack(fn, outdir) if e != nil { t.Fatalf("FAILED: %s", e) diff --git a/internal/home/dns.go b/internal/home/dns.go index f4167f12..9c420e94 100644 --- a/internal/home/dns.go +++ b/internal/home/dns.go @@ -142,14 +142,14 @@ func generateServerConfig() dnsforward.ServerConfig { return newconfig } -type DNSEncryption struct { +type dnsEncryption struct { https string tls string quic string } -func getDNSEncryption() DNSEncryption { - dnsEncryption := DNSEncryption{} +func getDNSEncryption() dnsEncryption { + dnsEncryption := dnsEncryption{} tlsConf := tlsConfigSettings{} diff --git a/internal/home/filter.go b/internal/home/filter.go index 668af955..b85336d9 100644 --- a/internal/home/filter.go +++ b/internal/home/filter.go @@ -255,7 +255,7 @@ func (f *Filtering) periodicallyRefreshFilters() { isNetworkErr := false if config.DNS.FiltersUpdateIntervalHours != 0 && atomic.CompareAndSwapUint32(&f.refreshStatus, 0, 1) { f.refreshLock.Lock() - _, isNetworkErr = f.refreshFiltersIfNecessary(FilterRefreshBlocklists | FilterRefreshAllowlists) + _, isNetworkErr = f.refreshFiltersIfNecessary(filterRefreshBlocklists | filterRefreshAllowlists) f.refreshLock.Unlock() f.refreshStatus = 0 if !isNetworkErr { @@ -275,7 +275,7 @@ func (f *Filtering) periodicallyRefreshFilters() { } // Refresh filters -// flags: FilterRefresh* +// flags: filterRefresh* // important: // TRUE: ignore the fact that we're currently updating the filters func (f *Filtering) refreshFilters(flags int, important bool) (int, error) { @@ -368,14 +368,14 @@ func (f *Filtering) refreshFiltersArray(filters *[]filter, force bool) (int, []f } const ( - FilterRefreshForce = 1 // ignore last file modification date - FilterRefreshAllowlists = 2 // update allow-lists - FilterRefreshBlocklists = 4 // update block-lists + filterRefreshForce = 1 // ignore last file modification date + filterRefreshAllowlists = 2 // update allow-lists + filterRefreshBlocklists = 4 // update block-lists ) // Checks filters updates if necessary // If force is true, it ignores the filter.LastUpdated field value -// flags: FilterRefresh* +// flags: filterRefresh* // // Algorithm: // . Get the list of filters to be updated @@ -401,13 +401,13 @@ func (f *Filtering) refreshFiltersIfNecessary(flags int) (int, bool) { netError := false netErrorW := false force := false - if (flags & FilterRefreshForce) != 0 { + if (flags & filterRefreshForce) != 0 { force = true } - if (flags & FilterRefreshBlocklists) != 0 { + if (flags & filterRefreshBlocklists) != 0 { updateCount, updateFilters, updateFlags, netError = f.refreshFiltersArray(&config.Filters, force) } - if (flags & FilterRefreshAllowlists) != 0 { + if (flags & filterRefreshAllowlists) != 0 { updateCountW := 0 var updateFiltersW []filter var updateFlagsW []bool diff --git a/internal/home/home.go b/internal/home/home.go index a4be016f..acf6c3e0 100644 --- a/internal/home/home.go +++ b/internal/home/home.go @@ -44,6 +44,7 @@ var ( updateChannel = "none" versionCheckURL = "" ARMVersion = "" + MIPSVersion = "" ) // Global context @@ -98,11 +99,12 @@ func (c *homeContext) getDataDir() string { var Context homeContext // Main is the entry point -func Main(version, channel, armVer string) { +func Main(version, channel, armVer, mipsVer string) { // Init update-related global variables versionString = version updateChannel = channel ARMVersion = armVer + MIPSVersion = mipsVer versionCheckURL = "https://static.adguard.com/adguardhome/" + updateChannel + "/version.json" // config can be specified, which reads options from there, but other command line flags have to override config values @@ -139,10 +141,15 @@ func Main(version, channel, armVer string) { // version - returns the current version string func version() string { + // TODO(a.garipov): I'm pretty sure we can extract some of this stuff + // from the build info. msg := "AdGuard Home, version %s, channel %s, arch %s %s" if ARMVersion != "" { msg = msg + " v" + ARMVersion + } else if MIPSVersion != "" { + msg = msg + " " + MIPSVersion } + return fmt.Sprintf(msg, versionString, updateChannel, runtime.GOOS, runtime.GOARCH) } @@ -308,7 +315,7 @@ func run(args options) { log.Fatalf("Can't initialize TLS module") } - webConf := WebConfig{ + webConf := webConfig{ firstRun: Context.firstRun, BindHost: config.BindHost, BindPort: config.BindPort, diff --git a/internal/home/home_test.go b/internal/home/home_test.go index 3a4db397..1b16e357 100644 --- a/internal/home/home_test.go +++ b/internal/home/home_test.go @@ -119,7 +119,7 @@ func TestHome(t *testing.T) { fn := filepath.Join(dir, "AdGuardHome.yaml") // Prepare the test config - assert.True(t, ioutil.WriteFile(fn, []byte(yamlConf), 0644) == nil) + assert.True(t, ioutil.WriteFile(fn, []byte(yamlConf), 0o644) == nil) fn, _ = filepath.Abs(fn) config = configuration{} // the global variable is dirty because of the previous tests run diff --git a/internal/home/mobileconfig.go b/internal/home/mobileconfig.go index 58bdea88..2f06329e 100644 --- a/internal/home/mobileconfig.go +++ b/internal/home/mobileconfig.go @@ -10,13 +10,13 @@ import ( "howett.net/plist" ) -type DNSSettings struct { +type dnsSettings struct { DNSProtocol string ServerURL string `plist:",omitempty"` ServerName string `plist:",omitempty"` } -type PayloadContent struct { +type payloadContent struct { Name string PayloadDescription string PayloadDisplayName string @@ -24,11 +24,11 @@ type PayloadContent struct { PayloadType string PayloadUUID string PayloadVersion int - DNSSettings DNSSettings + DNSSettings dnsSettings } -type MobileConfig struct { - PayloadContent []PayloadContent +type mobileConfig struct { + PayloadContent []payloadContent PayloadDescription string PayloadDisplayName string PayloadIdentifier string @@ -47,7 +47,7 @@ const ( dnsProtoTLS = "TLS" ) -func getMobileConfig(d DNSSettings) ([]byte, error) { +func getMobileConfig(d dnsSettings) ([]byte, error) { var name string switch d.DNSProtocol { case dnsProtoHTTPS: @@ -59,8 +59,8 @@ func getMobileConfig(d DNSSettings) ([]byte, error) { return nil, fmt.Errorf("bad dns protocol %q", d.DNSProtocol) } - data := MobileConfig{ - PayloadContent: []PayloadContent{{ + data := mobileConfig{ + PayloadContent: []payloadContent{{ Name: name, PayloadDescription: "Configures device to use AdGuard Home", PayloadDisplayName: name, @@ -102,7 +102,7 @@ func handleMobileConfig(w http.ResponseWriter, r *http.Request, dnsp string) { return } - d := DNSSettings{ + d := dnsSettings{ DNSProtocol: dnsp, ServerName: host, } diff --git a/internal/home/mobileconfig_test.go b/internal/home/mobileconfig_test.go index 520d70e1..f5bf3f2b 100644 --- a/internal/home/mobileconfig_test.go +++ b/internal/home/mobileconfig_test.go @@ -19,7 +19,7 @@ func TestHandleMobileConfigDOH(t *testing.T) { handleMobileConfigDOH(w, r) assert.Equal(t, http.StatusOK, w.Code) - var mc MobileConfig + var mc mobileConfig _, err = plist.Unmarshal(w.Body.Bytes(), &mc) assert.Nil(t, err) @@ -47,7 +47,7 @@ func TestHandleMobileConfigDOH(t *testing.T) { handleMobileConfigDOH(w, r) assert.Equal(t, http.StatusOK, w.Code) - var mc MobileConfig + var mc mobileConfig _, err = plist.Unmarshal(w.Body.Bytes(), &mc) assert.Nil(t, err) @@ -85,7 +85,7 @@ func TestHandleMobileConfigDOT(t *testing.T) { handleMobileConfigDOT(w, r) assert.Equal(t, http.StatusOK, w.Code) - var mc MobileConfig + var mc mobileConfig _, err = plist.Unmarshal(w.Body.Bytes(), &mc) assert.Nil(t, err) @@ -112,7 +112,7 @@ func TestHandleMobileConfigDOT(t *testing.T) { handleMobileConfigDOT(w, r) assert.Equal(t, http.StatusOK, w.Code) - var mc MobileConfig + var mc mobileConfig _, err = plist.Unmarshal(w.Body.Bytes(), &mc) assert.Nil(t, err) diff --git a/internal/home/upgrade.go b/internal/home/upgrade.go index 42fd83d6..335eb86c 100644 --- a/internal/home/upgrade.go +++ b/internal/home/upgrade.go @@ -3,9 +3,9 @@ package home import ( "fmt" "os" + "path" "path/filepath" - - "github.com/AdguardTeam/AdGuardHome/internal/util" + "runtime" "github.com/AdguardTeam/golibs/file" "github.com/AdguardTeam/golibs/log" @@ -122,7 +122,7 @@ func upgradeConfigSchema(oldVersion int, diskConfig *map[string]interface{}) err // The first schema upgrade: // No more "dnsfilter.txt", filters are now kept in data/filters/ func upgradeSchema0to1(diskConfig *map[string]interface{}) error { - log.Printf("%s(): called", util.FuncName()) + log.Printf("%s(): called", funcName()) dnsFilterPath := filepath.Join(Context.workDir, "dnsfilter.txt") if _, err := os.Stat(dnsFilterPath); !os.IsNotExist(err) { @@ -143,7 +143,7 @@ func upgradeSchema0to1(diskConfig *map[string]interface{}) error { // coredns is now dns in config // delete 'Corefile', since we don't use that anymore func upgradeSchema1to2(diskConfig *map[string]interface{}) error { - log.Printf("%s(): called", util.FuncName()) + log.Printf("%s(): called", funcName()) coreFilePath := filepath.Join(Context.workDir, "Corefile") if _, err := os.Stat(coreFilePath); !os.IsNotExist(err) { @@ -167,7 +167,7 @@ func upgradeSchema1to2(diskConfig *map[string]interface{}) error { // Third schema upgrade: // Bootstrap DNS becomes an array func upgradeSchema2to3(diskConfig *map[string]interface{}) error { - log.Printf("%s(): called", util.FuncName()) + log.Printf("%s(): called", funcName()) // Let's read dns configuration from diskConfig dnsConfig, ok := (*diskConfig)["dns"] @@ -204,7 +204,7 @@ func upgradeSchema2to3(diskConfig *map[string]interface{}) error { // Add use_global_blocked_services=true setting for existing "clients" array func upgradeSchema3to4(diskConfig *map[string]interface{}) error { - log.Printf("%s(): called", util.FuncName()) + log.Printf("%s(): called", funcName()) (*diskConfig)["schema_version"] = 4 @@ -240,7 +240,7 @@ func upgradeSchema3to4(diskConfig *map[string]interface{}) error { // password: "..." // ... func upgradeSchema4to5(diskConfig *map[string]interface{}) error { - log.Printf("%s(): called", util.FuncName()) + log.Printf("%s(): called", funcName()) (*diskConfig)["schema_version"] = 5 @@ -295,7 +295,7 @@ func upgradeSchema4to5(diskConfig *map[string]interface{}) error { // - 127.0.0.1 // - ... func upgradeSchema5to6(diskConfig *map[string]interface{}) error { - log.Printf("%s(): called", util.FuncName()) + log.Printf("%s(): called", funcName()) (*diskConfig)["schema_version"] = 6 @@ -433,3 +433,12 @@ func upgradeSchema6to7(diskConfig *map[string]interface{}) error { return nil } + +// TODO(a.garipov): Replace with log.Output when we port it to our logging +// package. +func funcName() string { + pc := make([]uintptr, 10) // at least 1 entry needed + runtime.Callers(2, pc) + f := runtime.FuncForPC(pc[0]) + return path.Base(f.Name()) +} diff --git a/internal/home/web.go b/internal/home/web.go index 974016da..97f06979 100644 --- a/internal/home/web.go +++ b/internal/home/web.go @@ -3,7 +3,6 @@ package home import ( "context" "crypto/tls" - golog "log" "net" "net/http" "strconv" @@ -30,7 +29,7 @@ const ( WriteTimeout = 10 * time.Second ) -type WebConfig struct { +type webConfig struct { firstRun bool BindHost string BindPort int @@ -61,34 +60,20 @@ type HTTPSServer struct { // Web - module object type Web struct { - conf *WebConfig + conf *webConfig forceHTTPS bool portHTTPS int httpServer *http.Server // HTTP module httpsServer HTTPSServer // HTTPS module - errLogger *golog.Logger -} - -// Proxy between Go's "log" and "golibs/log" -type logWriter struct { -} - -// HTTP server calls this function to log an error -func (w *logWriter) Write(p []byte) (int, error) { - log.Debug("Web: %s", string(p)) - return 0, nil } // CreateWeb - create module -func CreateWeb(conf *WebConfig) *Web { +func CreateWeb(conf *webConfig) *Web { log.Info("Initialize web module") w := Web{} w.conf = conf - lw := logWriter{} - w.errLogger = golog.New(&lw, "", 0) - // Initialize and run the admin Web interface box := packr.NewBox("../../build/static") @@ -166,13 +151,14 @@ func (web *Web) Start() { // we need to have new instance, because after Shutdown() the Server is not usable address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.conf.BindPort)) web.httpServer = &http.Server{ - ErrorLog: web.errLogger, + ErrorLog: log.StdLog("web: http", log.DEBUG), Addr: address, Handler: withMiddlewares(Context.mux, limitRequestBody), ReadTimeout: web.conf.ReadTimeout, ReadHeaderTimeout: web.conf.ReadHeaderTimeout, WriteTimeout: web.conf.WriteTimeout, } + err := web.httpServer.ListenAndServe() if err != http.ErrServerClosed { cleanupAlways() @@ -220,7 +206,7 @@ func (web *Web) tlsServerLoop() { // prepare HTTPS server address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.conf.PortHTTPS)) web.httpsServer.server = &http.Server{ - ErrorLog: web.errLogger, + ErrorLog: log.StdLog("web: https", log.DEBUG), Addr: address, TLSConfig: &tls.Config{ Certificates: []tls.Certificate{web.httpsServer.cert}, diff --git a/main.go b/main.go index a040da7d..ecbb3d3b 100644 --- a/main.go +++ b/main.go @@ -7,15 +7,20 @@ import ( "github.com/AdguardTeam/AdGuardHome/internal/home" ) -// version will be set through ldflags, contains current version +// version is the release version. It is set by the linker. var version = "undefined" -// channel can be set via ldflags +// channel is the release channel. It is set by the linker. var channel = "release" -// GOARM value - set via ldflags +// goarm is the GOARM value. It is set by the linker. var goarm = "" +// gomips is the GOMIPS value. It is set by the linker. +// +// TODO(a.garipov): Implement. +var gomips = "" + func main() { - home.Main(version, channel, goarm) + home.Main(version, channel, goarm, gomips) }