/
ipservice.go
214 lines (199 loc) · 5.37 KB
/
ipservice.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package badgegenerator
import (
"encoding/csv"
"fmt"
"io"
"net"
"os"
"sort"
"strconv"
"strings"
"github.com/praserx/ipconv"
"github.com/lavanet/lava/utils"
)
type IpService struct {
DefaultGeolocation int
CountryCsvFilePath string
IpTsvFilePath string
IpCountryData *[]*IpData
}
func InitIpService(defaultGeolocation int, countriesFilePath, ipFilePath string) (*IpService, error) {
service := IpService{DefaultGeolocation: defaultGeolocation, CountryCsvFilePath: countriesFilePath, IpTsvFilePath: ipFilePath}
err := service.ReadIpTsvFileData()
if err != nil {
utils.LavaFormatError("failed to setup the ip service.", err)
}
return &service, err
}
func (service *IpService) readCountryCsvFileData() (*map[string]int, error) {
countries := make(map[string]int)
if len(service.CountryCsvFilePath) == 0 {
utils.LavaFormatWarning("badge is not configured correctly- missing country csv file path.", nil)
return &countries, nil
}
file, err := os.Open(service.CountryCsvFilePath)
if err != nil {
utils.LavaFormatError("error opening country file", err)
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
for {
row, err := reader.Read()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
for _, rowData := range row {
countryData := strings.Split(rowData, ";")
countries[countryData[0]], _ = strconv.Atoi(countryData[len(countryData)-1])
}
}
return &countries, nil
}
func (service *IpService) ReadIpTsvFileData() error {
counties, err := service.readCountryCsvFileData()
if err != nil {
utils.LavaFormatError("error reading country data.", err)
return err
}
if len(service.IpTsvFilePath) == 0 {
utils.LavaFormatWarning("badge is not configured correctly- missing ip tsv file path.", nil)
service.IpCountryData = &[]*IpData{}
return nil
}
file, err := os.Open(service.IpTsvFilePath)
if err != nil {
utils.LavaFormatError("error opening ip file.", err)
return err
}
defer file.Close()
var result []*IpData
// Create a new CSV reader
reader := csv.NewReader(file)
// Read and print each row
for {
row, err := reader.Read()
if err != nil {
if len(row) > 1 {
row = []string{strings.Join(row, "")} // this is a necessary correction
} else if err == io.EOF {
break
} else {
return err
}
}
for _, rowData := range row {
ipData, err := convertRowToIpModel(rowData)
if err != nil {
utils.LavaFormatWarning("error reading ip data", err)
continue
}
geolocation, exist := (*counties)[ipData.CountryCode]
if !exist {
geolocation = service.DefaultGeolocation
}
ipData.Geolocation = geolocation
result = append(result, ipData)
}
}
sort.Slice(result, func(i, j int) bool {
return result[i].FromIp < result[j].FromIp
})
service.IpCountryData = &result
return nil
}
func (service *IpService) SearchForIp(toSearchIp string) (*IpData, error) {
if len(*service.IpCountryData) == 0 {
return nil, fmt.Errorf("ip servive not configured correctly")
}
needle, err := convertStringToIpInt(toSearchIp)
if err != nil {
return nil, err
}
haystack := *service.IpCountryData // for simplicity
low := 0
high := len(haystack) - 1
for low <= high {
median := (low + high) / 2
if haystack[median].FromIp < needle {
low = median + 1
} else {
high = median - 1
}
}
if low == len(haystack) {
return nil, fmt.Errorf("ip not found")
}
if needle >= haystack[low].FromIp && needle <= haystack[low].ToIP {
return haystack[low], nil
}
if needle >= haystack[high].FromIp && needle <= haystack[high].ToIP {
return haystack[high], nil
}
// theoretically we should never come here but, I added this to be on the safe side
for low <= high {
if needle >= haystack[low].FromIp && needle <= haystack[low].ToIP {
return haystack[low], nil
}
low++
}
return nil, fmt.Errorf("ip not found")
}
func convertRowToIpModel(rowData string) (*IpData, error) {
convertRowWith4tabs := func(ipStringDatas []string) (*IpData, error) {
ipSorce := strings.Split(ipStringDatas[0], " ")
if len(ipSorce) != 2 {
return nil, fmt.Errorf("unexpeted ip range on tsv data format. expected 2 separated with space(' ')")
}
fromIpData, err := convertStringToIpInt(ipSorce[0])
if err != nil {
return nil, err
}
toIpData, err := convertStringToIpInt(ipSorce[1])
if err != nil {
return nil, err
}
return &IpData{
FromIp: fromIpData,
ToIP: toIpData,
CountryCode: ipStringDatas[2],
}, nil
}
convertRowWith5tabs := func(ipStringDatas []string) (*IpData, error) {
fromIpData, err := convertStringToIpInt(ipStringDatas[0])
if err != nil {
return nil, err
}
toIpData, err := convertStringToIpInt(ipStringDatas[1])
if err != nil {
return nil, err
}
return &IpData{
FromIp: fromIpData,
ToIP: toIpData,
CountryCode: ipStringDatas[3],
}, nil
}
ipStringDatas := strings.Split(rowData, "\t")
if len(ipStringDatas) == 4 {
return convertRowWith4tabs(ipStringDatas)
} else if len(ipStringDatas) == 5 {
return convertRowWith5tabs(ipStringDatas)
} else {
return nil, fmt.Errorf("invalid tsv data format. expected 4")
}
}
func convertStringToIpInt(sc string) (int64, error) {
if len(sc) == 0 {
return 0, fmt.Errorf("invalid ip length to convert to number")
}
ip := net.ParseIP(sc)
if ip == nil {
return 0, fmt.Errorf("converting ip didn't succeed")
}
number, err := ipconv.IPv4ToInt(ip)
return int64(number), err
}