slimming/config.go

65 lines
1.1 KiB
Go
Raw Normal View History

2022-08-25 07:31:51 +00:00
package main
import (
2022-08-26 01:52:49 +00:00
"encoding/binary"
2022-08-25 07:31:51 +00:00
"log"
2022-08-26 01:52:49 +00:00
"net"
2022-08-25 07:31:51 +00:00
"os"
2022-08-26 01:52:49 +00:00
"strings"
"sync"
2022-08-25 07:31:51 +00:00
"gopkg.in/yaml.v3"
)
type Node struct {
Real string `yaml:"real"`
Virt string `yaml:"virt"`
}
type Config struct {
Network struct {
Self Node `yaml:"self"`
Nodes []Node `yaml:"nodes"`
} `yaml:"network"`
2022-08-26 01:52:49 +00:00
IPv4Nodes map[uint32]string
2022-08-26 01:57:09 +00:00
lock sync.Mutex
2022-08-25 07:31:51 +00:00
}
var config = NewConfig()
func NewConfig() *Config {
2022-08-26 01:52:49 +00:00
cnf := &Config{IPv4Nodes: make(map[uint32]string)}
2022-08-25 07:31:51 +00:00
f, err := os.Open("config.yaml")
if err != nil {
log.Panic(err)
}
dec := yaml.NewDecoder(f)
err = dec.Decode(cnf)
if err != nil {
log.Panic(err)
}
2022-08-26 01:52:49 +00:00
for _, node := range cnf.Network.Nodes {
values := strings.Split(node.Virt, "/")
if len(values) != 2 {
log.Panic("virt must have '/'. eg 1.1.1.1/24")
}
inet, net, err := net.ParseCIDR(node.Virt)
if err != nil {
log.Panic(err)
}
cnf.IPv4Nodes[binary.LittleEndian.Uint32([]byte(inet))] = node.Real
log.Printf("real %v virt %v %v", node.Real, inet, net)
}
2022-08-25 07:31:51 +00:00
return cnf
}
2022-08-26 01:57:09 +00:00
func (cnf *Config) Lock(do func()) {
cnf.lock.Lock()
defer cnf.lock.Unlock()
do()
}