2021-03-22 13:46:36 +00:00
|
|
|
package aghnet
|
|
|
|
|
|
|
|
// DefaultRefreshIvl is the default period of time between refreshing cached
|
|
|
|
// addresses.
|
|
|
|
// const DefaultRefreshIvl = 5 * time.Minute
|
|
|
|
|
|
|
|
// HostGenFunc is the signature for functions generating fake hostnames. The
|
|
|
|
// implementation must be safe for concurrent use.
|
|
|
|
type HostGenFunc func() (host string)
|
|
|
|
|
|
|
|
// SystemResolvers helps to work with local resolvers' addresses provided by OS.
|
|
|
|
type SystemResolvers interface {
|
2022-03-23 17:47:45 +00:00
|
|
|
// Get returns the slice of local resolvers' addresses. It must be safe for
|
|
|
|
// concurrent use.
|
2021-03-22 13:46:36 +00:00
|
|
|
Get() (rs []string)
|
2022-03-23 17:47:45 +00:00
|
|
|
// refresh refreshes the local resolvers' addresses cache. It must be safe
|
|
|
|
// for concurrent use.
|
2021-03-31 13:00:47 +01:00
|
|
|
refresh() (err error)
|
2021-03-22 13:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSystemResolvers returns a SystemResolvers with the cache refresh rate
|
2022-07-07 17:33:32 +01:00
|
|
|
// defined by refreshIvl. It disables auto-refreshing if refreshIvl is 0. If
|
2021-03-22 13:46:36 +00:00
|
|
|
// nil is passed for hostGenFunc, the default generator will be used.
|
|
|
|
func NewSystemResolvers(
|
|
|
|
hostGenFunc HostGenFunc,
|
|
|
|
) (sr SystemResolvers, err error) {
|
2022-03-23 17:47:45 +00:00
|
|
|
sr = newSystemResolvers(hostGenFunc)
|
2021-03-22 13:46:36 +00:00
|
|
|
|
|
|
|
// Fill cache.
|
2021-03-31 13:00:47 +01:00
|
|
|
err = sr.refresh()
|
2021-03-22 13:46:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sr, nil
|
|
|
|
}
|