Files
ctfetch/cmd/tiledump/main.go
2026-01-11 08:08:46 +01:00

75 lines
2.0 KiB
Go

// Command tiledump reads a CT log tile file and dumps all entries.
// (C) Copyright 2026 Pim van Pelt <pim@ipng.ch>
package main
import (
"fmt"
"os"
"strings"
"ctfetch/internal/utils"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s <tile-file-or-url>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Examples:\n")
fmt.Fprintf(os.Stderr, " %s tile.data\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s https://halloumi2026h1.mon.ct.ipng.ch/tile/data/x002/x460/135\n", os.Args[0])
os.Exit(1)
}
arg := os.Args[1]
var tileData []byte
var err error
// Check if argument is a URL
if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
// Fetch from URL
fmt.Fprintf(os.Stderr, "Fetching: %s\n", arg)
tileData, err = utils.FetchURL(arg)
if err != nil {
// If it's a 404 and the URL is for a partial tile, try the full tile
if err.Error() == "HTTP 404" && strings.Contains(arg, ".p/") {
fullTileURL := arg[:strings.Index(arg, ".p/")]
fmt.Fprintf(os.Stderr, "Partial tile not found, trying full tile: %s\n", fullTileURL)
tileData, err = utils.FetchURL(fullTileURL)
if err != nil {
fatal("failed to fetch full tile: %v", err)
}
fmt.Fprintf(os.Stderr, "Fetched %d bytes from full tile\n", len(tileData))
} else {
fatal("failed to fetch URL: %v", err)
}
} else {
fmt.Fprintf(os.Stderr, "Fetched %d bytes\n", len(tileData))
}
} else {
// Read from file
tileData, err = os.ReadFile(arg)
if err != nil {
fatal("failed to read file: %v", err)
}
fmt.Fprintf(os.Stderr, "Read %d bytes from %s\n", len(tileData), arg)
}
// Decompress if needed
tileData, err = utils.Decompress(tileData)
if err != nil {
fatal("failed to decompress tile: %v", err)
}
fmt.Fprintf(os.Stderr, "Tile size: %d bytes\n\n", len(tileData))
// Dump all entries
if err := utils.DumpAllEntries(tileData); err != nil {
fatal("%v", err)
}
}
func fatal(format string, args ...any) {
fmt.Fprintf(os.Stderr, "Error: "+format+"\n", args...)
os.Exit(1)
}