EtherGuard-VPN/path/path.go

300 lines
7.6 KiB
Go
Raw Normal View History

2021-08-16 20:58:15 +02:00
package path
import (
"fmt"
"math"
2021-08-21 16:54:24 +02:00
"sync"
2021-08-16 20:58:15 +02:00
"time"
2021-08-20 19:32:50 +02:00
"github.com/KusakabeSi/EtherGuardVPN/config"
2021-08-16 20:58:15 +02:00
yaml "gopkg.in/yaml.v2"
)
2021-08-20 19:32:50 +02:00
const Infinity = float64(99999)
const (
Boardcast config.Vertex = math.MaxUint32 - iota // Normal boardcast, boardcast with route table
ControlMessage config.Vertex = math.MaxUint32 - iota // p2p mode: boardcast to every know keer and prevent dup/ super mode: send to supernode
PingMessage config.Vertex = math.MaxUint32 - iota // boardsact to every know peer but don't transit
SuperNodeMessage config.Vertex = math.MaxUint32 - iota
Special_NodeID config.Vertex = SuperNodeMessage
2021-08-16 20:58:15 +02:00
)
2021-08-20 19:32:50 +02:00
func (g *IG) GetCurrentTime() time.Time {
2021-08-21 16:54:24 +02:00
return time.Now().Round(0)
2021-08-20 19:32:50 +02:00
}
2021-08-16 20:58:15 +02:00
// A Graph is the interface implemented by graphs that
// this algorithm can run on.
type Graph interface {
2021-08-20 19:32:50 +02:00
Vertices() map[config.Vertex]bool
Neighbors(v config.Vertex) []config.Vertex
Weight(u, v config.Vertex) float64
2021-08-16 20:58:15 +02:00
}
type Latency struct {
ping float64
time time.Time
}
type Fullroute struct {
2021-08-20 19:32:50 +02:00
Dist config.DistTable `json:"total distance"`
Next config.NextHopTable `json:"next hop"`
2021-08-16 20:58:15 +02:00
}
// IG is a graph of integers that satisfies the Graph interface.
type IG struct {
2021-08-20 19:32:50 +02:00
Vert map[config.Vertex]bool
2021-08-21 16:54:24 +02:00
edges map[config.Vertex]map[config.Vertex]Latency
2021-08-23 18:39:04 +02:00
edgelock *sync.RWMutex
2021-08-20 19:32:50 +02:00
JitterTolerance float64
JitterToleranceMultiplier float64
NodeReportTimeout time.Duration
SuperNodeInfoTimeout time.Duration
RecalculateCoolDown time.Duration
RecalculateTime time.Time
dlTable config.DistTable
NhTable config.NextHopTable
NhTableHash [32]byte
nhTableExpire time.Time
IsSuperMode bool
}
func S2TD(secs float64) time.Duration {
return time.Duration(secs * float64(time.Second))
2021-08-16 20:58:15 +02:00
}
2021-08-21 16:54:24 +02:00
func NewGraph(num_node int, IsSuperMode bool, theconfig config.GraphRecalculateSetting) *IG {
2021-08-20 19:32:50 +02:00
g := IG{
2021-08-23 18:39:04 +02:00
edgelock: &sync.RWMutex{},
2021-08-20 19:32:50 +02:00
JitterTolerance: theconfig.JitterTolerance,
JitterToleranceMultiplier: theconfig.JitterToleranceMultiplier,
NodeReportTimeout: S2TD(theconfig.NodeReportTimeout),
RecalculateCoolDown: S2TD(theconfig.RecalculateCoolDown),
}
g.Vert = make(map[config.Vertex]bool, num_node)
2021-08-21 16:54:24 +02:00
g.edges = make(map[config.Vertex]map[config.Vertex]Latency, num_node)
2021-08-20 19:32:50 +02:00
g.IsSuperMode = IsSuperMode
2021-08-21 16:54:24 +02:00
return &g
2021-08-16 20:58:15 +02:00
}
2021-08-20 19:32:50 +02:00
func (g *IG) GetWeightType(x float64) float64 {
x = math.Abs(x)
y := x
if g.JitterTolerance > 1 && g.JitterToleranceMultiplier > 0.001 {
r := g.JitterTolerance
m := g.JitterToleranceMultiplier
y = math.Pow(math.Ceil(math.Pow(x/m, 1/r)), r) * m
}
return y
}
func (g *IG) ShouldUpdate(u config.Vertex, v config.Vertex, newval float64) bool {
oldval := g.Weight(u, v) * 1000
newval *= 1000
if g.IsSuperMode {
2021-08-21 16:54:24 +02:00
return (oldval-newval)*(oldval*g.JitterToleranceMultiplier) >= g.JitterTolerance
2021-08-20 19:32:50 +02:00
} else {
return g.GetWeightType(oldval) == g.GetWeightType(newval)
}
}
func (g *IG) RecalculateNhTable(checkchange bool) (changed bool) {
if g.RecalculateTime.Add(g.RecalculateCoolDown).Before(time.Now()) {
dist, next := FloydWarshall(g)
2021-08-21 16:54:24 +02:00
changed = false
2021-08-20 19:32:50 +02:00
if checkchange {
CheckLoop:
for src, dsts := range next {
2021-08-21 16:54:24 +02:00
for dst, old_next := range dsts {
2021-08-20 19:32:50 +02:00
nexthop := g.Next(src, dst)
2021-08-21 16:54:24 +02:00
if old_next != nexthop {
changed = true
break CheckLoop
2021-08-20 19:32:50 +02:00
}
}
}
}
g.dlTable, g.NhTable = dist, next
g.nhTableExpire = time.Now().Add(g.NodeReportTimeout)
g.RecalculateTime = time.Now()
}
return
}
func (g *IG) UpdateLentancy(u, v config.Vertex, dt time.Duration, checkchange bool) (changed bool) {
2021-08-21 16:54:24 +02:00
g.edgelock.Lock()
2021-08-16 20:58:15 +02:00
g.Vert[u] = true
g.Vert[v] = true
2021-08-20 19:32:50 +02:00
w := float64(dt) / float64(time.Second)
2021-08-21 16:54:24 +02:00
if _, ok := g.edges[u]; !ok {
g.edges[u] = make(map[config.Vertex]Latency)
2021-08-20 19:32:50 +02:00
}
2021-08-23 18:39:04 +02:00
g.edgelock.Unlock()
2021-08-20 19:32:50 +02:00
if g.ShouldUpdate(u, v, w) {
changed = g.RecalculateNhTable(checkchange)
2021-08-16 20:58:15 +02:00
}
2021-08-21 16:54:24 +02:00
g.edgelock.Lock()
g.edges[u][v] = Latency{
2021-08-16 20:58:15 +02:00
ping: w,
time: time.Now(),
}
2021-08-21 16:54:24 +02:00
g.edgelock.Unlock()
2021-08-20 19:32:50 +02:00
return
}
func (g IG) Vertices() map[config.Vertex]bool {
2021-08-23 18:39:04 +02:00
vr := make(map[config.Vertex]bool)
2021-08-23 19:45:09 +02:00
g.edgelock.RLock()
defer g.edgelock.RUnlock()
2021-08-23 18:39:04 +02:00
for k, v := range g.Vert { //copy a new list
vr[k] = v
}
return vr
2021-08-16 20:58:15 +02:00
}
2021-08-20 19:32:50 +02:00
func (g IG) Neighbors(v config.Vertex) (vs []config.Vertex) {
2021-08-23 18:39:04 +02:00
g.edgelock.RLock()
defer g.edgelock.RUnlock()
for k := range g.edges[v] { //copy a new list
2021-08-16 20:58:15 +02:00
vs = append(vs, k)
}
return vs
}
2021-08-20 19:32:50 +02:00
func (g IG) Next(u, v config.Vertex) *config.Vertex {
if _, ok := g.NhTable[u]; !ok {
return nil
}
if _, ok := g.NhTable[u][v]; !ok {
return nil
}
return g.NhTable[u][v]
}
func (g IG) Weight(u, v config.Vertex) float64 {
2021-08-23 18:39:04 +02:00
g.edgelock.RLock()
defer g.edgelock.RUnlock()
2021-08-21 16:54:24 +02:00
if _, ok := g.edges[u]; !ok {
2021-08-23 18:39:04 +02:00
g.edgelock.RUnlock()
2021-08-21 16:54:24 +02:00
g.edgelock.Lock()
g.edges[u] = make(map[config.Vertex]Latency)
g.edgelock.Unlock()
2021-08-23 18:39:04 +02:00
g.edgelock.RLock()
2021-08-20 19:32:50 +02:00
return Infinity
}
2021-08-21 16:54:24 +02:00
if _, ok := g.edges[u][v]; !ok {
2021-08-20 19:32:50 +02:00
return Infinity
}
2021-08-21 16:54:24 +02:00
if time.Now().After(g.edges[u][v].time.Add(g.NodeReportTimeout)) {
2021-08-20 19:32:50 +02:00
return Infinity
2021-08-16 20:58:15 +02:00
}
2021-08-21 16:54:24 +02:00
return g.edges[u][v].ping
2021-08-16 20:58:15 +02:00
}
2021-08-20 19:32:50 +02:00
func FloydWarshall(g Graph) (dist config.DistTable, next config.NextHopTable) {
2021-08-16 20:58:15 +02:00
vert := g.Vertices()
2021-08-20 19:32:50 +02:00
dist = make(config.DistTable)
next = make(config.NextHopTable)
2021-08-16 20:58:15 +02:00
for u, _ := range vert {
2021-08-20 19:32:50 +02:00
dist[u] = make(map[config.Vertex]float64)
next[u] = make(map[config.Vertex]*config.Vertex)
2021-08-16 20:58:15 +02:00
for v, _ := range vert {
dist[u][v] = Infinity
}
dist[u][u] = 0
for _, v := range g.Neighbors(u) {
w := g.Weight(u, v)
if w < Infinity {
v := v
dist[u][v] = w
next[u][v] = &v
}
}
}
for k, _ := range vert {
for i, _ := range vert {
for j, _ := range vert {
if dist[i][k] < Infinity && dist[k][j] < Infinity {
if dist[i][j] > dist[i][k]+dist[k][j] {
dist[i][j] = dist[i][k] + dist[k][j]
next[i][j] = next[i][k]
}
}
}
}
}
return dist, next
}
2021-08-20 19:32:50 +02:00
func Path(u, v config.Vertex, next config.NextHopTable) (path []config.Vertex) {
2021-08-16 20:58:15 +02:00
if next[u][v] == nil {
2021-08-20 19:32:50 +02:00
return []config.Vertex{}
2021-08-16 20:58:15 +02:00
}
2021-08-20 19:32:50 +02:00
path = []config.Vertex{u}
2021-08-16 20:58:15 +02:00
for u != v {
u = *next[u][v]
path = append(path, u)
}
return path
}
2021-08-20 19:32:50 +02:00
func (g *IG) SetNHTable(nh config.NextHopTable, table_hash [32]byte) { // set nhTable from supernode
g.NhTable = nh
g.NhTableHash = table_hash
g.nhTableExpire = time.Now().Add(g.SuperNodeInfoTimeout)
}
func (g *IG) GetNHTable(checkChange bool) config.NextHopTable {
if time.Now().After(g.nhTableExpire) {
g.RecalculateNhTable(checkChange)
}
return g.NhTable
}
func (g *IG) GetBoardcastList(id config.Vertex) (tosend map[config.Vertex]bool) {
tosend = make(map[config.Vertex]bool)
for _, element := range g.NhTable[id] {
2021-08-16 20:58:15 +02:00
tosend[*element] = true
}
return
}
2021-08-20 19:32:50 +02:00
func (g *IG) GetBoardcastThroughList(self_id config.Vertex, in_id config.Vertex, src_id config.Vertex) (tosend map[config.Vertex]bool) {
tosend = make(map[config.Vertex]bool)
for check_id, _ := range g.GetBoardcastList(self_id) {
for _, path_node := range Path(src_id, check_id, g.NhTable) {
if path_node == self_id && check_id != in_id {
2021-08-16 20:58:15 +02:00
tosend[check_id] = true
continue
}
}
}
return
}
func Solve() {
var g IG
2021-08-20 19:32:50 +02:00
//g.Init()
g.UpdateLentancy(1, 2, S2TD(0.5), false)
g.UpdateLentancy(2, 1, S2TD(0.5), false)
g.UpdateLentancy(2, 3, S2TD(2), false)
g.UpdateLentancy(3, 2, S2TD(2), false)
g.UpdateLentancy(2, 4, S2TD(0.7), false)
g.UpdateLentancy(4, 2, S2TD(2), false)
2021-08-16 20:58:15 +02:00
dist, next := FloydWarshall(g)
fmt.Println("pair\tdist\tpath")
for u, m := range dist {
for v, d := range m {
if u != v {
fmt.Printf("%d -> %d\t%3f\t%s\n", u, v, d, fmt.Sprint(Path(u, v, next)))
}
}
}
fmt.Print("Finish")
rr, _ := yaml.Marshal(Fullroute{
Dist: dist,
Next: next,
})
fmt.Print(string(rr))
}