initial commit [141.14.140.180,mike]
This commit is contained in:
commit
e34ba75b7b
33 files changed
+6267
No files matched your search
@@ -0,0 +1,102 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/rand/v2"
|
||||
"net"
|
||||
"net/netip"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/dns/dnsmessage"
|
||||
)
|
||||
|
||||
// appleModel matches the model identifiers Apple devices announce, such as
|
||||
// "MacBookAir10,1", "Mac16,10", "iPhone15,3" or "AudioAccessory5,1" (HomePod).
|
||||
var appleModel = regexp.MustCompile(`^(Mac|MacBook|MacBookAir|MacBookPro|Macmini|MacPro|iMac|iMacPro|iPhone|iPad|iPod|AppleTV|AudioAccessory|Watch|RealityDevice|AirPort)\d+,\d+$`)
|
||||
|
||||
// PrivateMAC reports whether mac is locally administered, as the randomized
|
||||
// addresses phones and laptops use per network are. Such addresses carry no
|
||||
// vendor.
|
||||
func PrivateMAC(mac net.HardwareAddr) bool { return len(mac) > 0 && mac[0]&0x02 != 0 }
|
||||
|
||||
// Maker returns who made the device: the vendor registered for its MAC
|
||||
// address or, for private addresses, the maker implied by the announced model.
|
||||
func (h *Host) Maker() string {
|
||||
if h.Model != "" && (h.Vendor == "" || PrivateMAC(h.MAC)) && appleModel.MatchString(h.Model) {
|
||||
return "Apple"
|
||||
}
|
||||
return h.Vendor
|
||||
}
|
||||
|
||||
// VendorLabel is the maker followed by the model, e.g. "Apple Mac16,10".
|
||||
func (h *Host) VendorLabel() string {
|
||||
maker := h.Maker()
|
||||
switch {
|
||||
case h.Model == "":
|
||||
return maker
|
||||
case maker == "" || PrivateMAC(h.MAC) && maker == h.Vendor:
|
||||
return h.Model
|
||||
}
|
||||
return maker + " " + h.Model
|
||||
}
|
||||
|
||||
// deviceModels asks hosts for their "<name>._device-info._tcp.local." TXT
|
||||
// record, in which Apple devices announce their model identifier. Each host
|
||||
// is asked under all names it may use: its host name and its service names.
|
||||
func deviceModels(ctx context.Context, names map[netip.Addr][]string, timeout time.Duration) map[netip.Addr]string {
|
||||
conn, err := net.ListenUDP("udp4", nil)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer conn.Close()
|
||||
context.AfterFunc(ctx, func() { conn.Close() })
|
||||
|
||||
asked := map[string]netip.Addr{}
|
||||
for ip, ns := range names {
|
||||
for _, n := range ns {
|
||||
fq := n + "._device-info._tcp.local."
|
||||
q, err := question(fq, dnsmessage.TypeTXT)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if b, err := buildQuery(uint16(rand.N(0xffff)+1), q); err == nil {
|
||||
asked[strings.ToLower(fq)] = ip
|
||||
conn.WriteToUDPAddrPort(b, netip.AddrPortFrom(ip, 5353))
|
||||
}
|
||||
}
|
||||
}
|
||||
res := make(map[netip.Addr]string)
|
||||
conn.SetReadDeadline(time.Now().Add(timeout))
|
||||
buf := make([]byte, 9000)
|
||||
for len(res) < len(names) {
|
||||
n, _, err := conn.ReadFromUDPAddrPort(buf)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
for _, rr := range parseRecords(buf[:n]) {
|
||||
txt, ok := rr.Body.(*dnsmessage.TXTResource)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
ip, ok := asked[strings.ToLower(rr.Header.Name.String())]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if model := txtValue(txt.TXT, "model"); model != "" {
|
||||
res[ip] = model
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func txtValue(txt []string, key string) string {
|
||||
for _, kv := range txt {
|
||||
if v, ok := strings.CutPrefix(kv, key+"="); ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in new issue
Block a user