Skip to content

Commit cea5b9b

Browse files
committed
fix: reconnect after loss of CT connection
1 parent 90d5499 commit cea5b9b

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

internal/certificatetransparency/ct-watcher.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"sync"
1212
"sync/atomic"
13+
"time"
1314

1415
"github.com/d-Rickyy-b/certstream-server-go/internal/certstream"
1516
"github.com/d-Rickyy-b/certstream-server-go/internal/config"
@@ -211,18 +212,38 @@ func (w *worker) startDownloadingCerts(ctx context.Context) {
211212
w.running = true
212213
w.mu.Unlock()
213214

215+
for {
216+
workerErr := w.runWorker(ctx)
217+
if workerErr != nil {
218+
log.Printf("Worker for '%s' failed: %s - sleeping for 5 seconds\n", w.ctURL, workerErr)
219+
log.Printf("Restarting worker for '%s'\n", w.ctURL)
220+
}
221+
222+
time.Sleep(5 * time.Second)
223+
224+
// Check if the context was cancelled
225+
select {
226+
case <-ctx.Done():
227+
log.Printf("Context was cancelled; Stopping worker for '%s'\n", w.ctURL)
228+
return
229+
}
230+
}
231+
}
232+
233+
// runWorker runs a single worker for a single CT log. This method is blocking.
234+
func (w *worker) runWorker(ctx context.Context) error {
214235
userAgent := fmt.Sprintf("Certstream Server v%s (github.com/d-Rickyy-b/certstream-server-go)", config.Version)
215236

216237
jsonClient, e := client.New(w.ctURL, nil, jsonclient.Options{UserAgent: userAgent})
217238
if e != nil {
218239
log.Println("Error creating JSON client: ", e)
219-
return
240+
return e
220241
}
221242

222243
sth, getSTHerr := jsonClient.GetSTH(ctx)
223244
if getSTHerr != nil {
224245
log.Printf("Error retreiving STH for %s: %s\n", w.ctURL, getSTHerr)
225-
return
246+
return getSTHerr
226247
}
227248

228249
certScanner := scanner.NewScanner(jsonClient, scanner.ScannerOptions{
@@ -241,8 +262,10 @@ func (w *worker) startDownloadingCerts(ctx context.Context) {
241262
scanErr := certScanner.Scan(ctx, w.foundCertCallback, w.foundPrecertCallback)
242263
if scanErr != nil {
243264
log.Println("Scan error: ", scanErr)
244-
return
265+
return scanErr
245266
}
267+
268+
return nil
246269
}
247270

248271
// foundCertCallback is the callback that handles cases where new regular certs are found.

0 commit comments

Comments
 (0)