From b86250737e29b8656186ed34c6895ca1bd353cd3 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Fri, 21 Oct 2022 20:42:00 +0300 Subject: [PATCH] Pull request: 5035-slices-clone Updates #5035. Squashed commit of the following: commit 9a5be90a347d4da08649927731b20fd5ecb90be2 Author: Ainar Garipov Date: Fri Oct 21 20:11:54 2022 +0300 all: use slices.Clone, netip.AddrPort --- internal/aghnet/arpdb.go | 6 +++--- internal/dhcpd/dhcpd.go | 6 +++--- internal/dhcpd/dhcpd_unix_test.go | 4 ++-- internal/dhcpd/v4_unix.go | 2 +- internal/dnsforward/dns.go | 3 ++- internal/dnsforward/http.go | 17 +++++++++-------- internal/dnsforward/http_test.go | 14 ++++++-------- internal/dnsforward/stats.go | 3 ++- internal/filtering/rewrites.go | 4 ++-- internal/querylog/json.go | 10 +++++----- 10 files changed, 35 insertions(+), 34 deletions(-) diff --git a/internal/aghnet/arpdb.go b/internal/aghnet/arpdb.go index 4909af5f..0c2ac7d9 100644 --- a/internal/aghnet/arpdb.go +++ b/internal/aghnet/arpdb.go @@ -8,7 +8,7 @@ import ( "sync" "github.com/AdguardTeam/golibs/errors" - "github.com/AdguardTeam/golibs/netutil" + "golang.org/x/exp/slices" ) // ARPDB: The Network Neighborhood Database @@ -64,8 +64,8 @@ type Neighbor struct { func (n Neighbor) Clone() (clone Neighbor) { return Neighbor{ Name: n.Name, - IP: netutil.CloneIP(n.IP), - MAC: netutil.CloneMAC(n.MAC), + IP: slices.Clone(n.IP), + MAC: slices.Clone(n.MAC), } } diff --git a/internal/dhcpd/dhcpd.go b/internal/dhcpd/dhcpd.go index a98178ca..ea7725a9 100644 --- a/internal/dhcpd/dhcpd.go +++ b/internal/dhcpd/dhcpd.go @@ -9,8 +9,8 @@ import ( "time" "github.com/AdguardTeam/golibs/log" - "github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/timeutil" + "golang.org/x/exp/slices" ) const ( @@ -54,8 +54,8 @@ func (l *Lease) Clone() (clone *Lease) { return &Lease{ Expiry: l.Expiry, Hostname: l.Hostname, - HWAddr: netutil.CloneMAC(l.HWAddr), - IP: netutil.CloneIP(l.IP), + HWAddr: slices.Clone(l.HWAddr), + IP: slices.Clone(l.IP), } } diff --git a/internal/dhcpd/dhcpd_unix_test.go b/internal/dhcpd/dhcpd_unix_test.go index f13dbf98..38305076 100644 --- a/internal/dhcpd/dhcpd_unix_test.go +++ b/internal/dhcpd/dhcpd_unix_test.go @@ -10,10 +10,10 @@ import ( "time" "github.com/AdguardTeam/AdGuardHome/internal/aghtest" - "github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/testutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "golang.org/x/exp/slices" ) func TestMain(m *testing.M) { @@ -157,7 +157,7 @@ func TestV4Server_badRange(t *testing.T) { // cloneUDPAddr returns a deep copy of a. func cloneUDPAddr(a *net.UDPAddr) (clone *net.UDPAddr) { return &net.UDPAddr{ - IP: netutil.CloneIP(a.IP), + IP: slices.Clone(a.IP), Port: a.Port, Zone: a.Zone, } diff --git a/internal/dhcpd/v4_unix.go b/internal/dhcpd/v4_unix.go index af389fda..d83e0125 100644 --- a/internal/dhcpd/v4_unix.go +++ b/internal/dhcpd/v4_unix.go @@ -1018,7 +1018,7 @@ func (s *v4Server) handle(req, resp *dhcpv4.DHCPv4) int { } if l != nil { - resp.YourIPAddr = netutil.CloneIP(l.IP) + resp.YourIPAddr = slices.Clone(l.IP) } s.updateOptions(req, resp) diff --git a/internal/dnsforward/dns.go b/internal/dnsforward/dns.go index 947625e3..48bb81c0 100644 --- a/internal/dnsforward/dns.go +++ b/internal/dnsforward/dns.go @@ -13,6 +13,7 @@ import ( "github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/stringutil" "github.com/miekg/dns" + "golang.org/x/exp/slices" ) // To transfer information between modules @@ -237,7 +238,7 @@ func (s *Server) onDHCPLeaseChanged(flags int) { } lowhost := strings.ToLower(l.Hostname + "." + s.localDomainSuffix) - ip := netutil.CloneIP(l.IP) + ip := slices.Clone(l.IP) ipToHost.Set(ip, lowhost) hostToIP[lowhost] = ip diff --git a/internal/dnsforward/http.go b/internal/dnsforward/http.go index 97749c9c..8879bb1a 100644 --- a/internal/dnsforward/http.go +++ b/internal/dnsforward/http.go @@ -5,6 +5,7 @@ import ( "fmt" "net" "net/http" + "net/netip" "strings" "time" @@ -435,22 +436,22 @@ func validateUpstream(u string, domains []string) (useDefault bool, err error) { // TODO(e.burkov): Validate the domain name. for _, proto := range protocols { if strings.HasPrefix(u, proto) { - return useDefault, nil + return false, nil } } - if strings.Contains(u, "://") { - return useDefault, errors.Error("wrong protocol") + if proto, _, ok := strings.Cut(u, "://"); ok { + return false, fmt.Errorf("bad protocol %q", proto) } // Check if upstream is either an IP or IP with port. - if net.ParseIP(u) != nil { - return useDefault, nil - } else if _, err = netutil.ParseIPPort(u); err != nil { - return useDefault, err + if _, err = netip.ParseAddr(u); err == nil { + return false, nil + } else if _, err = netip.ParseAddrPort(u); err == nil { + return false, nil } - return useDefault, nil + return false, err } // separateUpstream returns the upstream and the specified domains. domains is diff --git a/internal/dnsforward/http_test.go b/internal/dnsforward/http_test.go index f591ac58..64ba21c4 100644 --- a/internal/dnsforward/http_test.go +++ b/internal/dnsforward/http_test.go @@ -188,10 +188,8 @@ func TestDNSForwardHTTP_handleSetConfig(t *testing.T) { name: "upstream_mode_fastest_addr", wantSet: "", }, { - name: "upstream_dns_bad", - wantSet: `validating upstream servers: ` + - `validating upstream "!!!": bad ipport address "!!!": ` + - `address !!!: missing port in address`, + name: "upstream_dns_bad", + wantSet: `validating upstream servers: validating upstream "!!!": not an ip:port`, }, { name: "bootstraps_bad", wantSet: `checking bootstrap a: invalid address: ` + @@ -297,15 +295,15 @@ func TestValidateUpstreams(t *testing.T) { }, }, { name: "invalid", - wantErr: `validating upstream "dhcp://fake.dns": wrong protocol`, + wantErr: `validating upstream "dhcp://fake.dns": bad protocol "dhcp"`, set: []string{"dhcp://fake.dns"}, }, { name: "invalid", - wantErr: `validating upstream "1.2.3.4.5": bad ipport address "1.2.3.4.5": address 1.2.3.4.5: missing port in address`, + wantErr: `validating upstream "1.2.3.4.5": not an ip:port`, set: []string{"1.2.3.4.5"}, }, { name: "invalid", - wantErr: `validating upstream "123.3.7m": bad ipport address "123.3.7m": address 123.3.7m: missing port in address`, + wantErr: `validating upstream "123.3.7m": not an ip:port`, set: []string{"123.3.7m"}, }, { name: "invalid", @@ -313,7 +311,7 @@ func TestValidateUpstreams(t *testing.T) { set: []string{"[/host.com]tls://dns.adguard.com"}, }, { name: "invalid", - wantErr: `validating upstream "[host.ru]#": bad ipport address "[host.ru]#": address [host.ru]#: missing port in address`, + wantErr: `validating upstream "[host.ru]#": not an ip:port`, set: []string{"[host.ru]#"}, }, { name: "valid_default", diff --git a/internal/dnsforward/stats.go b/internal/dnsforward/stats.go index 9a7b1ddb..4d63220d 100644 --- a/internal/dnsforward/stats.go +++ b/internal/dnsforward/stats.go @@ -12,6 +12,7 @@ import ( "github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/netutil" "github.com/miekg/dns" + "golang.org/x/exp/slices" ) // Write Stats data and logs @@ -28,7 +29,7 @@ func (s *Server) processQueryLogsAndStats(dctx *dnsContext) (rc resultCode) { } ip, _ := netutil.IPAndPortFromAddr(pctx.Addr) - ip = netutil.CloneIP(ip) + ip = slices.Clone(ip) s.serverLock.RLock() defer s.serverLock.RUnlock() diff --git a/internal/filtering/rewrites.go b/internal/filtering/rewrites.go index 2c09728f..675ab53e 100644 --- a/internal/filtering/rewrites.go +++ b/internal/filtering/rewrites.go @@ -13,8 +13,8 @@ import ( "github.com/AdguardTeam/AdGuardHome/internal/aghhttp" "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/log" - "github.com/AdguardTeam/golibs/netutil" "github.com/miekg/dns" + "golang.org/x/exp/slices" ) // LegacyRewrite is a single legacy DNS rewrite record. @@ -41,7 +41,7 @@ func (rw *LegacyRewrite) clone() (cloneRW *LegacyRewrite) { return &LegacyRewrite{ Domain: rw.Domain, Answer: rw.Answer, - IP: netutil.CloneIP(rw.IP), + IP: slices.Clone(rw.IP), Type: rw.Type, } } diff --git a/internal/querylog/json.go b/internal/querylog/json.go index 24acd468..f570d40f 100644 --- a/internal/querylog/json.go +++ b/internal/querylog/json.go @@ -9,8 +9,8 @@ import ( "github.com/AdguardTeam/AdGuardHome/internal/aghnet" "github.com/AdguardTeam/AdGuardHome/internal/filtering" "github.com/AdguardTeam/golibs/log" - "github.com/AdguardTeam/golibs/netutil" "github.com/miekg/dns" + "golang.org/x/exp/slices" "golang.org/x/net/idna" ) @@ -55,14 +55,14 @@ func (l *queryLog) entryToJSON(entry *logEntry, anonFunc aghnet.IPMutFunc) (json question["unicode_name"] = qhost } - eip := netutil.CloneIP(entry.IP) - anonFunc(eip) + entIP := slices.Clone(entry.IP) + anonFunc(entIP) jsonEntry = jobject{ "reason": entry.Result.Reason.String(), "elapsedMs": strconv.FormatFloat(entry.Elapsed.Seconds()*1000, 'f', -1, 64), "time": entry.Time.Format(time.RFC3339Nano), - "client": eip, + "client": entIP, "client_proto": entry.ClientProto, "cached": entry.Cached, "upstream": entry.Upstream, @@ -70,7 +70,7 @@ func (l *queryLog) entryToJSON(entry *logEntry, anonFunc aghnet.IPMutFunc) (json "rules": resultRulesToJSONRules(entry.Result.Rules), } - if eip.Equal(entry.IP) { + if entIP.Equal(entry.IP) { jsonEntry["client_info"] = entry.client }