-
Notifications
You must be signed in to change notification settings - Fork 51
/
ip17mon.go
54 lines (44 loc) · 896 Bytes
/
ip17mon.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
52
53
54
package ip17mon
import (
"errors"
"path/filepath"
"strings"
"github.com/wangtuanjie/ip17mon/datx"
"github.com/wangtuanjie/ip17mon/ipdb"
"github.com/wangtuanjie/ip17mon/internal/proto"
)
var def Locator
type (
Locator = proto.Locator
LocationInfo = proto.LocationInfo
)
func Init(dataFile string) {
var err error
def, err = New(dataFile)
if err != nil {
panic(err)
}
}
func InitWithDatx(b []byte) {
def = datx.NewWithDatx(b)
}
func InitWithIpdb(b []byte) {
var err error
def, err = ipdb.NewWith(b)
if err != nil {
panic(err)
}
}
func Find(ipstr string) (*LocationInfo, error) {
return def.Find(ipstr)
}
func New(dataFile string) (loc Locator, err error) {
switch strings.ToLower(filepath.Ext(dataFile)) {
case ".dat", ".datx":
return datx.New(dataFile)
case ".ipdb":
return ipdb.New(dataFile)
default:
return nil, errors.New("unsupported file")
}
}