-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrafpol.go
196 lines (161 loc) · 4.23 KB
/
trafpol.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Package trafpol contains the traffic policing.
package trafpol
import (
"context"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/telekom-mms/oc-daemon/internal/cpd"
"github.com/telekom-mms/oc-daemon/internal/devmon"
"github.com/telekom-mms/oc-daemon/internal/dnsmon"
)
// TrafPol is a traffic policing component.
type TrafPol struct {
config *Config
devmon *devmon.DevMon
dnsmon *dnsmon.DNSMon
cpd *cpd.CPD
// capPortal indicates if a captive portal is detected
capPortal bool
allowDevs *AllowDevs
allowHosts *AllowHosts
loopDone chan struct{}
done chan struct{}
}
// handleDeviceUpdate handles a device update.
func (t *TrafPol) handleDeviceUpdate(ctx context.Context, u *devmon.Update) {
// skip physical devices and only allow virtual devices
if u.Type == "device" {
return
}
// add or remove virtual device to/from allowed devices
if u.Add {
t.allowDevs.Add(ctx, u.Device)
return
}
t.allowDevs.Remove(ctx, u.Device)
}
// handleDNSUpdate handles a dns config update.
func (t *TrafPol) handleDNSUpdate() {
// update allowed hosts
t.allowHosts.Update()
// triger captive portal detection
t.cpd.Probe()
}
// handleCPDReport handles a CPD report.
func (t *TrafPol) handleCPDReport(ctx context.Context, report *cpd.Report) {
if !report.Detected {
// no captive portal detected
// check if there was a portal before
if t.capPortal {
// refresh all IPs, maybe they pointed to a
// portal host in case of dns-based portals
t.allowHosts.Update()
// remove ports from allowed ports
removePortalPorts(ctx, t.config.PortalPorts)
t.capPortal = false
log.WithField("capPortal", t.capPortal).Info("TrafPol changed CPD status")
}
return
}
// add ports to allowed ports
if !t.capPortal {
addPortalPorts(ctx, t.config.PortalPorts)
t.capPortal = true
log.WithField("capPortal", t.capPortal).Info("TrafPol changed CPD status")
}
}
// start starts the traffic policing component.
func (t *TrafPol) start(ctx context.Context) {
defer close(t.loopDone)
defer unsetFilterRules(ctx)
defer t.allowHosts.Stop()
defer t.cpd.Stop()
defer t.devmon.Stop()
defer t.dnsmon.Stop()
// enter main loop
for {
select {
case u := <-t.devmon.Updates():
// Device Update
log.WithField("update", u).Debug("TrafPol got DevMon update")
t.handleDeviceUpdate(ctx, u)
case <-t.dnsmon.Updates():
// DNS Update
log.Debug("TrafPol got DNSMon update")
t.handleDNSUpdate()
case r := <-t.cpd.Results():
// CPD Result
log.WithField("result", r).Debug("TrafPol got CPD result")
t.handleCPDReport(ctx, r)
case <-t.done:
// shutdown
return
}
}
}
// Start starts the traffic policing component.
func (t *TrafPol) Start() error {
log.Debug("TrafPol starting")
// create context
ctx := context.Background()
// set firewall config
setFilterRules(ctx, t.config.FirewallMark)
// add CPD hosts to allowed hosts
for _, h := range t.cpd.Hosts() {
t.allowHosts.Add(h)
}
// start allowed hosts
t.allowHosts.Start()
// start captive portal detection
t.cpd.Start()
// start device monitor
err := t.devmon.Start()
if err != nil {
err = fmt.Errorf("TrafPol could not start DevMon: %w", err)
goto cleanup_devmon
}
// start dns monitor
err = t.dnsmon.Start()
if err != nil {
err = fmt.Errorf("TrafPol could not start DNSMon: %w", err)
goto cleanup_dnsmon
}
go t.start(ctx)
return nil
// clean up after error
cleanup_dnsmon:
t.devmon.Stop()
cleanup_devmon:
t.cpd.Stop()
t.allowHosts.Stop()
unsetFilterRules(ctx)
return err
}
// Stop stops the traffic policing component.
func (t *TrafPol) Stop() {
close(t.done)
// wait for everything
<-t.loopDone
log.Debug("TrafPol stopped")
}
// NewTrafPol returns a new traffic policing component.
func NewTrafPol(config *Config) *TrafPol {
allowHosts := NewAllowHosts(config)
for _, h := range config.AllowedHosts {
allowHosts.Add(h)
}
return &TrafPol{
config: config,
devmon: devmon.NewDevMon(),
dnsmon: dnsmon.NewDNSMon(dnsmon.NewConfig()),
cpd: cpd.NewCPD(cpd.NewConfig()),
allowDevs: NewAllowDevs(),
allowHosts: allowHosts,
loopDone: make(chan struct{}),
done: make(chan struct{}),
}
}
// Cleanup cleans up old configuration after a failed shutdown.
func Cleanup(ctx context.Context) {
cleanupFilterRules(ctx)
}