yanic/respond/daemon/config.go

63 lines
1.7 KiB
Go
Raw Normal View History

2019-04-10 13:18:05 +02:00
package respondd
import (
"io/ioutil"
"strings"
2019-06-16 19:41:44 +02:00
"sync"
2019-04-10 13:18:05 +02:00
"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/lib/duration"
2019-05-18 23:33:13 +02:00
babelState "github.com/Vivena/babelweb2/state"
2019-04-10 13:18:05 +02:00
)
func trim(s string) string {
return strings.TrimSpace(strings.Trim(s, "\n"))
}
type Daemon struct {
DataInterval duration.Duration `toml:"data_interval"`
Listen []struct {
Address string `toml:"address"`
Interface string `toml:"interface"`
Port int `toml:"port"`
} `toml:"listen"`
2019-04-10 21:57:31 +02:00
2019-05-18 23:33:13 +02:00
Batman []string `toml:"batman"`
Babel string `toml:"babel"`
babelData *babelState.BabelState `toml:"-"`
2019-04-10 13:18:05 +02:00
2019-06-16 19:41:44 +02:00
dataMX sync.Mutex
2019-04-10 13:18:05 +02:00
dataByInterface map[string]*data.ResponseData
Answer *AnswerConfig `toml:"defaults"`
AnswerByZones map[string]*AnswerConfig `toml:"zones"`
}
type AnswerConfig struct {
2019-04-11 12:59:55 +02:00
NodeID string `toml:"node_id"`
Hostname string `toml:"hostname"`
SiteCode string `toml:"site_code"`
DomainCode string `toml:"domain_code"`
Location *data.Location `json:"location,omitempty"`
VPN bool `toml:"vpn"`
InterfaceMAC string `toml:"interface_mac"`
InterfacesTraffic []string `toml:"interfaces_traffic"`
InterfacesAddress []string `toml:"interfaces_address"`
2019-04-10 13:18:05 +02:00
}
func (d *Daemon) getAnswer(iface string) (*AnswerConfig, string) {
config := d.Answer
2019-05-13 21:12:15 +02:00
if v, ok := d.AnswerByZones[iface]; iface != "" && ok {
2019-04-10 13:18:05 +02:00
config = v
}
nodeID := config.NodeID
if nodeID == "" {
if v, err := ioutil.ReadFile("/etc/machine-id"); err == nil {
nodeID = trim(string(v))[:12]
}
}
return config, nodeID
}