From 7c49923aa701fc4c7a72106d6d54555939a18973 Mon Sep 17 00:00:00 2001 From: zu1k Date: Fri, 17 Jul 2020 09:50:06 +0800 Subject: [PATCH] add comment --- cmd/root.go | 1 + constant/path.go | 2 +- constant/version.go | 6 +++--- internal/app/parse.go | 4 ++++ internal/ipdb/db.go | 1 + internal/ipdb/ipdb.go | 5 +++-- pkg/geoip/geoip.go | 3 +++ pkg/qqwry/qqwry.go | 9 ++++++--- pkg/qqwry/update.go | 1 + 9 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index cd3aa34..211bc7f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,6 +24,7 @@ var rootCmd = &cobra.Command{ }, } +// parse subcommand and run func Execute() { if err := rootCmd.Execute(); err != nil { log.Fatal(err.Error()) diff --git a/constant/path.go b/constant/path.go index 6204f87..ec2c0db 100644 --- a/constant/path.go +++ b/constant/path.go @@ -1,5 +1,5 @@ package constant var ( - HomePath string + HomePath string // db home path ) diff --git a/constant/version.go b/constant/version.go index bfc944e..02b0172 100644 --- a/constant/version.go +++ b/constant/version.go @@ -1,8 +1,8 @@ package constant -const Name = "Nali" +const Name = "Nali" // bin name var ( - Version = "unknown version" - BuildTime = "unknown time" + Version = "unknown version" // version + BuildTime = "unknown time" //build time ) diff --git a/internal/app/parse.go b/internal/app/parse.go index 2439ca6..bd12e4b 100644 --- a/internal/app/parse.go +++ b/internal/app/parse.go @@ -18,6 +18,7 @@ var ( geoip geoip2.GeoIP ) +// init ip db content func InitIPDB() { qqip = qqwry.NewQQwry(filepath.Join(constant.HomePath, "qqwry.dat")) //geoip = geoip2.NewGeoIP(filepath.Join(constant.HomePath, "GeoLite2-City.mmdb")) @@ -25,6 +26,7 @@ func InitIPDB() { db = qqip } +// set db to use func SetDB(dbName ipdb.IPDBType) { switch dbName { case ipdb.GEOIP2: @@ -34,12 +36,14 @@ func SetDB(dbName ipdb.IPDBType) { } } +// parse several ips func ParseIPs(ips []string) { for _, ip := range ips { ParseIP(ip) } } +// parse one ip func ParseIP(ip string) { result := db.Find(ip) fmt.Println(formatResult(ip, result)) diff --git a/internal/ipdb/db.go b/internal/ipdb/db.go index c34f9d9..37e6bc8 100644 --- a/internal/ipdb/db.go +++ b/internal/ipdb/db.go @@ -1,5 +1,6 @@ package ipdb +// ip db interface type IPDB interface { Find(ip string) string } diff --git a/internal/ipdb/ipdb.go b/internal/ipdb/ipdb.go index 14caba5..e9d0f65 100644 --- a/internal/ipdb/ipdb.go +++ b/internal/ipdb/ipdb.go @@ -1,8 +1,9 @@ package ipdb +// ip db type type IPDBType int const ( - GEOIP2 = iota - QQIP + GEOIP2 = iota // geoip2 + QQIP // chunzhen ) diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index 7b2a0a7..069095a 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -8,10 +8,12 @@ import ( "github.com/oschwald/geoip2-golang" ) +// GeoIP2 type GeoIP struct { db *geoip2.Reader } +// new geoip from db file func NewGeoIP(filePath string) GeoIP { db, err := geoip2.Open(filePath) if err != nil { @@ -20,6 +22,7 @@ func NewGeoIP(filePath string) GeoIP { return GeoIP{db: db} } +// find ip info func (g GeoIP) Find(ip string) string { ipData := net.ParseIP(ip) record, err := g.db.City(ipData) diff --git a/pkg/qqwry/qqwry.go b/pkg/qqwry/qqwry.go index 6bb04c2..12c3141 100644 --- a/pkg/qqwry/qqwry.go +++ b/pkg/qqwry/qqwry.go @@ -12,6 +12,7 @@ import ( "golang.org/x/text/encoding/simplifiedchinese" ) +// db file info with data type FileInfo struct { Data []byte FilePath string @@ -19,17 +20,19 @@ type FileInfo struct { IPNum int64 } +// qq ip db type QQwry struct { data *FileInfo offset int64 } const ( - IndexLen = 7 - RedirectMode1 = 0x01 - RedirectMode2 = 0x02 + IndexLen = 7 // index length + RedirectMode1 = 0x01 // one mode + RedirectMode2 = 0x02 //another mode ) +// new db from path func NewQQwry(filePath string) QQwry { var tmpData []byte var fileInfo FileInfo diff --git a/pkg/qqwry/update.go b/pkg/qqwry/update.go index 6d77812..be871e1 100644 --- a/pkg/qqwry/update.go +++ b/pkg/qqwry/update.go @@ -22,6 +22,7 @@ func getKey() (uint32, error) { } } +// get db content from mirror func GetOnline() ([]byte, error) { resp, err := http.Get("https://qqwry.mirror.noc.one/qqwry.rar") if err != nil {