2023-06-21 12:53:53 +03:00
|
|
|
// Package whois provides WHOIS functionality.
|
|
|
|
package whois
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-04-04 15:52:39 +03:00
|
|
|
"cmp"
|
2023-06-21 12:53:53 +03:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-08-29 16:20:05 +03:00
|
|
|
"log/slog"
|
2023-06-21 12:53:53 +03:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-07-20 14:26:35 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2023-06-21 12:53:53 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2023-10-06 16:45:48 +03:00
|
|
|
"github.com/AdguardTeam/golibs/ioutil"
|
2024-08-29 16:20:05 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2023-06-21 12:53:53 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
|
|
"github.com/bluele/gcache"
|
2024-08-29 16:20:05 +03:00
|
|
|
"github.com/c2h5oh/datasize"
|
2023-06-21 12:53:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultServer is the default WHOIS server.
|
|
|
|
DefaultServer = "whois.arin.net"
|
|
|
|
|
|
|
|
// DefaultPort is the default port for WHOIS requests.
|
|
|
|
DefaultPort = 43
|
|
|
|
)
|
|
|
|
|
|
|
|
// Interface provides WHOIS functionality.
|
|
|
|
type Interface interface {
|
|
|
|
// Process makes WHOIS request and returns WHOIS information or nil.
|
|
|
|
// changed indicates that Info was updated since last request.
|
|
|
|
Process(ctx context.Context, ip netip.Addr) (info *Info, changed bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty is an empty [Interface] implementation which does nothing.
|
|
|
|
type Empty struct{}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ Interface = (*Empty)(nil)
|
|
|
|
|
|
|
|
// Process implements the [Interface] interface for Empty.
|
|
|
|
func (Empty) Process(_ context.Context, _ netip.Addr) (info *Info, changed bool) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config is the configuration structure for Default.
|
|
|
|
type Config struct {
|
2024-08-29 16:20:05 +03:00
|
|
|
// Logger is used for logging the operation of the WHOIS lookup queries. It
|
|
|
|
// must not be nil.
|
|
|
|
Logger *slog.Logger
|
|
|
|
|
2023-07-18 14:02:32 +03:00
|
|
|
// DialContext is used to create TCP connections to WHOIS servers.
|
2023-07-20 14:26:35 +03:00
|
|
|
DialContext aghnet.DialContextFunc
|
2023-06-21 12:53:53 +03:00
|
|
|
|
|
|
|
// ServerAddr is the address of the WHOIS server.
|
|
|
|
ServerAddr string
|
|
|
|
|
|
|
|
// Timeout is the timeout for WHOIS requests.
|
|
|
|
Timeout time.Duration
|
|
|
|
|
|
|
|
// CacheTTL is the Time to Live duration for cached IP addresses.
|
|
|
|
CacheTTL time.Duration
|
|
|
|
|
|
|
|
// MaxConnReadSize is an upper limit in bytes for reading from net.Conn.
|
2023-10-06 16:45:48 +03:00
|
|
|
MaxConnReadSize uint64
|
2023-06-21 12:53:53 +03:00
|
|
|
|
|
|
|
// MaxRedirects is the maximum redirects count.
|
|
|
|
MaxRedirects int
|
|
|
|
|
|
|
|
// MaxInfoLen is the maximum length of Info fields returned by Process.
|
|
|
|
MaxInfoLen int
|
|
|
|
|
|
|
|
// CacheSize is the maximum size of the cache. It must be greater than
|
|
|
|
// zero.
|
|
|
|
CacheSize int
|
|
|
|
|
|
|
|
// Port is the port for WHOIS requests.
|
|
|
|
Port uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default is the default WHOIS information processor.
|
|
|
|
type Default struct {
|
2024-08-29 16:20:05 +03:00
|
|
|
// logger is used for logging the operation of the WHOIS lookup queries. It
|
|
|
|
// must not be nil.
|
|
|
|
logger *slog.Logger
|
|
|
|
|
2023-06-21 12:53:53 +03:00
|
|
|
// cache is the cache containing IP addresses of clients. An active IP
|
|
|
|
// address is resolved once again after it expires. If IP address couldn't
|
|
|
|
// be resolved, it stays here for some time to prevent further attempts to
|
|
|
|
// resolve the same IP.
|
|
|
|
cache gcache.Cache
|
|
|
|
|
2023-07-18 14:02:32 +03:00
|
|
|
// dialContext is used to create TCP connections to WHOIS servers.
|
2023-07-20 14:26:35 +03:00
|
|
|
dialContext aghnet.DialContextFunc
|
2023-06-21 12:53:53 +03:00
|
|
|
|
|
|
|
// serverAddr is the address of the WHOIS server.
|
|
|
|
serverAddr string
|
|
|
|
|
|
|
|
// portStr is the port for WHOIS requests.
|
|
|
|
portStr string
|
|
|
|
|
|
|
|
// timeout is the timeout for WHOIS requests.
|
|
|
|
timeout time.Duration
|
|
|
|
|
|
|
|
// cacheTTL is the Time to Live duration for cached IP addresses.
|
|
|
|
cacheTTL time.Duration
|
|
|
|
|
|
|
|
// maxConnReadSize is an upper limit in bytes for reading from net.Conn.
|
2023-10-06 16:45:48 +03:00
|
|
|
maxConnReadSize uint64
|
2023-06-21 12:53:53 +03:00
|
|
|
|
|
|
|
// maxRedirects is the maximum redirects count.
|
|
|
|
maxRedirects int
|
|
|
|
|
|
|
|
// maxInfoLen is the maximum length of Info fields returned by Process.
|
|
|
|
maxInfoLen int
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new default WHOIS information processor. conf must not be
|
|
|
|
// nil.
|
|
|
|
func New(conf *Config) (w *Default) {
|
|
|
|
return &Default{
|
2024-08-29 16:20:05 +03:00
|
|
|
logger: conf.Logger,
|
2023-06-21 12:53:53 +03:00
|
|
|
serverAddr: conf.ServerAddr,
|
|
|
|
dialContext: conf.DialContext,
|
|
|
|
timeout: conf.Timeout,
|
|
|
|
cache: gcache.New(conf.CacheSize).LRU().Build(),
|
|
|
|
maxConnReadSize: conf.MaxConnReadSize,
|
|
|
|
maxRedirects: conf.MaxRedirects,
|
|
|
|
portStr: strconv.Itoa(int(conf.Port)),
|
|
|
|
maxInfoLen: conf.MaxInfoLen,
|
|
|
|
cacheTTL: conf.CacheTTL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// trimValue trims s and replaces the last 3 characters of the cut with "..."
|
|
|
|
// to fit into max. max must be greater than 3.
|
|
|
|
func trimValue(s string, max int) string {
|
|
|
|
if len(s) <= max {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
return s[:max-3] + "..."
|
|
|
|
}
|
|
|
|
|
|
|
|
// isWHOISComment returns true if the data is empty or is a WHOIS comment.
|
|
|
|
func isWHOISComment(data []byte) (ok bool) {
|
|
|
|
return len(data) == 0 || data[0] == '#' || data[0] == '%'
|
|
|
|
}
|
|
|
|
|
|
|
|
// whoisParse parses a subset of plain-text data from the WHOIS response into a
|
|
|
|
// string map. It trims values of the returned map to maxLen.
|
|
|
|
func whoisParse(data []byte, maxLen int) (info map[string]string) {
|
|
|
|
info = map[string]string{}
|
|
|
|
|
|
|
|
var orgname string
|
|
|
|
lines := bytes.Split(data, []byte("\n"))
|
|
|
|
for _, l := range lines {
|
|
|
|
if isWHOISComment(l) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
before, after, found := bytes.Cut(l, []byte(":"))
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key := strings.ToLower(string(before))
|
|
|
|
val := strings.TrimSpace(string(after))
|
|
|
|
if val == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "orgname", "org-name":
|
|
|
|
key = "orgname"
|
|
|
|
val = trimValue(val, maxLen)
|
|
|
|
orgname = val
|
|
|
|
case "city", "country":
|
|
|
|
val = trimValue(val, maxLen)
|
|
|
|
case "descr", "netname":
|
|
|
|
key = "orgname"
|
2024-04-04 15:52:39 +03:00
|
|
|
val = cmp.Or(orgname, val)
|
2023-06-21 12:53:53 +03:00
|
|
|
orgname = val
|
|
|
|
case "whois":
|
|
|
|
key = "whois"
|
|
|
|
case "referralserver":
|
|
|
|
key = "whois"
|
|
|
|
val = strings.TrimPrefix(val, "whois://")
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
info[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
|
|
|
// query sends request to a server and returns the response or error.
|
|
|
|
func (w *Default) query(ctx context.Context, target, serverAddr string) (data []byte, err error) {
|
|
|
|
addr, _, _ := net.SplitHostPort(serverAddr)
|
|
|
|
if addr == DefaultServer {
|
|
|
|
// Display type flags for query.
|
|
|
|
//
|
|
|
|
// See https://www.arin.net/resources/registry/whois/rws/api/#nicname-whois-queries.
|
|
|
|
target = "n + " + target
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := w.dialContext(ctx, "tcp", serverAddr)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() { err = errors.WithDeferred(err, conn.Close()) }()
|
|
|
|
|
2023-10-06 16:45:48 +03:00
|
|
|
r := ioutil.LimitReader(conn, w.maxConnReadSize)
|
2023-06-21 12:53:53 +03:00
|
|
|
|
2023-07-18 14:02:32 +03:00
|
|
|
_ = conn.SetDeadline(time.Now().Add(w.timeout))
|
2023-06-21 12:53:53 +03:00
|
|
|
_, err = io.WriteString(conn, target+"\r\n")
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This use of ReadAll is now safe, because we limited the conn Reader.
|
|
|
|
data, err = io.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// queryAll queries WHOIS server and handles redirects.
|
|
|
|
func (w *Default) queryAll(ctx context.Context, target string) (info map[string]string, err error) {
|
|
|
|
server := net.JoinHostPort(w.serverAddr, w.portStr)
|
|
|
|
|
2024-04-04 15:52:39 +03:00
|
|
|
for range w.maxRedirects {
|
2024-08-29 16:20:05 +03:00
|
|
|
var data []byte
|
2023-06-21 12:53:53 +03:00
|
|
|
data, err = w.query(ctx, target, server)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-29 16:20:05 +03:00
|
|
|
w.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"received response",
|
|
|
|
"size", datasize.ByteSize(len(data)),
|
|
|
|
"source", server,
|
|
|
|
"target", target,
|
|
|
|
)
|
2023-06-21 12:53:53 +03:00
|
|
|
|
|
|
|
info = whoisParse(data, w.maxInfoLen)
|
|
|
|
redir, ok := info["whois"]
|
|
|
|
if !ok {
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
redir = strings.ToLower(redir)
|
|
|
|
|
|
|
|
_, _, err = net.SplitHostPort(redir)
|
|
|
|
if err != nil {
|
|
|
|
server = net.JoinHostPort(redir, w.portStr)
|
|
|
|
} else {
|
|
|
|
server = redir
|
|
|
|
}
|
|
|
|
|
2024-08-29 16:20:05 +03:00
|
|
|
w.logger.DebugContext(ctx, "redirected", "destination", redir, "target", target)
|
2023-06-21 12:53:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("whois: redirect loop")
|
|
|
|
}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ Interface = (*Default)(nil)
|
|
|
|
|
|
|
|
// Process makes WHOIS request and returns WHOIS information or nil. changed
|
|
|
|
// indicates that Info was updated since last request.
|
|
|
|
func (w *Default) Process(ctx context.Context, ip netip.Addr) (wi *Info, changed bool) {
|
2024-01-17 15:06:16 +03:00
|
|
|
if netutil.IsSpecialPurpose(ip) {
|
2023-06-21 12:53:53 +03:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2024-08-29 16:20:05 +03:00
|
|
|
wi, expired := w.findInCache(ctx, ip)
|
2023-06-21 12:53:53 +03:00
|
|
|
if wi != nil && !expired {
|
|
|
|
// Don't return an empty struct so that the frontend doesn't get
|
|
|
|
// confused.
|
|
|
|
if (*wi == Info{}) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return wi, false
|
|
|
|
}
|
|
|
|
|
2023-06-29 19:35:33 +03:00
|
|
|
return w.requestInfo(ctx, ip, wi)
|
|
|
|
}
|
|
|
|
|
|
|
|
// requestInfo makes WHOIS request and returns WHOIS info. changed is false if
|
|
|
|
// received information is equal to cached.
|
|
|
|
func (w *Default) requestInfo(
|
|
|
|
ctx context.Context,
|
|
|
|
ip netip.Addr,
|
|
|
|
cached *Info,
|
|
|
|
) (wi *Info, changed bool) {
|
2023-06-21 12:53:53 +03:00
|
|
|
var info Info
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
item := toCacheItem(info, w.cacheTTL)
|
|
|
|
err := w.cache.Set(ip, item)
|
|
|
|
if err != nil {
|
2024-08-29 16:20:05 +03:00
|
|
|
w.logger.DebugContext(ctx, "adding item to cache", "key", ip, slogutil.KeyError, err)
|
2023-06-21 12:53:53 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
kv, err := w.queryAll(ctx, ip.String())
|
|
|
|
if err != nil {
|
2024-08-29 16:20:05 +03:00
|
|
|
w.logger.DebugContext(ctx, "querying", "target", ip, slogutil.KeyError, err)
|
2023-06-21 12:53:53 +03:00
|
|
|
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
|
|
|
info = Info{
|
|
|
|
City: kv["city"],
|
|
|
|
Country: kv["country"],
|
|
|
|
Orgname: kv["orgname"],
|
|
|
|
}
|
|
|
|
|
2023-06-29 19:35:33 +03:00
|
|
|
changed = cached == nil || info != *cached
|
|
|
|
|
2023-06-21 12:53:53 +03:00
|
|
|
// Don't return an empty struct so that the frontend doesn't get confused.
|
|
|
|
if (info == Info{}) {
|
2023-06-29 19:35:33 +03:00
|
|
|
return nil, changed
|
2023-06-21 12:53:53 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 19:35:33 +03:00
|
|
|
return &info, changed
|
2023-06-21 12:53:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// findInCache finds Info in the cache. expired indicates that Info is valid.
|
2024-08-29 16:20:05 +03:00
|
|
|
func (w *Default) findInCache(ctx context.Context, ip netip.Addr) (wi *Info, expired bool) {
|
2023-06-21 12:53:53 +03:00
|
|
|
val, err := w.cache.Get(ip)
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, gcache.KeyNotFoundError) {
|
2024-08-29 16:20:05 +03:00
|
|
|
w.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"retrieving item from cache",
|
|
|
|
"key", ip,
|
|
|
|
slogutil.KeyError, err,
|
|
|
|
)
|
2023-06-21 12:53:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2024-08-29 16:20:05 +03:00
|
|
|
return fromCacheItem(val.(*cacheItem))
|
2023-06-21 12:53:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info is the filtered WHOIS data for a runtime client.
|
|
|
|
type Info struct {
|
|
|
|
City string `json:"city,omitempty"`
|
|
|
|
Country string `json:"country,omitempty"`
|
|
|
|
Orgname string `json:"orgname,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-08-21 16:27:28 +03:00
|
|
|
// Clone returns a deep copy of the WHOIS info.
|
|
|
|
func (i *Info) Clone() (c *Info) {
|
|
|
|
if i == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Info{
|
|
|
|
City: i.City,
|
|
|
|
Country: i.Country,
|
|
|
|
Orgname: i.Orgname,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:53:53 +03:00
|
|
|
// cacheItem represents an item that we will store in the cache.
|
|
|
|
type cacheItem struct {
|
|
|
|
// expiry is the time when cacheItem will expire.
|
|
|
|
expiry time.Time
|
|
|
|
|
|
|
|
// info is the WHOIS data for a runtime client.
|
|
|
|
info *Info
|
|
|
|
}
|
|
|
|
|
|
|
|
// toCacheItem creates a cached item from a WHOIS info and Time to Live
|
|
|
|
// duration.
|
|
|
|
func toCacheItem(info Info, ttl time.Duration) (item *cacheItem) {
|
|
|
|
return &cacheItem{
|
|
|
|
expiry: time.Now().Add(ttl),
|
|
|
|
info: &info,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromCacheItem creates a WHOIS info from the cached item. expired indicates
|
|
|
|
// that WHOIS info is valid. item must not be nil.
|
|
|
|
func fromCacheItem(item *cacheItem) (info *Info, expired bool) {
|
|
|
|
if time.Now().After(item.expiry) {
|
|
|
|
return item.info, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.info, false
|
|
|
|
}
|