-
Notifications
You must be signed in to change notification settings - Fork 0
/
destinations.go
230 lines (191 loc) · 5.04 KB
/
destinations.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"errors"
"fmt"
"net"
"net/url"
"strconv"
"strings"
"time"
)
/*
This module handles all the business logic of validating destinations by
orchestrating all the various connectivity verification steps that a given
destination might support.
*/
type Destination struct {
Label string
URL string
Protocol string
Scheme string
Username string
Password string
PasswordSet bool
Host string
Port int
Path string
}
func (dest Destination) String() string {
if dest.Label != "" {
return fmt.Sprintf("%s:", dest.Label)
} else {
return fmt.Sprintf("%s:", dest.UrlString())
}
}
func (dest *Destination) UrlString() string {
s := fmt.Sprintf("%s://", dest.Scheme)
// Suppress passwords if one is provided
if dest.Username != "" && dest.PasswordSet {
s += fmt.Sprintf("%s:[...]@", dest.Username)
} else if dest.Username != "" && !dest.PasswordSet {
s += fmt.Sprintf("%s@", dest.Username)
}
s += fmt.Sprintf("%s", dest.Host)
// The port is -1 in the case of icmp:// where it's not relevant
if dest.Port != -1 {
s += fmt.Sprintf(":%d", dest.Port)
}
s += fmt.Sprintf("%s", dest.Path)
if dest.Scheme != dest.Protocol {
s += fmt.Sprintf(" (%s)", dest.Protocol)
}
return s
}
func (dest *Destination) tags() []string {
return []string{
fmt.Sprintf("dest_label:%s", EscapeTag(dest.Label)),
fmt.Sprintf("dest_scheme:%s", EscapeTag(dest.Scheme)),
fmt.Sprintf("dest_host:%s", EscapeTag(dest.Host)),
fmt.Sprintf("dest_port:%d", dest.Port),
fmt.Sprintf("dest_protocol:%s", EscapeTag(dest.Protocol)),
}
}
func (dest *Destination) Increment(metric string, tags []string) {
tags = append(tags, dest.tags()...)
Increment(metric, tags)
}
func (dest *Destination) Timer(metric string, took time.Duration, tags []string) {
tags = append(tags, dest.tags()...)
Timer(metric, took, tags)
}
func NewDestination(u Url) (*Destination, error) {
url, err := url.Parse(u.Url)
if err != nil {
return nil, errors.New(fmt.Sprintf("%v: Failed to parse URL: %v", u, err))
}
// Determine host
host := url.Hostname()
if host == "" {
return nil, errors.New(fmt.Sprintf("%v: Failed to parse a host in URL: %v", u, u.Url))
}
// Determine scheme
scheme := strings.ToLower(url.Scheme)
// Determine port number
port := url.Port()
if port != "" && scheme == "icmp" {
return nil, errors.New(fmt.Sprintf("%s: ICMP cannot be used with a port number: %v", u, u.Url))
}
var portNumber int
if port != "" {
portNumber, err = strconv.Atoi(url.Port())
}
if port == "" || err != nil {
portNumber, err = net.LookupPort("tcp", scheme)
if err != nil {
// Custom ports for schemes unknown to Go can be set here to avoid erroring
// If Go adopts support for one of these, this code won't be reached.
if scheme == "nats" {
portNumber = 4222
} else if scheme == "icmp" {
portNumber = -1
} else {
return nil, errors.New(fmt.Sprintf("%s: Unsupported scheme (try specifying tcp:// or udp:// and an explicit port, or icmp:// for ping-only): %s", u, err))
}
}
}
// Determine protocol
protocol := "tcp"
if scheme == "udp" || scheme == "icmp" {
protocol = scheme
}
username := url.User.Username()
password, passwordSet := url.User.Password()
return &Destination{
Label: u.Label,
URL: u.Url,
Protocol: protocol,
Scheme: scheme,
Username: username,
Password: password,
PasswordSet: passwordSet,
Host: host,
Port: portNumber,
Path: url.Path},
nil
}
func (dest *Destination) Check() bool {
dest.Increment("connectivity.check", []string{})
// Assume the destination is reachable until proven otherwise
reachable := true
dnsResults, err := Lookup(dest)
if err != nil {
LogDestinationError(dest, "Failed to resolve host", err)
reachable = false
}
if reachable {
for _, ip := range dnsResults {
// Check that this isn't an IPv6 result
if !strings.Contains(ip.String(), ":") {
// Check destination IP for routability
route, err := GetRoute(ip)
if err != nil {
LogDestinationError(dest, fmt.Sprintf("Failed to route to %s", ip.String()), err)
}
if dest.Protocol == "icmp" {
reachable = reachable && Ping(route, dest, ip)
} else {
reachable = reachable && Dial(route, dest, ip)
}
}
}
}
if reachable {
if dest.Scheme == "http" || dest.Scheme == "https" {
reachable = reachable && HTTPS(dest)
}
}
if reachable {
dest.Increment("connectivity.check.success", []string{})
} else {
dest.Increment("connectivity.check.error", []string{})
}
return reachable
}
func (dest *Destination) Monitor() {
confidence := 1
for {
reachable := dest.Check()
if reachable {
confidence += 1
if confidence > 10 {
confidence = 10
}
} else {
confidence -= 1
if confidence < 1 {
confidence = 1
}
}
time.Sleep(time.Duration(confidence) * time.Minute)
}
}
func (dest *Destination) WaitFor() {
for {
reachable := dest.Check()
if reachable {
LogDestination(dest, "Connected")
return
}
time.Sleep(15 * time.Second)
}
}