Documentation
¶
Overview ¶
Package qqwry implements download and query IP geo-location information facilities for the famous qqwry.dat database.
Inspired from github.com/tonywubo/qqwry, with bug fixes, unit tests and performance improvements.
Example (Download) ¶
package main import ( "fmt" "log" "github.com/chuangbo/xip/v2/pkg/qqwry" ) func main() { // Check remote version, and get the key to decrypt key, remoteVersion, err := qqwry.GetUpdateInfo() // Check local database verion db, err := qqwry.Open("testdata/qqwry.dat") if err == nil { localVersion := db.Version() // Skip update if local version is same as remote version if qqwry.SameVersion(remoteVersion, localVersion) { log.Fatal("No need to download") } } // Open the download url contentLength, reader, err := qqwry.Download(key) if err != nil { log.Fatal(err) } defer reader.Close() fmt.Printf("ContentLength: %d\n", contentLength) // Save to local disk // io.Copy(f, reader) }
Output:
Example (Query) ¶
package main import ( "fmt" "log" "net" "github.com/chuangbo/xip/v2/pkg/qqwry" ) func main() { db, err := qqwry.Open("testdata/qqwry.dat") if err != nil { log.Fatal(err) } ip := net.ParseIP("192.168.1.1") result, err := db.Query(ip) if err != nil { log.Fatal(err) } fmt.Printf("Country: %s, City: %s\n", result.Country, result.City) }
Output: Country: 局域网, City: 对方和您在同一内部网
Index ¶
Examples ¶
Constants ¶
const ( // KeyURL is url to download key KeyURL = "http://update.cz88.net/ip/copywrite.rar" // DbURL is url to download qqwry database DbURL = "http://update.cz88.net/ip/qqwry.rar" )
Variables ¶
This section is empty.
Functions ¶
func Download ¶
func Download(key uint32) (int64, io.ReadCloser, error)
Download create a io.ReadCloser from database url with provided key.
The first return value is total bytes of content length, the second return value is a io.ReadCloser, caller should read the content the close after use.
func GetUpdateInfo ¶
GetUpdateInfo reads key and version from key url.
The first return value is the key to decrypt the downloaded database. The second return value is remote version from the key url.
func SameVersion ¶
SameVersion compare remote version from copywrite.rar and local version from qqwry database.
Version formats: remote - "纯真IP地址数据库 2021年02月25日". local - "2021年02月02日IP数据".
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is qqwry database instance.
func (*DB) Dump ¶
func (db *DB) Dump()
Dump all the records from the database.
This function directly prints lots of lines to stdout.