package net
import (
)
const cacheMaxAge = 5 * time.Second
func ( string) string {
var IP
var string
= parseIPv4()
if == nil {
, = parseIPv6Zone()
}
if == nil {
return ""
}
if == "" {
return .String()
}
return .String() + "%" +
}
var hosts struct {
sync.Mutex
byName map[string][]string
byAddr map[string][]string
expire time.Time
path string
mtime time.Time
size int64
}
func () {
:= time.Now()
:= testHookHostsPath
if .Before(hosts.expire) && hosts.path == && len(hosts.byName) > 0 {
return
}
, , := stat()
if == nil && hosts.path == && hosts.mtime.Equal() && hosts.size == {
hosts.expire = .Add(cacheMaxAge)
return
}
:= make(map[string][]string)
:= make(map[string][]string)
var *file
if , _ = open(); == nil {
return
}
for , := .readLine(); ; , = .readLine() {
if := bytealg.IndexByteString(, '#'); >= 0 {
= [0:]
}
:= getFields()
if len() < 2 {
continue
}
:= parseLiteralIP([0])
if == "" {
continue
}
for := 1; < len(); ++ {
:= absDomainName([]byte([]))
:= []byte([])
lowerASCIIBytes()
:= absDomainName()
[] = append([], )
[] = append([], )
}
}
hosts.expire = .Add(cacheMaxAge)
hosts.path =
hosts.byName =
hosts.byAddr =
hosts.mtime =
hosts.size =
.close()
}
func ( string) []string {
hosts.Lock()
defer hosts.Unlock()
readHosts()
if len(hosts.byName) != 0 {
:= []byte()
lowerASCIIBytes()
if , := hosts.byName[absDomainName()]; {
:= make([]string, len())
copy(, )
return
}
}
return nil
}
func ( string) []string {
hosts.Lock()
defer hosts.Unlock()
readHosts()
= parseLiteralIP()
if == "" {
return nil
}
if len(hosts.byAddr) != 0 {
if , := hosts.byAddr[]; {
:= make([]string, len())
copy(, )
return
}
}
return nil
}