Skip to content

Commit

Permalink
all: imp docs
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Dec 18, 2023
1 parent a2118f5 commit c3b21c7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 71 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -28,6 +28,13 @@ NOTE: Add new changes BELOW THIS COMMENT.
- Ability to disable plain-DNS serving via UI if an encrypted protocol is
already used ([#1660]).

### Changed

#### Configuration changes

- The new property `clients.persistent.*.uid`, which is unique identifier of the
persistent client.

### Fixed

- Omitted CNAME records in safe search results, which can cause YouTube to not
Expand Down
51 changes: 26 additions & 25 deletions internal/home/clients.go
Expand Up @@ -289,7 +289,7 @@ func (clients *clientsContainer) addFromConfig(
return err
}

_, err = clients.Add(cli)
_, err = clients.add(cli)
if err != nil {
log.Error("clients: adding clients %s: %s", cli.Name, err)
}
Expand Down Expand Up @@ -379,10 +379,10 @@ func (clients *clientsContainer) clientSource(ip netip.Addr) (src client.Source)
return src
}

// findMultiple is a wrapper around Find to make it a valid client finder for
// the query log. c is never nil; if no information about the client is found,
// it returns an artificial client record by only setting the blocking-related
// fields. err is always nil.
// findMultiple is a wrapper around [clientsContainer.find] to make it a valid
// client finder for the query log. c is never nil; if no information about the
// client is found, it returns an artificial client record by only setting the
// blocking-related fields. err is always nil.
func (clients *clientsContainer) findMultiple(ids []string) (c *querylog.Client, err error) {
var artClient *querylog.Client
var art bool
Expand Down Expand Up @@ -416,7 +416,7 @@ func (clients *clientsContainer) clientOrArtificial(
}
}()

cli, ok := clients.Find(id)
cli, ok := clients.find(id)
if ok {
return &querylog.Client{
Name: cli.Name,
Expand All @@ -440,8 +440,8 @@ func (clients *clientsContainer) clientOrArtificial(
}, true
}

// Find returns a shallow copy of the client if there is one found.
func (clients *clientsContainer) Find(id string) (c *persistentClient, ok bool) {
// find returns a shallow copy of the client if there is one found.
func (clients *clientsContainer) find(id string) (c *persistentClient, ok bool) {
clients.lock.Lock()
defer clients.lock.Unlock()

Expand All @@ -453,9 +453,9 @@ func (clients *clientsContainer) Find(id string) (c *persistentClient, ok bool)
return c.ShallowClone(), true
}

// shouldCountClient is a wrapper around Find to make it a valid client
// information finder for the statistics. If no information about the client
// is found, it returns true.
// shouldCountClient is a wrapper around [clientsContainer.find] to make it a
// valid client information finder for the statistics. If no information about
// the client is found, it returns true.
func (clients *clientsContainer) shouldCountClient(ids []string) (y bool) {
clients.lock.Lock()
defer clients.lock.Unlock()
Expand Down Expand Up @@ -677,9 +677,9 @@ func normalizeClientIdentifier(idStr string) (norm string, err error) {
return "", fmt.Errorf("bad client identifier %q", idStr)
}

// Add adds a new client object. ok is false if such client already exists or
// add adds a new client object. ok is false if such client already exists or
// if an error occurred.
func (clients *clientsContainer) Add(c *persistentClient) (ok bool, err error) {
func (clients *clientsContainer) add(c *persistentClient) (ok bool, err error) {
err = clients.check(c)
if err != nil {
return false, err
Expand All @@ -703,15 +703,15 @@ func (clients *clientsContainer) Add(c *persistentClient) (ok bool, err error) {
}
}

clients.add(c)
clients.addLocked(c)

log.Debug("clients: added %q: ID:%q [%d]", c.Name, c.IDs, len(clients.list))

return true, nil
}

// add c to the indexes. clients.lock is expected to be locked.
func (clients *clientsContainer) add(c *persistentClient) {
// addLocked c to the indexes. clients.lock is expected to be locked.
func (clients *clientsContainer) addLocked(c *persistentClient) {
// update Name index
clients.list[c.Name] = c

Expand All @@ -721,8 +721,8 @@ func (clients *clientsContainer) add(c *persistentClient) {
}
}

// Del removes a client. ok is false if there is no such client.
func (clients *clientsContainer) Del(name string) (ok bool) {
// remove removes a client. ok is false if there is no such client.
func (clients *clientsContainer) remove(name string) (ok bool) {
clients.lock.Lock()
defer clients.lock.Unlock()

Expand All @@ -732,13 +732,14 @@ func (clients *clientsContainer) Del(name string) (ok bool) {
return false
}

clients.del(c)
clients.removeLocked(c)

return true
}

// del removes c from the indexes. clients.lock is expected to be locked.
func (clients *clientsContainer) del(c *persistentClient) {
// removeLocked removes c from the indexes. clients.lock is expected to be
// locked.
func (clients *clientsContainer) removeLocked(c *persistentClient) {
if err := c.closeUpstreams(); err != nil {
log.Error("client container: removing client %s: %s", c.Name, err)
}
Expand All @@ -752,8 +753,8 @@ func (clients *clientsContainer) del(c *persistentClient) {
}
}

// Update updates a client by its name.
func (clients *clientsContainer) Update(prev, c *persistentClient) (err error) {
// update updates a client by its name.
func (clients *clientsContainer) update(prev, c *persistentClient) (err error) {
err = clients.check(c)
if err != nil {
// Don't wrap the error since it's informative enough as is.
Expand Down Expand Up @@ -781,8 +782,8 @@ func (clients *clientsContainer) Update(prev, c *persistentClient) (err error) {
}
}

clients.del(prev)
clients.add(c)
clients.removeLocked(prev)
clients.addLocked(c)

return nil
}
Expand Down
44 changes: 22 additions & 22 deletions internal/home/clients_internal_test.go
Expand Up @@ -69,7 +69,7 @@ func TestClients(t *testing.T) {
Name: "client1",
}

ok, err := clients.Add(c)
ok, err := clients.add(c)
require.NoError(t, err)

assert.True(t, ok)
Expand All @@ -79,35 +79,35 @@ func TestClients(t *testing.T) {
Name: "client2",
}

ok, err = clients.Add(c)
ok, err = clients.add(c)
require.NoError(t, err)

assert.True(t, ok)

c, ok = clients.Find(cli1)
c, ok = clients.find(cli1)
require.True(t, ok)

assert.Equal(t, "client1", c.Name)

c, ok = clients.Find("1:2:3::4")
c, ok = clients.find("1:2:3::4")
require.True(t, ok)

assert.Equal(t, "client1", c.Name)

c, ok = clients.Find(cli2)
c, ok = clients.find(cli2)
require.True(t, ok)

assert.Equal(t, "client2", c.Name)

_, ok = clients.Find(cliNone)
_, ok = clients.find(cliNone)
assert.False(t, ok)

assert.Equal(t, clients.clientSource(cli1IP), client.SourcePersistent)
assert.Equal(t, clients.clientSource(cli2IP), client.SourcePersistent)
})

t.Run("add_fail_name", func(t *testing.T) {
ok, err := clients.Add(&persistentClient{
ok, err := clients.add(&persistentClient{
IDs: []string{"1.2.3.5"},
Name: "client1",
})
Expand All @@ -116,7 +116,7 @@ func TestClients(t *testing.T) {
})

t.Run("add_fail_ip", func(t *testing.T) {
ok, err := clients.Add(&persistentClient{
ok, err := clients.add(&persistentClient{
IDs: []string{"2.2.2.2"},
Name: "client3",
})
Expand All @@ -125,7 +125,7 @@ func TestClients(t *testing.T) {
})

t.Run("update_fail_ip", func(t *testing.T) {
err := clients.Update(&persistentClient{Name: "client1"}, &persistentClient{
err := clients.update(&persistentClient{Name: "client1"}, &persistentClient{
IDs: []string{"2.2.2.2"},
Name: "client1",
})
Expand All @@ -143,28 +143,28 @@ func TestClients(t *testing.T) {
prev, ok := clients.list["client1"]
require.True(t, ok)

err := clients.Update(prev, &persistentClient{
err := clients.update(prev, &persistentClient{
IDs: []string{cliNew},
Name: "client1",
})
require.NoError(t, err)

_, ok = clients.Find(cliOld)
_, ok = clients.find(cliOld)
assert.False(t, ok)

assert.Equal(t, clients.clientSource(cliNewIP), client.SourcePersistent)

prev, ok = clients.list["client1"]
require.True(t, ok)

err = clients.Update(prev, &persistentClient{
err = clients.update(prev, &persistentClient{
IDs: []string{cliNew},
Name: "client1-renamed",
UseOwnSettings: true,
})
require.NoError(t, err)

c, ok := clients.Find(cliNew)
c, ok := clients.find(cliNew)
require.True(t, ok)

assert.Equal(t, "client1-renamed", c.Name)
Expand All @@ -181,15 +181,15 @@ func TestClients(t *testing.T) {
})

t.Run("del_success", func(t *testing.T) {
ok := clients.Del("client1-renamed")
ok := clients.remove("client1-renamed")
require.True(t, ok)

_, ok = clients.Find("1.1.1.2")
_, ok = clients.find("1.1.1.2")
assert.False(t, ok)
})

t.Run("del_fail", func(t *testing.T) {
ok := clients.Del("client3")
ok := clients.remove("client3")
assert.False(t, ok)
})

Expand Down Expand Up @@ -258,7 +258,7 @@ func TestClientsWHOIS(t *testing.T) {
t.Run("can't_set_manually-added", func(t *testing.T) {
ip := netip.MustParseAddr("1.1.1.2")

ok, err := clients.Add(&persistentClient{
ok, err := clients.add(&persistentClient{
IDs: []string{"1.1.1.2"},
Name: "client1",
})
Expand All @@ -269,7 +269,7 @@ func TestClientsWHOIS(t *testing.T) {
rc := clients.ipToRC[ip]
require.Nil(t, rc)

assert.True(t, clients.Del("client1"))
assert.True(t, clients.remove("client1"))
})
}

Expand All @@ -280,7 +280,7 @@ func TestClientsAddExisting(t *testing.T) {
ip := netip.MustParseAddr("1.1.1.1")

// Add a client.
ok, err := clients.Add(&persistentClient{
ok, err := clients.add(&persistentClient{
IDs: []string{ip.String(), "1:2:3::4", "aa:aa:aa:aa:aa:aa", "2.2.2.0/24"},
Name: "client1",
})
Expand Down Expand Up @@ -328,15 +328,15 @@ func TestClientsAddExisting(t *testing.T) {
require.NoError(t, err)

// Add a new client with the same IP as for a client with MAC.
ok, err := clients.Add(&persistentClient{
ok, err := clients.add(&persistentClient{
IDs: []string{ip.String()},
Name: "client2",
})
require.NoError(t, err)
assert.True(t, ok)

// Add a new client with the IP from the first client's IP range.
ok, err = clients.Add(&persistentClient{
ok, err = clients.add(&persistentClient{
IDs: []string{"2.2.2.2"},
Name: "client3",
})
Expand All @@ -349,7 +349,7 @@ func TestClientsCustomUpstream(t *testing.T) {
clients := newClientsContainer(t)

// Add client with upstreams.
ok, err := clients.Add(&persistentClient{
ok, err := clients.add(&persistentClient{
IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa"},
Name: "client1",
Upstreams: []string{
Expand Down

0 comments on commit c3b21c7

Please sign in to comment.