-
Notifications
You must be signed in to change notification settings - Fork 204
/
http.go
125 lines (101 loc) · 2.97 KB
/
http.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
package hoverfly
import (
"crypto/tls"
"crypto/x509"
"errors"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/jackwakefield/gopac"
log "github.com/sirupsen/logrus"
)
func GetDefaultHoverflyHTTPClient(tlsVerification bool, upstreamProxy string) *http.Client {
var proxyURL func(*http.Request) (*url.URL, error)
if upstreamProxy == "" {
proxyURL = http.ProxyURL(nil)
} else {
if upstreamProxy[0:4] != "http" {
upstreamProxy = "http://" + upstreamProxy
}
u, err := url.Parse(upstreamProxy)
if err != nil {
log.Fatalf("Could not parse upstream proxy: %s", err.Error())
}
proxyURL = http.ProxyURL(u)
}
return &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}, Transport: &http.Transport{
Proxy: proxyURL,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !tlsVerification,
Renegotiation: tls.RenegotiateFreelyAsClient,
},
}}
}
func GetHttpClient(hf *Hoverfly, host string) (*http.Client, error) {
if hf.Cfg.PACFile != nil {
parser := new(gopac.Parser)
if err := parser.ParseBytes(hf.Cfg.PACFile); err != nil {
return nil, errors.New("Unable to parse PAC file\n\n" + err.Error())
}
result, err := parser.FindProxy("", host)
if err != nil {
return nil, errors.New("Unable to parse PAC file\n\n" + err.Error())
}
if client := parsePACFileResult(result, hf.Cfg.TLSVerification); client != nil {
return client, nil
}
}
if hf.Cfg.ClientAuthenticationDestination != "" {
re := regexp.MustCompile(hf.Cfg.ClientAuthenticationDestination)
if re.MatchString(host) {
// Load client cert
cert, err := tls.LoadX509KeyPair(
hf.Cfg.ClientAuthenticationClientCert,
hf.Cfg.ClientAuthenticationClientKey,
)
if err != nil {
return nil, errors.New("Unable to load client certs file\n\n" + err.Error())
}
caCertPool := x509.NewCertPool()
var tlsConfig *tls.Config
if hf.Cfg.ClientAuthenticationCACert != "" {
// Load CA cert
caCert, err := ioutil.ReadFile(hf.Cfg.ClientAuthenticationCACert)
if err != nil {
return nil, errors.New("Unable to load ca certs file\n\n" + err.Error())
}
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
} else {
tlsConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
InsecureSkipVerify: true,
}
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: tlsConfig}
client := &http.Client{Transport: transport}
return client, nil
}
}
return hf.HTTP, nil
}
func parsePACFileResult(result string, tlsVerification bool) *http.Client {
for _, s := range strings.Split(result, ";") {
if s == "DIRECT" {
return GetDefaultHoverflyHTTPClient(tlsVerification, "")
}
if s[0:6] == "PROXY " {
return GetDefaultHoverflyHTTPClient(tlsVerification, s[6:])
}
}
return nil
}