This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 774
/
transport.go
205 lines (178 loc) · 5.88 KB
/
transport.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package transport
import (
"context"
"crypto/tls"
"io/ioutil"
"net"
"net/http"
"os"
"regexp"
"time"
"github.com/pborman/uuid"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/dragonflyoss/Dragonfly/dfdaemon/downloader"
"github.com/dragonflyoss/Dragonfly/dfdaemon/exception"
)
var (
// layerReg the regex to determine if it is an image download
layerReg = regexp.MustCompile("^.+/blobs/sha256.*$")
)
// DFRoundTripper implements RoundTripper for dfget.
// It uses http.fileTransport to serve requests that need to use dfget,
// and uses http.Transport to serve the other requests.
type DFRoundTripper struct {
Round *http.Transport
Round2 http.RoundTripper
ShouldUseDfget func(req *http.Request) bool
Downloader downloader.Interface
StreamDownloader downloader.Stream
streamMode bool
}
// New returns the default DFRoundTripper.
func New(opts ...Option) (*DFRoundTripper, error) {
rt := &DFRoundTripper{
Round: defaultHTTPTransport(nil),
Round2: http.NewFileTransport(http.Dir("/")),
ShouldUseDfget: NeedUseGetter,
}
for _, opt := range opts {
if err := opt(rt); err != nil {
return nil, errors.Wrap(err, "apply options")
}
}
if rt.StreamDownloader == nil {
return nil, errors.Errorf("nil downloader")
}
return rt, nil
}
// Option is functional config for DFRoundTripper.
type Option func(rt *DFRoundTripper) error
// WithTLS configures TLS config used for http transport.
func WithTLS(cfg *tls.Config) Option {
return func(rt *DFRoundTripper) error {
rt.Round = defaultHTTPTransport(cfg)
return nil
}
}
func defaultHTTPTransport(cfg *tls.Config) *http.Transport {
if cfg == nil {
cfg = &tls.Config{InsecureSkipVerify: true}
}
return &http.Transport{
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: cfg,
}
}
// WithDownloader sets the downloader for the roundTripper.
func WithDownloader(d downloader.Interface) Option {
return func(rt *DFRoundTripper) error {
rt.Downloader = d
return nil
}
}
func WithStreamDownloader(d downloader.Stream) Option {
return func(rt *DFRoundTripper) error {
rt.StreamDownloader = d
return nil
}
}
func WithStreamMode(streamMode bool) Option {
return func(rt *DFRoundTripper) error {
rt.streamMode = streamMode
return nil
}
}
// WithCondition configures how to decide whether to use dfget or not.
func WithCondition(c func(r *http.Request) bool) Option {
return func(rt *DFRoundTripper) error {
rt.ShouldUseDfget = c
return nil
}
}
// RoundTrip only process first redirect at present
// fix resource release
func (roundTripper *DFRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if roundTripper.ShouldUseDfget(req) {
// delete the Accept-Encoding header to avoid returning the same cached
// result for different requests
req.Header.Del("Accept-Encoding")
logrus.Debugf("round trip with dfget: %s", req.URL.String())
if res, err := roundTripper.download(req, req.URL.String()); err == nil || exception.IsAuthError(err) {
return res, err
}
}
logrus.Debugf("round trip directly: %s %s", req.Method, req.URL.String())
req.Host = req.URL.Host
req.Header.Set("Host", req.Host)
res, err := roundTripper.Round.RoundTrip(req)
return res, err
}
// download uses dfget to download.
func (roundTripper *DFRoundTripper) download(req *http.Request, urlString string) (*http.Response, error) {
if roundTripper.streamMode {
return roundTripper.downloadByStream(req.Context(), urlString, req.Header, uuid.New())
}
dstPath, err := roundTripper.downloadByGetter(req.Context(), urlString, req.Header, uuid.New())
if err != nil {
logrus.Errorf("download fail: %v", err)
return nil, err
}
defer os.Remove(dstPath)
fileReq, err := http.NewRequest("GET", "file:///"+dstPath, nil)
if err != nil {
return nil, err
}
response, err := roundTripper.Round2.RoundTrip(fileReq)
if err == nil {
response.Header.Set("Content-Disposition", "attachment; filename="+dstPath)
} else {
logrus.Errorf("read response from file:%s error:%v", dstPath, err)
}
return response, err
}
// downloadByGetter is used to download file by DFGetter.
func (roundTripper *DFRoundTripper) downloadByGetter(ctx context.Context, url string, header map[string][]string, name string) (string, error) {
logrus.Infof("start download url:%s to %s in repo", url, name)
return roundTripper.Downloader.DownloadContext(ctx, url, header, name)
}
func (roundTripper *DFRoundTripper) downloadByStream(ctx context.Context, url string, header map[string][]string, name string) (*http.Response, error) {
logrus.Infof("start download url:%s to %s in repo", url, name)
reader, err := roundTripper.StreamDownloader.DownloadStreamContext(ctx, url, header, name)
if err != nil {
logrus.Errorf("download fail: %v", err)
return nil, err
}
resp := &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(reader),
}
return resp, nil
}
// needUseGetter is the default value for ShouldUseDfget, which downloads all
// images layers with dfget.
func NeedUseGetter(req *http.Request) bool {
return req.Method == http.MethodGet && layerReg.MatchString(req.URL.Path)
}