-
Notifications
You must be signed in to change notification settings - Fork 1
/
ip.go
51 lines (43 loc) 路 921 Bytes
/
ip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package helper
import (
"embed"
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
"github.com/ua-parser/uap-go/uaparser"
)
//go:embed db/*
var DbFs embed.FS
var IpSearch *xdb.Searcher
func IpParser(ip string) (string, error) {
if IpSearch == nil {
file, err := DbFs.ReadFile("db/ip2region.xdb")
if err != nil {
return "", err
}
searcher, err := xdb.NewWithBuffer(file)
if err != nil {
return "", err
}
IpSearch = searcher
}
region, err := IpSearch.SearchByStr(ip)
if err != nil {
return "", err
}
return region, nil
}
var UapParser *uaparser.Parser
func UaParser(ua string) (*uaparser.Client, error) {
if UapParser == nil {
file, err := DbFs.ReadFile("db/regexes.yaml")
if err != nil {
return nil, err
}
parser, err := uaparser.NewFromBytes(file)
if err != nil {
return nil, err
}
UapParser = parser
}
client := UapParser.Parse(ua)
return client, nil
}