-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcpd.go
224 lines (194 loc) · 4.25 KB
/
cpd.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Package cpd contains the Captive Portal Detection.
package cpd
import (
"io"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
// Report is a captive portal detection report.
type Report struct {
Detected bool
Host string
}
// CPD is a captive portal detection instance.
type CPD struct {
config *Config
reports chan *Report
probes chan struct{}
done chan struct{}
closed chan struct{}
// internal probe reports
probeReports chan *Report
// timer for periodic checks
timer *time.Timer
// is a captive portal detected, are probes currently running or
// have to run again?
detected bool
running bool
runAgain bool
}
// check probes the http server.
func (c *CPD) check() *Report {
// send http request
client := &http.Client{
Timeout: c.config.HTTPTimeout,
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Get("http://" + c.config.Host)
if err != nil {
log.WithError(err).Debug("CPD GET error")
return &Report{}
}
_, err = io.Copy(io.Discard, resp.Body)
if err != nil {
log.WithError(err).Error("CPD read error")
return &Report{}
}
_ = resp.Body.Close()
// check response code
switch resp.StatusCode {
case http.StatusNoContent:
// 204, what we want, no captive portal
return &Report{}
case http.StatusFound:
// 302, redirect, captive portal detected
hostname := ""
if url, err := resp.Location(); err != nil {
log.WithError(err).Error("CPD could not get location in response")
} else {
hostname = url.Hostname()
}
return &Report{
Detected: true,
Host: hostname,
}
default:
// other, captive protal detected
return &Report{
Detected: true,
}
}
}
// probe probes the http server.
func (c *CPD) probe() {
// TODO: improve this?
r := &Report{}
for i := 0; i < c.config.ProbeCount; i++ {
r = c.check()
if r.Detected {
break
}
time.Sleep(c.config.ProbeWait)
}
select {
case c.probeReports <- r:
case <-c.done:
return
}
}
// handleProbeRequest handles a probe request.
func (c *CPD) handleProbeRequest() {
if c.running {
c.runAgain = true
return
}
c.running = true
go c.probe()
}
// handleProbeReport handles an internal probe report.
func (c *CPD) handleProbeReport(r *Report) {
// try sending the report back,
// in the meantime read incoming probe requests and set the
// runAgain flag accordingly,
// stop when the report is sent or we are shutting down
for {
select {
case c.reports <- r:
c.running = false
if c.runAgain {
// we must trigger another probe
c.runAgain = false
c.running = true
go c.probe()
}
c.detected = r.Detected
return
case <-c.probes:
c.runAgain = true
case <-c.done:
return
}
}
}
// handleTimer handles a timer event.
func (c *CPD) handleTimer() {
if !c.running && !c.runAgain {
// no probes active, trigger new probe
log.Debug("periodic CPD timer")
c.running = true
go c.probe()
}
// reset timer
if c.detected {
c.timer.Reset(c.config.ProbeTimerDetected)
} else {
c.timer.Reset(c.config.ProbeTimer)
}
}
// start starts the captive portal detection.
func (c *CPD) start() {
defer close(c.closed)
defer close(c.reports)
// set timer for periodic checks
c.timer = time.NewTimer(c.config.ProbeTimer)
for {
select {
case <-c.probes:
c.handleProbeRequest()
case r := <-c.probeReports:
c.handleProbeReport(r)
case <-c.timer.C:
c.handleTimer()
case <-c.done:
if !c.timer.Stop() {
<-c.timer.C
}
return
}
}
}
// Start starts the captive portal detection.
func (c *CPD) Start() {
go c.start()
}
// Stop stops the captive portal detection.
func (c *CPD) Stop() {
close(c.done)
<-c.closed
}
// Hosts returns the host addresses used for captive protal detection.
func (c *CPD) Hosts() []string {
return []string{c.config.Host}
}
// Probe triggers the captive portal detection.
func (c *CPD) Probe() {
c.probes <- struct{}{}
}
// Results returns the results channel.
func (c *CPD) Results() chan *Report {
return c.reports
}
// NewCPD returns a new CPD.
func NewCPD(config *Config) *CPD {
return &CPD{
config: config,
reports: make(chan *Report),
probes: make(chan struct{}),
done: make(chan struct{}),
closed: make(chan struct{}),
probeReports: make(chan *Report),
}
}