Skip to content

Commit 56934a6

Browse files
committed
Log fingerprint
1 parent 5d2d50d commit 56934a6

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
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: 28 additions & 9 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) {
@@ -93,11 +111,12 @@ func (downloader *Downloader) DownloadBytes(fromURL string) (data []byte) {
93111

94112
func (downloader *Downloader) newDownload(resourceUrl string) *Download {
95113
return &Download{
96-
url: resourceUrl,
97-
client: downloader.client,
98-
ctx: downloader.ctx,
99-
handler: downloader.handler,
100-
workerId: 0,
114+
url: resourceUrl,
115+
client: downloader.client,
116+
ctx: downloader.ctx,
117+
handler: downloader.handler,
118+
workerId: 0,
119+
downloader: downloader,
101120
}
102121
}
103122

0 commit comments

Comments
 (0)