Documentation
¶
Overview ¶
Copyright © 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved EMail : <support@mwat.de>
Package `dnscache` implements a DNS cache with optional background refresh and configurable allow/deny lists (to block SPAM/Ad sites).
Copyright © 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved
EMail : <support@mwat.de>
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License along with this program. If not, see the [GNU General Public License](http://www.gnu.org/licenses/gpl.html) for details.
This package was inSPIRED on the abandoned `dnscache` project at <https://github.com/viki-org/dnscache>.
Copyright © 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved EMail : <support@mwat.de>
Copyright © 2025 M.Watermann, 10247 Berlin, Germany
All rights reserved EMail : <support@mwat.de>
Index ¶
- type TMetrics
- type TResolver
- func (r *TResolver) Fetch(aHostname string) ([]net.IP, error)
- func (r *TResolver) FetchFirst(aHostname string) (net.IP, error)
- func (r *TResolver) FetchFirstString(aHostname string) (string, error)
- func (r *TResolver) FetchRandom(aHostname string) (net.IP, error)
- func (r *TResolver) FetchRandomString(aHostname string) (string, error)
- func (r *TResolver) LoadAllowlist(aFilename string) error
- func (r *TResolver) LoadBlocklists(aURLs []string) error
- func (r *TResolver) LookupHost(aCtx context.Context, aHostname string) ([]net.IP, error)
- func (r *TResolver) Metrics() (rMetrics *TMetrics)
- func (r *TResolver) Refresh()
- func (r *TResolver) StopExpire() *TResolver
- func (r *TResolver) StopRefresh() *TResolver
- type TResolverOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TMetrics ¶ added in v0.5.0
type TMetrics struct {
Lookups uint32
Hits uint32
Misses uint32
Retries uint32
Errors uint32
Peak uint32
}
`TMetrics` contains the metrics data for the DNS cache.
These are the public fields to access the metrics data:
- `Lookups`: Total number of lookups,
- `Hits`: Number of cache hits,
- `Misses`: Number of cache misses,
- `Retries`: Number of lookup retries,
- `Errors`: Number of lookup errors,
- `Peak`: Peak number of cached entries.
type TResolver ¶
type TResolver struct {
sync.RWMutex
cache.ICacheList //list of DNS cache entries
// contains filtered or unexported fields
}
`TResolver` is a DNS resolver with an optional background refresh.
It embeds a map of DNS cache entries to store the DNS cache entries and uses a Mutex to synchronise access to that cache.
func New ¶
`New()` returns a new DNS resolver with an optional background refresh.
If `aRefreshInterval` is greater than zero, cached DNS entries will be automatically refreshed at the specified interval.
Parameters:
- `aRefreshInterval`: Optional interval in minutes to refresh the cache.
Returns:
- `*TResolver`: A new `TResolver` instance.
func NewWithOptions ¶ added in v0.2.0
func NewWithOptions(aOptions TResolverOptions) *TResolver
`NewWithOptions()` returns a new DNS resolver with custom options.
Parameters:
- `aOptions`: Options for the resolver.
Returns:
- `*TResolver`: A new `TResolver` instance.
func (*TResolver) Fetch ¶
`Fetch()` returns the IP addresses for a given hostname.
Parameters:
- `aHostname`: The hostname to resolve.
Returns:
- `[]net.IP`: List of IP addresses for the given hostname.
- `error`: `nil` if the hostname was resolved successfully, the error otherwise.
func (*TResolver) FetchFirst ¶ added in v0.11.0
`FetchFirst()` returns the first IP address for a given hostname.
If the hostname has multiple IP addresses, the first one is returned; to get a random IP address, use [FetchRandom].
Parameters:
- `aHostname`: The hostname to resolve.
Returns:
- `net.IP`: First IP address for the given hostname.
- `error`: `nil` if the hostname was resolved successfully, the error otherwise.
func (*TResolver) FetchFirstString ¶ added in v0.11.0
`FetchFirstString()` returns the first IP address for a given hostname as a string.
Parameters:
- `aHostname`: The hostname to resolve.
Returns:
- `string`: First IP address for the given hostname as a string.
- `error`: `nil` if the hostname was resolved successfully, the error otherwise.
func (*TResolver) FetchRandom ¶
`FetchRandom()` returns a random IP address for a given hostname.
If the hostname has multiple IP addresses, a random one is returned; to get always the first one, use [FetchFirst] instead.
Parameters:
- `aHostname`: The hostname to resolve.
Returns:
- `net.IP`: Random IP address for the given hostname.
- `error`: `nil` if the hostname was resolved successfully, the error otherwise.
func (*TResolver) FetchRandomString ¶
`FetchRandomString()` returns a random IP address for a given hostname as a string.
If the hostname has multiple IP addresses, a random one is returned; to get always the first one, use [FetchFirstString] instead.
Parameters:
- `aHostname`: The hostname to resolve.
Returns:
- `string`: Random IP address for the given hostname as a string.
- `error`: `nil` if the hostname was resolved successfully, the error otherwise.
func (*TResolver) LoadAllowlist ¶ added in v0.11.0
`LoadAllowlist()` loads the allowlist from the given file.
Parameters:
- `aFilename`: The path/file name to read the 'allow' patterns from.
Returns:
- `error`: An error in case of problems, or `nil` otherwise.
func (*TResolver) LoadBlocklists ¶ added in v0.11.0
`LoadBlocklists()` loads the blocklists from the given URLs.
Parameters:
- `aURLs`: The URLs to download the blocklists from.
Returns:
- `error`: An error in case of problems, or `nil` otherwise.
func (*TResolver) LookupHost ¶ added in v0.11.1
`LookupHost()` resolves a hostname with the given context and caches the result.
This method is called by `Fetch()` and `Refresh()` internally and not intended for public use because it bypasses both, the allow/deny lists and the internal cache.
Parameters:
- `aCtx`: Context for the lookup operation.
- `aHostname`: The hostname to resolve.
Returns:
- `[]net.IP`: List of IP addresses for the given hostname.
- `error`: `nil` if the hostname was resolved successfully, the error otherwise.
func (*TResolver) Metrics ¶ added in v0.5.0
`Metrics()` returns the current metrics data.
Returns:
- `*TMetrics`: Current metrics data.
func (*TResolver) Refresh ¶
func (r *TResolver) Refresh()
`Refresh()` resolves all cached hostnames and updates the cache.
This method is called automatically if a refresh interval was specified in the `New()` constructor.
func (*TResolver) StopExpire ¶ added in v0.6.0
`StopExpire()` stops the background expiration goroutine if it's running.
This method should be called when the background expirations are no longer needed. The resolver remains usable after calling `StopExpire()“, but cached entries will no longer be automatically expired.
func (*TResolver) StopRefresh ¶ added in v0.6.0
`StopRefresh()` stops the background refresh goroutine if it's running.
This method should be called when the background updates are no longer needed. The resolver remains usable after calling `StopRefresh()“, but cached entries will no longer be automatically refreshed.
type TResolverOptions ¶ added in v0.2.0
type TResolverOptions struct {
BlockLists []string
DNSservers []string
AllowList string
DataDir string
CacheSize int
Resolver *net.Resolver
ExpireInterval uint8
MaxRetries uint8
RefreshInterval uint8
TTL uint8
}
`TResolverOptions` contain configuration options for creating a resolver.
This are the public fields to configure a new `TResolver` instance:
- `BlockLists`: List of URLs to download blocklists from.
- `DNSservers`: List of DNS servers to use, `nil` means use system default.
- `AllowList`: Path/file name to read the 'allow' patterns from.
- `DataDir`: Directory to store local allow and deny lists.
- `CacheSize`: Initial cache size, `0` means use default (`512`).
- `Resolver`: Custom resolver, `nil` means use default.
- `ExpireInterval`: Optional interval (in minutes) to remove expired cache entries.
- `MaxRetries`: Maximum number of retries for DNS lookup, `0` means use default (`3`).
- `RefreshInterval`: Optional interval (in minutes) to refresh the cache.
- `TTL`: Optional time to live (in minutes) for cache entries.
