-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient.go
104 lines (87 loc) · 1.85 KB
/
client.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
package s3
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"os"
"strings"
"golang.org/x/net/proxy"
)
type Client struct {
AccessKeyID string
SecretAccessKey string
Token string
Region string
Bucket string
Domain string
Protocol string
SOCKS5Proxy string
SignatureVersion int
CACertificates []string
SkipSystemCAs bool
InsecureSkipVerify bool
UsePathBuckets bool
ua *http.Client
trace bool
traceTo io.Writer
traceBody bool
}
func NewClient(c *Client) (*Client, error) {
var (
roots *x509.CertPool
err error
)
if c.SignatureVersion == 0 {
c.SignatureVersion = 4
}
if trace := os.Getenv("S3_TRACE"); trace != "" {
switch strings.ToLower(trace) {
case "yes", "y", "1":
c.Trace(os.Stderr, true, true)
case "headers", "header":
c.Trace(os.Stderr, true, false)
default:
c.Trace(os.Stderr, false, false)
}
}
if !c.SkipSystemCAs {
roots, err = x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("unable to retrieve system root certificate authorities: %s", err)
}
} else {
roots = x509.NewCertPool()
}
for _, ca := range c.CACertificates {
if ok := roots.AppendCertsFromPEM([]byte(ca)); !ok {
return nil, fmt.Errorf("unable to append CA certificate")
}
}
dial := http.DefaultTransport.(*http.Transport).Dial
if c.SOCKS5Proxy != "" {
dialer, err := proxy.SOCKS5("tcp", c.SOCKS5Proxy, nil, proxy.Direct)
if err != nil {
return nil, err
}
dial = dialer.Dial
}
c.ua = &http.Client{
Transport: &http.Transport{
Dial: dial,
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
RootCAs: roots,
InsecureSkipVerify: c.InsecureSkipVerify,
},
},
}
return c, nil
}
func (c *Client) domain() string {
if c.Domain == "" {
return "s3.amazonaws.com"
}
return c.Domain
}