-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathip_checker.go
157 lines (126 loc) · 3.3 KB
/
ip_checker.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
package traefik_plugin_cloudflare
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"time"
"log"
"github.com/agence-gaya/traefik-plugin-cloudflare/internal"
)
type ipChecker interface {
CheckIP(context.Context, net.IP) (bool, error)
}
type staticIPChecker struct {
Cidrs []*net.IPNet
}
func (c *staticIPChecker) CheckIP(ctx context.Context, ip net.IP) (bool, error) {
for _, cidr := range c.Cidrs {
if cidr.Contains(ip) {
return true, nil
}
}
return false, nil
}
type cloudflareIPChecker struct {
RefreshInterval time.Duration
cidrs []*net.IPNet
lastRefresh time.Time
}
func (c *cloudflareIPChecker) CheckIP(ctx context.Context, ip net.IP) (bool, error) {
if c.RefreshInterval > 0 && internal.Now().Sub(c.lastRefresh) > c.RefreshInterval {
err := c.Refresh(ctx)
if err != nil {
if len(c.cidrs) == 0 {
return false, fmt.Errorf("error: failed to refresh Cloudflare IPs: %w", err)
}
log.Println(fmt.Errorf("warning: failed to refresh Cloudflare IPs: %w, keep current cidrs", err))
}
}
for _, cidr := range c.cidrs {
if cidr.Contains(ip) {
return true, nil
}
}
return false, nil
}
func (c *cloudflareIPChecker) Refresh(ctx context.Context) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.cloudflare.com/client/v4/ips", nil)
if err != nil {
c.lastRefresh = internal.Now().Add(5*time.Minute - c.RefreshInterval)
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
c.lastRefresh = internal.Now().Add(5*time.Minute - c.RefreshInterval)
return err
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode > 299 {
c.lastRefresh = internal.Now().Add(5*time.Minute - c.RefreshInterval)
return fmt.Errorf("invalid response: %s", res.Status)
}
var resp cloudflareResponse
err = json.NewDecoder(res.Body).Decode(&resp)
if err != nil {
c.lastRefresh = internal.Now().Add(5*time.Minute - c.RefreshInterval)
return err
}
cidrs, err := resp.Data()
if err != nil {
c.lastRefresh = internal.Now().Add(5*time.Minute - c.RefreshInterval)
return err
}
log.Println("info: refresh cidrs successfull")
c.cidrs = cidrs
c.lastRefresh = internal.Now()
return nil
}
type cloudflareResponse struct {
Success bool `json:"success"`
Errors []*cloudflareError `json:"errors"`
Result *cloudflareIPs `json:"result"`
}
func (r *cloudflareResponse) Data() ([]*net.IPNet, error) {
if !r.Success || r.Result == nil {
for _, e := range r.Errors {
err := e.Error()
if err != nil {
return nil, err
}
}
return nil, errors.New("invalid response")
}
res := make([]*net.IPNet, 0, len(r.Result.IPv4CIDRs)+len(r.Result.IPv6CIDRs))
for _, c := range r.Result.IPv4CIDRs {
_, cidr, err := net.ParseCIDR(c)
if err != nil {
return nil, err
}
res = append(res, cidr)
}
for _, c := range r.Result.IPv6CIDRs {
_, cidr, err := net.ParseCIDR(c)
if err != nil {
return nil, err
}
res = append(res, cidr)
}
return res, nil
}
type cloudflareError struct {
Code int `json:"code"`
Message string `json:"message"`
}
func (e *cloudflareError) Error() error {
if e == nil {
return nil
}
return fmt.Errorf("Error %d: %s", e.Code, e.Message)
}
type cloudflareIPs struct {
IPv4CIDRs []string `json:"ipv4_cidrs"`
IPv6CIDRs []string `json:"ipv6_cidrs"`
}