From 95eeccde8f43ac258386369cab6d0226ca1eef42 Mon Sep 17 00:00:00 2001
From: Simon Zolin <s.zolin@adguard.com>
Date: Mon, 23 Sep 2019 20:41:14 +0300
Subject: [PATCH] + whois: use "descr" or "netname" in case there's no
 "orgname"

---
 home/clients.go |  2 ++
 home/whois.go   | 41 +++++++++++++++++++++++++++++++++++------
 2 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/home/clients.go b/home/clients.go
index 7fcfeed3..061a8935 100644
--- a/home/clients.go
+++ b/home/clients.go
@@ -287,12 +287,14 @@ func (clients *clientsContainer) SetWhoisInfo(ip string, info [][]string) {
 	if ok {
 		c.WhoisInfo = info
 		log.Debug("Clients: set WHOIS info for client %s: %v", c.Name, c.WhoisInfo)
+		return
 	}
 
 	ch, ok := clients.ipHost[ip]
 	if ok {
 		ch.WhoisInfo = info
 		log.Debug("Clients: set WHOIS info for auto-client %s: %v", ch.Host, ch.WhoisInfo)
+		return
 	}
 
 	ch = ClientHost{
diff --git a/home/whois.go b/home/whois.go
index f5dd2ab2..ccd5e51e 100644
--- a/home/whois.go
+++ b/home/whois.go
@@ -8,6 +8,8 @@ import (
 	whois "github.com/likexian/whois-go"
 )
 
+const maxValueLength = 250
+
 // Whois - module context
 type Whois struct {
 	clients *clientsContainer
@@ -26,9 +28,19 @@ func initWhois(clients *clientsContainer) *Whois {
 	return &w
 }
 
+// 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] + "..."
+}
+
 // Parse plain-text data from the response
 func whoisParse(data string) map[string]string {
 	m := map[string]string{}
+	descr := ""
+	netname := ""
 	lines := strings.Split(data, "\n")
 	for _, ln := range lines {
 		ln = strings.TrimSpace(ln)
@@ -45,14 +57,31 @@ func whoisParse(data string) map[string]string {
 		k = strings.ToLower(k)
 		v := strings.TrimSpace(kv[1])
 
-		if k == "orgname" || k == "org-name" {
-			m["orgname"] = v
-		} else if k == "city" {
-			m["city"] = v
-		} else if k == "country" {
-			m["country"] = v
+		switch k {
+		case "org-name":
+			m["orgname"] = trimValue(v)
+		case "orgname":
+			fallthrough
+		case "city":
+			fallthrough
+		case "country":
+			m[k] = trimValue(v)
+
+		case "descr":
+			descr = v
+		case "netname":
+			netname = v
 		}
 	}
+
+	// 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)
+	}
+
 	return m
 }