forked from Gandem/bonjour-reflector
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathconfig.go
102 lines (87 loc) · 2.49 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
package main
import (
"errors"
"net"
"os"
"strconv"
"strings"
"github.com/pelletier/go-toml"
"github.com/sirupsen/logrus"
)
type macAddress string
type config struct {
NetInterface string `toml:"net_interface"`
Devices map[macAddress]multicastDevice `toml:"devices"`
VlanIPSource map[vlanID]vlanIpSource `toml:"vlan"`
}
type multicastDevice struct {
Description string `toml:"description,omitempty"`
OriginPool uint16 `toml:"origin_pool"`
SharedPools []uint16 `toml:"shared_pools"`
}
type vlanID string
type vlanIpSource struct {
IpSource net.IP `toml:"ip_source"`
}
func findConfigFile() (*string, error) {
// Check if the config file is specified in the environment
configFile := os.Getenv("CONFIG")
if configFile != "" {
return &configFile, nil
}
// Check if the config file is in the current directory
checkFile := "config.toml"
if _, err := os.Stat(checkFile); err == nil {
return &checkFile, nil
}
// Check if the config file is in the config directory
checkFile = "config/config.toml"
if _, err := os.Stat(checkFile); err == nil {
return &checkFile, nil
}
return nil, errors.New("no config file found")
}
func readConfig(path string) (cfg config, err error) {
content, err := os.ReadFile(path)
if err != nil {
return config{}, err
}
err = toml.Unmarshal(content, &cfg)
return cfg, err
}
func mapByPool(devices map[macAddress]multicastDevice) map[uint16]([]uint16) {
seen := make(map[uint16]map[uint16]bool)
poolsMap := make(map[uint16]([]uint16))
for _, device := range devices {
for _, pool := range device.SharedPools {
if _, ok := seen[pool]; !ok {
seen[pool] = make(map[uint16]bool)
}
if _, ok := seen[pool][device.OriginPool]; !ok {
seen[pool][device.OriginPool] = true
poolsMap[pool] = append(poolsMap[pool], device.OriginPool)
}
}
}
return poolsMap
}
func mapIpSourceByVlan(vlanipsource map[vlanID]vlanIpSource) map[uint16](net.IP) {
vlanMap := make(map[uint16](net.IP))
for vlan, value := range vlanipsource {
vlanID, err := strconv.Atoi(string(vlan))
if err != nil {
logrus.Errorf("cannot decode %s to vlanID\n", vlan)
continue
}
vlanMap[uint16(vlanID)] = value.IpSource
}
return vlanMap
}
func mapLowerCaseMac(devices map[macAddress]multicastDevice) map[macAddress]multicastDevice {
newDevices := make(map[macAddress]multicastDevice)
for mac, device := range devices {
lowerCaseMac := strings.ToLower(string(mac))
newDevices[macAddress(lowerCaseMac)] = device
}
return newDevices
}