-
Notifications
You must be signed in to change notification settings - Fork 669
/
resolver.go
47 lines (42 loc) · 1.3 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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package dynamicip
import (
"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"
)
// Resolver resolves our public IP
type Resolver interface {
// Resolve and return our public IP.
Resolve() (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("got unknown resolver: %s", resolverName)
}
}