@@ -3,6 +3,7 @@ package fetching
3
3
import (
4
4
"context"
5
5
"crypto/rsa"
6
+ "crypto/sha1"
6
7
"crypto/sha256"
7
8
"encoding/hex"
8
9
"fmt"
@@ -31,13 +32,30 @@ const MaxConcurrentDownloads = 5
31
32
// Downloader has helper functions for common use cases of Download, such as writing a resource to a file while downloading it,
32
33
// downloading multiple resources in parallel and verifying the hashsum or signature of downloading resources.
33
34
type Downloader struct {
34
- handler DownloadProgressHandler
35
- client * http.Client
36
- ctx context.Context
35
+ handler DownloadProgressHandler
36
+ client * http.Client
37
+ ctx context.Context
38
+ seenFingerprints map [string ]bool
37
39
}
38
40
39
41
func NewDownloader (ctx context.Context , handler DownloadProgressHandler ) * Downloader {
40
- return & Downloader {handler : handler , client : MakeClient (), ctx : ctx }
42
+ return & Downloader {handler : handler , client : MakeClient (), ctx : ctx , seenFingerprints : make (map [string ]bool )}
43
+ }
44
+
45
+ func (downloader * Downloader ) downloadInitiatedSuccessfully (dl * Download ) {
46
+ if dl .response .TLS == nil {
47
+ return
48
+ }
49
+ if len (dl .response .TLS .PeerCertificates ) == 0 {
50
+ return
51
+ }
52
+ cert := dl .response .TLS .PeerCertificates [0 ]
53
+ sha1Sum := sha1 .Sum (cert .Raw )
54
+ sha1SumHex := hex .EncodeToString (sha1Sum [:])
55
+ if _ , ok := downloader .seenFingerprints [sha1SumHex ]; ! ok {
56
+ downloader .seenFingerprints [sha1SumHex ] = true
57
+ log .Printf ("Seeing new fingerprint %s (sha1) for host %v" , sha1SumHex , dl .request .Host )
58
+ }
41
59
}
42
60
43
61
func (downloader * Downloader ) DownloadSignedResource (fromURL string , keys []* rsa.PublicKey ) ([]byte , error ) {
@@ -74,33 +92,6 @@ func (downloader *Downloader) DownloadSignedResources(urls []string, keys []*rsa
74
92
return validatedResources , nil
75
93
}
76
94
77
- func (downloader * Downloader ) DownloadBytes (fromURL string ) (data []byte ) {
78
- success := false
79
- var err error
80
- for ! success {
81
- dl := downloader .newDownload (fromURL )
82
- data , err = ioutil .ReadAll (dl )
83
- if err != nil {
84
- log .Printf ("Download of \" %s\" failed: %v" , fromURL , err )
85
- }
86
- if downloader .ctx .Err () != nil {
87
- panic (downloader .ctx .Err ())
88
- }
89
- success = err == nil
90
- }
91
- return
92
- }
93
-
94
- func (downloader * Downloader ) newDownload (resourceUrl string ) * Download {
95
- return & Download {
96
- url : resourceUrl ,
97
- client : downloader .client ,
98
- ctx : downloader .ctx ,
99
- handler : downloader .handler ,
100
- workerId : 0 ,
101
- }
102
- }
103
-
104
95
func (downloader * Downloader ) MustDownloadToTempDirectory (baseUrl string , fileMap config.FileInfoMap , localDirPath string ) (tempDirectoryPath string ) {
105
96
reachedEndOfFunction := false // https://stackoverflow.com/a/34851179/10513183
106
97
tempDirectoryPath = system .MustMakeTempDirectory (localDirPath )
@@ -200,6 +191,7 @@ func (downloader *Downloader) runDownloadWorkers(ctx context.Context, cancelFunc
200
191
break
201
192
case workerId := <- availableWorkerIds :
202
193
dl := NewDownloadForConcurrentUse (ctx , url , downloader .client , downloader .handler , workerId )
194
+ dl .downloader = downloader
203
195
go downloadWorker (dl , availableWorkerIds , allWorkersDoneCond , workerErrChan , processDownload )
204
196
}
205
197
}
0 commit comments