mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
internal/router: normalize ip address from dnsmasq lease file
dnsmasq may put an ip address with the interface index in lease file, causing bad data sent to the Control-D backend.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
@@ -80,9 +81,18 @@ func readClientInfoFile(name string) error {
|
|||||||
return lineread.File(name, func(line []byte) error {
|
return lineread.File(name, func(line []byte) error {
|
||||||
fields := bytes.Fields(line)
|
fields := bytes.Fields(line)
|
||||||
mac := string(fields[1])
|
mac := string(fields[1])
|
||||||
ip := string(fields[2])
|
ip := normalizeIP(string(fields[2]))
|
||||||
hostname := string(fields[3])
|
hostname := string(fields[3])
|
||||||
r.mac.Store(mac, &ctrld.ClientInfo{Mac: mac, IP: ip, Hostname: hostname})
|
r.mac.Store(mac, &ctrld.ClientInfo{Mac: mac, IP: ip, Hostname: hostname})
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normalizeIP(in string) string {
|
||||||
|
// dnsmasq may put ip with interface index in lease file, strip it here.
|
||||||
|
ip, _, found := strings.Cut(in, "%")
|
||||||
|
if found {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
return in
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package router
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_normalizeIP(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
in string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"v4", "127.0.0.1", "127.0.0.1"},
|
||||||
|
{"v4 with index", "127.0.0.1%lo", "127.0.0.1"},
|
||||||
|
{"v6", "fe80::1", "fe80::1"},
|
||||||
|
{"v6 with index", "fe80::1%22002", "fe80::1"},
|
||||||
|
}
|
||||||
|
for _, tc := range tests {
|
||||||
|
tc := tc
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
if got := normalizeIP(tc.in); got != tc.want {
|
||||||
|
t.Errorf("normalizeIP() = %v, want %v", got, tc.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user