-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
213 lines (189 loc) · 4.58 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
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
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/oschwald/geoip2-golang"
)
const (
defaultAddr = ":8080"
defaultMode = "release"
defaultLang = "en"
defaultAsnDB = "./dbip-asn-lite-2021-06.mmdb"
defaultGeoDB = "./dbip-city-lite-2021-06.mmdb"
)
var (
addr, asnDB, geoDB, lang *string
asnReader, locReader *geoip2.Reader
)
type as struct {
Number uint
Name string
}
type location struct {
Continent string
ContinentCode string
Country string
CountryCode string
City string
}
type ipinfo struct {
IP net.IP
Hostnames []string
AS as
Location location
}
func init() {
// Lookup environment variables
var a, m, aDB, gDB, l string
if a = os.Getenv("IPINFO_ADDR"); a == "" {
a = defaultAddr
}
if m = os.Getenv("IPINFO_MODE"); m == "" {
m = defaultMode
}
if aDB = os.Getenv("IPINFO_DB_ASN"); aDB == "" {
aDB = defaultAsnDB
}
if gDB = os.Getenv("IPINFO_DB_GEOIP"); gDB == "" {
gDB = defaultGeoDB
}
if l = os.Getenv("IPINFO_LANG"); l == "" {
l = defaultLang
}
// Parse arguments
addr = flag.String("a", a, "Listening address:port")
mode := flag.String("m", m, "Gin mode (available modes: debug, test, release)")
asnDB = flag.String("db_asn", aDB, "ASN mmdb file")
geoDB = flag.String("db_geoip", gDB, "GeoIP mmdb file")
lang = flag.String("l", l, "Language used for names (available languages: de, en, es, fr, ja, pt-BR, ru, zh-CN)")
flag.Parse()
switch *lang {
case "en", "de", "es", "fr", "ja", "pt-BR", "ru", "zh-CN":
default:
// Fallback to English
*lang = "en"
}
// Set Gin mode
gin.SetMode(*mode)
// Load databases
var err error
asnReader, err = loadDB(*asnDB)
if err != nil {
panic(err)
}
locReader, err = loadDB(*geoDB)
if err != nil {
panic(err)
}
}
func loadDB(file string) (*geoip2.Reader, error) {
return geoip2.Open(file)
}
func unloadDB(db *geoip2.Reader) {
db.Close()
}
func main() {
// Setup router
r := gin.Default()
// ASN
r.GET("/asn/reload", func(c *gin.Context) {
newReader, err := loadDB(*asnDB)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
"message": "failed to load database; using previous one...",
})
} else {
unloadDB(asnReader)
asnReader = newReader
c.JSON(http.StatusOK, gin.H{"message": "asn database reloaded successfully"})
}
})
r.GET("/asn/:ip", func(c *gin.Context) {
if asn, err := getAS(c.Param("ip")); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, asn)
}
})
// GeoIP data
r.GET("/geo/reload", func(c *gin.Context) {
newReader, err := loadDB(*geoDB)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
"message": "failed to load database; using previous one...",
})
} else {
unloadDB(locReader)
locReader = newReader
c.JSON(http.StatusOK, gin.H{"message": "geoip database reloaded successfully"})
}
})
r.GET("/geo/:ip", func(c *gin.Context) {
if geo, err := getLocation(c.Param("ip")); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, geo)
}
})
// IP Info (ASN + GeoIP combined)
r.GET("/ipinfo/:ip", func(c *gin.Context) {
if ipdata, err := getIPInfo(c.Param("ip")); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, ipdata)
}
})
r.Run(*addr)
}
func getAS(ip string) (as, error) {
ipaddr := net.ParseIP(ip)
if ipaddr == nil {
return as{}, fmt.Errorf("invalid ip address %q", ip)
}
data, err := asnReader.ASN(ipaddr)
if err != nil {
return as{}, err
}
return as{
Number: data.AutonomousSystemNumber,
Name: data.AutonomousSystemOrganization,
}, nil
}
func getLocation(ip string) (location, error) {
ipaddr := net.ParseIP(ip)
if ipaddr == nil {
return location{}, fmt.Errorf("invalid ip address %q", ip)
}
geo, err := locReader.City(ipaddr)
if err != nil {
return location{}, err
}
return location{
Continent: geo.Continent.Names[*lang],
ContinentCode: geo.Continent.Code,
Country: geo.Country.Names[*lang],
CountryCode: geo.Country.IsoCode,
City: geo.City.Names[*lang],
}, nil
}
func getIPInfo(ip string) (ipinfo, error) {
ipaddr := net.ParseIP(ip)
if ipaddr == nil {
return ipinfo{}, fmt.Errorf("invalid ip address %q", ip)
}
ptrs, _ := net.LookupAddr(ip)
asData, _ := getAS(ip)
loData, err := getLocation(ip)
return ipinfo{
IP: net.ParseIP(ip),
Hostnames: ptrs,
AS: asData,
Location: loData,
}, err
}