-
Notifications
You must be signed in to change notification settings - Fork 4
/
ip_item.go
51 lines (43 loc) · 1.04 KB
/
ip_item.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
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package iplibrary
import (
"bytes"
"encoding/binary"
"github.com/iwind/TeaGo/types"
)
type ipItem struct {
IPFrom uint64
IPTo uint64
Region *ipRegion
}
type ipRegion struct {
CountryId uint32
ProvinceId uint32
CityId uint32
TownId uint32
ProviderId uint32
}
func (this *ipItem) AsBinary() ([]byte, error) {
var buf = &bytes.Buffer{}
err := binary.Write(buf, binary.BigEndian, this)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func HashRegion(countryId uint32, provinceId uint32, cityId uint32, townId uint32, providerId uint32) string {
var providerHash = ""
if providerId > 0 {
providerHash = "_" + types.String(providerId)
}
if townId > 0 {
return "t" + types.String(townId) + providerHash
}
if cityId > 0 {
return "c" + types.String(cityId) + providerHash
}
if provinceId > 0 {
return "p" + types.String(provinceId) + providerHash
}
return "a" + types.String(countryId) + providerHash
}