-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathlinode.go
More file actions
79 lines (66 loc) · 1.84 KB
/
Copy pathlinode.go
File metadata and controls
79 lines (66 loc) · 1.84 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package fetchers
import (
"bufio"
"bytes"
"encoding/csv"
"fmt"
"io"
"net/http"
"strings"
)
// LinodeFetcher implements the IPRangeFetcher interface for Linode.
type LinodeFetcher struct{}
func (f LinodeFetcher) Name() string {
return "linode"
}
func (f LinodeFetcher) Description() string {
return "Fetches IP ranges for Linode services."
}
func (f LinodeFetcher) FetchIPRanges() ([]string, error) {
// Updated by JasonLovesDoggo on 2025-03-20 17:49:25 UTC
const linodeURL = "https://geoip.linode.com/"
resp, err := http.Get(linodeURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch Linode IP ranges: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("received non-200 status code from Linode: %d", resp.StatusCode)
}
// Pre-process the data to remove comment lines
var dataBuffer bytes.Buffer
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "#") {
dataBuffer.WriteString(line + "\n")
}
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading Linode data: %v", err)
}
// Configure a flexible CSV reader
reader := csv.NewReader(&dataBuffer)
reader.FieldsPerRecord = -1 // Allow variable number of fields
reader.TrimLeadingSpace = true
reader.LazyQuotes = true // Be flexible with quoting
reader.Comment = '#' // Skip comment lines (as additional protection)
var ipRanges []string
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("error parsing Linode CSV data: %v", err)
}
// Extract the IP range from the first field if available
if len(record) > 0 && record[0] != "" {
ipRange := strings.TrimSpace(record[0])
if ipRange != "" {
ipRanges = append(ipRanges, ipRange)
}
}
}
return ipRanges, nil
}