91 lines
3.1 KiB
Go
91 lines
3.1 KiB
Go
package app
|
|
|
|
import "time"
|
|
|
|
func buildResolverJobContext(opts ResolverOpts, logf func(string, ...any)) resolverJobContext {
|
|
ctx := resolverJobContext{}
|
|
|
|
ctx.domains = loadList(opts.DomainsPath)
|
|
ctx.metaSpecial = loadList(opts.MetaPath)
|
|
ctx.staticLines = readLinesAllowMissing(opts.StaticPath)
|
|
ctx.wildcards = newWildcardMatcher(opts.SmartDNSWildcards)
|
|
|
|
ctx.cfg = loadDNSConfig(opts.DNSConfigPath, logf)
|
|
ctx.cfg = applyResolverDNSModeRuntime(ctx.cfg, opts)
|
|
logResolverDNSMode(ctx.cfg, ctx.wildcards, logf)
|
|
|
|
ctx.domainCache = loadDomainCacheState(opts.CachePath, logf)
|
|
ctx.ptrCache = loadJSONMap(opts.PtrCachePath)
|
|
ctx.now = int(time.Now().Unix())
|
|
ctx.precheckStatePath = opts.CachePath + ".precheck.json"
|
|
ctx.precheckEnvForced = resolvePrecheckForceEnvEnabled()
|
|
ctx.precheckFileForced = resolvePrecheckForceFileEnabled(precheckForcePath)
|
|
ctx.tuning = buildResolverRuntimeTuning(opts, ctx.now, ctx.precheckStatePath, ctx.precheckEnvForced, ctx.precheckFileForced)
|
|
|
|
ctx.cacheSourceForHost = func(host string) domainCacheSource {
|
|
switch ctx.cfg.Mode {
|
|
case DNSModeSmartDNS:
|
|
return domainCacheSourceWildcard
|
|
case DNSModeHybridWildcard:
|
|
if ctx.wildcards.Match(host) {
|
|
return domainCacheSourceWildcard
|
|
}
|
|
}
|
|
return domainCacheSourceDirect
|
|
}
|
|
ctx.cooldown = newDNSRunCooldown()
|
|
|
|
if ctx.tuning.PrecheckDue && ctx.tuning.TimeoutRecheckMax > 0 {
|
|
ctx.timeoutRecheck = runTimeoutQuarantineRecheck(
|
|
ctx.domains,
|
|
ctx.cfg,
|
|
ctx.metaSpecial,
|
|
ctx.wildcards,
|
|
ctx.tuning.DNSTimeout,
|
|
&ctx.domainCache,
|
|
ctx.cacheSourceForHost,
|
|
ctx.now,
|
|
ctx.tuning.TimeoutRecheckMax,
|
|
ctx.tuning.Workers,
|
|
)
|
|
}
|
|
|
|
directPolicy := directDNSAttemptPolicy(len(ctx.cfg.Default))
|
|
wildcardPolicy := wildcardDNSAttemptPolicy(1)
|
|
cEnabled, cMin, cRate, cStreak, cBan, cMaxBan := ctx.cooldown.configSnapshot()
|
|
logResolverStart(
|
|
resolverStartLogInput{
|
|
DomainsTotal: len(ctx.domains),
|
|
TTL: ctx.tuning.TTL,
|
|
Workers: ctx.tuning.Workers,
|
|
DNSTimeoutMS: ctx.tuning.DNSTimeoutMS,
|
|
DirectTry: directPolicy.TryLimit,
|
|
DirectBudgetMS: directPolicy.DomainBudget.Milliseconds(),
|
|
WildcardTry: wildcardPolicy.TryLimit,
|
|
WildcardBudgetMS: wildcardPolicy.DomainBudget.Milliseconds(),
|
|
NXEarlyStop: resolveNXEarlyStopEnabled(),
|
|
NXHardQuarantine: resolveNXHardQuarantineEnabled(),
|
|
CooldownEnabled: cEnabled,
|
|
CooldownMinAttempts: cMin,
|
|
CooldownTimeoutRate: cRate,
|
|
CooldownFailStreak: cStreak,
|
|
CooldownBanSec: cBan,
|
|
CooldownMaxBanSec: cMaxBan,
|
|
LiveBatchTarget: ctx.tuning.LiveBatchTarget,
|
|
LiveBatchMin: ctx.tuning.LiveBatchMin,
|
|
LiveBatchMax: ctx.tuning.LiveBatchMax,
|
|
LiveBatchNXHeavyPct: ctx.tuning.LiveBatchNXHeavyPct,
|
|
LiveBatchNXHeavyMin: ctx.tuning.LiveBatchNXHeavyMin,
|
|
LiveBatchNXHeavyMax: ctx.tuning.LiveBatchNXHeavyMax,
|
|
StaleKeepSec: ctx.tuning.StaleKeepSec,
|
|
PrecheckEverySec: ctx.tuning.PrecheckEverySec,
|
|
PrecheckMaxDomains: ctx.tuning.PrecheckMaxDomains,
|
|
PrecheckForcedEnv: ctx.precheckEnvForced,
|
|
PrecheckForcedFile: ctx.precheckFileForced,
|
|
},
|
|
logf,
|
|
)
|
|
|
|
return ctx
|
|
}
|