-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
117 lines (96 loc) · 2.03 KB
/
config.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"encoding/csv"
"os"
"path/filepath"
"github.com/fsnotify/fsnotify"
)
type peer struct {
ASN string
Label string
Company string
HWAddr string
IPv4 string
IPv6 string
RouteServerIPv4 bool
RouteServerIPv6 bool
LinkSpeed string
IRR string
Note string
}
var members map[string]*peer
var overrides map[string]*peer
func loadMembers(fileName string) error {
return loadFile(fileName, &members)
}
func loadOverrides(fileName string) error {
return loadFile(fileName, &overrides)
}
func loadFile(fileName string, target *map[string]*peer) error {
// Parse the current file.
entries, err := parseFile(fileName)
if err != nil {
return err
}
// Update the global data.
*target = entries
// Setup watcher.
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go func() {
for {
event, ok := <-watcher.Events
if !ok {
return
}
if !event.Has(fsnotify.Write) || event.Name != fileName {
continue
}
// Refresh the list.
entries, err := parseFile(fileName)
if err != nil {
continue
}
*target = entries
}
}()
err = watcher.Add(filepath.Dir(fileName))
if err != nil {
return err
}
return nil
}
func parseFile(fileName string) (map[string]*peer, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, err
}
defer f.Close()
reader := csv.NewReader(f)
records, err := reader.ReadAll()
if err != nil {
return nil, err
}
entries := map[string]*peer{}
for _, record := range records {
entry := &peer{
ASN: record[0],
Label: record[1],
Company: record[2],
HWAddr: record[3],
IPv4: record[4],
IPv6: record[5],
RouteServerIPv4: record[6] == "Yes",
RouteServerIPv6: record[7] == "Yes",
LinkSpeed: record[8],
IRR: record[9],
}
if len(record) > 10 {
entry.Note = record[10]
}
entries[entry.HWAddr] = entry
}
return entries, nil
}