dnscache

package
v0.0.0-...-680f785 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultTTL 默认缓存时间 10 分钟
	DefaultTTL = 10 * time.Minute
	// DefaultCleanupInterval 默认清理间隔 5 分钟
	DefaultCleanupInterval = 5 * time.Minute
	// DefaultSaveInterval 默认全量保存间隔 30 秒
	DefaultSaveInterval = 30 * time.Second
	// DefaultAOFInterval 默认AOF增量保存间隔 1 秒
	DefaultAOFInterval = 1 * time.Second
)

Variables

This section is empty.

Functions

func IsIP

func IsIP(s string) bool

IsIP 判断给定的字符串是否是有效的 IPv4 或 IPv6 地址。

func Proxy_net_DialCached

func Proxy_net_DialCached(network string, addr string, proxyoptions options.ProxyOptionsDNSSLICE, upstreamResolveIPs bool, dnsCache *DNSCache, Proxy func(*http.Request) (*url.URL, error), tranportConfigurations ...func(*http.Transport) *http.Transport) (net.Conn, error)

Proxy_net_DialCached 带DNS缓存的网络连接拨号函数

func Proxy_net_DialContextCached

func Proxy_net_DialContextCached(ctx context.Context, network string, addr string, proxyoptions options.ProxyOptionsDNSSLICE, dnsCache *DNSCache, upstreamResolveIPs bool, Proxy func(*http.Request) (*url.URL, error), tranportConfigurations ...func(*http.Transport) *http.Transport) (net.Conn, error)

Proxy_net_DialContextCached 带DNS缓存的上下文网络连接拨号函数

func ResolveUpstreamDomainToIPs

func ResolveUpstreamDomainToIPs(upstreamAddress string, proxyoptions options.ProxyOptionsDNSSLICE, dnsCache interface{}, Proxy func(*http.Request) (*url.URL, error), tranportConfigurations ...func(*http.Transport) *http.Transport) ([]net.IP, error)

ResolveUpstreamDomainToIPs 解析上游代理地址到IP地址

func Shuffle

func Shuffle[T any](slice []T)

Shuffle 对切片进行随机排序

Types

type AOFEntry

type AOFEntry struct {
	Timestamp time.Time   `json:"timestamp"`
	Operation string      `json:"operation"` // "SET" or "DELETE"
	Key       string      `json:"key"`
	Value     interface{} `json:"value,omitempty"`
	TTL       int64       `json:"ttl,omitempty"` // TTL秒数
}

AOFEntry AOF日志条目

type CachingResolver

type CachingResolver struct {
	// contains filtered or unexported fields
}

CachingResolver 包装器,为DNS解析添加缓存功能

func NewCachingResolver

func NewCachingResolver(original NameResolver, cache *DNSCache) *CachingResolver

NewCachingResolver 创建一个新的带缓存的解析器

func (*CachingResolver) LookupIP

func (c *CachingResolver) LookupIP(ctx context.Context, network, host string) ([]net.IP, error)

LookupIP 使用缓存查找IP地址

func (*CachingResolver) Resolve

func (c *CachingResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error)

Resolve 使用缓存解析域名到IP

type Config

type Config struct {
	FilePath        string        `json:"file_path"`
	AOFPath         string        `json:"aof_path"`
	DefaultTTL      time.Duration `json:"default_ttl"`
	CleanupInterval time.Duration `json:"cleanup_interval"`
	SaveInterval    time.Duration `json:"save_interval"`
	AOFInterval     time.Duration `json:"aof_interval"`
	Enabled         bool          `json:"enabled"`
}

Config DNS缓存配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 返回默认配置

type DNSCache

type DNSCache struct {
	// contains filtered or unexported fields
}

DNSCache DNS缓存管理器

func New

func New(filePath string) (*DNSCache, error)

New 创建新的DNS缓存实例

func NewWithConfig

func NewWithConfig(config *Config) (*DNSCache, error)

NewWithConfig 使用配置创建DNS缓存实例

func (*DNSCache) Close

func (dc *DNSCache) Close()

Close 关闭缓存(会触发最后一次保存)

func (*DNSCache) Delete

func (dc *DNSCache) Delete(dnsType, domain string)

Delete 删除DNS记录

func (*DNSCache) Flush

func (dc *DNSCache) Flush()

Flush 清空所有缓存

func (*DNSCache) Get

func (dc *DNSCache) Get(dnsType, domain string) (interface{}, bool)

Get 获取通用DNS记录

func (*DNSCache) GetIP

func (dc *DNSCache) GetIP(dnsType, domain string) (net.IP, bool)

GetIP 获取单个IP地址

func (*DNSCache) GetIPs

func (dc *DNSCache) GetIPs(dnsType, domain string) ([]net.IP, bool)

GetIPs 获取DNS记录(IP地址列表)

func (*DNSCache) ItemCount

func (dc *DNSCache) ItemCount() int

ItemCount 返回缓存项数量

func (*DNSCache) Load

func (dc *DNSCache) Load() error

Load 从文件加载缓存

func (*DNSCache) Save

func (dc *DNSCache) Save() error

Save 保存缓存到文件(原子操作)

func (*DNSCache) Set

func (dc *DNSCache) Set(dnsType, domain string, value interface{}, ttl time.Duration)

Set 设置通用DNS记录

func (*DNSCache) SetIP

func (dc *DNSCache) SetIP(dnsType, domain string, ip net.IP, ttl time.Duration)

SetIP 设置单个IP地址

func (*DNSCache) SetIPs

func (dc *DNSCache) SetIPs(dnsType, domain string, ips []net.IP, ttl time.Duration)

SetIPs 设置DNS记录(IP地址列表)

func (*DNSCache) Stats

func (dc *DNSCache) Stats() map[string]interface{}

Stats 返回统计信息

type DOH3Resolver

type DOH3Resolver struct {
	// contains filtered or unexported fields
}

func (*DOH3Resolver) LookupIP

func (d *DOH3Resolver) LookupIP(ctx context.Context, network string, host string) ([]net.IP, error)

LookupIP implements NameResolver.

func (*DOH3Resolver) Resolve

func (d *DOH3Resolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error)

Resolve implements NameResolver.

type DOHResolver

type DOHResolver struct {
	Proxy func(*http.Request) (*url.URL, error)
	// contains filtered or unexported fields
}

func (*DOHResolver) LookupIP

func (d *DOHResolver) LookupIP(ctx context.Context, network string, host string) ([]net.IP, error)

LookupIP implements NameResolver.

func (*DOHResolver) Resolve

func (d *DOHResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error)

Resolve implements NameResolver.

type ErrorArray

type ErrorArray []error

ErrorArray 错误数组类型

func (ErrorArray) Error

func (e ErrorArray) Error() string

Error implements error.

type HostsAndDohResolver

type HostsAndDohResolver struct {
	Proxy func(*http.Request) (*url.URL, error)
	// contains filtered or unexported fields
}

func (*HostsAndDohResolver) LookupIP

func (h *HostsAndDohResolver) LookupIP(ctx context.Context, network string, host string) ([]net.IP, error)

LookupIP implements NameResolver.

func (*HostsAndDohResolver) Resolve

func (h *HostsAndDohResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error)

Resolve implements NameResolver.

type HostsResolver

type HostsResolver struct{}

func (*HostsResolver) LookupIP

func (h *HostsResolver) LookupIP(ctx context.Context, network string, host string) ([]net.IP, error)

LookupIP implements NameResolver.

func (*HostsResolver) Resolve

func (h *HostsResolver) Resolve(ctx context.Context, name string) (context.Context, net.IP, error)

Resolve implements NameResolver.

type NameResolver

type NameResolver interface {
	Resolve(ctx context.Context, name string) (context.Context, net.IP, error)
	LookupIP(ctx context.Context, network, host string) ([]net.IP, error)
}

NameResolver 接口定义

func CreateDOH3ResolverCached

func CreateDOH3ResolverCached(Proxy func(*http.Request) (*url.URL, error), proxyoptions options.ProxyOptionsDNSSLICE, dnsCache *DNSCache) NameResolver

CreateDOH3ResolverCached 创建带缓存的DOH3解析器

func CreateDOHResolverCached

func CreateDOHResolverCached(Proxy func(*http.Request) (*url.URL, error), proxyoptions options.ProxyOptionsDNSSLICE, dnsCache *DNSCache, transportConfigurations ...func(*http.Transport) *http.Transport) NameResolver

CreateDOHResolverCached 创建带缓存的DOH解析器

func CreateHostsAndDohResolverCached

func CreateHostsAndDohResolverCached(proxyoptions options.ProxyOptionsDNSSLICE, dnsCache *DNSCache, Proxy func(*http.Request) (*url.URL, error), transportConfigurations ...func(*http.Transport) *http.Transport) NameResolver

CreateHostsAndDohResolverCached 创建带缓存的Hosts+DOH解析器

func CreateHostsAndDohResolverCachedSimple

func CreateHostsAndDohResolverCachedSimple(proxyoptions options.ProxyOptionsDNSSLICE, dnsCache *DNSCache, Proxy func(*http.Request) (*url.URL, error), transportConfigurations ...func(*http.Transport) *http.Transport) NameResolver

CreateHostsAndDohResolverCachedSimple 创建带缓存的Hosts+DOH解析器

func CreateHostsResolverCached

func CreateHostsResolverCached(dnsCache *DNSCache) NameResolver

CreateHostsResolverCached 创建带缓存的Hosts解析器

type Record

type Record struct {
	Type   string        `json:"type"`
	Domain string        `json:"domain"`
	Value  interface{}   `json:"value"`
	TTL    time.Duration `json:"ttl"`
}

Record DNS记录结构 (用于可能的统计和调试)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL