Refactor some utils.go; add another tool 'tiledump'
This commit is contained in:
@@ -4,17 +4,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"ctfetch/internal/utils"
|
||||
|
||||
"filippo.io/sunlight"
|
||||
"golang.org/x/mod/sumdb/tlog"
|
||||
)
|
||||
@@ -66,7 +63,7 @@ func main() {
|
||||
// Try partial tile first
|
||||
partialURL := logURL + "/" + partialPath
|
||||
fmt.Fprintf(os.Stderr, "Trying: %s\n", partialURL)
|
||||
tileData, err = fetchURL(partialURL)
|
||||
tileData, err = utils.FetchURL(partialURL)
|
||||
if err == nil {
|
||||
fetchedPath = partialPath
|
||||
fmt.Fprintf(os.Stderr, "Successfully fetched partial tile\n")
|
||||
@@ -74,7 +71,7 @@ func main() {
|
||||
// Fall back to full tile
|
||||
fullURL := logURL + "/" + fullPath
|
||||
fmt.Fprintf(os.Stderr, "Partial tile failed, trying: %s\n", fullURL)
|
||||
tileData, err = fetchURL(fullURL)
|
||||
tileData, err = utils.FetchURL(fullURL)
|
||||
if err != nil {
|
||||
fatal("failed to fetch tile: %v", err)
|
||||
}
|
||||
@@ -83,7 +80,7 @@ func main() {
|
||||
}
|
||||
|
||||
// Decompress if needed
|
||||
tileData, err = decompress(tileData)
|
||||
tileData, err = utils.Decompress(tileData)
|
||||
if err != nil {
|
||||
fatal("failed to decompress tile: %v", err)
|
||||
}
|
||||
@@ -93,107 +90,17 @@ func main() {
|
||||
|
||||
if *dumpAll {
|
||||
// Dump all entries in the tile
|
||||
dumpAllEntries(tileData)
|
||||
if err := utils.DumpAllEntries(tileData); err != nil {
|
||||
fatal("%v", err)
|
||||
}
|
||||
} else {
|
||||
// Dump only the specific entry at the position
|
||||
dumpEntryAtPosition(tileData, int(positionInTile), leafIndex)
|
||||
}
|
||||
}
|
||||
|
||||
func fetchURL(url string) ([]byte, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
func dumpAllEntries(tileData []byte) {
|
||||
entryNum := 0
|
||||
for len(tileData) > 0 {
|
||||
e, remaining, err := sunlight.ReadTileLeaf(tileData)
|
||||
if err != nil {
|
||||
fatal("failed to read entry %d: %v", entryNum, err)
|
||||
}
|
||||
tileData = remaining
|
||||
|
||||
dumpEntry(e, entryNum)
|
||||
fmt.Println()
|
||||
entryNum++
|
||||
}
|
||||
|
||||
fmt.Printf("Total entries: %d\n", entryNum)
|
||||
}
|
||||
|
||||
func dumpEntryAtPosition(tileData []byte, position int, expectedIndex int64) {
|
||||
entryNum := 0
|
||||
for len(tileData) > 0 {
|
||||
e, remaining, err := sunlight.ReadTileLeaf(tileData)
|
||||
if err != nil {
|
||||
fatal("failed to read entry %d: %v", entryNum, err)
|
||||
}
|
||||
tileData = remaining
|
||||
|
||||
if entryNum == position {
|
||||
if e.LeafIndex != expectedIndex {
|
||||
fmt.Fprintf(os.Stderr, "WARNING: Expected leaf index %d but found %d at position %d\n",
|
||||
expectedIndex, e.LeafIndex, position)
|
||||
}
|
||||
dumpEntry(e, entryNum)
|
||||
return
|
||||
}
|
||||
entryNum++
|
||||
}
|
||||
|
||||
fatal("position %d not found in tile (only %d entries)", position, entryNum)
|
||||
}
|
||||
|
||||
func dumpEntry(e *sunlight.LogEntry, entryNum int) {
|
||||
fmt.Printf("=== Entry %d ===\n", entryNum)
|
||||
fmt.Printf("Leaf Index: %d\n", e.LeafIndex)
|
||||
fmt.Printf("Timestamp: %d\n", e.Timestamp)
|
||||
fmt.Printf("Is Precert: %v\n", e.IsPrecert)
|
||||
|
||||
if e.IsPrecert {
|
||||
fmt.Printf("Issuer Key Hash: %x\n", e.IssuerKeyHash)
|
||||
}
|
||||
|
||||
fmt.Printf("Certificate: %d bytes\n", len(e.Certificate))
|
||||
if e.PreCertificate != nil {
|
||||
fmt.Printf("PreCertificate: %d bytes\n", len(e.PreCertificate))
|
||||
}
|
||||
|
||||
fmt.Printf("Chain Fingerprints: %d entries\n", len(e.ChainFingerprints))
|
||||
for i, fp := range e.ChainFingerprints {
|
||||
fmt.Printf(" [%d]: %x\n", i, fp)
|
||||
}
|
||||
|
||||
// Try to extract parsed certificate info
|
||||
if trimmed, err := e.TrimmedEntry(); err == nil {
|
||||
if data, err := json.MarshalIndent(trimmed, " ", " "); err == nil {
|
||||
fmt.Printf("Parsed Certificate Info:\n %s\n", data)
|
||||
if err := utils.DumpEntryAtPosition(tileData, int(positionInTile), leafIndex); err != nil {
|
||||
fatal("%v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const maxCompressRatio = 100
|
||||
|
||||
func decompress(data []byte) ([]byte, error) {
|
||||
r, err := gzip.NewReader(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
// Not gzipped, return as-is
|
||||
return data, nil
|
||||
}
|
||||
maxSize := int64(len(data)) * maxCompressRatio
|
||||
return io.ReadAll(io.LimitReader(r, maxSize))
|
||||
}
|
||||
|
||||
func fatal(format string, args ...any) {
|
||||
fmt.Fprintf(os.Stderr, "Error: "+format+"\n", args...)
|
||||
os.Exit(1)
|
||||
|
||||
Reference in New Issue
Block a user