aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/parse/host.go
blob: 67cfa86d86fedcf38d851d9ce6c7d8764ad368cb (plain) (blame)
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
105
106
107
108
109
110
111
112
package parse

import (
	"fmt"
	"net"
	"os"
	"strings"

	"github.com/coredns/coredns/plugin/pkg/transport"

	"github.com/miekg/dns"
)

// Strips the zone, but preserves any port that comes after the zone
func stripZone(host string) string {
	if strings.Contains(host, "%") {
		lastPercent := strings.LastIndex(host, "%")
		newHost := host[:lastPercent]
		return newHost
	}
	return host
}

// HostPortOrFile parses the strings in s, each string can either be a
// address, [scheme://]address:port or a filename. The address part is checked
// and in case of filename a resolv.conf like file is (assumed) and parsed and
// the nameservers found are returned.
func HostPortOrFile(s ...string) ([]string, error) {
	var servers []string
	for _, h := range s {

		trans, host := Transport(h)

		addr, _, err := net.SplitHostPort(host)

		if err != nil {
			// Parse didn't work, it is not a addr:port combo
			hostNoZone := stripZone(host)
			if net.ParseIP(hostNoZone) == nil {
				ss, err := tryFile(host)
				if err == nil {
					servers = append(servers, ss...)
					continue
				}
				return servers, fmt.Errorf("not an IP address or file: %q", host)
			}
			var ss string
			switch trans {
			case transport.DNS:
				ss = net.JoinHostPort(host, transport.Port)
			case transport.TLS:
				ss = transport.TLS + "://" + net.JoinHostPort(host, transport.TLSPort)
			case transport.GRPC:
				ss = transport.GRPC + "://" + net.JoinHostPort(host, transport.GRPCPort)
			case transport.HTTPS:
				ss = transport.HTTPS + "://" + net.JoinHostPort(host, transport.HTTPSPort)
			}
			servers = append(servers, ss)
			continue
		}

		if net.ParseIP(stripZone(addr)) == nil {
			ss, err := tryFile(host)
			if err == nil {
				servers = append(servers, ss...)
				continue
			}
			return servers, fmt.Errorf("not an IP address or file: %q", host)
		}
		servers = append(servers, h)
	}
	if len(servers) == 0 {
		return servers, fmt.Errorf("no nameservers found")
	}
	return servers, nil
}

// Try to open this is a file first.
func tryFile(s string) ([]string, error) {
	c, err := dns.ClientConfigFromFile(s)
	if err == os.ErrNotExist {
		return nil, fmt.Errorf("failed to open file %q: %q", s, err)
	} else if err != nil {
		return nil, err
	}

	servers := []string{}
	for _, s := range c.Servers {
		servers = append(servers, net.JoinHostPort(s, c.Port))
	}
	return servers, nil
}

// HostPort will check if the host part is a valid IP address, if the
// IP address is valid, but no port is found, defaultPort is added.
func HostPort(s, defaultPort string) (string, error) {
	addr, port, err := net.SplitHostPort(s)
	if port == "" {
		port = defaultPort
	}
	if err != nil {
		if net.ParseIP(s) == nil {
			return "", fmt.Errorf("must specify an IP address: `%s'", s)
		}
		return net.JoinHostPort(s, port), nil
	}

	if net.ParseIP(addr) == nil {
		return "", fmt.Errorf("must specify an IP address: `%s'", addr)
	}
	return net.JoinHostPort(addr, port), nil
}
='txt' type='search' size='10' name='q' value=''/>
path: root/examples/docs (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2021-11-19Add Debug tests (#1882)Gravatar Matthew Phillips 4-0/+62
2021-11-19[ci] collect statsGravatar FredKSchott 1-0/+1
2021-11-19Version Packages (next) (#1881)astro@0.21.0-next.8@astrojs/renderer-vue@0.2.0-next.2Gravatar github-actions[bot] 25-23/+42
2021-11-18Improve HMR (#1896)Gravatar Drew Powers 2-10/+18
2021-11-18update depsGravatar Fred K. Schott 5-352/+65
2021-11-18fix #1778Gravatar Fred K. Schott 2-1/+8
2021-11-18Update compiler (#1869)Gravatar Nate Moore 3-11/+6
2021-11-18remove unused remark dependency (#1894)Gravatar Fred K. Schott 2-245/+13
2021-11-18Improve error messages (#1875)Gravatar Drew Powers 39-61/+448
2021-11-18pin astro compiler to older versionGravatar Fred K. Schott 2-5/+5
2021-11-18Update yarn.lock to reflect the state of the package.json files (#1892)Gravatar Jonathan Neal 5-1300/+1000
2021-11-19[ci] yarn formatGravatar FredKSchott 1-5/+3