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