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"
|
|
|
|
"io/ioutil"
|
|
|
|
"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"
|
2020-10-30 03:32:02 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/util"
|
2019-10-22 03:11:22 -07:00
|
|
|
"github.com/AdguardTeam/golibs/cache"
|
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
|
|
|
|
|
|
|
// 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,
|
|
|
|
}),
|
|
|
|
ipChan: make(chan net.IP, 255),
|
|
|
|
}
|
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] + "..."
|
|
|
|
}
|
|
|
|
|
2019-09-18 08:44:27 -07:00
|
|
|
// Parse plain-text data from the response
|
|
|
|
func whoisParse(data string) map[string]string {
|
|
|
|
m := map[string]string{}
|
2019-09-23 10:41:14 -07:00
|
|
|
descr := ""
|
|
|
|
netname := ""
|
2019-10-07 09:13:06 -07:00
|
|
|
for len(data) != 0 {
|
2020-02-13 08:42:07 -07:00
|
|
|
ln := util.SplitNext(&data, '\n')
|
2019-10-07 09:13:06 -07:00
|
|
|
if len(ln) == 0 || ln[0] == '#' || ln[0] == '%' {
|
2019-09-18 08:44:27 -07: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 10:41:14 -07:00
|
|
|
switch k {
|
|
|
|
case "org-name":
|
|
|
|
m["orgname"] = trimValue(v)
|
|
|
|
case "orgname":
|
|
|
|
fallthrough
|
|
|
|
case "city":
|
|
|
|
fallthrough
|
|
|
|
case "country":
|
|
|
|
m[k] = trimValue(v)
|
|
|
|
|
|
|
|
case "descr":
|
2019-10-07 09:27:16 -07:00
|
|
|
if len(descr) == 0 {
|
|
|
|
descr = v
|
|
|
|
}
|
2019-09-23 10:41:14 -07:00
|
|
|
case "netname":
|
|
|
|
netname = v
|
2019-10-07 09:13:06 -07: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 08:44:27 -07:00
|
|
|
}
|
|
|
|
}
|
2019-09-23 10:41:14 -07:00
|
|
|
|
|
|
|
// descr or netname -> orgname
|
|
|
|
_, ok := m["orgname"]
|
|
|
|
if !ok && len(descr) != 0 {
|
|
|
|
m["orgname"] = trimValue(descr)
|
|
|
|
} else if !ok && len(netname) != 0 {
|
|
|
|
m["orgname"] = trimValue(netname)
|
|
|
|
}
|
|
|
|
|
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-01-26 09:44:19 -07:00
|
|
|
func (w *Whois) query(ctx context.Context, target, serverAddr string) (string, error) {
|
2019-10-07 09:13:06 -07:00
|
|
|
addr, _, _ := net.SplitHostPort(serverAddr)
|
|
|
|
if addr == "whois.arin.net" {
|
|
|
|
target = "n + " + target
|
|
|
|
}
|
2021-01-26 09:44:19 -07:00
|
|
|
conn, err := customDialContext(ctx, "tcp", serverAddr)
|
2019-10-07 09:13:06 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2020-11-23 04:14:08 -07:00
|
|
|
connReadCloser, err := aghio.LimitReadCloser(conn, MaxConnReadSize)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer connReadCloser.Close()
|
|
|
|
|
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.
|
|
|
|
data, err := ioutil.ReadAll(connReadCloser)
|
2019-10-07 09:13:06 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-01-26 09:44:19 -07:00
|
|
|
func (w *Whois) process(ctx context.Context, ip net.IP) [][]string {
|
2019-09-18 08:44:27 -07:00
|
|
|
data := [][]string{}
|
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)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Whois: IP:%s response: %d bytes", ip, len(resp))
|
|
|
|
|
|
|
|
m := whoisParse(resp)
|
|
|
|
|
|
|
|
keys := []string{"orgname", "country", "city"}
|
|
|
|
for _, k := range keys {
|
|
|
|
v, found := m[k]
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pair := []string{k, v}
|
|
|
|
data = append(data, pair)
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
for {
|
2020-11-06 02:15:08 -07:00
|
|
|
ip := <-w.ipChan
|
2019-09-18 08:44:27 -07:00
|
|
|
|
2021-01-26 09:44:19 -07:00
|
|
|
info := w.process(context.Background(), ip)
|
2019-09-18 08:44:27 -07:00
|
|
|
if len(info) == 0 {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|