-
Notifications
You must be signed in to change notification settings - Fork 0
/
icloudpr.go
212 lines (172 loc) · 4.26 KB
/
icloudpr.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
package icloudpr
import (
"bytes"
"encoding/csv"
"fmt"
"io"
"log"
"log/slog"
"net"
"net/http"
"net/netip"
"regexp"
"strings"
"time"
"github.com/hashicorp/go-retryablehttp"
"github.com/jonhadfield/ip-fetcher/internal/pflog"
"github.com/jonhadfield/ip-fetcher/internal/web"
"github.com/jszwec/csvutil"
"github.com/sirupsen/logrus"
)
const (
ShortName = "icloudpr"
FullName = "iCloud Private Relay"
HostType = "anonymiser"
SourceURL = "-"
DownloadURL = "https://mask-api.icloud.com/egress-ip-ranges.csv"
errFailedToDownload = "failed to download iCloud Private Relay prefixes document "
)
func IsIPv4(address string) bool {
return strings.Count(address, ":") < 2
}
func IsIPv6(address string) bool {
return strings.Count(address, ":") >= 2
}
func extractNetFromString(in string) string {
r := regexp.MustCompile(`^[0-9a-fA-F]([^\s]+)`)
s := r.FindString(in)
if !strings.Contains(s, "/") {
switch {
case IsIPv4(s):
s += "/32"
case IsIPv6(s):
s += "/128"
default:
slog.Debug("failed to parse file line", "line", s)
return ""
}
}
if _, _, err := net.ParseCIDR(s); err != nil {
slog.Debug("failed to parse file line", "line", s, "error", err)
return ""
}
return s
}
type ICloudPrivateRelay struct {
Client *retryablehttp.Client
DownloadURL string
}
func New() ICloudPrivateRelay {
pflog.SetLogLevel()
c := web.NewHTTPClient()
if logrus.GetLevel() < logrus.DebugLevel {
c.Logger = nil
}
return ICloudPrivateRelay{
DownloadURL: DownloadURL,
Client: c,
}
}
func (a *ICloudPrivateRelay) FetchData() (data []byte, headers http.Header, status int, err error) {
// get download url if not specified
if a.DownloadURL == "" {
a.DownloadURL = DownloadURL
}
data, headers, status, err = web.Request(a.Client, a.DownloadURL, http.MethodGet, nil, nil, 5*time.Second)
if status >= 400 {
return nil, nil, status, fmt.Errorf("failed to download prefixes. http status code: %d", status)
}
return data, headers, status, err
}
type Doc struct {
LastModified time.Time
ETag string
Records []Record
}
func (a *ICloudPrivateRelay) Fetch() (doc Doc, err error) {
data, headers, _, err := a.FetchData()
if err != nil {
return
}
records, err := Parse(data)
if err != nil {
return
}
doc.Records = records
var etag string
etags := headers.Values("etag")
if len(etags) != 0 {
etag = etags[0]
}
doc.ETag = etag
var lastModifiedTime time.Time
lastModifiedRaw := headers.Values("last-modified")
if len(lastModifiedRaw) != 0 {
if lastModifiedTime, err = time.Parse(time.RFC1123, lastModifiedRaw[0]); err != nil {
return
}
}
doc.LastModified = lastModifiedTime
return doc, err
}
type Entry struct {
Prefix string `csv:"ip_prefix,omitempty"`
Alpha2Code string `csv:"alpha2code,omitempty"`
Region string `csv:"region,omitempty"`
City string `csv:"city,omitempty"`
PostalCode string `csv:"postal_code,omitempty"`
}
func Parse(data []byte) (records []Record, err error) {
reader := bytes.NewReader(data)
csvReader := csv.NewReader(reader)
csvReader.Comment = '#'
csvReader.TrimLeadingSpace = true
doHeader, err := csvutil.Header(Entry{}, "csv")
if err != nil {
log.Fatal(err)
}
dec, err := csvutil.NewDecoder(csvReader, doHeader...)
if err != nil {
log.Fatal(err)
}
Loop:
for {
var c Record
err = dec.Decode(&c)
switch err {
case io.EOF:
err = nil
break Loop
case nil:
var pcn netip.Prefix
if c.PrefixText == "" {
continue
}
pcn, err = netip.ParsePrefix(extractNetFromString(c.PrefixText))
if err != nil {
return records, err
}
c.Prefix = pcn
records = append(records, c)
default:
return
}
}
return
}
// prefix, alpha2code, region, city, postal_code
type Record struct {
Prefix netip.Prefix
PrefixText string `csv:"ip_prefix,omitempty"`
Alpha2Code string `csv:"alpha2code,omitempty"`
Region string `csv:"region,omitempty"`
City string `csv:"city,omitempty"`
PostalCode string `csv:"postal_code,omitempty"`
}
type CSVEntry struct {
Prefix string `csv:"ip_prefix,omitempty"`
Alpha2Code string `csv:"alpha2code,omitempty"`
Region string `csv:"region,omitempty"`
City string `csv:"city,omitempty"`
PostalCode string `csv:"postal_code,omitempty"`
}