Skip to content

Commit

Permalink
all: improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Jan 12, 2021
1 parent 3bf03a7 commit f629b15
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 85 deletions.
12 changes: 6 additions & 6 deletions internal/dhcpd/dhcpd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion internal/dhcpd/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
64 changes: 32 additions & 32 deletions internal/dhcpd/v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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",
Expand All @@ -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")
Expand Down Expand Up @@ -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}))
}
16 changes: 8 additions & 8 deletions internal/dnsfilter/rewrites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions internal/dnsfilter/safebrowsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions internal/dnsforward/dnsforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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:
Expand All @@ -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()
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions internal/dnsforward/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit f629b15

Please sign in to comment.