forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
resolver.go
51 lines (45 loc) · 1.39 KB
/
resolver.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package dynamicip
import (
"context"
"errors"
"fmt"
"net"
"strings"
)
const (
ifConfigCoURL = "http://ifconfig.co"
ifConfigMeURL = "http://ifconfig.me"
// Note: All of the names below must be lowercase
// because we lowercase the user's input in NewResolver.
// TODO remove either ifConfig or ifConfigCo.
// They do the same thing.
OpenDNSName = "opendns"
IFConfigName = "ifconfig"
IFConfigCoName = "ifconfigco"
IFConfigMeName = "ifconfigme"
)
var errUnknownResolver = errors.New("unknown resolver")
// Resolver resolves our public IP
type Resolver interface {
// Resolve and return our public IP.
Resolve(context.Context) (net.IP, error)
}
// Returns a new Resolver that uses the given service
// to resolve our public IP.
// [resolverName] must be one of:
// [OpenDNSName], [IFConfigName], [IFConfigCoName], [IFConfigMeName].
// If [resolverService] isn't one of the above, returns an error
func NewResolver(resolverName string) (Resolver, error) {
switch strings.ToLower(resolverName) {
case OpenDNSName:
return newOpenDNSResolver(), nil
case IFConfigName, IFConfigCoName:
return &ifConfigResolver{url: ifConfigCoURL}, nil
case IFConfigMeName:
return &ifConfigResolver{url: ifConfigMeURL}, nil
default:
return nil, fmt.Errorf("%w: %s", errUnknownResolver, resolverName)
}
}