-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipsources.go
65 lines (52 loc) · 1.24 KB
/
ipsources.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
package main
import (
"fmt"
"io"
"strings"
)
type Source interface {
Fetch() (string, error)
String() string
}
type SimpleSource struct {
EndPoint string
IPFamily int
}
func (s *SimpleSource) Fetch() (string, error) {
resp, err := httpGetRequest(s.EndPoint, nil, s.IPFamily)
if err != nil {
return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return strings.TrimSpace(string(body)), nil
}
func (s *SimpleSource) String() string {
return fmt.Sprintf("<Simple source; endpoint=%s, IPFamily=%d>", s.EndPoint, s.IPFamily)
}
type CFTrace struct {
EndPoint string
IPFamily int
}
func (s *CFTrace) Fetch() (string, error) {
resp, err := httpGetRequest(fmt.Sprintf("https://%s/cdn-cgi/trace", s.EndPoint), nil, s.IPFamily)
if err != nil {
return "", err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
bodyText := strings.TrimSpace(string(body))
for _, line := range strings.Split(bodyText, "\n") {
if strings.HasPrefix(line, "ip=") {
return strings.TrimSpace(line[3:]), nil
}
}
return "", fmt.Errorf("no ip provided in cloudflare trace info")
}
func (s *CFTrace) String() string {
return fmt.Sprintf("<CF trace; endpoint=%s, IPFamily=%d>", s.EndPoint, s.IPFamily)
}