From 214175eb4163e25b1d955017e798926062bad472 Mon Sep 17 00:00:00 2001 From: Stanislav Chzhen Date: Wed, 6 Dec 2023 19:28:56 +0300 Subject: [PATCH] Pull request 2097: 6420-ipset-currenly-known-sets Updates #6420. Squashed commit of the following: commit 9c08b4206845547bae07b5c456eaf74df4d55921 Merge: a9ee2fe56 083abaac0 Author: Stanislav Chzhen Date: Wed Dec 6 19:20:53 2023 +0300 Merge branch 'master' into 6420-ipset-currenly-known-sets commit a9ee2fe5603168ce75a5c8cba5b2356c039b0970 Author: Stanislav Chzhen Date: Wed Dec 6 18:26:01 2023 +0300 all: imp docs commit fabaab0cc40256cb486366e1cc2d345159178cd1 Author: Stanislav Chzhen Date: Wed Dec 6 15:00:58 2023 +0300 ipset: currently known sets --- internal/ipset/ipset_linux.go | 36 +++++++++++++-------- internal/schedule/schedule.go | 6 ++-- internal/schedule/schedule_internal_test.go | 12 +++---- scripts/make/go-lint.sh | 4 +++ 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/internal/ipset/ipset_linux.go b/internal/ipset/ipset_linux.go index 6bc9add1..4cf1fe12 100644 --- a/internal/ipset/ipset_linux.go +++ b/internal/ipset/ipset_linux.go @@ -269,8 +269,9 @@ func (m *manager) parseIpsetConfig(ipsetConf []string) (err error) { return err } + currentlyKnown := map[string]props{} for _, p := range all { - m.nameToIpset[p.name] = p + currentlyKnown[p.name] = p } for i, confStr := range ipsetConf { @@ -281,7 +282,7 @@ func (m *manager) parseIpsetConfig(ipsetConf []string) (err error) { } var ipsets []props - ipsets, err = m.ipsets(ipsetNames) + ipsets, err = m.ipsets(ipsetNames, currentlyKnown) if err != nil { return fmt.Errorf("getting ipsets from config line at idx %d: %w", i, err) } @@ -297,35 +298,43 @@ func (m *manager) parseIpsetConfig(ipsetConf []string) (err error) { // ipsetProps returns the properties of an ipset with the given name. // // Additional header data query. See https://github.com/AdguardTeam/AdGuardHome/issues/6420. -func (m *manager) ipsetProps(p props) (err error) { +// +// TODO(s.chzhen): Use *props. +func (m *manager) ipsetProps(name string) (p props, err error) { // The family doesn't seem to matter when we use a header query, so // query only the IPv4 one. // // TODO(a.garipov): Find out if this is a bug or a feature. var res *ipset.HeaderPolicy - res, err = m.ipv4Conn.Header(p.name) + res, err = m.ipv4Conn.Header(name) if err != nil { - return err + return props{}, err } if res == nil || res.Family == nil { - return errors.Error("empty response or no family data") + return props{}, errors.Error("empty response or no family data") } family := netfilter.ProtoFamily(res.Family.Value) if family != netfilter.ProtoIPv4 && family != netfilter.ProtoIPv6 { - return fmt.Errorf("unexpected ipset family %q", family) + return props{}, fmt.Errorf("unexpected ipset family %q", family) } - p.family = family + typeName := res.TypeName.Get() - return nil + return props{ + name: name, + typeName: typeName, + family: family, + isPersistent: false, + }, nil } -// ipsets returns currently known ipsets. -func (m *manager) ipsets(names []string) (sets []props, err error) { +// ipsets returns ipset properties of currently known ipsets. It also makes an +// additional ipset header data query if needed. +func (m *manager) ipsets(names []string, currentlyKnown map[string]props) (sets []props, err error) { for _, n := range names { - p, ok := m.nameToIpset[n] + p, ok := currentlyKnown[n] if !ok { return nil, fmt.Errorf("unknown ipset %q", n) } @@ -337,12 +346,13 @@ func (m *manager) ipsets(names []string) (sets []props, err error) { p.family, ) - err = m.ipsetProps(p) + p, err = m.ipsetProps(n) if err != nil { return nil, fmt.Errorf("%q %q making header query: %w", p.name, p.typeName, err) } } + m.nameToIpset[n] = p sets = append(sets, p) } diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go index fe5aeb82..8335a79e 100644 --- a/internal/schedule/schedule.go +++ b/internal/schedule/schedule.go @@ -339,9 +339,6 @@ func (r dayRange) toDayConfigJSON() (j *dayConfigJSON) { // weeklyConfigJSON is the JSON configuration structure of Weekly. type weeklyConfigJSON struct { - // TimeZone is the local time zone. - TimeZone string `json:"time_zone"` - // Days of the week. Sunday *dayConfigJSON `json:"sun,omitempty"` @@ -351,6 +348,9 @@ type weeklyConfigJSON struct { Thursday *dayConfigJSON `json:"thu,omitempty"` Friday *dayConfigJSON `json:"fri,omitempty"` Saturday *dayConfigJSON `json:"sat,omitempty"` + + // TimeZone is the local time zone. + TimeZone string `json:"time_zone"` } // dayConfigJSON is the JSON configuration structure of dayRange. diff --git a/internal/schedule/schedule_internal_test.go b/internal/schedule/schedule_internal_test.go index 0d9aa705..3e95676c 100644 --- a/internal/schedule/schedule_internal_test.go +++ b/internal/schedule/schedule_internal_test.go @@ -163,10 +163,10 @@ yaml: "bad" } testCases := []struct { + want *Weekly name string wantErrMsg string data []byte - want *Weekly }{{ name: "empty", wantErrMsg: "", @@ -228,9 +228,9 @@ func TestWeekly_MarshalYAML(t *testing.T) { } testCases := []struct { + want *Weekly name string data []byte - want *Weekly }{{ name: "empty", data: []byte(""), @@ -263,8 +263,8 @@ func TestWeekly_MarshalYAML(t *testing.T) { func TestWeekly_Validate(t *testing.T) { testCases := []struct { name string - in dayRange wantErrMsg string + in dayRange }{{ name: "empty", wantErrMsg: "", @@ -298,8 +298,8 @@ func TestWeekly_Validate(t *testing.T) { func TestDayRange_Validate(t *testing.T) { testCases := []struct { name string - in dayRange wantErrMsg string + in dayRange }{{ name: "empty", wantErrMsg: "", @@ -413,10 +413,10 @@ func TestWeekly_UnmarshalJSON(t *testing.T) { } testCases := []struct { + want *Weekly name string wantErrMsg string data []byte - want *Weekly }{{ name: "empty", wantErrMsg: "unexpected end of JSON input", @@ -478,9 +478,9 @@ func TestWeekly_MarshalJSON(t *testing.T) { } testCases := []struct { + want *Weekly name string data []byte - want *Weekly }{{ name: "empty", data: []byte(""), diff --git a/scripts/make/go-lint.sh b/scripts/make/go-lint.sh index c04499a1..4ebcdcff 100644 --- a/scripts/make/go-lint.sh +++ b/scripts/make/go-lint.sh @@ -218,6 +218,7 @@ run_linter gocognit --over='10'\ ./internal/filtering/hashprefix/\ ./internal/filtering/rulelist/\ ./internal/filtering/safesearch/\ + ./internal/ipset\ ./internal/next/\ ./internal/rdns/\ ./internal/schedule/\ @@ -257,9 +258,11 @@ run_linter fieldalignment \ ./internal/filtering/rewrite/\ ./internal/filtering/rulelist/\ ./internal/filtering/safesearch/\ + ./internal/ipset/\ ./internal/next/...\ ./internal/querylog/\ ./internal/rdns/\ + ./internal/schedule/\ ./internal/stats/\ ./internal/updater/\ ./internal/version/\ @@ -287,6 +290,7 @@ run_linter gosec --quiet\ ./internal/filtering/rewrite/\ ./internal/filtering/rulelist/\ ./internal/filtering/safesearch/\ + ./internal/ipset/\ ./internal/next/\ ./internal/rdns/\ ./internal/schedule/\