diff --git a/config_example.yaml b/config_example.yaml index 0a540ec..a430d90 100644 --- a/config_example.yaml +++ b/config_example.yaml @@ -15,3 +15,6 @@ nodes: saveinterval: 5 aliases_enable: false aliases_path: webroot/aliases.json + vpn_addresses: + - da:25:d6:5c:97:6f + - da:62:f2:70:c8:8d diff --git a/main.go b/main.go index cfd0b4a..45769d1 100644 --- a/main.go +++ b/main.go @@ -32,10 +32,9 @@ func main() { config = models.ConfigReadFile(configFile) collectInterval := time.Second * time.Duration(config.Responedd.CollectInterval) - saveInterval := time.Second * time.Duration(config.Nodes.SaveInterval) if config.Nodes.Enable { - go nodes.Saver(config.Nodes.NodesPath, config.Nodes.GraphsPath, saveInterval) + go nodes.Saver(config) } if config.Nodes.AliasesEnable { // FIXME what does this do? diff --git a/models/config.go b/models/config.go index ff61593..e81ce96 100644 --- a/models/config.go +++ b/models/config.go @@ -24,12 +24,13 @@ type Config struct { WebsocketAliases bool `yaml:"websocketaliases"` } `yaml:"webserver"` Nodes struct { - Enable bool `yaml:"enable"` - NodesPath string `yaml:"nodes_path"` - GraphsPath string `yaml:"graphs_path"` - AliasesEnable bool `yaml:"aliases_enable"` - AliasesPath string `yaml:"aliases_path"` - SaveInterval int `yaml:"saveinterval"` + Enable bool `yaml:"enable"` + NodesPath string `yaml:"nodes_path"` + GraphsPath string `yaml:"graphs_path"` + AliasesEnable bool `yaml:"aliases_enable"` + AliasesPath string `yaml:"aliases_path"` + SaveInterval int `yaml:"saveinterval"` + VpnAddresses []string `yaml:"vpn_addresses"` } `yaml:"nodes"` } diff --git a/models/graph.go b/models/graph.go index 16d6f33..67b1fb4 100644 --- a/models/graph.go +++ b/models/graph.go @@ -21,14 +21,21 @@ type GraphLink struct { } type GraphBuilder struct { - macToID map[string]string // mapping from MAC address to node id - links map[string]*GraphLink // mapping from $idA-$idB to existing link + macToID map[string]string // mapping from MAC address to node id + links map[string]*GraphLink // mapping from $idA-$idB to existing link + vpn map[string]interface{} // IDs/addresses of VPN servers } -func (nodes *Nodes) BuildGraph() *Graph { +func (nodes *Nodes) BuildGraph(vpnAddresses []string) *Graph { builder := &GraphBuilder{ macToID: make(map[string]string), links: make(map[string]*GraphLink), + vpn: make(map[string]interface{}), + } + + // read VPN addresses into map + for _, address := range vpnAddresses { + builder.vpn[address] = nil } builder.readNodes(nodes.List) @@ -44,6 +51,11 @@ func (builder *GraphBuilder) readNodes(nodes map[string]*Node) { if neighbours := node.Neighbours; neighbours != nil { for sourceAddress, _ := range neighbours.Batadv { builder.macToID[sourceAddress] = sourceId + + // is VPN address? + if _, found := builder.vpn[sourceAddress]; found { + builder.vpn[sourceId] = nil + } } } } @@ -73,6 +85,15 @@ func (builder *GraphBuilder) Links() []*GraphLink { return links } +func (builder *GraphBuilder) isVPN(ids ...string) bool { + for _, id := range ids { + if _, found := builder.vpn[id]; found { + return true + } + } + return false +} + func (builder *GraphBuilder) addLink(targetId string, sourceId string, linkTq int) { // Order IDs to get generate the key var key string @@ -91,6 +112,7 @@ func (builder *GraphBuilder) addLink(targetId string, sourceId string, linkTq in builder.links[key] = &GraphLink{ Source: sourceId, Target: targetId, + VPN: builder.isVPN(sourceId, targetId), TQ: tq, } } else { diff --git a/models/nodes.go b/models/nodes.go index 8616913..f042cf8 100644 --- a/models/nodes.go +++ b/models/nodes.go @@ -62,15 +62,19 @@ func (nodes *Nodes) Get(nodeID string) *Node { } // Saves the cached DB to json file periodically -func (nodes *Nodes) Saver(nodesPath string, graphPath string, saveInterval time.Duration) { - c := time.Tick(saveInterval) +func (nodes *Nodes) Saver(config *Config) { + c := time.Tick(time.Second * time.Duration(config.Nodes.SaveInterval)) for range c { log.Println("saving", len(nodes.List), "nodes") nodes.Timestamp = time.Now() nodes.Lock() - save(nodes, nodesPath) - save(nodes.BuildGraph(), graphPath) + if path := config.Nodes.NodesPath; path != "" { + save(nodes, path) + } + if path := config.Nodes.GraphsPath; path != "" { + save(nodes.BuildGraph(config.Nodes.VpnAddresses), path) + } nodes.Unlock() } }