diff --git a/internal/dhcpd/dhcpd_test.go b/internal/dhcpd/dhcpd_test.go index 27a48b235ad..1e6c9238434 100644 --- a/internal/dhcpd/dhcpd_test.go +++ b/internal/dhcpd/dhcpd_test.go @@ -28,10 +28,10 @@ func TestDB(t *testing.T) { conf := V4ServerConf{ Enabled: true, - RangeStart: net.ParseIP("192.168.10.100"), - RangeEnd: net.ParseIP("192.168.10.200"), - GatewayIP: net.ParseIP("192.168.10.1"), - SubnetMask: net.ParseIP("255.255.255.0"), + RangeStart: net.IP{192, 168, 10, 100}, + RangeEnd: net.IP{192, 168, 10, 200}, + GatewayIP: net.IP{192, 168, 10, 1}, + SubnetMask: net.IP{255, 255, 255, 0}, notify: testNotify, } s.srv4, err = v4Create(conf) @@ -41,14 +41,14 @@ func TestDB(t *testing.T) { assert.True(t, err == nil) l := Lease{} - l.IP = net.ParseIP("192.168.10.100").To4() + l.IP = net.IP{192, 168, 10, 100} l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") exp1 := time.Now().Add(time.Hour) l.Expiry = exp1 s.srv4.(*v4Server).addLease(&l) l2 := Lease{} - l2.IP = net.ParseIP("192.168.10.101").To4() + l2.IP = net.IP{192, 168, 10, 101} l2.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:bb") s.srv4.AddStaticLease(l2) diff --git a/internal/dhcpd/v4.go b/internal/dhcpd/v4.go index f89d81a5c1f..038d8f9c06f 100644 --- a/internal/dhcpd/v4.go +++ b/internal/dhcpd/v4.go @@ -589,7 +589,7 @@ func (s *v4Server) Start() error { s.conf.dnsIPAddrs = dnsIPAddrs laddr := &net.UDPAddr{ - IP: net.ParseIP("0.0.0.0"), + IP: net.IP{0, 0, 0, 0}, Port: dhcpv4.ServerPort, } s.srv, err = server4.NewServer(iface.Name, laddr, s.packetHandler, server4.WithDebugLogger()) diff --git a/internal/dhcpd/v4_test.go b/internal/dhcpd/v4_test.go index 27ee672b852..c2edd31e599 100644 --- a/internal/dhcpd/v4_test.go +++ b/internal/dhcpd/v4_test.go @@ -16,10 +16,10 @@ func notify4(flags uint32) { func TestV4StaticLeaseAddRemove(t *testing.T) { conf := V4ServerConf{ Enabled: true, - RangeStart: net.ParseIP("192.168.10.100"), - RangeEnd: net.ParseIP("192.168.10.200"), - GatewayIP: net.ParseIP("192.168.10.1"), - SubnetMask: net.ParseIP("255.255.255.0"), + RangeStart: net.IP{192, 168, 10, 100}, + RangeEnd: net.IP{192, 168, 10, 200}, + GatewayIP: net.IP{192, 168, 10, 1}, + SubnetMask: net.IP{255, 255, 255, 0}, notify: notify4, } s, err := v4Create(conf) @@ -30,7 +30,7 @@ func TestV4StaticLeaseAddRemove(t *testing.T) { // add static lease l := Lease{} - l.IP = net.ParseIP("192.168.10.150").To4() + l.IP = net.IP{192, 168, 10, 150} l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") assert.True(t, s.AddStaticLease(l) == nil) @@ -45,12 +45,12 @@ func TestV4StaticLeaseAddRemove(t *testing.T) { assert.True(t, ls[0].Expiry.Unix() == leaseExpireStatic) // try to remove static lease - fail - l.IP = net.ParseIP("192.168.10.110").To4() + l.IP = net.IP{192, 168, 10, 110} l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") assert.True(t, s.RemoveStaticLease(l) != nil) // remove static lease - l.IP = net.ParseIP("192.168.10.150").To4() + l.IP = net.IP{192, 168, 10, 150} l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") assert.True(t, s.RemoveStaticLease(l) == nil) @@ -62,10 +62,10 @@ func TestV4StaticLeaseAddRemove(t *testing.T) { func TestV4StaticLeaseAddReplaceDynamic(t *testing.T) { conf := V4ServerConf{ Enabled: true, - RangeStart: net.ParseIP("192.168.10.100"), - RangeEnd: net.ParseIP("192.168.10.200"), - GatewayIP: net.ParseIP("192.168.10.1"), - SubnetMask: net.ParseIP("255.255.255.0"), + RangeStart: net.IP{192, 168, 10, 100}, + RangeEnd: net.IP{192, 168, 10, 200}, + GatewayIP: net.IP{192, 168, 10, 1}, + SubnetMask: net.IP{255, 255, 255, 0}, notify: notify4, } sIface, err := v4Create(conf) @@ -74,27 +74,27 @@ func TestV4StaticLeaseAddReplaceDynamic(t *testing.T) { // add dynamic lease ld := Lease{} - ld.IP = net.ParseIP("192.168.10.150").To4() + ld.IP = net.IP{192, 168, 10, 150} ld.HWAddr, _ = net.ParseMAC("11:aa:aa:aa:aa:aa") s.addLease(&ld) // add dynamic lease { ld := Lease{} - ld.IP = net.ParseIP("192.168.10.151").To4() + ld.IP = net.IP{192, 168, 10, 151} ld.HWAddr, _ = net.ParseMAC("22:aa:aa:aa:aa:aa") s.addLease(&ld) } // add static lease with the same IP l := Lease{} - l.IP = net.ParseIP("192.168.10.150").To4() + l.IP = net.IP{192, 168, 10, 150} l.HWAddr, _ = net.ParseMAC("33:aa:aa:aa:aa:aa") assert.True(t, s.AddStaticLease(l) == nil) // add static lease with the same MAC l = Lease{} - l.IP = net.ParseIP("192.168.10.152").To4() + l.IP = net.IP{192, 168, 10, 152} l.HWAddr, _ = net.ParseMAC("22:aa:aa:aa:aa:aa") assert.True(t, s.AddStaticLease(l) == nil) @@ -114,19 +114,19 @@ func TestV4StaticLeaseAddReplaceDynamic(t *testing.T) { func TestV4StaticLeaseGet(t *testing.T) { conf := V4ServerConf{ Enabled: true, - RangeStart: net.ParseIP("192.168.10.100"), - RangeEnd: net.ParseIP("192.168.10.200"), - GatewayIP: net.ParseIP("192.168.10.1"), - SubnetMask: net.ParseIP("255.255.255.0"), + RangeStart: net.IP{192, 168, 10, 100}, + RangeEnd: net.IP{192, 168, 10, 200}, + GatewayIP: net.IP{192, 168, 10, 1}, + SubnetMask: net.IP{255, 255, 255, 0}, notify: notify4, } sIface, err := v4Create(conf) s := sIface.(*v4Server) assert.True(t, err == nil) - s.conf.dnsIPAddrs = []net.IP{net.ParseIP("192.168.10.1").To4()} + s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}} l := Lease{} - l.IP = net.ParseIP("192.168.10.150").To4() + l.IP = net.IP{192, 168, 10, 150} l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") assert.True(t, s.AddStaticLease(l) == nil) @@ -173,10 +173,10 @@ func TestV4StaticLeaseGet(t *testing.T) { func TestV4DynamicLeaseGet(t *testing.T) { conf := V4ServerConf{ Enabled: true, - RangeStart: net.ParseIP("192.168.10.100"), - RangeEnd: net.ParseIP("192.168.10.200"), - GatewayIP: net.ParseIP("192.168.10.1"), - SubnetMask: net.ParseIP("255.255.255.0"), + RangeStart: net.IP{192, 168, 10, 100}, + RangeEnd: net.IP{192, 168, 10, 200}, + GatewayIP: net.IP{192, 168, 10, 1}, + SubnetMask: net.IP{255, 255, 255, 0}, notify: notify4, Options: []string{ "81 hex 303132", @@ -186,7 +186,7 @@ func TestV4DynamicLeaseGet(t *testing.T) { sIface, err := v4Create(conf) s := sIface.(*v4Server) assert.True(t, err == nil) - s.conf.dnsIPAddrs = []net.IP{net.ParseIP("192.168.10.1").To4()} + s.conf.dnsIPAddrs = []net.IP{{192, 168, 10, 1}} // "Discover" mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa") @@ -229,10 +229,10 @@ func TestV4DynamicLeaseGet(t *testing.T) { assert.Equal(t, "192.168.10.100", ls[0].IP.String()) assert.Equal(t, "aa:aa:aa:aa:aa:aa", ls[0].HWAddr.String()) - start := net.ParseIP("192.168.10.100").To4() - stop := net.ParseIP("192.168.10.200").To4() - assert.True(t, !ip4InRange(start, stop, net.ParseIP("192.168.10.99").To4())) - assert.True(t, !ip4InRange(start, stop, net.ParseIP("192.168.11.100").To4())) - assert.True(t, !ip4InRange(start, stop, net.ParseIP("192.168.11.201").To4())) - assert.True(t, ip4InRange(start, stop, net.ParseIP("192.168.10.100").To4())) + start := net.IP{192, 168, 10, 100} + stop := net.IP{192, 168, 10, 200} + assert.True(t, !ip4InRange(start, stop, net.IP{192, 168, 10, 99})) + assert.True(t, !ip4InRange(start, stop, net.IP{192, 168, 11, 100})) + assert.True(t, !ip4InRange(start, stop, net.IP{192, 168, 11, 201})) + assert.True(t, ip4InRange(start, stop, net.IP{192, 168, 10, 100})) } diff --git a/internal/dnsfilter/rewrites_test.go b/internal/dnsfilter/rewrites_test.go index 3a3284ec13d..66c89939cc8 100644 --- a/internal/dnsfilter/rewrites_test.go +++ b/internal/dnsfilter/rewrites_test.go @@ -28,8 +28,8 @@ func TestRewrites(t *testing.T) { assert.Equal(t, Rewritten, r.Reason) assert.Equal(t, "host.com", r.CanonName) assert.Equal(t, 2, len(r.IPList)) - assert.True(t, r.IPList[0].Equal(net.ParseIP("1.2.3.4"))) - assert.True(t, r.IPList[1].Equal(net.ParseIP("1.2.3.5"))) + assert.True(t, r.IPList[0].Equal(net.IP{1, 2, 3, 4})) + assert.True(t, r.IPList[1].Equal(net.IP{1, 2, 3, 5})) r = d.processRewrites("www.host.com", dns.TypeAAAA) assert.Equal(t, Rewritten, r.Reason) @@ -45,11 +45,11 @@ func TestRewrites(t *testing.T) { d.prepareRewrites() r = d.processRewrites("host.com", dns.TypeA) assert.Equal(t, Rewritten, r.Reason) - assert.True(t, r.IPList[0].Equal(net.ParseIP("1.2.3.4"))) + assert.True(t, r.IPList[0].Equal(net.IP{1, 2, 3, 4})) r = d.processRewrites("www.host.com", dns.TypeA) assert.Equal(t, Rewritten, r.Reason) - assert.True(t, r.IPList[0].Equal(net.ParseIP("1.2.3.5"))) + assert.True(t, r.IPList[0].Equal(net.IP{1, 2, 3, 5})) r = d.processRewrites("www.host2.com", dns.TypeA) assert.Equal(t, NotFilteredNotFound, r.Reason) @@ -63,7 +63,7 @@ func TestRewrites(t *testing.T) { r = d.processRewrites("a.host.com", dns.TypeA) assert.Equal(t, Rewritten, r.Reason) assert.True(t, len(r.IPList) == 1) - assert.True(t, r.IPList[0].Equal(net.ParseIP("1.2.3.4"))) + assert.True(t, r.IPList[0].Equal(net.IP{1, 2, 3, 4})) // wildcard + CNAME d.Rewrites = []RewriteEntry{ @@ -74,7 +74,7 @@ func TestRewrites(t *testing.T) { r = d.processRewrites("www.host.com", dns.TypeA) assert.Equal(t, Rewritten, r.Reason) assert.Equal(t, "host.com", r.CanonName) - assert.True(t, r.IPList[0].Equal(net.ParseIP("1.2.3.4"))) + assert.True(t, r.IPList[0].Equal(net.IP{1, 2, 3, 4})) // 2 CNAMEs d.Rewrites = []RewriteEntry{ @@ -87,7 +87,7 @@ func TestRewrites(t *testing.T) { assert.Equal(t, Rewritten, r.Reason) assert.Equal(t, "host.com", r.CanonName) assert.True(t, len(r.IPList) == 1) - assert.True(t, r.IPList[0].Equal(net.ParseIP("1.2.3.4"))) + assert.True(t, r.IPList[0].Equal(net.IP{1, 2, 3, 4})) // 2 CNAMEs + wildcard d.Rewrites = []RewriteEntry{ @@ -100,7 +100,7 @@ func TestRewrites(t *testing.T) { assert.Equal(t, Rewritten, r.Reason) assert.Equal(t, "x.somehost.com", r.CanonName) assert.True(t, len(r.IPList) == 1) - assert.True(t, r.IPList[0].Equal(net.ParseIP("1.2.3.4"))) + assert.True(t, r.IPList[0].Equal(net.IP{1, 2, 3, 4})) } func TestRewritesLevels(t *testing.T) { diff --git a/internal/dnsfilter/safebrowsing.go b/internal/dnsfilter/safebrowsing.go index f5aaca9f379..8f4c8f306ac 100644 --- a/internal/dnsfilter/safebrowsing.go +++ b/internal/dnsfilter/safebrowsing.go @@ -37,8 +37,8 @@ func (d *DNSFilter) initSecurityServices() error { opts := upstream.Options{ Timeout: dnsTimeout, ServerIPAddrs: []net.IP{ - net.ParseIP("94.140.14.15"), - net.ParseIP("94.140.15.16"), + {94, 140, 14, 15}, + {94, 140, 15, 16}, net.ParseIP("2a10:50c0::bad1:ff"), net.ParseIP("2a10:50c0::bad2:ff"), }, diff --git a/internal/dnsforward/dns.go b/internal/dnsforward/dns.go index 2861ef1aff9..10a965e25ea 100644 --- a/internal/dnsforward/dns.go +++ b/internal/dnsforward/dns.go @@ -287,7 +287,7 @@ func processUpstream(ctx *dnsContext) int { } if d.Addr != nil && s.conf.GetCustomUpstreamByClient != nil { - clientIP := GetIPString(d.Addr) + clientIP := IPStringFromAddr(d.Addr) upstreamsConf := s.conf.GetCustomUpstreamByClient(clientIP) if upstreamsConf != nil { log.Debug("Using custom upstreams for %s", clientIP) diff --git a/internal/dnsforward/dnsforward_test.go b/internal/dnsforward/dnsforward_test.go index 1329b2b8fc7..f9b6b9c3875 100644 --- a/internal/dnsforward/dnsforward_test.go +++ b/internal/dnsforward/dnsforward_test.go @@ -219,7 +219,7 @@ func TestSafeSearch(t *testing.T) { ip := ips[0] for _, i := range ips { - if i.To4() != nil { + if i != nil { ip = i break } @@ -286,7 +286,7 @@ func TestBlockedRequest(t *testing.T) { t.Fatalf("Couldn't talk to server %s: %s", addr, err) } assert.Equal(t, dns.RcodeSuccess, reply.Rcode) - assert.True(t, reply.Answer[0].(*dns.A).A.Equal(net.ParseIP("0.0.0.0"))) + assert.True(t, reply.Answer[0].(*dns.A).A.Equal(net.IP{0, 0, 0, 0})) err = s.Stop() if err != nil { @@ -300,7 +300,7 @@ func TestServerCustomClientUpstream(t *testing.T) { uc := &proxy.UpstreamConfig{} u := &testUpstream{} u.ipv4 = map[string][]net.IP{} - u.ipv4["host."] = []net.IP{net.ParseIP("192.168.0.1")} + u.ipv4["host."] = []net.IP{{192, 168, 0, 1}} uc.Upstreams = append(uc.Upstreams, u) return uc } @@ -449,7 +449,7 @@ func TestBlockCNAME(t *testing.T) { reply, err := dns.Exchange(req, addr.String()) assert.Nil(t, err, nil) assert.Equal(t, dns.RcodeSuccess, reply.Rcode) - assert.True(t, reply.Answer[0].(*dns.A).A.Equal(net.ParseIP("0.0.0.0"))) + assert.True(t, reply.Answer[0].(*dns.A).A.Equal(net.IP{0, 0, 0, 0})) // 'whitelist.example.org' has a canonical name 'null.example.org' which is blocked by filters // but 'whitelist.example.org' is in a whitelist: @@ -465,7 +465,7 @@ func TestBlockCNAME(t *testing.T) { reply, err = dns.Exchange(req, addr.String()) assert.Nil(t, err) assert.Equal(t, dns.RcodeSuccess, reply.Rcode) - assert.True(t, reply.Answer[0].(*dns.A).A.Equal(net.ParseIP("0.0.0.0"))) + assert.True(t, reply.Answer[0].(*dns.A).A.Equal(net.IP{0, 0, 0, 0})) _ = s.Stop() } @@ -553,7 +553,7 @@ func TestBlockedCustomIP(t *testing.T) { err := s.Prepare(&conf) assert.True(t, err != nil) // invalid BlockingIPv4 - conf.BlockingIPv4 = net.ParseIP("0.0.0.1") + conf.BlockingIPv4 = net.IP{0, 0, 0, 1} conf.BlockingIPv6 = net.ParseIP("::1") err = s.Prepare(&conf) assert.Nil(t, err) @@ -1011,14 +1011,14 @@ func TestValidateUpstreamsSet(t *testing.T) { assert.NotNil(t, err, "there is an invalid upstream in set, but it pass through validation") } -func TestGetIPString(t *testing.T) { +func TestIPStringFromAddr(t *testing.T) { addr := net.UDPAddr{} addr.IP = net.ParseIP("1:2:3::4") addr.Port = 12345 addr.Zone = "eth0" - assert.Equal(t, GetIPString(&addr), net.ParseIP("1:2:3::4").String()) + assert.Equal(t, IPStringFromAddr(&addr), net.ParseIP("1:2:3::4").String()) - assert.Equal(t, GetIPString(nil), "") + assert.Equal(t, IPStringFromAddr(nil), "") } func TestMatchDNSName(t *testing.T) { @@ -1038,7 +1038,7 @@ type testDHCP struct { func (d *testDHCP) Leases(flags int) []dhcpd.Lease { l := dhcpd.Lease{} - l.IP = net.ParseIP("127.0.0.1").To4() + l.IP = net.IP{127, 0, 0, 1} l.HWAddr, _ = net.ParseMAC("aa:aa:aa:aa:aa:aa") l.Hostname = "localhost" return []dhcpd.Lease{l} diff --git a/internal/dnsforward/filter.go b/internal/dnsforward/filter.go index 2d147af785e..77ae30a94d5 100644 --- a/internal/dnsforward/filter.go +++ b/internal/dnsforward/filter.go @@ -12,7 +12,7 @@ import ( ) func (s *Server) beforeRequestHandler(_ *proxy.Proxy, d *proxy.DNSContext) (bool, error) { - ip := GetIPString(d.Addr) + ip := IPStringFromAddr(d.Addr) disallowed, _ := s.access.IsBlockedIP(ip) if disallowed { log.Tracef("Client IP %s is blocked by settings", ip) @@ -36,7 +36,7 @@ func (s *Server) getClientRequestFilteringSettings(d *proxy.DNSContext) *dnsfilt setts := s.dnsFilter.GetConfig() setts.FilteringEnabled = true if s.conf.FilterHandler != nil { - clientAddr := GetIPString(d.Addr) + clientAddr := IPStringFromAddr(d.Addr) s.conf.FilterHandler(clientAddr, &setts) } return &setts diff --git a/internal/dnsforward/http.go b/internal/dnsforward/http.go index d5d61729750..1f5780a61cf 100644 --- a/internal/dnsforward/http.go +++ b/internal/dnsforward/http.go @@ -100,7 +100,7 @@ func (req *dnsConfig) checkBlockingMode() bool { bm := *req.BlockingMode if bm == "custom_ip" { - if req.BlockingIPv4 == nil || req.BlockingIPv4.To4() == nil { + if req.BlockingIPv4.To4() == nil { return false } diff --git a/internal/dnsforward/stats.go b/internal/dnsforward/stats.go index c447be05c0b..822df6a0a7d 100644 --- a/internal/dnsforward/stats.go +++ b/internal/dnsforward/stats.go @@ -36,7 +36,7 @@ func processQueryLogsAndStats(ctx *dnsContext) int { OrigAnswer: ctx.origResp, Result: ctx.result, Elapsed: elapsed, - ClientIP: getIP(d.Addr), + ClientIP: ipFromAddr(d.Addr), } switch d.Proto { diff --git a/internal/dnsforward/util.go b/internal/dnsforward/util.go index a8e3da4c003..3a8c1cb3027 100644 --- a/internal/dnsforward/util.go +++ b/internal/dnsforward/util.go @@ -8,16 +8,25 @@ import ( "github.com/AdguardTeam/golibs/utils" ) -// GetIPString extracts IP address from net.Addr. -// Note: we can't use net.SplitHostPort(a.String()) because of IPv6 zone: -// https://github.com/AdguardTeam/AdGuardHome/internal/issues/1261 -func GetIPString(addr net.Addr) string { +// ipFromAddr gets IP address from addr. +func ipFromAddr(addr net.Addr) (ip net.IP) { switch addr := addr.(type) { case *net.UDPAddr: - return addr.IP.String() + return addr.IP case *net.TCPAddr: - return addr.IP.String() + return addr.IP + } + return nil +} + +// IPStringFromAddr extracts IP address from net.Addr. +// Note: we can't use net.SplitHostPort(a.String()) because of IPv6 zone: +// https://github.com/AdguardTeam/AdGuardHome/internal/issues/1261 +func IPStringFromAddr(addr net.Addr) (ipstr string) { + if ip := ipFromAddr(addr); ip != nil { + return ip.String() } + return "" } @@ -27,17 +36,6 @@ func stringArrayDup(a []string) []string { return a2 } -// Get IP address from net.Addr -func getIP(addr net.Addr) net.IP { - switch addr := addr.(type) { - case *net.UDPAddr: - return addr.IP - case *net.TCPAddr: - return addr.IP - } - return nil -} - // Find value in a sorted array func findSorted(ar []string, val string) int { i := sort.SearchStrings(ar, val) diff --git a/internal/home/dns.go b/internal/home/dns.go index 1090d9be255..a988062954f 100644 --- a/internal/home/dns.go +++ b/internal/home/dns.go @@ -98,7 +98,7 @@ func isRunning() bool { } func onDNSRequest(d *proxy.DNSContext) { - ip := dnsforward.GetIPString(d.Addr) + ip := dnsforward.IPStringFromAddr(d.Addr) if ip == "" { // This would be quite weird if we get here return diff --git a/internal/stats/stats_test.go b/internal/stats/stats_test.go index 3a4bed66b7f..5c022faee18 100644 --- a/internal/stats/stats_test.go +++ b/internal/stats/stats_test.go @@ -39,13 +39,13 @@ func TestStats(t *testing.T) { e := Entry{} e.Domain = "domain" - e.Client = net.ParseIP("127.0.0.1") + e.Client = net.IP{127, 0, 0, 1} e.Result = RFiltered e.Time = 123456 s.Update(e) e.Domain = "domain" - e.Client = net.ParseIP("127.0.0.1") + e.Client = net.IP{127, 0, 0, 1} e.Result = RNotFiltered e.Time = 123456 s.Update(e) @@ -111,7 +111,7 @@ func TestLargeNumbers(t *testing.T) { } for i := 0; i != n; i++ { e.Domain = fmt.Sprintf("domain%d", i) - e.Client = net.ParseIP("127.0.0.1") + e.Client = net.IP{127, 0, 0, 1} e.Client[2] = byte((i & 0xff00) >> 8) e.Client[3] = byte(i & 0xff) e.Result = RNotFiltered