/
probe_config.go
executable file
·74 lines (64 loc) · 1.66 KB
/
probe_config.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
package appclient
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net"
"net/http"
"time"
"github.com/certifi/gocertifi"
"github.com/hashicorp/go-cleanhttp"
"github.com/weaveworks/scope/common/xfer"
)
const (
dialTimeout = 5 * time.Second
)
var certPool *x509.CertPool
func init() {
var err error
certPool, err = gocertifi.CACerts()
if err != nil {
panic(err)
}
}
// ProbeConfig contains all the info needed for a probe to do HTTP requests
type ProbeConfig struct {
BasicAuth bool
Token string
ProbeVersion string
ProbeID string
Insecure bool
}
func (pc ProbeConfig) authorizeHeaders(headers http.Header) {
if pc.BasicAuth {
headers.Set("Authorization", fmt.Sprintf("Basic %s", pc.Token))
} else {
headers.Set("Authorization", fmt.Sprintf("Scope-Probe token=%s", pc.Token))
}
headers.Set(xfer.ScopeProbeIDHeader, pc.ProbeID)
headers.Set(xfer.ScopeProbeVersionHeader, pc.ProbeVersion)
}
func (pc ProbeConfig) authorizedRequest(method string, urlStr string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, urlStr, body)
if err == nil {
pc.authorizeHeaders(req.Header)
}
return req, err
}
func (pc ProbeConfig) getHTTPTransport(hostname string) *http.Transport {
transport := cleanhttp.DefaultTransport()
transport.DialContext = (&net.Dialer{
Timeout: dialTimeout,
KeepAlive: 30 * time.Second,
}).DialContext
if pc.Insecure {
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
} else {
transport.TLSClientConfig = &tls.Config{
RootCAs: certPool,
ServerName: hostname,
}
}
return transport
}