Mark VPN links

This commit is contained in:
Julian Kornberger 2016-03-07 12:05:53 +01:00
parent 93fafcb8e2
commit a575c7132d
5 changed files with 44 additions and 15 deletions

View File

@ -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

View File

@ -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?

View File

@ -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"`
}

View File

@ -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 {

View File

@ -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()
}
}