-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
186 lines (155 loc) · 4.75 KB
/
options.go
File metadata and controls
186 lines (155 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package inspect
import (
"log/slog"
"net/http"
"time"
)
// Option configures a scan operation.
type Option interface {
apply(*config)
}
type optFunc func(*config)
func (f optFunc) apply(c *config) { f(c) }
type config struct {
depth int
checks []string
exclude []string
concurrency int
timeout time.Duration
pageTimeout time.Duration
rateLimit int
authHeader string
authValue string
failOn Severity
userAgent string
followRedirects int
respectRobots bool
logger *slog.Logger
cookieJar http.CookieJar
acceptedStatusCodes []int
browser BrowserEngine
blockPrivateIPs bool
}
func defaultConfig() *config {
return &config{
depth: 5,
checks: []string{"links", "security", "forms", "a11y", "perf", "seo"},
concurrency: 10,
timeout: 60 * time.Second,
pageTimeout: 15 * time.Second,
rateLimit: 20,
userAgent: "inspect/1.0",
followRedirects: 5,
respectRobots: true,
failOn: SeverityCritical,
}
}
func buildConfig(opts []Option) *config {
cfg := defaultConfig()
for _, o := range opts {
o.apply(cfg)
}
return cfg
}
// Presets
// Quick performs a shallow crawl checking only broken links.
var Quick Option = optFunc(func(c *config) {
c.depth = 2
c.checks = []string{"links"}
c.concurrency = 5
})
// Standard performs a balanced crawl with all checks enabled.
var Standard Option = optFunc(func(c *config) {
c.depth = 5
c.checks = []string{"links", "security", "forms", "a11y", "perf", "seo"}
c.concurrency = 10
})
// Deep performs an exhaustive crawl with no depth limit.
var Deep Option = optFunc(func(c *config) {
c.depth = 0
c.checks = []string{"links", "security", "forms", "a11y", "perf", "seo"}
c.concurrency = 20
})
// Security limits the scan to security-related checks.
var SecurityOnly Option = optFunc(func(c *config) {
c.checks = []string{"security"}
})
// CI configures for continuous integration: standard checks, fail on high, JSON output.
var CI Option = optFunc(func(c *config) {
c.depth = 5
c.checks = []string{"links", "security", "forms", "a11y", "perf", "seo"}
c.concurrency = 10
c.failOn = SeverityHigh
})
// Configuration functions
func WithDepth(n int) Option {
return optFunc(func(c *config) { c.depth = n })
}
func WithChecks(checks ...string) Option {
return optFunc(func(c *config) { c.checks = checks })
}
func WithExclude(patterns ...string) Option {
return optFunc(func(c *config) { c.exclude = patterns })
}
func WithConcurrency(n int) Option {
return optFunc(func(c *config) {
if n > 0 {
c.concurrency = n
}
})
}
func WithTimeout(d time.Duration) Option {
return optFunc(func(c *config) { c.timeout = d })
}
func WithRateLimit(reqPerSec int) Option {
return optFunc(func(c *config) {
if reqPerSec > 0 {
c.rateLimit = reqPerSec
}
})
}
func WithAuth(header, value string) Option {
return optFunc(func(c *config) {
c.authHeader = header
c.authValue = value
})
}
func WithFailOn(sev Severity) Option {
return optFunc(func(c *config) { c.failOn = sev })
}
func WithUserAgent(ua string) Option {
return optFunc(func(c *config) { c.userAgent = ua })
}
func WithFollowRedirects(max int) Option {
return optFunc(func(c *config) { c.followRedirects = max })
}
func WithRespectRobots(enabled bool) Option {
return optFunc(func(c *config) { c.respectRobots = enabled })
}
func WithPageTimeout(d time.Duration) Option {
return optFunc(func(c *config) { c.pageTimeout = d })
}
func WithLogger(l *slog.Logger) Option {
return optFunc(func(c *config) { c.logger = l })
}
func WithCookieJar(jar http.CookieJar) Option {
return optFunc(func(c *config) { c.cookieJar = jar })
}
// WithAcceptedStatusCodes sets the HTTP status codes considered acceptable by the link checker.
// By default (when no codes are specified), status codes 200-399 are accepted.
func WithAcceptedStatusCodes(codes ...int) Option {
return optFunc(func(c *config) { c.acceptedStatusCodes = codes })
}
// WithBrowser sets a BrowserEngine for browser-rendered page analysis.
// When set, the scanner uses the engine to render pages with full JavaScript
// execution instead of relying solely on raw HTTP fetches for HTML analysis.
// The browser sub-module (github.com/GrayCodeAI/inspect/browser) provides
// a rod-based implementation.
func WithBrowser(engine BrowserEngine) Option {
return optFunc(func(c *config) { c.browser = engine })
}
// WithBlockPrivateIPs enables SSRF protection that blocks requests to private IP ranges.
// By default, private IPs are allowed since inspect is typically used to scan your own servers.
func WithBlockPrivateIPs() Option {
return optFunc(func(c *config) { c.blockPrivateIPs = true })
}