summaryrefslogtreecommitdiff
path: root/source/source.go
blob: 571abdaa2ad404fa55ef6dd80c930c5c6020199e (plain)
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
package source

import (
	"fmt"
	"io"
	"log/slog"
	"net/http"
	"net/netip"
	"strings"
	"time"
)

type Kind string

const (
	KindStatic    Kind = "static"
	KindURL       Kind = "url"
	KindFile      Kind = "file"
	KindICanHazIp Kind = "icanhazip"
)

type Family string

const (
	IPv4 Family = "ipv4"
	IPv6 Family = "ipv6"
)

type Config struct {
	Kind   Kind   `json:"kind"`
	Family Family `json:"family,omitempty"`

	URL   string `json:"url,omitempty"`
	Path  string `json:"path,omitempty"`
	Value string `json:"value,omitempty"`
}

type Source interface {
	Supports(Family) bool
	Lookup(Family) (netip.Addr, error)
}

func New(log *slog.Logger, name string, cfg Config) (Source, error) {
	log = log.With(
		slog.Group(
			"source",
			slog.String("name", name),
			slog.String("kind", string(cfg.Kind)),
		),
	)

	if cfg.Kind == KindICanHazIp {
		return newICanHazIp(log)
	}

	// all sources below need an explicit family
	if cfg.Family != IPv4 && cfg.Family != IPv6 {
		return nil, fmt.Errorf("%s source needs family set to ipv4 or ipv6", cfg.Kind)
	}

	if cfg.Kind == KindStatic {
		return newStatic(log, cfg)
	}

	if cfg.Kind == KindURL {
		return newURL(log, cfg)
	}

	if cfg.Kind == KindFile {
		return newFile(log, cfg)
	}

	return nil, fmt.Errorf("unknown source kind %q", cfg.Kind)
}

// parsing function for ipv4 and ipv6 strings
func parse(raw string, family Family) (netip.Addr, error) {
	addr, err := netip.ParseAddr(strings.TrimSpace(raw))
	if err != nil {
		return netip.Addr{}, err
	}
	if addr.Is6() != (family == IPv6) {
		return netip.Addr{}, fmt.Errorf("%s is not an %s address", addr, family)
	}
	return addr, nil
}

// generic http fetching helper since a lot of sources will need it.
func fetch(url string, family Family) (netip.Addr, error) {
	client := http.Client{Timeout: 15 * time.Second}
	resp, err := client.Get(url)
	if err != nil {
		return netip.Addr{}, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return netip.Addr{}, fmt.Errorf("%s: %s", url, resp.Status)
	}
	raw, err := io.ReadAll(io.LimitReader(resp.Body, 256))
	if err != nil {
		return netip.Addr{}, err
	}
	return parse(string(raw), family)
}