/
createclient.go
53 lines (46 loc) · 1.34 KB
/
createclient.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
package orcclient
import (
"crypto/tls"
"net/http"
"strings"
"github.com/sirupsen/logrus"
canonicalhost "github.com/steinarvk/orclib/module/orc-canonicalhost"
"golang.org/x/net/publicsuffix"
)
func (c *Config) createClient() (*http.Client, error) {
return &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: c.RootCAs,
},
},
Timeout: c.OutboundRequestTimeout,
}, nil
}
func (c *Config) isHostAllowed(host string) bool {
// If there's a restriction to suffixes in place, apply it.
if len(c.AllowOutboundOnlyToSuffixes) > 0 {
for _, suffix := range c.AllowOutboundOnlyToSuffixes {
if strings.HasSuffix(host, suffix) {
return true
}
}
return false
}
// Otherwise, if we have a canonical host, require the same TLD.
if myHost := canonicalhost.CanonicalHost; myHost != "" {
myPublicSuffix, _ := publicsuffix.PublicSuffix(myHost)
wantPublicSuffix, _ := publicsuffix.PublicSuffix(host)
allow := myPublicSuffix == wantPublicSuffix
logrus.WithFields(logrus.Fields{
"requiredSuffix": myPublicSuffix,
"requestedHost": host,
"allow": allow,
}).Warningf("Deciding to allow or deny outbound request based on own canonical host")
return allow
}
logrus.WithFields(logrus.Fields{
"requestedHost": host,
}).Warningf("Allowing outbound request by default")
return true
}