-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendor.go
86 lines (75 loc) · 1.95 KB
/
vendor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package vendor
import (
"bufio"
// "fmt"
"github.com/c0ze/golang-utils"
"log"
"os"
"regexp"
"strings"
)
// readLines reads a whole file into memory
// and returns a slice of its lines.
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
var randomMac = regexp.MustCompile(`^([A-F0-9][2,6,A,E][A-F0-9]*)$`)
// VendorMap holds the Mac address -> Vendor name mapping
var VendorMap map[string]string
// Inits Wifi vendors.
// You must call this function once before performing any look ups.
func Init() {
VendorMap = make(map[string]string)
var lines []string
var err error
lines, err = readLines("/etc/fup/oui.txt")
if err != nil {
lines, err = readLines("oui.txt")
if err != nil {
log.Println("readLines: %s", err)
return
}
}
for _, line := range lines {
if len(line) > 1 {
if string(line[0]) != " " && string(line[0]) != "" && string(line[0]) != "\t" && string(line[2]) != "-" {
words := strings.Fields(line)
if len(words) > 3 {
// VendorMap[words[0]] = fmt.Sprintf("%v\n", strings.Join(words[3:], " "))
VendorMap[words[0]] = strings.Join(words[3:], " ")
}
}
}
}
}
// Lookup function returns the vendor string matched from oui.txt.
// If the mac address is in random space, it will return Random.
// If the vendor is not in oui, it returns Unknown (you may want to update oui in this case).
// If the mac address is invalid, it returns Malformed.
func Lookup(mac string) string {
vendor := ""
sanitizedMac := utils.StripColon(mac)
if len(sanitizedMac) > 5 {
vendor = VendorMap[sanitizedMac[0:6]]
if vendor == "" {
if randomMac.MatchString(utils.StripColon(mac)) {
vendor = "Random"
} else {
vendor = "Unknown"
}
}
} else {
vendor = "Malformed"
}
return vendor
}