-
Notifications
You must be signed in to change notification settings - Fork 76
/
httpconfig.go
77 lines (71 loc) · 2.39 KB
/
httpconfig.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
package httpconfig
import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
ld "gopkg.in/launchdarkly/go-server-sdk.v4"
"gopkg.in/launchdarkly/go-server-sdk.v4/ldhttp"
"gopkg.in/launchdarkly/go-server-sdk.v4/ldntlm"
"gopkg.in/launchdarkly/ld-relay.v5/logging"
)
// ProxyConfig represents all the supported proxy options. This is used in the Config struct in relay.go.
type ProxyConfig struct {
Url string
NtlmAuth bool
User string
Password string
Domain string
CaCertFiles string
}
// HTTPConfig encapsulates ProxyConfig plus any other HTTP options we may support in the future (currently none).
type HTTPConfig struct {
ProxyConfig
ProxyURL *url.URL
HTTPClientFactory ld.HTTPClientFactory
}
// NewHTTPConfig validates all of the HTTP-related options and returns an HTTPConfig if successful.
func NewHTTPConfig(proxyConfig ProxyConfig) (HTTPConfig, error) {
ret := HTTPConfig{ProxyConfig: proxyConfig}
if proxyConfig.Url == "" && proxyConfig.NtlmAuth {
return ret, errors.New("Cannot specify proxy authentication without a proxy URL")
}
if proxyConfig.Url != "" {
u, err := url.Parse(proxyConfig.Url)
if err != nil {
return ret, fmt.Errorf("Invalid proxy URL: %s", proxyConfig.Url)
}
logging.GlobalLoggers.Infof("Using proxy server at %s", proxyConfig.Url)
ret.ProxyURL = u
}
var transportOpts []ldhttp.TransportOption
for _, filePath := range strings.Split(strings.TrimSpace(proxyConfig.CaCertFiles), ",") {
if filePath != "" {
transportOpts = append(transportOpts, ldhttp.CACertFileOption(filePath))
}
}
if proxyConfig.NtlmAuth {
if proxyConfig.User == "" || proxyConfig.Password == "" {
return ret, errors.New("NTLM proxy authentication requires username and password")
}
var err error
ret.HTTPClientFactory, err = ldntlm.NewNTLMProxyHTTPClientFactory(proxyConfig.Url,
proxyConfig.User, proxyConfig.Password, proxyConfig.Domain, transportOpts...)
if err != nil {
return ret, err
}
logging.GlobalLoggers.Info("NTLM proxy authentication enabled")
} else {
if ret.ProxyURL != nil {
transportOpts = append(transportOpts, ldhttp.ProxyOption(*ret.ProxyURL))
}
ret.HTTPClientFactory = ld.NewHTTPClientFactory(transportOpts...)
}
return ret, nil
}
// Client creates a new HTTP client instance that isn't for SDK use.
func (c HTTPConfig) Client() *http.Client {
client := c.HTTPClientFactory(ld.DefaultConfig)
return &client
}