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 <A.Garipov@AdGuard.COM>
Date:   Mon Dec 7 15:55:45 2020 +0300

    all: fix lint and naming issues vol. 3
This commit is contained in:
Ainar Garipov 2020-12-07 16:04:53 +03:00
parent 7f29d4e546
commit 9b963fc777
23 changed files with 234 additions and 226 deletions

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.14
require ( require (
github.com/AdguardTeam/dnsproxy v0.33.2 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/AdguardTeam/urlfilter v0.13.0
github.com/NYTimes/gziphandler v1.1.1 github.com/NYTimes/gziphandler v1.1.1
github.com/beefsack/go-rate v0.0.0-20200827232406-6cde80facd47 // indirect github.com/beefsack/go-rate v0.0.0-20200827232406-6cde80facd47 // indirect

2
go.sum
View File

@ -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.2/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4=
github.com/AdguardTeam/golibs v0.4.3 h1:nXTLLLlIyU4BSRF0An5azS0uimSK/YpIMOBAO0/v1RY= 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.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/gomitmproxy v0.2.0/go.mod h1:Qdv0Mktnzer5zpdpi5rAwixNJzW2FN91LjKJCkVbYGU=
github.com/AdguardTeam/urlfilter v0.12.3 h1:FMjQG0eTgrr8xA3z2zaLVcCgGdpzoECPGWwgPjtwPNs= github.com/AdguardTeam/urlfilter v0.12.3 h1:FMjQG0eTgrr8xA3z2zaLVcCgGdpzoECPGWwgPjtwPNs=
github.com/AdguardTeam/urlfilter v0.12.3/go.mod h1:1fcCQx5TGJANrQN6sHNNM9KPBl7qx7BJml45ko6vru0= github.com/AdguardTeam/urlfilter v0.12.3/go.mod h1:1fcCQx5TGJANrQN6sHNNM9KPBl7qx7BJml45ko6vru0=

View File

@ -20,7 +20,7 @@ func TestMain(m *testing.M) {
func prepareTestDir() string { func prepareTestDir() string {
const dir = "./agh-test" const dir = "./agh-test"
_ = os.RemoveAll(dir) _ = os.RemoveAll(dir)
_ = os.MkdirAll(dir, 0755) _ = os.MkdirAll(dir, 0o755)
return dir return dir
} }
@ -30,7 +30,7 @@ func TestAuth(t *testing.T) {
fn := filepath.Join(dir, "sessions.db") fn := filepath.Join(dir, "sessions.db")
users := []User{ users := []User{
User{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"}, {Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"},
} }
a := InitAuth(fn, nil, 60) a := InitAuth(fn, nil, 60)
s := session{} s := session{}
@ -106,7 +106,7 @@ func TestAuthHTTP(t *testing.T) {
fn := filepath.Join(dir, "sessions.db") fn := filepath.Join(dir, "sessions.db")
users := []User{ users := []User{
User{Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"}, {Name: "name", PasswordHash: "$2y$05$..vyzAECIhJPfaQiOK17IukcQnqEgKJHy0iETyYqxn3YXJl8yZuo2"},
} }
Context.auth = InitAuth(fn, users, 60) Context.auth = InitAuth(fn, users, 60)

View File

@ -25,7 +25,7 @@ func TestAuthGL(t *testing.T) {
} else { } else {
binary.BigEndian.PutUint32(data, tval) 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")) assert.False(t, glCheckToken("test"))
tval = uint32(time.Now().UTC().Unix() + 60) tval = uint32(time.Now().UTC().Unix() + 60)
@ -35,7 +35,7 @@ func TestAuthGL(t *testing.T) {
} else { } else {
binary.BigEndian.PutUint32(data, tval) 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, _ := http.NewRequest("GET", "http://localhost/", nil)
r.AddCookie(&http.Cookie{Name: glCookieName, Value: "test"}) r.AddCookie(&http.Cookie{Name: glCookieName, Value: "test"})
assert.True(t, glProcessCookie(r)) assert.True(t, glProcessCookie(r))

View File

@ -570,31 +570,35 @@ func (clients *clientsContainer) SetWhoisInfo(ip string, info [][]string) {
// so we overwrite existing entries with an equal or higher priority // so we overwrite existing entries with an equal or higher priority
func (clients *clientsContainer) AddHost(ip, host string, source clientSource) (bool, error) { func (clients *clientsContainer) AddHost(ip, host string, source clientSource) (bool, error) {
clients.lock.Lock() clients.lock.Lock()
b, e := clients.addHost(ip, host, source) b := clients.addHost(ip, host, source)
clients.lock.Unlock() clients.lock.Unlock()
return b, e return b, nil
} }
func (clients *clientsContainer) addHost(ip, host string, source clientSource) (bool, error) { func (clients *clientsContainer) addHost(ip, host string, source clientSource) (addedNew bool) {
// check auto-clients index
ch, ok := clients.ipHost[ip] ch, ok := clients.ipHost[ip]
if ok && ch.Source > source { if ok {
return false, nil if ch.Source > source {
} else if ok { return false
}
ch.Source = source ch.Source = source
} else { } else {
ch = &ClientHost{ ch = &ClientHost{
Host: host, Host: host,
Source: source, Source: source,
} }
clients.ipHost[ip] = ch 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 // Remove all entries that match the specified source
func (clients *clientsContainer) rmHosts(source clientSource) int { func (clients *clientsContainer) rmHosts(source clientSource) {
n := 0 n := 0
for k, v := range clients.ipHost { for k, v := range clients.ipHost {
if v.Source == source { if v.Source == source {
@ -602,8 +606,8 @@ func (clients *clientsContainer) rmHosts(source clientSource) int {
n++ 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. // addFromHostsFile fills the clients hosts list from the system's hosts files.
@ -613,15 +617,12 @@ func (clients *clientsContainer) addFromHostsFile() {
clients.lock.Lock() clients.lock.Lock()
defer clients.lock.Unlock() defer clients.lock.Unlock()
_ = clients.rmHosts(ClientSourceHostsFile) clients.rmHosts(ClientSourceHostsFile)
n := 0 n := 0
for ip, names := range hosts { for ip, names := range hosts {
for _, name := range names { for _, name := range names {
ok, err := clients.addHost(ip, name, ClientSourceHostsFile) ok := clients.addHost(ip, name, ClientSourceHostsFile)
if err != nil {
log.Debug("Clients: %s", err)
}
if ok { if ok {
n++ n++
} }
@ -650,7 +651,7 @@ func (clients *clientsContainer) addFromSystemARP() {
clients.lock.Lock() clients.lock.Lock()
defer clients.lock.Unlock() defer clients.lock.Unlock()
_ = clients.rmHosts(ClientSourceARP) clients.rmHosts(ClientSourceARP)
n := 0 n := 0
lines := strings.Split(string(data), "\n") lines := strings.Split(string(data), "\n")
@ -668,10 +669,7 @@ func (clients *clientsContainer) addFromSystemARP() {
continue continue
} }
ok, e := clients.addHost(ip, host, ClientSourceARP) ok := clients.addHost(ip, host, ClientSourceARP)
if e != nil {
log.Tracef("%s", e)
}
if ok { if ok {
n++ n++
} }
@ -689,7 +687,7 @@ func (clients *clientsContainer) addFromDHCP() {
clients.lock.Lock() clients.lock.Lock()
defer clients.lock.Unlock() defer clients.lock.Unlock()
_ = clients.rmHosts(ClientSourceDHCP) clients.rmHosts(ClientSourceDHCP)
leases := clients.dhcpServer.Leases(dhcpd.LeasesAll) leases := clients.dhcpServer.Leases(dhcpd.LeasesAll)
n := 0 n := 0
@ -697,7 +695,7 @@ func (clients *clientsContainer) addFromDHCP() {
if len(l.Hostname) == 0 { if len(l.Hostname) == 0 {
continue continue
} }
ok, _ := clients.addHost(l.IP.String(), l.Hostname, ClientSourceDHCP) ok := clients.addHost(l.IP.String(), l.Hostname, ClientSourceDHCP)
if ok { if ok {
n++ n++
} }

View File

@ -12,144 +12,155 @@ import (
) )
func TestClients(t *testing.T) { func TestClients(t *testing.T) {
var c Client
var e error
var b bool
clients := clientsContainer{} clients := clientsContainer{}
clients.testing = true clients.testing = true
clients.Init(nil, nil, nil) clients.Init(nil, nil, nil)
// add t.Run("add_success", func(t *testing.T) {
c = Client{ c := Client{
IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa"}, IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa"},
Name: "client1", Name: "client1",
} }
b, e = clients.Add(c)
if !b || e != nil {
t.Fatalf("Add #1")
}
// add #2 b, err := clients.Add(c)
c = Client{ assert.True(t, b)
IDs: []string{"2.2.2.2"}, assert.Nil(t, err)
Name: "client2",
}
b, e = clients.Add(c)
if !b || e != nil {
t.Fatalf("Add #2")
}
c, b = clients.Find("1.1.1.1") c = Client{
assert.True(t, b && c.Name == "client1") IDs: []string{"2.2.2.2"},
Name: "client2",
}
c, b = clients.Find("1:2:3::4") b, err = clients.Add(c)
assert.True(t, b && c.Name == "client1") assert.True(t, b)
assert.Nil(t, err)
c, b = clients.Find("2.2.2.2") c, b = clients.Find("1.1.1.1")
assert.True(t, b && c.Name == "client2") assert.True(t, b && c.Name == "client1")
// failed add - name in use c, b = clients.Find("1:2:3::4")
c = Client{ assert.True(t, b && c.Name == "client1")
IDs: []string{"1.2.3.5"},
Name: "client1",
}
b, _ = clients.Add(c)
if b {
t.Fatalf("Add - name in use")
}
// failed add - ip in use c, b = clients.Find("2.2.2.2")
c = Client{ assert.True(t, b && c.Name == "client2")
IDs: []string{"2.2.2.2"},
Name: "client3",
}
b, e = clients.Add(c)
if b || e == nil {
t.Fatalf("Add - ip in use")
}
// get assert.True(t, !clients.Exists("1.2.3.4", 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("1.1.1.1", ClientSourceHostsFile)) assert.True(t, clients.Exists("2.2.2.2", ClientSourceHostsFile))
assert.True(t, clients.Exists("2.2.2.2", ClientSourceHostsFile)) })
// failed update - no such name t.Run("add_fail_name", func(t *testing.T) {
c.IDs = []string{"1.2.3.0"} c := Client{
c.Name = "client3" IDs: []string{"1.2.3.5"},
if clients.Update("client3", c) == nil { Name: "client1",
t.Fatalf("Update") }
}
// failed update - name in use b, err := clients.Add(c)
c.IDs = []string{"1.2.3.0"} assert.False(t, b)
c.Name = "client2" assert.Nil(t, err)
if clients.Update("client1", c) == nil { })
t.Fatalf("Update - name in use")
}
// failed update - ip in use t.Run("add_fail_ip", func(t *testing.T) {
c.IDs = []string{"2.2.2.2"} c := Client{
c.Name = "client1" IDs: []string{"2.2.2.2"},
if clients.Update("client1", c) == nil { Name: "client3",
t.Fatalf("Update - ip in use") }
}
// update b, err := clients.Add(c)
c.IDs = []string{"1.1.1.2"} assert.False(t, b)
c.Name = "client1" assert.NotNil(t, err)
if clients.Update("client1", c) != nil { })
t.Fatalf("Update")
}
// get after update t.Run("update_fail_name", func(t *testing.T) {
assert.True(t, !clients.Exists("1.1.1.1", ClientSourceHostsFile)) c := Client{
assert.True(t, clients.Exists("1.1.1.2", ClientSourceHostsFile)) IDs: []string{"1.2.3.0"},
Name: "client3",
}
// update - rename err := clients.Update("client3", c)
c.IDs = []string{"1.1.1.2"} assert.NotNil(t, err)
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)
// failed remove - no such name c = Client{
if clients.Del("client3") { IDs: []string{"1.2.3.0"},
t.Fatalf("Del - no such name") Name: "client2",
} }
// remove err = clients.Update("client3", c)
assert.True(t, !(!clients.Del("client1-renamed") || clients.Exists("1.1.1.2", ClientSourceHostsFile))) assert.NotNil(t, err)
})
// add host client t.Run("update_fail_ip", func(t *testing.T) {
b, e = clients.AddHost("1.1.1.1", "host", ClientSourceARP) c := Client{
if !b || e != nil { IDs: []string{"2.2.2.2"},
t.Fatalf("clientAddHost") Name: "client1",
} }
// failed add - ip exists err := clients.Update("client1", c)
b, e = clients.AddHost("1.1.1.1", "host1", ClientSourceRDNS) assert.NotNil(t, err)
if b || e != nil { })
t.Fatalf("clientAddHost - ip exists")
}
// overwrite with new data t.Run("update_success", func(t *testing.T) {
b, e = clients.AddHost("1.1.1.1", "host2", ClientSourceARP) c := Client{
if !b || e != nil { IDs: []string{"1.1.1.2"},
t.Fatalf("clientAddHost - overwrite with new data") Name: "client1",
} }
// overwrite with new data (higher priority) err := clients.Update("client1", c)
b, e = clients.AddHost("1.1.1.1", "host3", ClientSourceHostsFile) assert.Nil(t, err)
if !b || e != nil {
t.Fatalf("clientAddHost - overwrite with new data (higher priority)")
}
// 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) { func TestClientsWhois(t *testing.T) {

View File

@ -94,8 +94,8 @@ func (clients *clientsContainer) handleGetClients(w http.ResponseWriter, _ *http
} }
// Convert JSON object to Client object // Convert JSON object to Client object
func jsonToClient(cj clientJSON) (*Client, error) { func jsonToClient(cj clientJSON) (c *Client) {
c := Client{ return &Client{
Name: cj.Name, Name: cj.Name,
IDs: cj.IDs, IDs: cj.IDs,
Tags: cj.Tags, Tags: cj.Tags,
@ -110,7 +110,6 @@ func jsonToClient(cj clientJSON) (*Client, error) {
Upstreams: cj.Upstreams, Upstreams: cj.Upstreams,
} }
return &c, nil
} }
// Convert Client object to JSON // Convert Client object to JSON
@ -157,11 +156,7 @@ func (clients *clientsContainer) handleAddClient(w http.ResponseWriter, r *http.
return return
} }
c, err := jsonToClient(cj) c := jsonToClient(cj)
if err != nil {
httpError(w, http.StatusBadRequest, "%s", err)
return
}
ok, err := clients.Add(*c) ok, err := clients.Add(*c)
if err != nil { if err != nil {
httpError(w, http.StatusBadRequest, "%s", err) httpError(w, http.StatusBadRequest, "%s", err)
@ -219,12 +214,7 @@ func (clients *clientsContainer) handleUpdateClient(w http.ResponseWriter, r *ht
return return
} }
c, err := jsonToClient(dj.Data) c := jsonToClient(dj.Data)
if err != nil {
httpError(w, http.StatusBadRequest, "%s", err)
return
}
err = clients.Update(dj.Name, *c) err = clients.Update(dj.Name, *c)
if err != nil { if err != nil {
httpError(w, http.StatusBadRequest, "%s", err) httpError(w, http.StatusBadRequest, "%s", err)

View File

@ -117,7 +117,7 @@ func registerControlHandlers() {
RegisterAuthHandlers() 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 { if len(method) == 0 {
// "/dns-query" handler doesn't need auth, gzip and isn't restricted by 1 HTTP method // "/dns-query" handler doesn't need auth, gzip and isn't restricted by 1 HTTP method
Context.mux.HandleFunc(url, postInstall(handler)) Context.mux.HandleFunc(url, postInstall(handler))

View File

@ -196,9 +196,9 @@ func (f *Filtering) handleFilteringSetURL(w http.ResponseWriter, r *http.Request
} }
if (status&statusUpdateRequired) != 0 && fj.Data.Enabled { if (status&statusUpdateRequired) != 0 && fj.Data.Enabled {
// download new filter and apply its rules // download new filter and apply its rules
flags := FilterRefreshBlocklists flags := filterRefreshBlocklists
if fj.Whitelist { if fj.Whitelist {
flags = FilterRefreshAllowlists flags = filterRefreshAllowlists
} }
nUpdated, _ := f.refreshFilters(flags, true) nUpdated, _ := f.refreshFilters(flags, true)
// if at least 1 filter has been updated, refreshFilters() restarts the filtering automatically // 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() Context.controlLock.Unlock()
flags := FilterRefreshBlocklists flags := filterRefreshBlocklists
if req.White { 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() Context.controlLock.Lock()
if err != nil { if err != nil {
httpError(w, http.StatusInternalServerError, "%s", err) httpError(w, http.StatusInternalServerError, "%s", err)

View File

@ -273,7 +273,7 @@ type applyConfigReq struct {
} }
// Copy installation parameters between two configuration objects // Copy installation parameters between two configuration objects
func copyInstallSettings(dst *configuration, src *configuration) { func copyInstallSettings(dst, src *configuration) {
dst.BindHost = src.BindHost dst.BindHost = src.BindHost
dst.BindPort = src.BindPort dst.BindPort = src.BindPort
dst.DNS.BindHost = src.DNS.BindHost dst.DNS.BindHost = src.DNS.BindHost

View File

@ -81,7 +81,7 @@ func TestTargzFileUnpack(t *testing.T) {
fn := "../dist/AdGuardHome_linux_amd64.tar.gz" fn := "../dist/AdGuardHome_linux_amd64.tar.gz"
outdir := "../test-unpack" outdir := "../test-unpack"
defer os.RemoveAll(outdir) defer os.RemoveAll(outdir)
_ = os.Mkdir(outdir, 0755) _ = os.Mkdir(outdir, 0o755)
files, e := targzFileUnpack(fn, outdir) files, e := targzFileUnpack(fn, outdir)
if e != nil { if e != nil {
t.Fatalf("FAILED: %s", e) t.Fatalf("FAILED: %s", e)
@ -92,7 +92,7 @@ func TestTargzFileUnpack(t *testing.T) {
func TestZipFileUnpack(t *testing.T) { func TestZipFileUnpack(t *testing.T) {
fn := "../dist/AdGuardHome_windows_amd64.zip" fn := "../dist/AdGuardHome_windows_amd64.zip"
outdir := "../test-unpack" outdir := "../test-unpack"
_ = os.Mkdir(outdir, 0755) _ = os.Mkdir(outdir, 0o755)
files, e := zipFileUnpack(fn, outdir) files, e := zipFileUnpack(fn, outdir)
if e != nil { if e != nil {
t.Fatalf("FAILED: %s", e) t.Fatalf("FAILED: %s", e)

View File

@ -142,14 +142,14 @@ func generateServerConfig() dnsforward.ServerConfig {
return newconfig return newconfig
} }
type DNSEncryption struct { type dnsEncryption struct {
https string https string
tls string tls string
quic string quic string
} }
func getDNSEncryption() DNSEncryption { func getDNSEncryption() dnsEncryption {
dnsEncryption := DNSEncryption{} dnsEncryption := dnsEncryption{}
tlsConf := tlsConfigSettings{} tlsConf := tlsConfigSettings{}

View File

@ -255,7 +255,7 @@ func (f *Filtering) periodicallyRefreshFilters() {
isNetworkErr := false isNetworkErr := false
if config.DNS.FiltersUpdateIntervalHours != 0 && atomic.CompareAndSwapUint32(&f.refreshStatus, 0, 1) { if config.DNS.FiltersUpdateIntervalHours != 0 && atomic.CompareAndSwapUint32(&f.refreshStatus, 0, 1) {
f.refreshLock.Lock() f.refreshLock.Lock()
_, isNetworkErr = f.refreshFiltersIfNecessary(FilterRefreshBlocklists | FilterRefreshAllowlists) _, isNetworkErr = f.refreshFiltersIfNecessary(filterRefreshBlocklists | filterRefreshAllowlists)
f.refreshLock.Unlock() f.refreshLock.Unlock()
f.refreshStatus = 0 f.refreshStatus = 0
if !isNetworkErr { if !isNetworkErr {
@ -275,7 +275,7 @@ func (f *Filtering) periodicallyRefreshFilters() {
} }
// Refresh filters // Refresh filters
// flags: FilterRefresh* // flags: filterRefresh*
// important: // important:
// TRUE: ignore the fact that we're currently updating the filters // TRUE: ignore the fact that we're currently updating the filters
func (f *Filtering) refreshFilters(flags int, important bool) (int, error) { 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 ( const (
FilterRefreshForce = 1 // ignore last file modification date filterRefreshForce = 1 // ignore last file modification date
FilterRefreshAllowlists = 2 // update allow-lists filterRefreshAllowlists = 2 // update allow-lists
FilterRefreshBlocklists = 4 // update block-lists filterRefreshBlocklists = 4 // update block-lists
) )
// Checks filters updates if necessary // Checks filters updates if necessary
// If force is true, it ignores the filter.LastUpdated field value // If force is true, it ignores the filter.LastUpdated field value
// flags: FilterRefresh* // flags: filterRefresh*
// //
// Algorithm: // Algorithm:
// . Get the list of filters to be updated // . Get the list of filters to be updated
@ -401,13 +401,13 @@ func (f *Filtering) refreshFiltersIfNecessary(flags int) (int, bool) {
netError := false netError := false
netErrorW := false netErrorW := false
force := false force := false
if (flags & FilterRefreshForce) != 0 { if (flags & filterRefreshForce) != 0 {
force = true force = true
} }
if (flags & FilterRefreshBlocklists) != 0 { if (flags & filterRefreshBlocklists) != 0 {
updateCount, updateFilters, updateFlags, netError = f.refreshFiltersArray(&config.Filters, force) updateCount, updateFilters, updateFlags, netError = f.refreshFiltersArray(&config.Filters, force)
} }
if (flags & FilterRefreshAllowlists) != 0 { if (flags & filterRefreshAllowlists) != 0 {
updateCountW := 0 updateCountW := 0
var updateFiltersW []filter var updateFiltersW []filter
var updateFlagsW []bool var updateFlagsW []bool

View File

@ -44,6 +44,7 @@ var (
updateChannel = "none" updateChannel = "none"
versionCheckURL = "" versionCheckURL = ""
ARMVersion = "" ARMVersion = ""
MIPSVersion = ""
) )
// Global context // Global context
@ -98,11 +99,12 @@ func (c *homeContext) getDataDir() string {
var Context homeContext var Context homeContext
// Main is the entry point // Main is the entry point
func Main(version, channel, armVer string) { func Main(version, channel, armVer, mipsVer string) {
// Init update-related global variables // Init update-related global variables
versionString = version versionString = version
updateChannel = channel updateChannel = channel
ARMVersion = armVer ARMVersion = armVer
MIPSVersion = mipsVer
versionCheckURL = "https://static.adguard.com/adguardhome/" + updateChannel + "/version.json" 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 // 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 // version - returns the current version string
func 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" msg := "AdGuard Home, version %s, channel %s, arch %s %s"
if ARMVersion != "" { if ARMVersion != "" {
msg = msg + " v" + ARMVersion msg = msg + " v" + ARMVersion
} else if MIPSVersion != "" {
msg = msg + " " + MIPSVersion
} }
return fmt.Sprintf(msg, versionString, updateChannel, runtime.GOOS, runtime.GOARCH) 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") log.Fatalf("Can't initialize TLS module")
} }
webConf := WebConfig{ webConf := webConfig{
firstRun: Context.firstRun, firstRun: Context.firstRun,
BindHost: config.BindHost, BindHost: config.BindHost,
BindPort: config.BindPort, BindPort: config.BindPort,

View File

@ -119,7 +119,7 @@ func TestHome(t *testing.T) {
fn := filepath.Join(dir, "AdGuardHome.yaml") fn := filepath.Join(dir, "AdGuardHome.yaml")
// Prepare the test config // 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) fn, _ = filepath.Abs(fn)
config = configuration{} // the global variable is dirty because of the previous tests run config = configuration{} // the global variable is dirty because of the previous tests run

View File

@ -10,13 +10,13 @@ import (
"howett.net/plist" "howett.net/plist"
) )
type DNSSettings struct { type dnsSettings struct {
DNSProtocol string DNSProtocol string
ServerURL string `plist:",omitempty"` ServerURL string `plist:",omitempty"`
ServerName string `plist:",omitempty"` ServerName string `plist:",omitempty"`
} }
type PayloadContent struct { type payloadContent struct {
Name string Name string
PayloadDescription string PayloadDescription string
PayloadDisplayName string PayloadDisplayName string
@ -24,11 +24,11 @@ type PayloadContent struct {
PayloadType string PayloadType string
PayloadUUID string PayloadUUID string
PayloadVersion int PayloadVersion int
DNSSettings DNSSettings DNSSettings dnsSettings
} }
type MobileConfig struct { type mobileConfig struct {
PayloadContent []PayloadContent PayloadContent []payloadContent
PayloadDescription string PayloadDescription string
PayloadDisplayName string PayloadDisplayName string
PayloadIdentifier string PayloadIdentifier string
@ -47,7 +47,7 @@ const (
dnsProtoTLS = "TLS" dnsProtoTLS = "TLS"
) )
func getMobileConfig(d DNSSettings) ([]byte, error) { func getMobileConfig(d dnsSettings) ([]byte, error) {
var name string var name string
switch d.DNSProtocol { switch d.DNSProtocol {
case dnsProtoHTTPS: case dnsProtoHTTPS:
@ -59,8 +59,8 @@ func getMobileConfig(d DNSSettings) ([]byte, error) {
return nil, fmt.Errorf("bad dns protocol %q", d.DNSProtocol) return nil, fmt.Errorf("bad dns protocol %q", d.DNSProtocol)
} }
data := MobileConfig{ data := mobileConfig{
PayloadContent: []PayloadContent{{ PayloadContent: []payloadContent{{
Name: name, Name: name,
PayloadDescription: "Configures device to use AdGuard Home", PayloadDescription: "Configures device to use AdGuard Home",
PayloadDisplayName: name, PayloadDisplayName: name,
@ -102,7 +102,7 @@ func handleMobileConfig(w http.ResponseWriter, r *http.Request, dnsp string) {
return return
} }
d := DNSSettings{ d := dnsSettings{
DNSProtocol: dnsp, DNSProtocol: dnsp,
ServerName: host, ServerName: host,
} }

View File

@ -19,7 +19,7 @@ func TestHandleMobileConfigDOH(t *testing.T) {
handleMobileConfigDOH(w, r) handleMobileConfigDOH(w, r)
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
var mc MobileConfig var mc mobileConfig
_, err = plist.Unmarshal(w.Body.Bytes(), &mc) _, err = plist.Unmarshal(w.Body.Bytes(), &mc)
assert.Nil(t, err) assert.Nil(t, err)
@ -47,7 +47,7 @@ func TestHandleMobileConfigDOH(t *testing.T) {
handleMobileConfigDOH(w, r) handleMobileConfigDOH(w, r)
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
var mc MobileConfig var mc mobileConfig
_, err = plist.Unmarshal(w.Body.Bytes(), &mc) _, err = plist.Unmarshal(w.Body.Bytes(), &mc)
assert.Nil(t, err) assert.Nil(t, err)
@ -85,7 +85,7 @@ func TestHandleMobileConfigDOT(t *testing.T) {
handleMobileConfigDOT(w, r) handleMobileConfigDOT(w, r)
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
var mc MobileConfig var mc mobileConfig
_, err = plist.Unmarshal(w.Body.Bytes(), &mc) _, err = plist.Unmarshal(w.Body.Bytes(), &mc)
assert.Nil(t, err) assert.Nil(t, err)
@ -112,7 +112,7 @@ func TestHandleMobileConfigDOT(t *testing.T) {
handleMobileConfigDOT(w, r) handleMobileConfigDOT(w, r)
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, http.StatusOK, w.Code)
var mc MobileConfig var mc mobileConfig
_, err = plist.Unmarshal(w.Body.Bytes(), &mc) _, err = plist.Unmarshal(w.Body.Bytes(), &mc)
assert.Nil(t, err) assert.Nil(t, err)

View File

@ -3,9 +3,9 @@ package home
import ( import (
"fmt" "fmt"
"os" "os"
"path"
"path/filepath" "path/filepath"
"runtime"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/golibs/file" "github.com/AdguardTeam/golibs/file"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
@ -122,7 +122,7 @@ func upgradeConfigSchema(oldVersion int, diskConfig *map[string]interface{}) err
// The first schema upgrade: // The first schema upgrade:
// No more "dnsfilter.txt", filters are now kept in data/filters/ // No more "dnsfilter.txt", filters are now kept in data/filters/
func upgradeSchema0to1(diskConfig *map[string]interface{}) error { 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") dnsFilterPath := filepath.Join(Context.workDir, "dnsfilter.txt")
if _, err := os.Stat(dnsFilterPath); !os.IsNotExist(err) { 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 // coredns is now dns in config
// delete 'Corefile', since we don't use that anymore // delete 'Corefile', since we don't use that anymore
func upgradeSchema1to2(diskConfig *map[string]interface{}) error { func upgradeSchema1to2(diskConfig *map[string]interface{}) error {
log.Printf("%s(): called", util.FuncName()) log.Printf("%s(): called", funcName())
coreFilePath := filepath.Join(Context.workDir, "Corefile") coreFilePath := filepath.Join(Context.workDir, "Corefile")
if _, err := os.Stat(coreFilePath); !os.IsNotExist(err) { if _, err := os.Stat(coreFilePath); !os.IsNotExist(err) {
@ -167,7 +167,7 @@ func upgradeSchema1to2(diskConfig *map[string]interface{}) error {
// Third schema upgrade: // Third schema upgrade:
// Bootstrap DNS becomes an array // Bootstrap DNS becomes an array
func upgradeSchema2to3(diskConfig *map[string]interface{}) error { 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 // Let's read dns configuration from diskConfig
dnsConfig, ok := (*diskConfig)["dns"] 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 // Add use_global_blocked_services=true setting for existing "clients" array
func upgradeSchema3to4(diskConfig *map[string]interface{}) error { func upgradeSchema3to4(diskConfig *map[string]interface{}) error {
log.Printf("%s(): called", util.FuncName()) log.Printf("%s(): called", funcName())
(*diskConfig)["schema_version"] = 4 (*diskConfig)["schema_version"] = 4
@ -240,7 +240,7 @@ func upgradeSchema3to4(diskConfig *map[string]interface{}) error {
// password: "..." // password: "..."
// ... // ...
func upgradeSchema4to5(diskConfig *map[string]interface{}) error { func upgradeSchema4to5(diskConfig *map[string]interface{}) error {
log.Printf("%s(): called", util.FuncName()) log.Printf("%s(): called", funcName())
(*diskConfig)["schema_version"] = 5 (*diskConfig)["schema_version"] = 5
@ -295,7 +295,7 @@ func upgradeSchema4to5(diskConfig *map[string]interface{}) error {
// - 127.0.0.1 // - 127.0.0.1
// - ... // - ...
func upgradeSchema5to6(diskConfig *map[string]interface{}) error { func upgradeSchema5to6(diskConfig *map[string]interface{}) error {
log.Printf("%s(): called", util.FuncName()) log.Printf("%s(): called", funcName())
(*diskConfig)["schema_version"] = 6 (*diskConfig)["schema_version"] = 6
@ -433,3 +433,12 @@ func upgradeSchema6to7(diskConfig *map[string]interface{}) error {
return nil 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())
}

View File

@ -3,7 +3,6 @@ package home
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
golog "log"
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
@ -30,7 +29,7 @@ const (
WriteTimeout = 10 * time.Second WriteTimeout = 10 * time.Second
) )
type WebConfig struct { type webConfig struct {
firstRun bool firstRun bool
BindHost string BindHost string
BindPort int BindPort int
@ -61,34 +60,20 @@ type HTTPSServer struct {
// Web - module object // Web - module object
type Web struct { type Web struct {
conf *WebConfig conf *webConfig
forceHTTPS bool forceHTTPS bool
portHTTPS int portHTTPS int
httpServer *http.Server // HTTP module httpServer *http.Server // HTTP module
httpsServer HTTPSServer // HTTPS 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 // CreateWeb - create module
func CreateWeb(conf *WebConfig) *Web { func CreateWeb(conf *webConfig) *Web {
log.Info("Initialize web module") log.Info("Initialize web module")
w := Web{} w := Web{}
w.conf = conf w.conf = conf
lw := logWriter{}
w.errLogger = golog.New(&lw, "", 0)
// Initialize and run the admin Web interface // Initialize and run the admin Web interface
box := packr.NewBox("../../build/static") 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 // 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)) address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.conf.BindPort))
web.httpServer = &http.Server{ web.httpServer = &http.Server{
ErrorLog: web.errLogger, ErrorLog: log.StdLog("web: http", log.DEBUG),
Addr: address, Addr: address,
Handler: withMiddlewares(Context.mux, limitRequestBody), Handler: withMiddlewares(Context.mux, limitRequestBody),
ReadTimeout: web.conf.ReadTimeout, ReadTimeout: web.conf.ReadTimeout,
ReadHeaderTimeout: web.conf.ReadHeaderTimeout, ReadHeaderTimeout: web.conf.ReadHeaderTimeout,
WriteTimeout: web.conf.WriteTimeout, WriteTimeout: web.conf.WriteTimeout,
} }
err := web.httpServer.ListenAndServe() err := web.httpServer.ListenAndServe()
if err != http.ErrServerClosed { if err != http.ErrServerClosed {
cleanupAlways() cleanupAlways()
@ -220,7 +206,7 @@ func (web *Web) tlsServerLoop() {
// prepare HTTPS server // prepare HTTPS server
address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.conf.PortHTTPS)) address := net.JoinHostPort(web.conf.BindHost, strconv.Itoa(web.conf.PortHTTPS))
web.httpsServer.server = &http.Server{ web.httpsServer.server = &http.Server{
ErrorLog: web.errLogger, ErrorLog: log.StdLog("web: https", log.DEBUG),
Addr: address, Addr: address,
TLSConfig: &tls.Config{ TLSConfig: &tls.Config{
Certificates: []tls.Certificate{web.httpsServer.cert}, Certificates: []tls.Certificate{web.httpsServer.cert},

13
main.go
View File

@ -7,15 +7,20 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/home" "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" var version = "undefined"
// channel can be set via ldflags // channel is the release channel. It is set by the linker.
var channel = "release" var channel = "release"
// GOARM value - set via ldflags // goarm is the GOARM value. It is set by the linker.
var goarm = "" var goarm = ""
// gomips is the GOMIPS value. It is set by the linker.
//
// TODO(a.garipov): Implement.
var gomips = ""
func main() { func main() {
home.Main(version, channel, goarm) home.Main(version, channel, goarm, gomips)
} }