-
Notifications
You must be signed in to change notification settings - Fork 4
/
geoip.go
98 lines (95 loc) · 2.65 KB
/
geoip.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
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"net"
"strconv"
)
func GetGeoIP(pip net.IP) ([]byte, error) {
if pip == nil {
return nil, fmt.Errorf("Bad Request (IP)")
}
if kv == nil || kv.db == nil {
return nil, fmt.Errorf("Service Unavailable")
}
if pip.To4() != nil {
//Test, Google DNS - https://localhost:8443/tr/v1/ppi/geoip?ip=8.8.8.8
ips := strconv.FormatInt(int64(binary.BigEndian.Uint32(pip.To4())), 10)
ipp := FixedLengthNumberString(10, ips)
key := IDX_PREFIX_IPV4 + ipp
var value []byte
find := func(val []byte) error {
if len(val) > 0 {
value = val
return nil
} else {
iter := kv.db.NewIter(kv.ro)
defer iter.Close()
for iter.SeekLT([]byte(key)); iteratorIsValid(iter); iter.Next() {
k := iter.Key()
val = iter.Value()
var geoip GeoIP
err := json.Unmarshal(val, &geoip)
if err != nil {
fmt.Println("Error marshalling :", key, string(k), string(val))
return fmt.Errorf("Server Error (IP)")
} else {
if key > string(k) && FixedLengthNumberString(10, geoip.IPEnd) > FixedLengthNumberString(10, ipp) {
value = val
return nil
} else {
fmt.Println("Not found:", key, string(k), string(val))
return fmt.Errorf("Not Found (IP)")
}
}
}
}
return fmt.Errorf("Not Found (IP)")
}
return value, kv.GetValue([]byte(key), find)
} else if pip.To16() != nil {
//Test, Google DNS - https://localhost:8443/tr/v1/ppi/geoip?ip=2001:4860:4860::8888
var hi uint64
var lo uint64
buf := bytes.NewReader(pip)
binary.Read(buf, binary.BigEndian, &hi)
binary.Read(buf, binary.BigEndian, &lo)
ips := New(lo, hi).String()
ipp := FixedLengthNumberString(39, ips)
key := IDX_PREFIX_IPV6 + ipp
var value []byte
find := func(val []byte) error {
if len(val) > 0 {
value = val
return nil
} else {
iter := kv.db.NewIter(kv.ro)
defer iter.Close()
for iter.SeekLT([]byte(key)); iteratorIsValid(iter); iter.Next() {
k := iter.Key()
val = iter.Value()
var geoip GeoIP
err := json.Unmarshal(val, &geoip)
if err != nil {
fmt.Println("Error marshalling :", key, string(k), string(val))
return fmt.Errorf("Server Error (IP)")
} else {
if key > string(k) && FixedLengthNumberString(39, geoip.IPEnd) > FixedLengthNumberString(39, ipp) {
value = val
return nil
} else {
fmt.Println("Not found:", key, string(k), string(val))
return fmt.Errorf("Not Found (IP)")
}
}
}
}
return fmt.Errorf("Not Found (IP)")
}
return value, kv.GetValue([]byte(key), find)
} else {
return nil, fmt.Errorf("Bad Request (IP)")
}
}