From 2144fa6c86cb6407ebabcf4128292f465c949598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Sun, 1 Nov 2026 08:14:58 +0000 Subject: [PATCH 1/3] feat: add batch mode for multiple URLs via --list (#4) Process a list of URLs from a file: dit run --list urls.txt Reads one URL per line (blank lines and "#" comments are skipped), loads the model once, classifies every page, and streams one JSON result per line to stdout. Fetch or classification errors are reported per URL in the output stream without aborting the batch. Works with --proba, --render, and --model. --- README.md | 4 ++ internal/cli/run.go | 107 +++++++++++++++++++++++++++++++++++++++ internal/cli/run_test.go | 38 ++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 internal/cli/run_test.go diff --git a/README.md b/README.md index 65d8a0b..019e49e 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ dit run https://github.com/login # Classify forms in a local file dit run login.html +# Batch mode: classify URLs listed in a file (one per line), +# streaming one JSON result per line +dit run --list urls.txt + # With probabilities dit run https://github.com/login --proba diff --git a/internal/cli/run.go b/internal/cli/run.go index a2590c4..87c7895 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -1,6 +1,7 @@ package cli import ( + "bufio" "context" "encoding/json" "fmt" @@ -22,6 +23,7 @@ func (c *CLI) newRunCommand() *cobra.Command { var proba bool var render bool var renderTimeout int + var list string cmd := &cobra.Command{ Use: "run [url-or-file]", @@ -42,6 +44,9 @@ func (c *CLI) newRunCommand() *cobra.Command { # Pipe HTML content from a URL using curl curl -s https://github.com/login | dit run + # Batch mode: classify URLs listed in a file (one per line) + dit run --list urls.txt + # Show probability scores dit run https://github.com/login --proba @@ -68,6 +73,16 @@ func (c *CLI) newRunCommand() *cobra.Command { timeout: time.Duration(renderTimeout) * time.Second, } + if list != "" { + if len(args) > 0 { + return fmt.Errorf("cannot combine --list with a positional argument") + } + if fetchOpts.render && renderTimeout <= 0 { + return fmt.Errorf("--timeout must be a positive integer") + } + return runBatch(list, modelPath, fetchOpts, proba, threshold) + } + if len(args) == 0 { if isStdinTerminal() { return cmd.Help() @@ -145,9 +160,101 @@ func (c *CLI) newRunCommand() *cobra.Command { cmd.Flags().BoolVar(&proba, "proba", false, "Show probabilities") cmd.Flags().BoolVar(&render, "render", false, "Render JavaScript-driven pages in a headless browser") cmd.Flags().IntVar(&renderTimeout, "timeout", 30, "Render browser timeout in seconds") + cmd.Flags().StringVar(&list, "list", "", "File with URLs to classify, one per line (batch mode)") return cmd } +// batchResult is one line of batch output, streamed as newline-delimited JSON. +type batchResult struct { + URL string `json:"url"` + Page any `json:"page,omitempty"` + Forms any `json:"forms,omitempty"` + Error string `json:"error,omitempty"` +} + +// runBatch classifies every URL in the list file and streams one JSON result +// per line to stdout. Fetch and classification errors are reported per URL +// without stopping the batch. +func runBatch(listPath, modelPath string, opts fetchOptions, proba bool, threshold float64) error { + f, err := os.Open(listPath) + if err != nil { + return fmt.Errorf("open list file: %w", err) + } + defer func() { _ = f.Close() }() + + urls, err := readURLList(f) + if err != nil { + return fmt.Errorf("read list file: %w", err) + } + if len(urls) == 0 { + return fmt.Errorf("no URLs found in %s", listPath) + } + + start := time.Now() + cl, err := loadModel(modelPath) + if err != nil { + return err + } + slog.Debug("Model loaded", "duration", time.Since(start)) + + enc := json.NewEncoder(os.Stdout) + for _, target := range urls { + res := batchResult{URL: target} + slog.Debug("Fetching HTML", "target", target, "render", opts.render) + htmlContent, err := fetchHTML(target, opts) + if err != nil { + res.Error = err.Error() + } else if res.Page, res.Forms, err = classifyTarget(cl, htmlContent, proba, threshold); err != nil { + res.Error = err.Error() + } + if err := enc.Encode(res); err != nil { + return fmt.Errorf("write result: %w", err) + } + } + return nil +} + +// readURLList reads one URL per line from r, skipping blank lines and lines +// starting with "#". +func readURLList(r io.Reader) ([]string, error) { + var urls []string + scanner := bufio.NewScanner(r) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if line == "" || strings.HasPrefix(line, "#") { + continue + } + urls = append(urls, line) + } + if err := scanner.Err(); err != nil { + return nil, err + } + return urls, nil +} + +// classifyTarget classifies HTML content, returning the page result or, when +// the page model is unavailable, the form results. +func classifyTarget(cl *dit.Classifier, htmlContent string, proba bool, threshold float64) (page, forms any, err error) { + if proba { + if pageResult, pageErr := cl.ExtractPageTypeProba(htmlContent, threshold); pageErr == nil { + return pageResult, nil, nil + } + results, err := cl.ExtractFormsProba(htmlContent, threshold) + if err != nil { + return nil, nil, err + } + return nil, results, nil + } + if pageResult, pageErr := cl.ExtractPageType(htmlContent); pageErr == nil { + return pageResult, nil, nil + } + results, err := cl.ExtractForms(htmlContent) + if err != nil { + return nil, nil, err + } + return nil, results, nil +} + func isStdinTerminal() bool { fi, err := os.Stdin.Stat() if err != nil { diff --git a/internal/cli/run_test.go b/internal/cli/run_test.go new file mode 100644 index 0000000..6854b80 --- /dev/null +++ b/internal/cli/run_test.go @@ -0,0 +1,38 @@ +package cli + +import ( + "reflect" + "strings" + "testing" +) + +func TestReadURLList(t *testing.T) { + input := `https://example.com/login + +# a comment + https://example.org/signup +https://example.net/search +` + urls, err := readURLList(strings.NewReader(input)) + if err != nil { + t.Fatalf("readURLList: %v", err) + } + want := []string{ + "https://example.com/login", + "https://example.org/signup", + "https://example.net/search", + } + if !reflect.DeepEqual(urls, want) { + t.Errorf("expected %v, got %v", want, urls) + } +} + +func TestReadURLListEmpty(t *testing.T) { + urls, err := readURLList(strings.NewReader("\n# only comments\n\n")) + if err != nil { + t.Fatalf("readURLList: %v", err) + } + if len(urls) != 0 { + t.Errorf("expected no URLs, got %v", urls) + } +} From b4a80df6ab33cfab62c4924c33932923629dbab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Sun, 1 Nov 2026 09:16:43 +0000 Subject: [PATCH 2/3] fix: bump toolchain to go1.26.10 for GO-2026-6809 Clears the repo-wide Govulncheck CI failure; identical to the bump in the sibling PRs, merges cleanly in any order. --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index f4f0bbf..6fa668b 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/happyhackingspace/dit go 1.26.0 -toolchain go1.26.9 +toolchain go1.26.10 require ( github.com/PuerkitoBio/goquery v1.13.0 From 3b5c31308e5beb6d872f05c3d9e572279cae1da1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Sun, 1 Nov 2026 10:08:07 +0000 Subject: [PATCH 3/3] fix: bump x/net to v0.62.0 (GO-2026-6866, GO-2026-6860) Same security bump as the dedicated fix PR; identical changes merge cleanly in any order. --- go.mod | 6 +++--- go.sum | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 6fa668b..f040291 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/chromedp/chromedp v0.16.0 github.com/creativeprojects/go-selfupdate v1.6.0 github.com/spf13/cobra v1.10.2 - golang.org/x/net v0.59.0 + golang.org/x/net v0.62.0 ) require ( @@ -34,9 +34,9 @@ require ( github.com/spf13/pflag v1.0.9 // indirect github.com/ulikunitz/xz v0.5.15 // indirect gitlab.com/gitlab-org/api/client-go v1.46.0 // indirect - golang.org/x/crypto v0.57.0 // indirect + golang.org/x/crypto v0.58.0 // indirect golang.org/x/oauth2 v0.36.0 // indirect - golang.org/x/sys v0.48.0 // indirect + golang.org/x/sys v0.49.0 // indirect golang.org/x/time v0.15.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 321555e..1b6a7f9 100644 --- a/go.sum +++ b/go.sum @@ -77,23 +77,23 @@ go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= -golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= -golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= +golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= +golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues= -golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY= +golang.org/x/net v0.62.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI= golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs= golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= -golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= +golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE= -golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc= +golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM= +golang.org/x/term v0.47.0/go.mod h1:efAfAuG6RKABvg1ArGebRkvX6LXul14SM67o2e4mUCY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=