qqwry

package
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2021 License: MIT Imports: 10 Imported by: 0

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

View Source
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

func GetUpdateInfo() (uint32, string, error)

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

func SameVersion(remote, local string) bool

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 Open

func Open(file string) (*DB, error)

Open the qqwry.dat database.

func (*DB) Dump

func (db *DB) Dump()

Dump all the records from the database.

This function directly prints lots of lines to stdout.

func (*DB) Query

func (db *DB) Query(ip net.IP) (*Record, error)

Query ip geo location information from giving net.IP.

func (*DB) Total

func (db *DB) Total() uint32

Total returns the total number of records for the database

func (*DB) Version

func (db *DB) Version() string

Version return the version record for the database, from the last record for 255.255.255.255.

type Record

type Record struct {
	Country, City string
}

Record is query result.

func (*Record) String

func (record *Record) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL