-
Notifications
You must be signed in to change notification settings - Fork 2
/
caddyfile.go
193 lines (166 loc) · 4.1 KB
/
caddyfile.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
package caddy_cloudflare_ip
import (
"bufio"
"context"
"net/http"
"net/netip"
"sync"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
const (
ipv4 = "https://www.cloudflare.com/ips-v4"
ipv6 = "https://www.cloudflare.com/ips-v6"
)
func init() {
caddy.RegisterModule(CloudflareIPRange{})
}
// CloudflareIPRange provides a range of IP address prefixes (CIDRs) retrieved from cloudflare.
type CloudflareIPRange struct {
// refresh Interval
Interval caddy.Duration `json:"interval,omitempty"`
// request Timeout
Timeout caddy.Duration `json:"timeout,omitempty"`
// Holds the parsed CIDR ranges from Ranges.
ranges []netip.Prefix
ctx caddy.Context
lock *sync.RWMutex
}
// CaddyModule returns the Caddy module information.
func (CloudflareIPRange) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.ip_sources.cloudflare",
New: func() caddy.Module { return new(CloudflareIPRange) },
}
}
// getContext returns a cancelable context, with a timeout if configured.
func (s *CloudflareIPRange) getContext() (context.Context, context.CancelFunc) {
if s.Timeout > 0 {
return context.WithTimeout(s.ctx, time.Duration(s.Timeout))
}
return context.WithCancel(s.ctx)
}
func (s *CloudflareIPRange) fetch(api string) ([]netip.Prefix, error) {
ctx, cancel := s.getContext()
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, api, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
var prefixes []netip.Prefix
for scanner.Scan() {
prefix, err := caddyhttp.CIDRExpressionToPrefix(scanner.Text())
if err != nil {
return nil, err
}
prefixes = append(prefixes, prefix)
}
return prefixes, nil
}
func (s *CloudflareIPRange) getPrefixes() ([]netip.Prefix, error) {
var fullPrefixes []netip.Prefix
// fetch ipv4 list
prefixes, err := s.fetch(ipv4)
if err != nil {
return nil, err
}
fullPrefixes = append(fullPrefixes, prefixes...)
// fetch ipv6 list
prefixes, err = s.fetch(ipv6)
if err != nil {
return nil, err
}
fullPrefixes = append(fullPrefixes, prefixes...)
return fullPrefixes, nil
}
func (s *CloudflareIPRange) Provision(ctx caddy.Context) error {
s.ctx = ctx
s.lock = new(sync.RWMutex)
// update in background
go s.refreshLoop()
return nil
}
func (s *CloudflareIPRange) refreshLoop() {
if s.Interval == 0 {
s.Interval = caddy.Duration(time.Hour)
}
ticker := time.NewTicker(time.Duration(s.Interval))
// first time update
s.lock.Lock()
// it's nil anyway if there is an error
s.ranges, _ = s.getPrefixes()
s.lock.Unlock()
for {
select {
case <-ticker.C:
fullPrefixes, err := s.getPrefixes()
if err != nil {
break
}
s.lock.Lock()
s.ranges = fullPrefixes
s.lock.Unlock()
case <-s.ctx.Done():
ticker.Stop()
return
}
}
}
func (s *CloudflareIPRange) GetIPRanges(_ *http.Request) []netip.Prefix {
s.lock.RLock()
defer s.lock.RUnlock()
return s.ranges
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
//
// cloudflare {
// interval val
// timeout val
// }
func (m *CloudflareIPRange) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
d.Next() // Skip module name.
// No same-line options are supported
if d.NextArg() {
return d.ArgErr()
}
for nesting := d.Nesting(); d.NextBlock(nesting); {
switch d.Val() {
case "interval":
if !d.NextArg() {
return d.ArgErr()
}
val, err := caddy.ParseDuration(d.Val())
if err != nil {
return err
}
m.Interval = caddy.Duration(val)
case "timeout":
if !d.NextArg() {
return d.ArgErr()
}
val, err := caddy.ParseDuration(d.Val())
if err != nil {
return err
}
m.Timeout = caddy.Duration(val)
default:
return d.ArgErr()
}
}
return nil
}
// interface guards
var (
_ caddy.Module = (*CloudflareIPRange)(nil)
_ caddy.Provisioner = (*CloudflareIPRange)(nil)
_ caddyfile.Unmarshaler = (*CloudflareIPRange)(nil)
_ caddyhttp.IPRangeSource = (*CloudflareIPRange)(nil)
)