Files
elmprodvpn/selective-vpn-api/app/resolver_bridge_utils.go

172 lines
4.4 KiB
Go

package app
import (
"os"
"strings"
resolverpkg "selective-vpn-api/app/resolver"
)
func normalizeWildcardDomain(raw string) string {
return resolverpkg.NormalizeWildcardDomain(raw)
}
func newWildcardMatcher(domains []string) wildcardMatcher {
return resolverpkg.NewWildcardMatcher(domains)
}
func uniqueStrings(in []string) []string {
return resolverpkg.UniqueStrings(in)
}
func pickDNSStartIndex(host string, size int) int {
return resolverpkg.PickDNSStartIndex(host, size)
}
func stripANSI(s string) string {
return resolverpkg.StripANSI(s)
}
func isPrivateIPv4(ip string) bool {
return resolverpkg.IsPrivateIPv4(ip)
}
func readLinesAllowMissing(path string) []string {
return resolverpkg.ReadLinesAllowMissing(path)
}
func loadJSONMap(path string) map[string]any {
return resolverpkg.LoadJSONMap(path)
}
func saveJSON(data any, path string) {
resolverpkg.SaveJSON(data, path)
}
func loadResolverPrecheckLastRun(path string) int {
return resolverpkg.LoadResolverPrecheckLastRun(path)
}
func loadResolverLiveBatchTarget(path string, fallback, minV, maxV int) int {
return resolverpkg.LoadResolverLiveBatchTarget(path, fallback, minV, maxV)
}
func loadResolverLiveBatchNXHeavyPct(path string, fallback, minV, maxV int) int {
return resolverpkg.LoadResolverLiveBatchNXHeavyPct(path, fallback, minV, maxV)
}
func splitDNS(dns string) (string, string) {
return resolverpkg.SplitDNS(dns)
}
func classifyDNSError(err error) dnsErrorKind {
return dnsErrorKind(resolverpkg.ClassifyDNSError(err))
}
func computeNextLiveBatchTarget(current, minV, maxV int, dnsStats dnsMetrics, deferred int) (int, string) {
return resolverpkg.ComputeNextLiveBatchTarget(current, minV, maxV, dnsStats, deferred)
}
func computeNextLiveBatchNXHeavyPct(
current, minV, maxV int,
dnsStats dnsMetrics,
resolvedNowDNS int,
selectedP3 int,
nxTotal int,
liveNXHeavySkip int,
) (int, string) {
return resolverpkg.ComputeNextLiveBatchNXHeavyPct(
current,
minV,
maxV,
dnsStats,
resolvedNowDNS,
selectedP3,
nxTotal,
liveNXHeavySkip,
)
}
func classifyLiveBatchHost(
host string,
cache domainCacheState,
cacheSourceForHost func(string) domainCacheSource,
wildcards wildcardMatcher,
) (priority int, nxHeavy bool) {
return resolverpkg.ClassifyLiveBatchHost(
host,
resolverpkg.DomainCacheState(cache),
func(h string) resolverpkg.DomainCacheSource { return cacheSourceForHost(h) },
wildcards,
)
}
func splitLiveBatchCandidates(
candidates []string,
cache domainCacheState,
cacheSourceForHost func(string) domainCacheSource,
wildcards wildcardMatcher,
) (p1, p2, p3 []string, nxHeavyTotal int) {
return resolverpkg.SplitLiveBatchCandidates(
candidates,
resolverpkg.DomainCacheState(cache),
func(h string) resolverpkg.DomainCacheSource { return cacheSourceForHost(h) },
wildcards,
)
}
func pickAdaptiveLiveBatch(
candidates []string,
target int,
now int,
nxHeavyPct int,
cache domainCacheState,
cacheSourceForHost func(string) domainCacheSource,
wildcards wildcardMatcher,
) ([]string, int, int, int, int, int) {
return resolverpkg.PickAdaptiveLiveBatch(
candidates,
target,
now,
nxHeavyPct,
resolverpkg.DomainCacheState(cache),
func(h string) resolverpkg.DomainCacheSource { return cacheSourceForHost(h) },
wildcards,
)
}
func smartDNSFallbackForTimeoutEnabled() bool {
return resolverpkg.SmartDNSFallbackForTimeoutEnabled()
}
func shouldFallbackToSmartDNS(stats dnsMetrics) bool {
return resolverpkg.ShouldFallbackToSmartDNS(stats)
}
func classifyHostErrorKind(stats dnsMetrics) (dnsErrorKind, bool) {
kind, ok := resolverpkg.ClassifyHostErrorKind(stats)
return dnsErrorKind(kind), ok
}
func shouldUseStaleOnError(stats dnsMetrics) bool {
return resolverpkg.ShouldUseStaleOnError(stats)
}
func resolverFallbackPool() []string {
raw := strings.TrimSpace(os.Getenv("RESOLVE_DNS_FALLBACKS"))
return resolverpkg.BuildResolverFallbackPool(raw, resolverFallbackDNS, func(item string) string {
return normalizeDNSUpstream(item, "53")
})
}
func mergeDNSUpstreamPools(primary, fallback []string) []string {
maxUpstreams := envInt("RESOLVE_DNS_MAX_UPSTREAMS", 12)
return resolverpkg.MergeDNSUpstreamPools(primary, fallback, maxUpstreams, func(item string) string {
return normalizeDNSUpstream(item, "53")
})
}
func saveResolverPrecheckState(path string, ts int, timeoutStats resolverTimeoutRecheckStats, live resolverLiveBatchStats) {
resolverpkg.SaveResolverPrecheckState(path, ts, timeoutStats, live)
}