-
Notifications
You must be signed in to change notification settings - Fork 1
/
port.go
50 lines (43 loc) · 788 Bytes
/
port.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package l2
import (
"fmt"
"hash/fnv"
"time"
)
func shiftingPort(
kind string,
portShiftInterval time.Duration,
) (
get func(node *Node, t time.Time) int,
) {
type PortInfo struct {
Time time.Time
Port int
}
portInfos := make(map[*Node]*PortInfo)
get = func(node *Node, t time.Time) int {
t = t.Round(portShiftInterval)
info, ok := portInfos[node]
if !ok {
info = new(PortInfo)
portInfos[node] = info
}
if t != info.Time {
info.Time = t
f := fnv.New64a()
if node.ID > 0 {
fmt.Fprintf(f, "%s-%s-%d-%d",
kind, node.lanIPStr, node.ID, t.Unix(),
)
} else {
fmt.Fprintf(f, "%s-%s-%d",
kind, node.lanIPStr, t.Unix(),
)
}
port := 10000 + f.Sum64()%45000
info.Port = int(port)
}
return info.Port
}
return
}