yanic/respond/daemon/config.go

56 lines
1.4 KiB
Go
Raw Normal View History

2019-04-10 13:18:05 +02:00
package respondd
import (
"io/ioutil"
"strings"
"github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/lib/duration"
)
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
Batman []string `toml:"batman"`
Babel string `toml:"babel"`
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"`
TrafficInterfaces []string `toml:"traffic_interfaces"`
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
}