-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdigitalocean.go
More file actions
55 lines (44 loc) · 1.23 KB
/
digitalocean.go
File metadata and controls
55 lines (44 loc) · 1.23 KB
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
package fetchers
import (
"encoding/csv"
"fmt"
"io"
"net/http"
)
// DigitalOceanFetcher implements the IPRangeFetcher interface for Digital Ocean.
type DigitalOceanFetcher struct{}
func (f DigitalOceanFetcher) Name() string {
return "digitalocean"
}
func (f DigitalOceanFetcher) Description() string {
return "Fetches IP ranges for Digital Ocean services."
}
func (f DigitalOceanFetcher) FetchIPRanges() ([]string, error) {
const doURL = "https://digitalocean.com/geo/google.csv"
resp, err := http.Get(doURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch Digital Ocean IP ranges: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("received non-200 status code from Digital Ocean: %d", resp.StatusCode)
}
// Parse the CSV file
csvReader := csv.NewReader(resp.Body)
// No official headers in the file, so skip first row handling
var ipRanges []string
for {
record, err := csvReader.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("error reading Digital Ocean CSV: %v", err)
}
// The first field is the IP prefix
if len(record) > 0 && record[0] != "" {
ipRanges = append(ipRanges, record[0])
}
}
return ipRanges, nil
}