-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
144 lines (119 loc) · 2.79 KB
/
main.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
package main
import (
"encoding/binary"
"encoding/csv"
"math/big"
"net"
"net/http"
"sort"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator"
"github.com/guregu/null/v5"
)
type IpAddressRange struct {
start *big.Int
end *big.Int
country string
}
func downloadCsv() []IpAddressRange {
arr := []IpAddressRange{}
urls := []string{
"https://cdn.jsdelivr.net/gh/sapics/ip-location-db/geo-whois-asn-country/geo-whois-asn-country-ipv4-num.csv",
"https://cdn.jsdelivr.net/gh/sapics/ip-location-db/geo-asn-country/geo-asn-country-ipv6-num.csv",
}
for _, url := range urls {
resp, err := http.Get(url)
if err != nil {
return arr
}
defer resp.Body.Close()
r := csv.NewReader(resp.Body)
for {
record, err := r.Read()
if err != nil {
break
}
start, ok := new(big.Int).SetString(record[0], 10)
if !ok {
continue
}
end, ok := new(big.Int).SetString(record[1], 10)
if !ok {
continue
}
arr = append(arr, IpAddressRange{start, end, record[2]})
}
}
return arr
}
type IpAddress struct {
IpAddr null.String `json:"ip_addr"`
IpV6 null.Bool `json:"ip_v6"`
}
func parseIpAddress(rawIpAddr string) *IpAddress {
v := validator.New()
err := v.Var(rawIpAddr, "required,ip4_addr")
if err == nil {
return &IpAddress{
IpAddr: null.StringFrom(rawIpAddr),
IpV6: null.BoolFrom(false),
}
}
err = v.Var(rawIpAddr, "required,ip6_addr")
if err == nil {
return &IpAddress{
IpAddr: null.StringFrom(rawIpAddr),
IpV6: null.BoolFrom(true),
}
}
return nil
}
type ApiResponse struct {
Ok bool `json:"ok"`
Country null.String `json:"country"`
IpAddress
}
func main() {
arr := downloadCsv()
sort.Slice(arr, func(i, j int) bool {
return arr[i].start.Cmp(arr[j].start) == -1
})
r := gin.New()
r.Use(gin.Recovery())
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"*"},
AllowHeaders: []string{},
AllowCredentials: true,
}))
r.GET("/getIpInfo", func(c *gin.Context) {
ipAddress := parseIpAddress(c.Query("addr"))
if ipAddress != nil && ipAddress.IpAddr.Valid {
addr := net.ParseIP(ipAddress.IpAddr.String)
if addr != nil {
ipNum := big.NewInt(0)
if addr.To4() != nil {
ipNum = new(big.Int).SetUint64(uint64(binary.BigEndian.Uint32(addr.To4())))
} else {
ipNum.SetBytes(addr)
}
idx := sort.Search(len(arr), func(i int) bool {
return arr[i].start.Cmp(ipNum) > 0
})
if idx > 0 && arr[idx-1].end.Cmp(ipNum) >= 0 && ipNum.Sign() != 0 {
c.JSON(http.StatusOK, ApiResponse{
Ok: true,
Country: null.StringFrom(arr[idx-1].country),
IpAddress: *ipAddress,
})
return
}
}
}
c.JSON(http.StatusOK, ApiResponse{
Ok: false,
})
})
r.Run(":8080")
}