From fa750269e7dff9546574633d8e2c38637c7a8068 Mon Sep 17 00:00:00 2001 From: shuhai Date: Mon, 10 Jul 2017 18:26:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=80=E8=A1=8C=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E5=9F=9F=E5=90=8D=EF=BC=8C=E6=88=96=E8=80=85=E4=B8=8D?= =?UTF-8?q?=E6=98=AF=E6=A0=87=E5=87=86=E5=9F=9F=E5=90=8D=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hosts.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/hosts.go b/hosts.go index 690c3cc..d094f8e 100644 --- a/hosts.go +++ b/hosts.go @@ -205,29 +205,36 @@ func (f *FileHosts) Refresh() { line := scanner.Text() line = strings.TrimSpace(line) + line = strings.Replace(line, "\t", " ", -1) if strings.HasPrefix(line, "#") || line == "" { continue } sli := strings.Split(line, " ") - if len(sli) == 1 { - sli = strings.Split(line, "\t") - } if len(sli) < 2 { continue } - domain := sli[len(sli)-1] ip := sli[0] - if !f.isDomain(domain) || !f.isIP(ip) { + if !f.isIP(ip) { continue } - f.hosts[strings.ToLower(domain)] = ip + // Would have multiple columns of domain in line. + // Such as "127.0.0.1 localhost localhost.domain" on linux. + // The domains may not strict standard, like "local" so don't check with f.isDomain(domain). + for i := 1; i <= len(sli)-1; i++ { + domain := strings.TrimSpace(sli[i]) + if domain == "" { + continue + } + + f.hosts[strings.ToLower(domain)] = ip + } } - logger.Debug("update hosts records from %s", f.file) + logger.Debug("update hosts records from %s, total %d records.", f.file, len(f.hosts)) } func (f *FileHosts) clear() {