2019-09-18 18:44:27 +03:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2020-05-27 14:54:31 +03:00
|
|
|
"context"
|
2019-10-22 13:11:22 +03:00
|
|
|
"encoding/binary"
|
2019-10-07 19:13:06 +03:00
|
|
|
"fmt"
|
2021-05-21 14:55:42 +03:00
|
|
|
"io"
|
2019-10-07 19:13:06 +03:00
|
|
|
"net"
|
2019-09-18 18:44:27 +03:00
|
|
|
"strings"
|
2019-10-07 19:13:06 +03:00
|
|
|
"time"
|
2019-09-18 18:44:27 +03:00
|
|
|
|
2020-11-23 14:14:08 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
|
2019-10-22 13:11:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/cache"
|
2021-05-24 17:28:11 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2019-09-18 18:44:27 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-07-29 17:40:31 +03:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2019-09-18 18:44:27 +03:00
|
|
|
)
|
|
|
|
|
2019-10-07 19:13:06 +03:00
|
|
|
const (
|
|
|
|
defaultServer = "whois.arin.net"
|
|
|
|
defaultPort = "43"
|
|
|
|
maxValueLength = 250
|
2019-12-23 20:02:06 +03:00
|
|
|
whoisTTL = 1 * 60 * 60 // 1 hour
|
2019-10-07 19:13:06 +03:00
|
|
|
)
|
2019-09-23 20:41:14 +03:00
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
// WHOIS - module context
|
|
|
|
type WHOIS struct {
|
2021-01-27 18:32:13 +03:00
|
|
|
clients *clientsContainer
|
|
|
|
ipChan chan net.IP
|
2019-10-22 13:11:22 +03:00
|
|
|
|
2021-04-22 13:38:24 +03:00
|
|
|
// dialContext specifies the dial function for creating unencrypted TCP
|
|
|
|
// connections.
|
|
|
|
dialContext func(ctx context.Context, network, addr string) (conn net.Conn, err error)
|
|
|
|
|
2019-10-22 13:11:22 +03:00
|
|
|
// Contains 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.
|
|
|
|
ipAddrs cache.Cache
|
2021-01-27 18:32:13 +03:00
|
|
|
|
|
|
|
// TODO(a.garipov): Rewrite to use time.Duration. Like, seriously, why?
|
|
|
|
timeoutMsec uint
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
// initWHOIS creates the WHOIS module context.
|
|
|
|
func initWHOIS(clients *clientsContainer) *WHOIS {
|
|
|
|
w := WHOIS{
|
2021-01-26 19:44:19 +03:00
|
|
|
timeoutMsec: 5000,
|
|
|
|
clients: clients,
|
|
|
|
ipAddrs: cache.New(cache.Config{
|
|
|
|
EnableLRU: true,
|
|
|
|
MaxCount: 10000,
|
|
|
|
}),
|
2021-04-22 13:38:24 +03:00
|
|
|
dialContext: customDialContext,
|
|
|
|
ipChan: make(chan net.IP, 255),
|
2021-01-26 19:44:19 +03:00
|
|
|
}
|
2019-10-22 13:11:22 +03:00
|
|
|
|
2019-09-18 18:44:27 +03:00
|
|
|
go w.workerLoop()
|
2021-01-26 19:44:19 +03:00
|
|
|
|
2019-09-18 18:44:27 +03:00
|
|
|
return &w
|
|
|
|
}
|
|
|
|
|
2019-09-23 20:41:14 +03:00
|
|
|
// If the value is too large - cut it and append "..."
|
|
|
|
func trimValue(s string) string {
|
|
|
|
if len(s) <= maxValueLength {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s[:maxValueLength-3] + "..."
|
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
// isWHOISComment returns true if the string is empty or is a WHOIS comment.
|
|
|
|
func isWHOISComment(s string) (ok bool) {
|
2021-05-20 14:22:06 +03:00
|
|
|
return len(s) == 0 || s[0] == '#' || s[0] == '%'
|
|
|
|
}
|
|
|
|
|
|
|
|
// strmap is an alias for convenience.
|
|
|
|
type strmap = map[string]string
|
|
|
|
|
|
|
|
// whoisParse parses a subset of plain-text data from the WHOIS response into
|
|
|
|
// a string map.
|
|
|
|
func whoisParse(data string) (m strmap) {
|
|
|
|
m = strmap{}
|
|
|
|
|
|
|
|
var orgname string
|
|
|
|
lines := strings.Split(data, "\n")
|
|
|
|
for _, l := range lines {
|
2021-06-18 18:13:36 +03:00
|
|
|
if isWHOISComment(l) {
|
2019-09-18 18:44:27 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:22:06 +03:00
|
|
|
kv := strings.SplitN(l, ":", 2)
|
2019-09-18 18:44:27 +03:00
|
|
|
if len(kv) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
2021-05-20 14:22:06 +03:00
|
|
|
|
|
|
|
k := strings.ToLower(strings.TrimSpace(kv[0]))
|
2019-09-18 18:44:27 +03:00
|
|
|
v := strings.TrimSpace(kv[1])
|
2021-05-20 14:22:06 +03:00
|
|
|
if v == "" {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-18 18:44:27 +03:00
|
|
|
|
2019-09-23 20:41:14 +03:00
|
|
|
switch k {
|
2021-05-20 14:22:06 +03:00
|
|
|
case "orgname", "org-name":
|
|
|
|
k = "orgname"
|
|
|
|
v = trimValue(v)
|
|
|
|
orgname = v
|
|
|
|
case "city", "country":
|
|
|
|
v = trimValue(v)
|
|
|
|
case "descr", "netname":
|
|
|
|
k = "orgname"
|
2021-07-29 17:40:31 +03:00
|
|
|
v = stringutil.Coalesce(orgname, v)
|
2021-05-20 14:22:06 +03:00
|
|
|
orgname = v
|
|
|
|
case "whois":
|
|
|
|
k = "whois"
|
|
|
|
case "referralserver":
|
|
|
|
k = "whois"
|
|
|
|
v = strings.TrimPrefix(v, "whois://")
|
|
|
|
default:
|
|
|
|
continue
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
2019-09-23 20:41:14 +03:00
|
|
|
|
2021-05-20 14:22:06 +03:00
|
|
|
m[k] = v
|
2019-09-23 20:41:14 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 18:44:27 +03:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-11-23 14:14:08 +03:00
|
|
|
// MaxConnReadSize is an upper limit in bytes for reading from net.Conn.
|
|
|
|
const MaxConnReadSize = 64 * 1024
|
|
|
|
|
2019-10-07 19:13:06 +03:00
|
|
|
// Send request to a server and receive the response
|
2021-06-18 18:13:36 +03:00
|
|
|
func (w *WHOIS) query(ctx context.Context, target, serverAddr string) (data string, err error) {
|
2019-10-07 19:13:06 +03:00
|
|
|
addr, _, _ := net.SplitHostPort(serverAddr)
|
|
|
|
if addr == "whois.arin.net" {
|
|
|
|
target = "n + " + target
|
|
|
|
}
|
2021-05-24 17:28:11 +03:00
|
|
|
|
2021-04-22 13:38:24 +03:00
|
|
|
conn, err := w.dialContext(ctx, "tcp", serverAddr)
|
2019-10-07 19:13:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-05-24 17:28:11 +03:00
|
|
|
defer func() { err = errors.WithDeferred(err, conn.Close()) }()
|
2019-10-07 19:13:06 +03:00
|
|
|
|
2021-05-24 17:28:11 +03:00
|
|
|
r, err := aghio.LimitReader(conn, MaxConnReadSize)
|
2020-11-23 14:14:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-10-07 19:13:06 +03:00
|
|
|
_ = conn.SetReadDeadline(time.Now().Add(time.Duration(w.timeoutMsec) * time.Millisecond))
|
|
|
|
_, err = conn.Write([]byte(target + "\r\n"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-11-23 14:14:08 +03:00
|
|
|
// This use of ReadAll is now safe, because we limited the conn Reader.
|
2021-05-24 17:28:11 +03:00
|
|
|
var whoisData []byte
|
|
|
|
whoisData, err = io.ReadAll(r)
|
2019-10-07 19:13:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-05-24 17:28:11 +03:00
|
|
|
return string(whoisData), nil
|
2019-10-07 19:13:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query WHOIS servers (handle redirects)
|
2021-06-18 18:13:36 +03:00
|
|
|
func (w *WHOIS) queryAll(ctx context.Context, target string) (string, error) {
|
2019-10-07 19:13:06 +03:00
|
|
|
server := net.JoinHostPort(defaultServer, defaultPort)
|
|
|
|
const maxRedirects = 5
|
|
|
|
for i := 0; i != maxRedirects; i++ {
|
2021-01-26 19:44:19 +03:00
|
|
|
resp, err := w.query(ctx, target, server)
|
2019-10-07 19:13:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-06-18 18:13:36 +03:00
|
|
|
log.Debug("whois: received response (%d bytes) from %s IP:%s", len(resp), server, target)
|
2019-10-07 19:13:06 +03:00
|
|
|
|
|
|
|
m := whoisParse(resp)
|
|
|
|
redir, ok := m["whois"]
|
|
|
|
if !ok {
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
redir = strings.ToLower(redir)
|
|
|
|
|
|
|
|
_, _, err = net.SplitHostPort(redir)
|
|
|
|
if err != nil {
|
|
|
|
server = net.JoinHostPort(redir, defaultPort)
|
|
|
|
} else {
|
|
|
|
server = redir
|
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
log.Debug("whois: redirected to %s IP:%s", redir, target)
|
2019-10-07 19:13:06 +03:00
|
|
|
}
|
2020-11-05 15:20:57 +03:00
|
|
|
return "", fmt.Errorf("whois: redirect loop")
|
2019-10-07 19:13:06 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 18:44:27 +03:00
|
|
|
// Request WHOIS information
|
2021-06-18 18:13:36 +03:00
|
|
|
func (w *WHOIS) process(ctx context.Context, ip net.IP) (wi *RuntimeClientWHOISInfo) {
|
2021-01-26 19:44:19 +03:00
|
|
|
resp, err := w.queryAll(ctx, ip.String())
|
2019-09-18 18:44:27 +03:00
|
|
|
if err != nil {
|
2021-06-18 18:13:36 +03:00
|
|
|
log.Debug("whois: error: %s IP:%s", err, ip)
|
2021-04-02 17:30:39 +03:00
|
|
|
|
|
|
|
return nil
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
log.Debug("whois: IP:%s response: %d bytes", ip, len(resp))
|
2019-09-18 18:44:27 +03:00
|
|
|
|
|
|
|
m := whoisParse(resp)
|
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
wi = &RuntimeClientWHOISInfo{
|
2021-04-02 17:30:39 +03:00
|
|
|
City: m["city"],
|
|
|
|
Country: m["country"],
|
|
|
|
Orgname: m["orgname"],
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
2021-04-02 17:30:39 +03:00
|
|
|
// Don't return an empty struct so that the frontend doesn't get
|
|
|
|
// confused.
|
2021-06-18 18:13:36 +03:00
|
|
|
if *wi == (RuntimeClientWHOISInfo{}) {
|
2021-04-02 17:30:39 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return wi
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Begin - begin requesting WHOIS info
|
2021-06-18 18:13:36 +03:00
|
|
|
func (w *WHOIS) Begin(ip net.IP) {
|
2019-10-22 13:11:22 +03:00
|
|
|
now := uint64(time.Now().Unix())
|
|
|
|
expire := w.ipAddrs.Get([]byte(ip))
|
|
|
|
if len(expire) != 0 {
|
|
|
|
exp := binary.BigEndian.Uint64(expire)
|
|
|
|
if exp > now {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TTL expired
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
2019-10-22 13:11:22 +03:00
|
|
|
expire = make([]byte, 8)
|
2019-12-23 20:02:06 +03:00
|
|
|
binary.BigEndian.PutUint64(expire, now+whoisTTL)
|
2019-10-22 13:11:22 +03:00
|
|
|
_ = w.ipAddrs.Set([]byte(ip), expire)
|
2019-09-18 18:44:27 +03:00
|
|
|
|
2021-06-18 18:13:36 +03:00
|
|
|
log.Debug("whois: adding %s", ip)
|
2019-09-18 18:44:27 +03:00
|
|
|
select {
|
|
|
|
case w.ipChan <- ip:
|
|
|
|
//
|
|
|
|
default:
|
2021-06-18 18:13:36 +03:00
|
|
|
log.Debug("whois: queue is full")
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:44:19 +03:00
|
|
|
// workerLoop processes the IP addresses it got from the channel and associates
|
|
|
|
// the retrieving WHOIS info with a client.
|
2021-06-18 18:13:36 +03:00
|
|
|
func (w *WHOIS) workerLoop() {
|
2021-04-02 17:30:39 +03:00
|
|
|
for ip := range w.ipChan {
|
2021-01-26 19:44:19 +03:00
|
|
|
info := w.process(context.Background(), ip)
|
2021-04-02 17:30:39 +03:00
|
|
|
if info == nil {
|
2019-09-18 18:44:27 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:53:28 +03:00
|
|
|
w.clients.SetWHOISInfo(ip, info)
|
2019-09-18 18:44:27 +03:00
|
|
|
}
|
|
|
|
}
|