mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-12-10 09:51:55 +08:00
IP库加密/其他对IP库的改进
This commit is contained in:
@@ -7,26 +7,48 @@ import (
|
||||
"compress/gzip"
|
||||
_ "embed"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
//go:embed internal-ip-library.db
|
||||
var ipLibraryData []byte
|
||||
|
||||
var defaultLibrary = NewIPLibrary()
|
||||
var commonLibrary *IPLibrary
|
||||
|
||||
var libraryLocker = &sync.Mutex{} // 为了保持加载顺序性
|
||||
|
||||
func DefaultIPLibraryData() []byte {
|
||||
return ipLibraryData
|
||||
}
|
||||
|
||||
// InitDefault 加载默认的IP库
|
||||
func InitDefault() error {
|
||||
defaultLibrary.reader = nil
|
||||
return defaultLibrary.InitFromData(ipLibraryData)
|
||||
libraryLocker.Lock()
|
||||
defer libraryLocker.Unlock()
|
||||
|
||||
if commonLibrary != nil {
|
||||
defaultLibrary = commonLibrary
|
||||
return nil
|
||||
}
|
||||
|
||||
var library = NewIPLibrary()
|
||||
err := library.InitFromData(ipLibraryData, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
commonLibrary = library
|
||||
defaultLibrary = commonLibrary
|
||||
return nil
|
||||
}
|
||||
|
||||
// Lookup 查询IP信息
|
||||
func Lookup(ip net.IP) *QueryResult {
|
||||
return defaultLibrary.Lookup(ip)
|
||||
}
|
||||
|
||||
// LookupIP 查询IP信息
|
||||
func LookupIP(ip string) *QueryResult {
|
||||
return defaultLibrary.LookupIP(ip)
|
||||
}
|
||||
@@ -43,10 +65,19 @@ func NewIPLibraryWithReader(reader *Reader) *IPLibrary {
|
||||
return &IPLibrary{reader: reader}
|
||||
}
|
||||
|
||||
func (this *IPLibrary) InitFromData(data []byte) error {
|
||||
func (this *IPLibrary) InitFromData(data []byte, password string) error {
|
||||
if len(data) == 0 || this.reader != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(password) > 0 {
|
||||
srcData, err := NewEncrypt().Decode(data, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data = srcData
|
||||
}
|
||||
|
||||
var reader = bytes.NewReader(data)
|
||||
gzipReader, err := gzip.NewReader(reader)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user