-
Notifications
You must be signed in to change notification settings - Fork 1
/
mock.go
90 lines (70 loc) · 1.85 KB
/
mock.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
package egresseddie
import (
"context"
"errors"
"fmt"
"net"
"net/netip"
"sync"
"github.com/florianl/go-nfqueue"
"go.uber.org/zap"
)
var mockEnforcers map[uint16]*mockEnforcer
type mockEnforcer struct {
mtx sync.Mutex
hook nfqueue.HookFunc
verdicts map[uint32]int
}
func initMockEnforcers() {
mockEnforcers = make(map[uint16]*mockEnforcer)
}
func newMockEnforcer(_ context.Context, _ *zap.Logger, queueNum uint16, _ bool, hook nfqueue.HookFunc) (enforcer, error) {
if _, ok := mockEnforcers[queueNum]; ok {
return nil, fmt.Errorf("a nfqueue with the queue number %d has already been started", queueNum)
}
mEnforcer := &mockEnforcer{
hook: hook,
verdicts: make(map[uint32]int),
}
mockEnforcers[queueNum] = mEnforcer
return mEnforcer, nil
}
func (m *mockEnforcer) SetVerdict(id uint32, verdict int) error {
if id == 0 {
return errors.New("id is zero")
}
// these are not the only valid verdicts, but they are the only
// verdicts egress eddie will pass
if verdict != nfqueue.NfDrop && verdict != nfqueue.NfAccept {
return fmt.Errorf("invalid verdict %d", verdict)
}
m.mtx.Lock()
m.verdicts[id] = verdict
m.mtx.Unlock()
return nil
}
func (m *mockEnforcer) Close() error {
return nil
}
type mockResolver struct {
addrs map[string][]netip.Addr
hostnames map[string][]string
}
func (m *mockResolver) LookupNetIP(_ context.Context, _ string, host string) ([]netip.Addr, error) {
if m.addrs == nil {
return nil, &net.DNSError{IsNotFound: true}
}
if addrs, ok := m.addrs[host]; ok {
return addrs, nil
}
return nil, &net.DNSError{IsNotFound: true}
}
func (m *mockResolver) LookupAddr(_ context.Context, addr string) ([]string, error) {
if m.hostnames == nil {
return nil, &net.DNSError{IsNotFound: true}
}
if addrs, ok := m.hostnames[addr]; ok {
return addrs, nil
}
return nil, &net.DNSError{IsNotFound: true}
}