fix: refactored routing logic (fixes #701)

This commit is contained in:
Simone Margaritelli 2021-04-10 21:55:00 +02:00
parent 88a83192ef
commit 43a93fd866
11 changed files with 202 additions and 80 deletions

39
routing/tables.go Normal file
View file

@ -0,0 +1,39 @@
package routing
import "sync"
var (
lock = sync.RWMutex{}
table = make([]Route, 0)
)
func Table() []Route {
lock.RLock()
defer lock.RUnlock()
return table
}
func Update() ([]Route, error) {
lock.Lock()
defer lock.Unlock()
return update()
}
func Gateway(ip RouteType, device string) (string, error) {
Update()
lock.RLock()
defer lock.RUnlock()
for _, r := range table {
if r.Type == ip {
if device == "" || r.Device == device || r.Device == "" /* windows case */ {
if r.Default {
return r.Gateway, nil
}
}
}
}
return "", nil
}