@@ -104,13 +104,11 @@ func (r *SPF2IPResolver) processDomain(ctx context.Context, domain string, depth
104
104
currentDomainIPs := make (map [string ]struct {})
105
105
106
106
spfString , err := r .getSPFRecord (ctx , domain )
107
- if err != nil {
108
- if ! errors .Is (err , errIgnorableDNSErr ) {
109
- log .Printf ("Warning: Failed to get SPF record for %s: %v" , domain , err )
110
- r .resolvedIPsCache [domain ] = nil
107
+ if err != nil && ! errors .Is (err , errIgnorableDNSErr ) {
108
+ log .Printf ("Warning: Failed to get SPF record for %s: %v" , domain , err )
109
+ r .resolvedIPsCache [domain ] = nil
111
110
112
- return nil , err
113
- }
111
+ return nil , err
114
112
}
115
113
116
114
if spfString == "" {
@@ -209,37 +207,37 @@ func (r *SPF2IPResolver) processDomain(ctx context.Context, domain string, depth
209
207
}
210
208
211
209
case "include" :
212
- if value != "" {
213
- includedIPs , includeErr := r .processDomain (ctx , value , depth + 1 )
214
- if includeErr != nil {
215
- return nil , fmt .Errorf ("include failed for %s (directive in %s): %w" , value , domain , includeErr )
216
- } else {
217
- for ip := range includedIPs {
218
- currentDomainIPs [ip ] = struct {}{}
219
- }
220
- }
221
- } else {
210
+ if value == "" {
222
211
log .Printf ("Warning: 'include' modifier without domain in %s" , domain )
223
212
r .resolvedIPsCache [domain ] = nil
224
213
225
214
return nil , fmt .Errorf ("include without domain in %s" , domain )
226
215
}
227
216
228
- case "redirect" :
229
- if value != "" {
230
- r .debugLogPrintf ("Debug: Redirecting from %s to %s. Discarding IPs found so far for %s." , domain , value , domain )
217
+ includedIPs , includeErr := r .processDomain (ctx , value , depth + 1 )
218
+ if includeErr != nil {
219
+ return nil , fmt .Errorf ("include failed for %s (directive in %s): %w" , value , domain , includeErr )
220
+ }
221
+
222
+ for ip := range includedIPs {
223
+ currentDomainIPs [ip ] = struct {}{}
224
+ }
231
225
232
- // The result of this domain's processing is now entirely determined by the redirect target.
233
- redirectedIPs , redirectErr := r .processDomain (ctx , value , depth + 1 )
234
- r .resolvedIPsCache [domain ] = deepCopyMap (redirectedIPs ) // Overwrite cache with redirected IPs
226
+ case "redirect" :
227
+ if value == "" {
228
+ log .Printf ("Warning: 'redirect' modifier without domain in %s" , domain )
229
+ r .resolvedIPsCache [domain ] = nil
235
230
236
- return redirectedIPs , redirectErr
231
+ return nil , fmt . Errorf ( "redirect without domain in %s" , domain )
237
232
}
238
233
239
- log .Printf ("Warning: 'redirect' modifier without domain in %s" , domain )
240
- r .resolvedIPsCache [domain ] = nil
234
+ r .debugLogPrintf ("Debug: Redirecting from %s to %s. Discarding IPs found so far for %s." , domain , value , domain )
235
+
236
+ // The result of this domain's processing is now entirely determined by the redirect target.
237
+ redirectedIPs , redirectErr := r .processDomain (ctx , value , depth + 1 )
238
+ r .resolvedIPsCache [domain ] = deepCopyMap (redirectedIPs ) // Overwrite cache with redirected IPs
241
239
242
- return nil , fmt . Errorf ( "redirect without domain in %s" , domain )
240
+ return redirectedIPs , redirectErr
243
241
244
242
case "exists" , "ptr" , "all" :
245
243
// These mechanisms don't directly define IPs in the same way, ignore for IP extraction.
@@ -255,6 +253,7 @@ func (r *SPF2IPResolver) processDomain(ctx context.Context, domain string, depth
255
253
return currentDomainIPs , nil
256
254
}
257
255
256
+ // lookupIPNetwork returns the appropriate network type for IP lookups based on the resolver's IP version.
258
257
func (r * SPF2IPResolver ) lookupIPNetwork () string {
259
258
switch r .ipVersion {
260
259
case ipv4 :
@@ -266,6 +265,7 @@ func (r *SPF2IPResolver) lookupIPNetwork() string {
266
265
}
267
266
}
268
267
268
+ // parseSPFMechanismTargetAndMask extracts the target host and mask suffix from an SPF mechanism value.
269
269
func parseSPFMechanismTargetAndMask (defaultDomain , mechanismValue string ) (targetHost , maskSuffix string ) {
270
270
targetHost = defaultDomain
271
271
maskSuffix = ""
@@ -353,7 +353,8 @@ func (r *SPF2IPResolver) addIPOrCIDRToSet(value string, targetSet map[string]str
353
353
return fmt .Errorf ("value '%s' is not a valid IP address or CIDR block" , value )
354
354
}
355
355
356
- func (r * SPF2IPResolver ) debugLogPrintf (format string , args ... interface {}) {
356
+ // debugLogPrintf logs debug messages if debug logging is enabled.
357
+ func (r * SPF2IPResolver ) debugLogPrintf (format string , args ... any ) {
357
358
if r .debugLogging {
358
359
log .Printf (format , args ... )
359
360
}
0 commit comments