-
Notifications
You must be signed in to change notification settings - Fork 0
/
oui_helper.go
44 lines (34 loc) · 848 Bytes
/
oui_helper.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
package main
import (
"strings"
"sync"
"github.com/spf13/viper"
"github.com/alufers/wrtman/ouidb"
)
// OuiHelper helps with Mac address lookups, by caching them
// TODO: add cache limit
type OuiHelper struct {
db *ouidb.OuiDb
cacheMutex sync.Mutex
cache map[string]string
}
func NewOuiHelper() *OuiHelper {
return &OuiHelper{
db: ouidb.New(viper.GetString("extra.oui_db")),
cache: map[string]string{},
}
}
func (oh *OuiHelper) LookupVendor(mac string) (string, error) {
oh.cacheMutex.Lock()
defer oh.cacheMutex.Unlock()
if cached, ok := oh.cache[mac]; ok {
return cached, nil
}
result, err := oh.db.VendorLookup(mac)
if err != nil {
return "", err
}
result.Organization = strings.TrimPrefix(result.Organization, "Beijing ")
oh.cache[mac] = result.Organization
return result.Organization, nil
}