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 saveinterval: 5
aliases_enable: false aliases_enable: false
aliases_path: webroot/aliases.json 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) config = models.ConfigReadFile(configFile)
collectInterval := time.Second * time.Duration(config.Responedd.CollectInterval) collectInterval := time.Second * time.Duration(config.Responedd.CollectInterval)
saveInterval := time.Second * time.Duration(config.Nodes.SaveInterval)
if config.Nodes.Enable { if config.Nodes.Enable {
go nodes.Saver(config.Nodes.NodesPath, config.Nodes.GraphsPath, saveInterval) go nodes.Saver(config)
} }
if config.Nodes.AliasesEnable { if config.Nodes.AliasesEnable {
// FIXME what does this do? // FIXME what does this do?

View File

@ -30,6 +30,7 @@ type Config struct {
AliasesEnable bool `yaml:"aliases_enable"` AliasesEnable bool `yaml:"aliases_enable"`
AliasesPath string `yaml:"aliases_path"` AliasesPath string `yaml:"aliases_path"`
SaveInterval int `yaml:"saveinterval"` SaveInterval int `yaml:"saveinterval"`
VpnAddresses []string `yaml:"vpn_addresses"`
} `yaml:"nodes"` } `yaml:"nodes"`
} }

View File

@ -23,12 +23,19 @@ type GraphLink struct {
type GraphBuilder struct { type GraphBuilder struct {
macToID map[string]string // mapping from MAC address to node id macToID map[string]string // mapping from MAC address to node id
links map[string]*GraphLink // mapping from $idA-$idB to existing link 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{ builder := &GraphBuilder{
macToID: make(map[string]string), macToID: make(map[string]string),
links: make(map[string]*GraphLink), 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) builder.readNodes(nodes.List)
@ -44,6 +51,11 @@ func (builder *GraphBuilder) readNodes(nodes map[string]*Node) {
if neighbours := node.Neighbours; neighbours != nil { if neighbours := node.Neighbours; neighbours != nil {
for sourceAddress, _ := range neighbours.Batadv { for sourceAddress, _ := range neighbours.Batadv {
builder.macToID[sourceAddress] = sourceId 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 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) { func (builder *GraphBuilder) addLink(targetId string, sourceId string, linkTq int) {
// Order IDs to get generate the key // Order IDs to get generate the key
var key string var key string
@ -91,6 +112,7 @@ func (builder *GraphBuilder) addLink(targetId string, sourceId string, linkTq in
builder.links[key] = &GraphLink{ builder.links[key] = &GraphLink{
Source: sourceId, Source: sourceId,
Target: targetId, Target: targetId,
VPN: builder.isVPN(sourceId, targetId),
TQ: tq, TQ: tq,
} }
} else { } else {

View File

@ -62,15 +62,19 @@ func (nodes *Nodes) Get(nodeID string) *Node {
} }
// Saves the cached DB to json file periodically // Saves the cached DB to json file periodically
func (nodes *Nodes) Saver(nodesPath string, graphPath string, saveInterval time.Duration) { func (nodes *Nodes) Saver(config *Config) {
c := time.Tick(saveInterval) c := time.Tick(time.Second * time.Duration(config.Nodes.SaveInterval))
for range c { for range c {
log.Println("saving", len(nodes.List), "nodes") log.Println("saving", len(nodes.List), "nodes")
nodes.Timestamp = time.Now() nodes.Timestamp = time.Now()
nodes.Lock() nodes.Lock()
save(nodes, nodesPath) if path := config.Nodes.NodesPath; path != "" {
save(nodes.BuildGraph(), graphPath) save(nodes, path)
}
if path := config.Nodes.GraphsPath; path != "" {
save(nodes.BuildGraph(config.Nodes.VpnAddresses), path)
}
nodes.Unlock() nodes.Unlock()
} }
} }