Repository navigation
Expand file tree
/
Copy pathvalidator.go
More file actions
273 lines (242 loc) · 6.94 KB
/
Copy pathvalidator.go
File metadata and controls
273 lines (242 loc) · 6.94 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package emailkit
import (
"context"
"fmt"
"sort"
"strings"
"sync"
"time"
"github.com/optimode/emailkit/check"
"github.com/optimode/emailkit/internal/dnscache"
"github.com/optimode/emailkit/internal/parse"
"github.com/optimode/emailkit/internal/smtppool"
"github.com/optimode/emailkit/types"
)
// checker is the internal interface for all validation levels.
// Every check/ package type implements this.
type checker interface {
Check(ctx context.Context, email parse.Email) types.CheckResult
}
// Validator is the main fluent builder struct.
// Instantiate with the New() function.
// When using SMTP validation, call Close() when done to release pooled connections.
type Validator struct {
checkers []checker
err error // configuration error, returned on Validate()
dnsCache *dnscache.Cache
smtpPool *smtppool.Pool
}
// New creates a new Validator. By default it only performs syntax checking.
// Syntax checking always runs and cannot be disabled, because a valid email
// address is a prerequisite for the other levels.
func New() *Validator {
return &Validator{
checkers: []checker{
check.NewSyntaxChecker(),
},
}
}
// WithDNS adds MX lookup validation to the pipeline.
// Optionally overrides the default DNSOptions.
// MX lookup results are cached and shared with the SMTP checker.
func (v *Validator) WithDNS(opts ...DNSOptions) *Validator {
o := defaultDNSOptions()
if len(opts) > 0 {
o = opts[0]
}
v.ensureDNSCache(o.Timeout)
v.checkers = append(v.checkers, check.NewDNSCheckerWithLookup(
check.DNSConfig{
Timeout: o.Timeout,
FallbackToA: o.FallbackToA,
},
v.dnsCache.LookupMX,
))
return v
}
// WithDomain adds domain-level validation (disposable + typo).
func (v *Validator) WithDomain(opts ...DomainOptions) *Validator {
o := defaultDomainOptions()
if len(opts) > 0 {
o = opts[0]
}
v.checkers = append(v.checkers, check.NewDomainChecker(check.DomainConfig{
CheckDisposable: o.CheckDisposable,
CheckTypos: o.CheckTypos,
TypoThreshold: o.TypoThreshold,
AdditionalKnownProviders: o.AdditionalKnownProviders,
}))
return v
}
// WithSMTP adds the SMTP RCPT TO probe to the pipeline.
// SMTPOptions.HeloDomain and MailFrom are required.
// Uses a connection pool for efficient bulk validation (connections reused via RSET).
// Call Close() when done to release pooled connections.
func (v *Validator) WithSMTP(opts SMTPOptions) *Validator {
if opts.HeloDomain == "" || opts.MailFrom == "" {
v.err = ErrInvalidSMTPOptions
return v
}
// Apply defaults for unset values
def := defaultSMTPOptions()
if opts.ConnectTimeout == 0 {
opts.ConnectTimeout = def.ConnectTimeout
}
if opts.CommandTimeout == 0 {
opts.CommandTimeout = def.CommandTimeout
}
if opts.MaxMXHosts == 0 {
opts.MaxMXHosts = def.MaxMXHosts
}
if opts.Port == "" {
opts.Port = def.Port
}
if opts.MaxConnsPerHost == 0 {
opts.MaxConnsPerHost = def.MaxConnsPerHost
}
// Ensure DNS cache exists (SMTP checker shares it for MX lookups)
v.ensureDNSCache(5 * opts.ConnectTimeout)
// Create SMTP connection pool
v.smtpPool = smtppool.New(smtppool.Config{
HeloDomain: opts.HeloDomain,
MailFrom: opts.MailFrom,
ConnectTimeout: opts.ConnectTimeout,
CommandTimeout: opts.CommandTimeout,
Port: opts.Port,
MaxConnsPerHost: opts.MaxConnsPerHost,
})
v.checkers = append(v.checkers, check.NewSMTPChecker(
check.SMTPConfig{
HeloDomain: opts.HeloDomain,
MailFrom: opts.MailFrom,
MaxMXHosts: opts.MaxMXHosts,
},
v.dnsCache,
v.smtpPool,
))
return v
}
// Close releases resources held by the Validator.
// Must be called when using SMTP validation to close pooled connections.
// Safe to call multiple times. No-op if no pooled resources exist.
func (v *Validator) Close() error {
if v.smtpPool != nil {
return v.smtpPool.Close()
}
return nil
}
// ensureDNSCache creates a shared DNS cache if one doesn't exist yet.
func (v *Validator) ensureDNSCache(lookupTimeout time.Duration) {
if v.dnsCache == nil {
v.dnsCache = dnscache.New(lookupTimeout, 5*time.Minute)
}
}
// Validate runs all configured checks on the given email.
// The pipeline short-circuits: if a level fails, subsequent levels are skipped.
// Context can be used for timeout or cancellation.
func (v *Validator) Validate(ctx context.Context, email string) (Result, error) {
if v.err != nil {
return Result{}, v.err
}
parsed := parse.NewEmail(email)
result := Result{Email: email}
for _, c := range v.checkers {
cr := c.Check(ctx, parsed)
result.Checks = append(result.Checks, cr)
if !cr.Passed {
result.Valid = false
return result, nil // short-circuit
}
}
result.Valid = true
return result, nil
}
// ValidateAll runs all checks without short-circuiting.
// Useful when you want to know exactly which levels fail.
func (v *Validator) ValidateAll(ctx context.Context, email string) (Result, error) {
if v.err != nil {
return Result{}, v.err
}
parsed := parse.NewEmail(email)
result := Result{Email: email, Valid: true}
for _, c := range v.checkers {
cr := c.Check(ctx, parsed)
result.Checks = append(result.Checks, cr)
if !cr.Passed {
result.Valid = false
// don't stop, continue
}
}
return result, nil
}
// ConcurrencyOptions configures concurrent processing for ValidateMany.
type ConcurrencyOptions struct {
// Workers is the number of concurrent goroutines. Default: 5
Workers int
}
// ValidateMany validates multiple emails concurrently.
// The result order matches the input slice order.
// Emails are sorted by domain internally for optimal DNS cache and
// SMTP connection pool utilization.
func (v *Validator) ValidateMany(ctx context.Context, emails []string, opts ...ConcurrencyOptions) ([]Result, error) {
if v.err != nil {
return nil, v.err
}
workers := 5
if len(opts) > 0 && opts[0].Workers > 0 {
workers = opts[0].Workers
}
results := make([]Result, len(emails))
type job struct {
idx int
email string
domain string
}
// Build and sort jobs by domain for cache/pool locality
jobSlice := make([]job, len(emails))
for i, e := range emails {
domain := ""
if atIdx := strings.LastIndex(e, "@"); atIdx >= 0 {
domain = strings.ToLower(e[atIdx+1:])
}
jobSlice[i] = job{idx: i, email: e, domain: domain}
}
sort.Slice(jobSlice, func(i, j int) bool {
return jobSlice[i].domain < jobSlice[j].domain
})
// Feed sorted jobs into bounded channel
bufSize := len(emails)
if bufSize > 1000 {
bufSize = 1000
}
jobs := make(chan job, bufSize)
go func() {
for _, j := range jobSlice {
jobs <- j
}
close(jobs)
}()
var wg sync.WaitGroup
var mu sync.Mutex
var firstErr error
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := range jobs {
res, err := v.Validate(ctx, j.email)
if err != nil {
mu.Lock()
if firstErr == nil {
firstErr = fmt.Errorf("validating %q: %w", j.email, err)
}
mu.Unlock()
continue
}
results[j.idx] = res
}
}()
}
wg.Wait()
return results, firstErr
}