Skip to content

Commit e24d992

Browse files
authored
Merge pull request #242 from setlog/feature-log-fingerprint
Log fingerprint
2 parents 5d2d50d + 893a08d commit e24d992

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## TBD (TBD)
44
### Changes
5+
* Every TLS Certificate fingerprint will be logged once with the host name it has first been seen on.
56
* DWARF symbols are now stripped from the trivrost binary to reduce file size. This can save a few bytes on some platforms.
67
* The binary is now compressed with UPX when using `make compress`. Reduces the final filesize to less than 50%.
78
* Shorter log-output for proxy detection. Reduces average size of the log output by 5–15%.

pkg/fetching/download.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ type Download struct {
8080

8181
response *http.Response
8282
responseReader io.Reader
83+
84+
// Communicate some TLS information to the downloader which is managing this download.
85+
downloader *Downloader
8386
}
8487

8588
func NewDownload(ctx context.Context, resourceUrl string) *Download {
@@ -139,7 +142,7 @@ func (dl *Download) Close() error {
139142
func (dl *Download) readDownload(p []byte) (bytesReadCount int, err error) {
140143
if dl.response == nil {
141144
dl.request, dl.cancelRequest = dl.createRequest()
142-
dl.response = dl.sendRequest(dl.request)
145+
dl.sendRequest(dl.request)
143146
if dl.response == nil {
144147
return 0, nil
145148
}
@@ -159,18 +162,22 @@ func (dl *Download) createRequest() (*http.Request, context.CancelFunc) {
159162
return newRangeRequestWithCancel(dl.ctx, dl.url, dl.firstByteIndex, dl.lastByteIndex)
160163
}
161164

162-
func (dl *Download) sendRequest(req *http.Request) *http.Response {
165+
func (dl *Download) sendRequest(req *http.Request) {
163166
resp, err := DoForClientFunc(dl.client, req)
164167
if err != nil {
165168
dl.cleanUp()
166169
dl.handler.HandleHttpGetError(dl.url, err)
170+
dl.response = nil
167171
dl.inscribeCooldown()
168172
} else {
173+
dl.response = resp
174+
if dl.downloader != nil {
175+
dl.downloader.downloadInitiatedSuccessfully(dl)
176+
}
169177
counter := &writeCounter{counted: uint64(dl.firstByteIndex), url: dl.url, workerId: dl.workerId, handler: dl.handler}
170178
timeoutingBodyReader := &TimeoutingReader{Reader: resp.Body, Timeout: defaultTimeout * 30}
171179
dl.responseReader = io.TeeReader(timeoutingBodyReader, counter)
172180
}
173-
return resp
174181
}
175182

176183
func (dl *Download) processResponse() {

pkg/fetching/downloader.go

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fetching
33
import (
44
"context"
55
"crypto/rsa"
6+
"crypto/sha1"
67
"crypto/sha256"
78
"encoding/hex"
89
"fmt"
@@ -31,13 +32,30 @@ const MaxConcurrentDownloads = 5
3132
// Downloader has helper functions for common use cases of Download, such as writing a resource to a file while downloading it,
3233
// downloading multiple resources in parallel and verifying the hashsum or signature of downloading resources.
3334
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
3739
}
3840

3941
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+
}
4159
}
4260

4361
func (downloader *Downloader) DownloadSignedResource(fromURL string, keys []*rsa.PublicKey) ([]byte, error) {
@@ -74,33 +92,6 @@ func (downloader *Downloader) DownloadSignedResources(urls []string, keys []*rsa
7492
return validatedResources, nil
7593
}
7694

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-
10495
func (downloader *Downloader) MustDownloadToTempDirectory(baseUrl string, fileMap config.FileInfoMap, localDirPath string) (tempDirectoryPath string) {
10596
reachedEndOfFunction := false // https://stackoverflow.com/a/34851179/10513183
10697
tempDirectoryPath = system.MustMakeTempDirectory(localDirPath)
@@ -200,6 +191,7 @@ func (downloader *Downloader) runDownloadWorkers(ctx context.Context, cancelFunc
200191
break
201192
case workerId := <-availableWorkerIds:
202193
dl := NewDownloadForConcurrentUse(ctx, url, downloader.client, downloader.handler, workerId)
194+
dl.downloader = downloader
203195
go downloadWorker(dl, availableWorkerIds, allWorkersDoneCond, workerErrChan, processDownload)
204196
}
205197
}

0 commit comments

Comments
 (0)