-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
327 lines (266 loc) · 8.31 KB
/
helpers.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"math/rand/v2"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/seancfoley/ipaddress-go/ipaddr"
"github.com/praserx/ipconv"
)
func decompressFile(filePath string, compression string) error {
reader, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer reader.Close()
uncompressedStream, err := gzip.NewReader(reader)
if err != nil {
panic(err)
}
decompressedFilePath := strings.Replace(filePath, compression, "", -1)
decompressedFile, err := os.Create(decompressedFilePath)
if err != nil {
panic(err)
}
defer decompressedFile.Close()
fmt.Println("decompressing: " + filePath)
buf := make([]byte, 1024)
for {
_, readErr := uncompressedStream.Read(buf)
if readErr != nil && !errors.Is(readErr, io.EOF) {
panic(readErr)
}
_, writeErr := decompressedFile.Write(buf)
if writeErr != nil {
panic(writeErr);
}
if errors.Is(readErr, io.EOF) {
break
}
}
return err
}
func durationUntil(timeUntil string) time.Duration {
updateTime := strings.Split(timeUntil, ":")
hours, _ := strconv.Atoi(updateTime[0])
minutes, _ := strconv.Atoi(updateTime[1])
nsNow := time.Now().Nanosecond()
secondsNow := time.Now().Second()
minutesNow := time.Now().Minute()
hoursNow := time.Now().Hour()
adjNanoseconds := 0
if nsNow > 0 {
adjNanoseconds = 1000000000 - nsNow
secondsNow++
}
adjSeconds := 0
if secondsNow > 0 {
adjSeconds = 60 - secondsNow
minutesNow++
}
adjMinutes := 60 - (minutesNow - minutes)
if minutesNow > minutes {
hoursNow++
}
adjHours := 0
if hoursNow >= hours {
adjHours = hoursNow - hours
} else {
adjHours = 24 - (hoursNow - hours)
//daysNow++
}
duration, err := time.ParseDuration(fmt.Sprintf("%dh%dm%ds%dns", adjHours, adjMinutes, adjSeconds, adjNanoseconds))
if err != nil {
panic(err);
}
return duration
}
func fetchIP(ipString string) (*Ip, error) {
start := time.Now()
ip := net.ParseIP(ipString)
if ip == nil || ip.IsPrivate() || ip.IsLoopback() {
return nil, errors.New("invalid IP address passed (" + ipString + "); private / loopback IP ranges are not processed")
}
IpResult := dbIp(ip)
IpResult.Milliseconds = time.Now().Sub(start).Milliseconds()
IpResult.Microseconds = time.Now().Sub(start).Microseconds()
return IpResult, nil
}
func fetchIPJson(ipString string) ([]byte, error) {
ipResult, err := fetchIP(ipString)
if err != nil {
return nil, err
}
jsonResult, err := json.Marshal(ipResult)
if err != nil {
return nil, errors.New("system error")
}
return jsonResult, nil
}
func fileExists(filePath string) bool {
_, err := os.Stat(filePath)
if err != nil {
return false
}
return true
}
func fileReadSmall(filePath string) string {
content, err := os.ReadFile(filePath)
if err != nil {
panic(err)
}
return string(content)
}
func fileWriteSmall(filePath string, content string) {
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
panic(err)
}
defer file.Close()
_, err = file.WriteString(content)
if err != nil {
panic(err)
}
}
func findIPRanges(ipRangeStart string, ipRangeEnd string) []*net.IPNet {
ipStart := ipaddr.NewIPAddressString(ipRangeStart)
ipEnd := ipaddr.NewIPAddressString(ipRangeEnd)
addressStart := ipStart.GetAddress()
addressEnd := ipEnd.GetAddress()
ipRange := addressStart.SpanWithRange(addressEnd)
rangeSlice := ipRange.SpanWithPrefixBlocks()
var ipNets []*net.IPNet
for _, val := range rangeSlice {
_, network, err := net.ParseCIDR(val.String())
if err != nil {
panic(err)
}
ipNets = append(ipNets, network)
}
return ipNets
}
func getEtag(url string) string {
resp, err := http.Head(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
etag, ok := resp.Header["Etag"]
if ok {
return etag[0];
}
return ""
}
func getIpVersion(ipString string) int {
ipVersion := 4
if strings.Contains(ipString, ":") {
ipVersion = 6
}
return ipVersion
}
func hasASNDatabase() bool {
return len(os.Getenv("ASN")) > 0
}
func hasCityDatabase() bool {
return len(os.Getenv("CITY")) > 0
}
func hasCountryDatabase() bool {
return len(os.Getenv("COUNTRY")) > 0
}
func ipv4ToNumber(ipString string) int64 {
ip, ipVersion, err := ipconv.ParseIP(ipString);
if err == nil && ipVersion == 4 {
number, err := ipconv.IPv4ToInt(ip)
if err == nil {
return int64(number)
}
panic(err)
}
return 0
}
func ipv6ToNumber(ipString string) string {
ip, ipVersion, err := ipconv.ParseIP(ipString);
if err == nil && ipVersion == 16 {
arr, err := ipconv.IPv6ToInt(ip)
if err == nil {
bigInt := big.NewInt(0)
for _, val := range arr {
newNigInt := new(big.Int).SetUint64(val)
bigInt.Add(bigInt, newNigInt)
}
return bigInt.String()
}
panic(err)
}
return ""
}
func isIpv4Reserved(ip string) bool {
return strings.HasPrefix(ip, "0.") || strings.HasPrefix(ip, "127.") || strings.HasPrefix(ip, "10.") ||
strings.HasPrefix(ip, "100.64.") || strings.HasPrefix(ip, "169.254.") || strings.HasPrefix(ip, "172.16.") ||
strings.HasPrefix(ip, "172.17.") || strings.HasPrefix(ip, "172.18.") || strings.HasPrefix(ip, "172.19.") ||
strings.HasPrefix(ip, "172.20.") || strings.HasPrefix(ip, "172.21.") || strings.HasPrefix(ip, "172.22.") ||
strings.HasPrefix(ip, "172.23.") || strings.HasPrefix(ip, "172.24.") || strings.HasPrefix(ip, "172.25.") ||
strings.HasPrefix(ip, "172.26.") || strings.HasPrefix(ip, "172.27.") || strings.HasPrefix(ip, "172.28.") ||
strings.HasPrefix(ip, "172.29.") || strings.HasPrefix(ip, "172.30.") || strings.HasPrefix(ip, "172.31.") ||
strings.HasPrefix(ip, "192.0.0.") || strings.HasPrefix(ip, "192.0.2.") || strings.HasPrefix(ip, "192.88.") ||
strings.HasPrefix(ip, "192.168.") || strings.HasPrefix(ip, "198.18.") || strings.HasPrefix(ip, "198.19.") ||
strings.HasPrefix(ip, "198.51.100") || strings.HasPrefix(ip, "203.0.113") || strings.HasPrefix(ip, "224.") ||
strings.HasPrefix(ip, "225.") || strings.HasPrefix(ip, "226.") || strings.HasPrefix(ip, "227.") ||
strings.HasPrefix(ip, "228.") || strings.HasPrefix(ip, "229.") || strings.HasPrefix(ip, "230.") ||
strings.HasPrefix(ip, "231.") || strings.HasPrefix(ip, "232.") || strings.HasPrefix(ip, "233.") ||
strings.HasPrefix(ip, "234.") || strings.HasPrefix(ip, "235.") || strings.HasPrefix(ip, "236.") ||
strings.HasPrefix(ip, "237.") || strings.HasPrefix(ip, "238.") || strings.HasPrefix(ip, "239.") ||
strings.HasPrefix(ip, "240.") || strings.HasPrefix(ip, "241.") || strings.HasPrefix(ip, "242.") ||
strings.HasPrefix(ip, "243.") || strings.HasPrefix(ip, "244.") || strings.HasPrefix(ip, "245.") ||
strings.HasPrefix(ip, "246.") || strings.HasPrefix(ip, "247.") || strings.HasPrefix(ip, "248.") ||
strings.HasPrefix(ip, "249.") || strings.HasPrefix(ip, "250.") || strings.HasPrefix(ip, "251.") ||
strings.HasPrefix(ip, "252.") || strings.HasPrefix(ip, "253.") || strings.HasPrefix(ip, "254.") ||
strings.HasPrefix(ip, "255.")
}
func randomIpv4() string {
numbers := []int{ randomNumber(0, 255), randomNumber(0, 255), randomNumber(0, 255), randomNumber(0, 255) }
var parts []string
for _, number := range numbers {
parts = append(parts, strconv.Itoa(number))
}
ip := strings.Join(parts, ".")
if isIpv4Reserved(ip) {
return randomIpv4()
}
return ip
}
func randomIpv6() string {
var parts []string
first := []string{ "2001", "2002", "2003", "2400", "2401", "2402", "2403", "2404", "2405", "2406", "2407", "2408",
"2409", "240a", "2600", "2601", "2602", "2603", "2604", "2605", "2606", "2607", "2608", "2609", "2610", "2620",
"2800", "2801", "2802", "2803", "2804", "2806", "2a00", "2a01", "2a2", "2a03", "2a04", "2a05", "2a06", "2a07",
"2a08", "2a09", "2a0a", "2a0b", "2a0c", "2a0d", "2a0e", "2a0f", "2a10", "2a11", "2a12", "2a13", "2a14", "2c0e",
"2c0f" }
pick := randomNumber(0, len(first))
parts = append(parts, first[pick])
// Not strictly accurate, but good enough
for i := 0; i < 7; i++ {
parts = append(parts, fmt.Sprintf("%02x", randomNumber(0, 255)) + fmt.Sprintf("%02x", randomNumber(0, 255)))
}
return strings.Join(parts, ":")
}
func randomNumber(min, max int) int {
return rand.IntN(max-min) + min
}
func validApiKey(request *http.Request, enforceKey bool) bool {
if len(os.Getenv("API_KEY")) > 0 || enforceKey {
if len(os.Getenv("API_KEY")) == 0 || request.Header.Get("API-KEY") != os.Getenv("API_KEY") {
return false
}
}
return true
}