compiler.go RawHistoryFolder
1package filtering
2
3import (
4 "bufio"
5 "context"
6 "fmt"
7 "strings"
8 "time"
9
10 "github.com/bnema/dumber/internal/filtering/converter"
11 "github.com/bnema/dumber/internal/logging"
12)
13
14// DefaultFilterCompiler implements FilterCompiler using the converter package
15type DefaultFilterCompiler struct{}
16
17// NewDefaultFilterCompiler creates a new default filter compiler
18func NewDefaultFilterCompiler() *DefaultFilterCompiler {
19 return &DefaultFilterCompiler{}
20}
21
22// CompileFromSources downloads and compiles filters from multiple URLs
23func (dfc *DefaultFilterCompiler) CompileFromSources(ctx context.Context, sources []string) (*CompiledFilters, error) {
24 compiled := NewCompiledFilters()
25
26 for _, url := range sources {
27 select {
28 case <-ctx.Done():
29 return nil, ctx.Err()
30 default:
31 }
32
33 filters, err := dfc.downloadAndCompile(ctx, url)
34 if err != nil {
35 logging.Error(fmt.Sprintf("Failed to compile from %s: %v", url, err))
36 continue
37 }
38
39 compiled.Merge(filters)
40 }
41
42 compiled.Version = fmt.Sprintf("compiled-%d", time.Now().Unix())
43 compiled.CompiledAt = time.Now()
44
45 return compiled, nil
46}
47
48// CompileFromData compiles filters from raw data bytes
49func (dfc *DefaultFilterCompiler) CompileFromData(data []byte) (*CompiledFilters, error) {
50 compiled := NewCompiledFilters()
51
52 // Use local converter for thread safety (avoid shared state)
53 conv := converter.NewFilterConverter()
54
55 // Parse line by line
56 scanner := bufio.NewScanner(strings.NewReader(string(data)))
57 lineCount := 0
58
59 for scanner.Scan() {
60 line := scanner.Text()
61 lineCount++
62
63 // Convert each line
64 if err := conv.ConvertEasyListLine(line); err != nil {
65 logging.Debug(fmt.Sprintf("Failed to convert filter line: %s, error: %v", line, err))
66 continue
67 }
68 }
69
70 if err := scanner.Err(); err != nil {
71 return nil, fmt.Errorf("error reading filter data: %w", err)
72 }
73
74 // Get compiled rules from converter
75 compiled.NetworkRules = conv.GetNetworkRules()
76 compiled.CosmeticRules = conv.GetCosmeticRules()
77 compiled.GenericHiding = conv.GetGenericHiding()
78 compiled.CompiledAt = time.Now()
79 compiled.Version = fmt.Sprintf("data-compile-%d", time.Now().Unix())
80
81 logging.Info(fmt.Sprintf("Compiled %d filter lines into %d network rules and %d cosmetic rules",
82 lineCount, len(compiled.NetworkRules), len(compiled.CosmeticRules)))
83
84 return compiled, nil
85}
86
87// downloadAndCompile downloads and compiles a single filter list
88func (dfc *DefaultFilterCompiler) downloadAndCompile(ctx context.Context, url string) (*CompiledFilters, error) {
89 // This would implement HTTP download - for now, return empty filters
90 logging.Debug(fmt.Sprintf("Would download and compile from %s", url))
91
92 compiled := NewCompiledFilters()
93 compiled.Version = fmt.Sprintf("stub-%d", time.Now().Unix())
94 compiled.CompiledAt = time.Now()
95
96 return compiled, nil
97}