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