forked from florianl/go-conntrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
119 lines (110 loc) · 2.78 KB
/
example_test.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package conntrack_test
import (
"context"
"fmt"
"net"
"time"
ct "github.com/florianl/go-conntrack"
)
func ExampleNfct_Dump() {
nfct, err := ct.Open()
if err != nil {
fmt.Println("Could not create nfct:", err)
return
}
defer nfct.Close()
sessions, err := nfct.Dump(ct.Ct, ct.CtIPv4)
if err != nil {
fmt.Println("Could not dump sessions:", err)
return
}
for _, x := range sessions {
oSrcIP, _ := x.OrigSrcIP()
oDstIP, _ := x.OrigDstIP()
fmt.Printf("src: %s\tdst: %s \n", oSrcIP, oDstIP)
}
}
func ExampleNfct_Flush() {
nfct, err := ct.Open()
if err != nil {
fmt.Println("Could not create nfct:", err)
return
}
defer nfct.Close()
err = nfct.Flush(ct.Ct, ct.CtIPv4)
if err != nil {
fmt.Println("Could not flush conntrack subsystem:", err)
return
}
}
func ExampleNfct_Create() {
nfct, err := ct.Open()
if err != nil {
fmt.Println("Could not create nfct:", err)
return
}
defer nfct.Close()
ipA := net.ParseIP("1.1.1.1")
ipB := net.ParseIP("8.8.8.8")
err = nfct.Create(ct.Ct, ct.CtIPv4, []ct.ConnAttr{
{Type: ct.AttrOrigIPv4Src, Data: []byte{ipA[12], ipA[13], ipA[14], ipA[15]}},
{Type: ct.AttrOrigIPv4Dst, Data: []byte{ipB[12], ipB[13], ipB[14], ipB[15]}},
{Type: ct.AttrReplIPv4Src, Data: []byte{ipB[12], ipB[13], ipB[14], ipB[15]}},
{Type: ct.AttrReplIPv4Dst, Data: []byte{ipA[12], ipA[13], ipA[14], ipA[15]}},
{Type: ct.AttrOrigL4Proto, Data: []byte{0x02}},
{Type: ct.AttrReplL4Proto, Data: []byte{0x02}},
{Type: ct.AttrStatus, Data: []byte{0x0, 0x0, 0x0, 0x08}},
{Type: ct.AttrTimeout, Data: []byte{0x0, 0x0, 0x0, 0x42}}})
if err != nil {
fmt.Println("Could not create new session:", err)
return
}
}
func ExampleNfct_Query() {
nfct, err := ct.Open()
if err != nil {
fmt.Println("Could not create nfct:", err)
return
}
defer nfct.Close()
sessions, err := nfct.Query(ct.Ct, ct.CtIPv6,
ct.FilterAttr{Mark: []byte{0x00, 0x0, 0x00, 0x01},
MarkMask: []byte{0x00, 0x0, 0x0, 0xFF}})
if err != nil {
fmt.Println("Could not query sessions:", err)
return
}
for _, x := range sessions {
oSrcIP, _ := x.OrigSrcIP()
oDstIP, _ := x.OrigDstIP()
fmt.Printf("src: %s\tdst: %s \n", oSrcIP, oDstIP)
}
}
func ExampleNfct_RegisterFiltered() {
nfct, err := ct.Open()
if err != nil {
fmt.Println("Could not create nfct:", err)
return
}
defer nfct.Close()
errChan, err := nfct.RegisterFiltered(
context.Background(), ct.Ct, ct.NetlinkCtUpdate,
[]ct.ConnAttr{
{Type: ct.AttrOrigL4Proto, Data: []byte{0x11}}, // TCP
{Type: ct.AttrOrigL4Proto, Data: []byte{0x06}}, // UDP
},
func(c ct.Conn) int { fmt.Println(c); return 0 },
)
if err != nil {
fmt.Println("Could not register with filter:", err)
return
}
for {
select {
case errMsg := <-errChan:
fmt.Println(errMsg)
case <-time.After(10 * time.Second):
break
}
}
}