From e69316b95db00ca98958b43493153f21b75e1d7f Mon Sep 17 00:00:00 2001 From: zu1k Date: Tue, 21 Jul 2020 17:23:38 +0800 Subject: [PATCH] fix space after ip --- internal/app/cdn.go | 4 +++- internal/app/parse.go | 15 +++++++-------- internal/{iptools => tools}/ipparser.go | 2 +- internal/{iptools => tools}/iptools_test.go | 2 +- internal/tools/replace.go | 12 ++++++++++++ pkg/qqwry/qqwry.go | 3 +++ 6 files changed, 27 insertions(+), 11 deletions(-) rename internal/{iptools => tools}/ipparser.go (98%) rename internal/{iptools => tools}/iptools_test.go (91%) create mode 100644 internal/tools/replace.go diff --git a/internal/app/cdn.go b/internal/app/cdn.go index 28c83d6..604f163 100644 --- a/internal/app/cdn.go +++ b/internal/app/cdn.go @@ -9,6 +9,8 @@ import ( "regexp" "strings" + "github.com/zu1k/nali/internal/tools" + "github.com/zu1k/nali/constant" "github.com/zu1k/nali/pkg/cdn" @@ -57,7 +59,7 @@ func ReplaceCDNInString(str string) (result string) { if _, found := done[cname]; found { continue } - result = strings.ReplaceAll(result, cname, fmt.Sprintf("%s [%s]", cname, name)) + result = tools.ReplaceAdd(result, cname, fmt.Sprintf("%s [%s]", cname, name)) done[cname] = true } } diff --git a/internal/app/parse.go b/internal/app/parse.go index 50edd53..5fd252f 100644 --- a/internal/app/parse.go +++ b/internal/app/parse.go @@ -3,11 +3,10 @@ package app import ( "fmt" "path/filepath" - "strings" "github.com/zu1k/nali/constant" "github.com/zu1k/nali/internal/ipdb" - "github.com/zu1k/nali/internal/iptools" + "github.com/zu1k/nali/internal/tools" geoip2 "github.com/zu1k/nali/pkg/geoip" "github.com/zu1k/nali/pkg/ipip" "github.com/zu1k/nali/pkg/qqwry" @@ -45,10 +44,10 @@ func ParseIPs(ips []string) { db1 = nil } for _, ip := range ips { - if iptools.ValidIP4(ip) { + if tools.ValidIP4(ip) { result := db0.Find(ip) fmt.Println(formatResult(ip, result)) - } else if iptools.ValidIP6(ip) && db1 != nil { + } else if tools.ValidIP6(ip) && db1 != nil { result := db1.Find(ip) fmt.Println(formatResult(ip, result)) } else { @@ -67,16 +66,16 @@ func ReplaceIPInString(str string) (result string) { } result = str - ip4s := iptools.GetIP4FromString(str) + ip4s := tools.GetIP4FromString(str) for _, ip := range ip4s { info := db0.Find(ip) - result = strings.ReplaceAll(result, ip, formatResult(ip, info)) + result = tools.ReplaceAdd(result, ip, formatResult(ip, info)) } - ip6s := iptools.GetIP6FromString(str) + ip6s := tools.GetIP6FromString(str) for _, ip := range ip6s { info := db1.Find(ip) - result = strings.ReplaceAll(result, ip, formatResult(ip, info)) + result = tools.ReplaceAdd(result, ip, formatResult(ip, info)) } return } diff --git a/internal/iptools/ipparser.go b/internal/tools/ipparser.go similarity index 98% rename from internal/iptools/ipparser.go rename to internal/tools/ipparser.go index 2796974..296b32c 100644 --- a/internal/iptools/ipparser.go +++ b/internal/tools/ipparser.go @@ -1,4 +1,4 @@ -package iptools +package tools import ( "regexp" diff --git a/internal/iptools/iptools_test.go b/internal/tools/iptools_test.go similarity index 91% rename from internal/iptools/iptools_test.go rename to internal/tools/iptools_test.go index 9cd3fa3..b00aa47 100644 --- a/internal/iptools/iptools_test.go +++ b/internal/tools/iptools_test.go @@ -1,4 +1,4 @@ -package iptools +package tools import ( "fmt" diff --git a/internal/tools/replace.go b/internal/tools/replace.go new file mode 100644 index 0000000..c30a58f --- /dev/null +++ b/internal/tools/replace.go @@ -0,0 +1,12 @@ +package tools + +import "strings" + +func ReplaceAdd(origin string, old string, new string) string { + subLen := len(new) - len(old) + wanted := old + strings.Repeat(" ", subLen) + if strings.Contains(origin, wanted) { + return strings.ReplaceAll(origin, wanted, new) + } + return strings.ReplaceAll(origin, old, new) +} diff --git a/pkg/qqwry/qqwry.go b/pkg/qqwry/qqwry.go index 2abfaca..17aa871 100644 --- a/pkg/qqwry/qqwry.go +++ b/pkg/qqwry/qqwry.go @@ -109,6 +109,9 @@ func (db QQwry) Find(ip string) (res string) { country = strings.ReplaceAll(country, " CZ88.NET", "") area = strings.ReplaceAll(area, " CZ88.NET", "") + if area == "" { + return country + } return fmt.Sprintf("%s %s", country, area) }