This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
/
transport.go
167 lines (146 loc) · 4.85 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
/*
* 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 (
"crypto/tls"
"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
}
// New return 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.Downloader == 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
}
}
// 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.IsNotAuth(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) {
dstPath, err := roundTripper.downloadByGetter(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 to download file by DFGetter
func (roundTripper *DFRoundTripper) downloadByGetter(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.Download(url, header, name)
}
// 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)
}