mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-15 18:08:30 -07:00
-(home): fix searching clients by mac address
This commit is contained in:
parent
9b93d43ac6
commit
6bf512f96f
@ -709,9 +709,17 @@ func (s *Server) FindMACbyIP(ip net.IP) net.HardwareAddr {
|
||||
s.leasesLock.RLock()
|
||||
defer s.leasesLock.RUnlock()
|
||||
|
||||
ip4 := ip.To4()
|
||||
if ip4 == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, l := range s.leases {
|
||||
if l.Expiry.Unix() > now && l.IP.Equal(ip) {
|
||||
return l.HWAddr
|
||||
if l.IP.Equal(ip4) {
|
||||
unix := l.Expiry.Unix()
|
||||
if unix > now || unix == leaseExpireStatic {
|
||||
return l.HWAddr
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
@ -23,6 +23,7 @@ func check(t *testing.T, result bool, msg string) {
|
||||
func TestDHCP(t *testing.T) {
|
||||
var s = Server{}
|
||||
s.conf.DBFilePath = dbFilename
|
||||
defer func() { _ = os.Remove(dbFilename) }()
|
||||
var p, p2 dhcp4.Packet
|
||||
var hw net.HardwareAddr
|
||||
var lease *Lease
|
||||
@ -185,7 +186,7 @@ func TestDB(t *testing.T) {
|
||||
lease, _ = s.reserveLease(p)
|
||||
lease.Expiry = time.Unix(4000000002, 0)
|
||||
|
||||
os.Remove("leases.db")
|
||||
_ = os.Remove("leases.db")
|
||||
s.dbStore()
|
||||
s.reset()
|
||||
|
||||
@ -198,7 +199,7 @@ func TestDB(t *testing.T) {
|
||||
check(t, bytes.Equal(s.leases[1].IP, []byte{1, 1, 1, 2}), "leases[1].IP")
|
||||
check(t, s.leases[1].Expiry.Unix() == 4000000002, "leases[1].Expiry")
|
||||
|
||||
os.Remove("leases.db")
|
||||
_ = os.Remove("leases.db")
|
||||
}
|
||||
|
||||
func TestIsValidSubnetMask(t *testing.T) {
|
||||
|
@ -1,7 +1,12 @@
|
||||
package home
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/AdguardTeam/AdGuardHome/dhcpd"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@ -172,3 +177,48 @@ func TestClientsWhois(t *testing.T) {
|
||||
assert.True(t, clients.idIndex["1.1.1.2"].WhoisInfo[0][1] == "orgname-val")
|
||||
_ = clients.Del("client1")
|
||||
}
|
||||
|
||||
func TestClientsAddExistingHost(t *testing.T) {
|
||||
var c Client
|
||||
clients := clientsContainer{}
|
||||
clients.testing = true
|
||||
clients.Init(nil, nil)
|
||||
|
||||
// some test variables
|
||||
mac, _ := net.ParseMAC("aa:aa:aa:aa:aa:aa")
|
||||
testIP := "1.2.3.4"
|
||||
|
||||
// add a client
|
||||
c = Client{
|
||||
IDs: []string{"1.1.1.1", "1:2:3::4", "aa:aa:aa:aa:aa:aa"},
|
||||
Name: "client1",
|
||||
}
|
||||
ok, err := clients.Add(c)
|
||||
assert.True(t, ok)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// try adding a duplicate by IP
|
||||
ok, err = clients.AddHost("1.1.1.1", "test", ClientSourceRDNS)
|
||||
assert.False(t, ok)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// now some more complicated stuff
|
||||
// first, init a DHCP server with a single static lease
|
||||
config := dhcpd.ServerConfig{
|
||||
DBFilePath: "leases.db",
|
||||
}
|
||||
defer func() { _ = os.Remove("leases.db") }()
|
||||
clients.dhcpServer = dhcpd.Create(config)
|
||||
err = clients.dhcpServer.AddStaticLease(dhcpd.Lease{
|
||||
HWAddr: mac,
|
||||
IP: net.ParseIP(testIP).To4(),
|
||||
Hostname: "testhost",
|
||||
Expiry: time.Now().Add(time.Hour),
|
||||
})
|
||||
assert.Nil(t, err)
|
||||
|
||||
// try adding a duplicate IP which for a Mac-based client
|
||||
ok, err = clients.AddHost(testIP, "test", ClientSourceRDNS)
|
||||
assert.False(t, ok)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user