代码拉取完成,页面将自动刷新
package socket
import (
"fmt"
"net"
"time"
"golang.org/x/net/publicsuffix"
)
type ptrRecord struct {
hostname string
error error
expires time.Time
}
func (r ptrRecord) IsExpired(now time.Time) bool {
return now.After(r.expires)
}
// ReverseLookupCache is a cache for storing and retrieving the results of
// reverse DNS queries. It caches the results of queries regardless of their
// outcome (success or failure). The result is cached for the amount of time
// specified by parameters and not based on the TTL from the PTR record.
type ReverseLookupCache struct {
data map[string]ptrRecord
successTTL, failureTTL time.Duration
}
// NewReverseLookupCache returns a new cache.
func NewReverseLookupCache(successTTL, failureTTL time.Duration) *ReverseLookupCache {
c := &ReverseLookupCache{
data: map[string]ptrRecord{},
successTTL: successTTL,
failureTTL: failureTTL,
}
return c
}
// Cleanup removes expired entries from the cache.
func (c *ReverseLookupCache) Cleanup() {
now := time.Now()
for k, ptr := range c.data {
if ptr.IsExpired(now) {
delete(c.data, k)
}
}
}
// Lookup performs a reverse lookup on the given IP address. A cached result
// will be returned if it is contained in the cache, otherwise a lookup is
// performed.
func (c ReverseLookupCache) Lookup(ip net.IP) (string, error) {
// Go doesn't expose a lookup method that accepts net.IP so
// unfortunately we must convert the IP to a string.
ipStr := ip.String()
// XXX: This could be implemented using common.Cache with a separate
// cleanup thread.
c.Cleanup()
// Check the cache.
now := time.Now()
if ptr, found := c.data[ipStr]; found && !ptr.IsExpired(now) {
return ptr.hostname, ptr.error
}
// Do a new lookup.
names, err := net.LookupAddr(ipStr)
now = time.Now()
var ptr ptrRecord
switch {
case err != nil:
ptr.expires = now.Add(c.failureTTL)
ptr.error = err
case len(names) == 0:
ptr.expires = now.Add(c.failureTTL)
ptr.error = fmt.Errorf("empty dns response")
default:
ptr.expires = now.Add(c.successTTL)
ptr.hostname = names[0]
}
c.data[ipStr] = ptr
return ptr.hostname, ptr.error
}
// etldPlusOne returns the effective top-level domain plus one domain for the
// given hostname.
func etldPlusOne(hostname string) (string, error) {
if hostname == "" {
return "", nil
}
trimmed := false
if hostname[len(hostname)-1] == '.' {
hostname = hostname[:len(hostname)-1]
trimmed = true
}
domain, err := publicsuffix.EffectiveTLDPlusOne(hostname)
if err != nil {
return "", err
}
if trimmed {
return domain + ".", nil
}
return domain, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。