-
Notifications
You must be signed in to change notification settings - Fork 681
/
nat.go
50 lines (40 loc) · 824 Bytes
/
nat.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 nat
import (
"fmt"
"sort"
"strings"
)
type commonTranslator struct {
Name string
Mappings map[Address]string
}
type Address struct {
Proto string
Ip string
Port string
}
type Entry struct {
Destination Address
Port string
}
func (e *Entry) String() string {
return fmt.Sprintf("%s:%s->%s", e.Destination.Proto, e.Destination.Ip, e.Port)
}
func (t *Translator) sorted() []Entry {
entries := make([]Entry, len(t.Mappings))
index := 0
for k, v := range t.Mappings {
entries[index] = Entry{k, v}
index += 1
}
sort.Slice(entries, func(i, j int) bool {
return strings.Compare(entries[i].String(), entries[j].String()) < 0
})
return entries
}
func NewTranslator(name string) *Translator {
var t Translator
t.Name = name
t.Mappings = make(map[Address]string)
return &t
}