-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_ip_colors.go
58 lines (49 loc) · 1.02 KB
/
utils_ip_colors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package glog
import (
"fmt"
"strings"
"sync"
)
type ipColCache struct {
*sync.Mutex
data map[string]int
}
func newIPColorCache() *ipColCache {
ipcc := &ipColCache{
Mutex: &sync.Mutex{},
data: map[string]int{},
}
return ipcc
}
func (ipcc *ipColCache) get(ip string) int {
ipcc.Lock()
defer ipcc.Unlock()
if v, ok := ipcc.data[ip]; ok {
return v
}
parts := strings.Split(ip, ".")
pt := 0.0
pl := float64(len(parts))
for _, p := range parts {
f, _ := GetFloat(p)
pt += f / pl
}
ipcc.data[ip] = int(32.0 + Max(0.0, Min(185.0, 185.0*(pt/255.0))))
return ipcc.data[ip]
}
var ipColorCache *ipColCache = newIPColorCache()
func enrichAndColorIPv4(ip string, useReverseDNS bool) string {
revDNS := "N/A"
if useReverseDNS {
if _, ok := LoggerConfig.reverseDNSCache[ip]; !ok {
LoggerConfig.reverseDNSCache[ip] = ReverseDNS(ip)
}
revDNS = LoggerConfig.reverseDNSCache[ip]
}
ipColor := ipColorCache.get(ip)
if revDNS != "N/A" {
ip = fmt.Sprintf("%s (%s)", ip, revDNS)
}
ip = Wrap(ip, ipColor)
return ip
}