add comment

This commit is contained in:
zu1k 2020-07-17 09:50:06 +08:00
parent ea9fd5d6c3
commit 7c49923aa7
9 changed files with 23 additions and 9 deletions

View File

@ -24,6 +24,7 @@ var rootCmd = &cobra.Command{
},
}
// parse subcommand and run
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err.Error())

View File

@ -1,5 +1,5 @@
package constant
var (
HomePath string
HomePath string // db home path
)

View File

@ -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
)

View File

@ -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))

View File

@ -1,5 +1,6 @@
package ipdb
// ip db interface
type IPDB interface {
Find(ip string) string
}

View File

@ -1,8 +1,9 @@
package ipdb
// ip db type
type IPDBType int
const (
GEOIP2 = iota
QQIP
GEOIP2 = iota // geoip2
QQIP // chunzhen
)

View File

@ -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)

View File

@ -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

View File

@ -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 {