-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
379 lines (329 loc) · 9.46 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package main
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/yl2chen/cidranger"
"io/ioutil"
"log"
"net"
"os"
"regexp"
"time"
"bufio"
"strings"
"github.com/gin-gonic/gin"
"github.com/ip2location/ip2location-go"
"github.com/joho/godotenv"
)
type IPEntry struct {
list_index int
value string
}
type Msg struct {
IP string `json:"ip"`
IPStatus string `json:"reputation"`
Data MyIP2L `json:"ip2location"`
Duration string `json:"execution_time"`
OK bool `json:"ok"`
}
type Error struct {
OK bool `json:"ok"`
}
type MyIP2L struct {
Country_short string `json:",omitempty"`
Country_long string `json:",omitempty"`
Region string `json:",omitempty"`
City string `json:",omitempty"`
Isp string `json:",omitempty"`
Latitude float32 `json:",omitempty"`
Longitude float32 `json:",omitempty"`
Domain string `json:",omitempty"`
Zipcode string `json:",omitempty"`
Timezone string `json:",omitempty"`
Netspeed string `json:",omitempty"`
Iddcode string `json:",omitempty"`
Areacode string `json:",omitempty"`
Weatherstationcode string `json:",omitempty"`
Weatherstationname string `json:",omitempty"`
Mcc string `json:",omitempty"`
Mnc string `json:",omitempty"`
Mobilebrand string `json:",omitempty"`
Elevation float32 `json:",omitempty"`
Usagetype string `json:",omitempty"`
}
var IPMap = map[string]IPEntry{}
var ranger cidranger.Ranger
var ourRanger cidranger.Ranger
var firehol_lists = [...]string{
"firehol_level1.netset",
"firehol_level2.netset",
"firehol_level3.netset",
"firehol_level4.netset",
"firehol_abusers_1d.netset",
"firehol_abusers_30d.netset",
"firehol_webserver.netset",
"firehol_webclient.netset",
"firehol_proxies.netset",
"firehol_anonymous.netset",
}
// Loads IP2Location binary file
func loadIp2Location() {
fmt.Println("Loading IP2Location Bin File")
logrus.Info("Loading IP2Location Bin File");
start := time.Now()
ip2location.Open("./ip2location/IP-COUNTRY-REGION-CITY-ISP.BIN")
elapsed := time.Since(start)
logrus.Info(fmt.Sprintf("IP2Country Loaded in %s \n", elapsed))
}
// Loads the IP Whitelists
func loadWhiteList() {
// add our CIDR ranger
start := time.Now();
total := 0
logrus.Info("Reading Whitelist to memory \n")
filename := "whitelist/whitelist.ipset"
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer func() {
if err = f.Close(); err != nil {
log.Fatal(err)
}
}()
s := bufio.NewScanner(f)
for s.Scan() {
line := s.Text()
if !strings.HasPrefix(line, "#") {
_, network, _ := net.ParseCIDR(line)
ourRanger.Insert(cidranger.NewBasicRangerEntry(*network))
total++
}
}
err = s.Err()
if err != nil {
log.Fatal(err)
}
elapsed := time.Since(start);
logrus.Info(fmt.Sprintf("Total %d WhiteList Records added to memory in %s \n", total, elapsed))
}
// Loads Firehole data files to memory
func loadFireHoleData() {
start := time.Now();
total := 0
for i := 0; i < len(firehol_lists); i++ {
logrus.Info(fmt.Sprintf("Reading %dth element of Firehol List [%s] to memory \n", i+1, firehol_lists[i]))
filename := "firehol/" + firehol_lists[i]
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer func() {
if err = f.Close(); err != nil {
log.Fatal(err)
}
}()
s := bufio.NewScanner(f)
for s.Scan() {
line := s.Text()
if !strings.HasPrefix(line, "#") {
isCIDRValue := strings.Contains(line, "/")
if (isCIDRValue) {
_, network, _ := net.ParseCIDR(line)
ranger.Insert(cidranger.NewBasicRangerEntry(*network))
} else {
n := IPEntry{list_index: i, value: line}
IPMap[line] = n;
}
total++
}
}
err = s.Err()
if err != nil {
log.Fatal(err)
}
}
elapsed := time.Since(start);
logrus.Info(fmt.Sprintf("Total %d Block Records added to memory in %s \n", total, elapsed))
}
// Displays Welcome text in CLI
func welcomeText() {
b, err := ioutil.ReadFile("ascii.txt")
if err != nil {
panic(err)
}
fmt.Println(string(b))
}
// Check if IP is black listed + execution time
func isIPBlackList(ip string) (bool, time.Duration) {
start := time.Now();
// Test our ips
isUs, err2 := ranger.Contains(net.ParseIP(ip))
if err2 != nil {
log.Fatal("Error Parsing IP")
}
if isUs {
elapsed := time.Since(start);
return false, elapsed
}
contains, err := ranger.Contains(net.ParseIP(ip))
if err != nil {
log.Fatal("Error Parsing IP")
}
if contains {
elapsed := time.Since(start);
return true, elapsed
}
_, hasKey := IPMap[ip]
if (hasKey) {
elapsed := time.Since(start);
return true, elapsed
}
elapsed := time.Since(start);
return false, elapsed
}
// Cleanup Data
func cleanIP2LocationData(results ip2location.IP2Locationrecord) MyIP2L {
myIP2L := MyIP2L{}
if (results.Country_short != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Country_short = results.Country_short
}
if (results.Country_long != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Country_long = results.Country_long
}
if (results.Isp != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Isp = results.Isp
}
if (results.Region != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Region = results.Region
}
if (results.City != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.City = results.City
}
if (results.Domain != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Domain = results.Domain
}
if (results.Zipcode != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Zipcode = results.Zipcode
}
if (results.Timezone != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Timezone = results.Timezone
}
if (results.Netspeed != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Netspeed = results.Netspeed
}
if (results.Iddcode != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Iddcode = results.Iddcode
}
if (results.Weatherstationcode != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Weatherstationcode = results.Weatherstationcode
}
if (results.Weatherstationname != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Weatherstationname = results.Weatherstationname
}
if (results.Mcc != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Mcc = results.Mcc
}
if (results.Mnc != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Mnc = results.Mnc
}
if (results.Mobilebrand != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Mobilebrand = results.Mobilebrand
}
if (results.Usagetype != "This parameter is unavailable for selected data file. Please upgrade the data file.") {
myIP2L.Usagetype = results.Usagetype
}
return myIP2L
}
// Provide data for the IP
func getIP2LocationData(ip string) Msg {
start := time.Now()
results := ip2location.Get_all(ip)
status, _ := isIPBlackList(ip)
elapsed := time.Since(start)
msg := Msg{}
status_str := "good"
if (status) {
status_str = "bad"
}
myIP2L := cleanIP2LocationData(results)
msg.Data = myIP2L
msg.IP = ip
msg.IPStatus = status_str
msg.Duration = elapsed.String()
return msg
}
// Validate IP Address
func validIP4(ipAddress string) bool {
ipAddress = strings.Trim(ipAddress, " ")
re, _ := regexp.Compile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`)
if re.MatchString(ipAddress) {
return true
}
return false
}
// Main Program
func main() {
welcomeText()
ranger = cidranger.NewPCTrieRanger();
ourRanger = cidranger.NewPCTrieRanger();
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
loadIp2Location()
loadFireHoleData()
loadWhiteList()
mod := os.Getenv("GIN_MODE")
port:= os.Getenv("GIN_PORT")
fmt.Println("Staring Gin WebServer")
fmt.Println("Gin Mode : ", mod)
gin.SetMode(mod)
r := gin.Default()
r.GET("/", func(c *gin.Context) {
origin :=""
xff:= c.Request.Header.Get("X-Forwarded-For")
if xff !="" {
origin = xff
}else{
origin = c.Request.RemoteAddr
}
i := strings.Index(origin, ":")
ipAddress := origin
// Cleanup ip address
if i > -1 {
ipAddress = origin[:i]
}
// replace ::1 with 127.0.0.1
if ipAddress == "[::1]" {
ipAddress = "127.0.0.1"
}
if (c.Request.Header.Get("Accept") == "application/json") {
if ipValid := validIP4(ipAddress); ipValid {
msg := getIP2LocationData(ipAddress)
msg.OK = true
c.JSON(200, msg)
} else {
msg := Error{}
msg.OK = false
c.JSON(400, msg)
}
} else {
//log.Printf("debug: header origin ip: %v", origin)
c.String(200, ipAddress)
}
})
r.GET("/:ip", func(c *gin.Context) {
ip := c.Param("ip")
if ipValid := validIP4(ip); ipValid {
msg := getIP2LocationData(ip)
msg.OK = true
c.JSON(200, msg)
} else {
msg := Error{}
msg.OK = false
c.JSON(400, msg)
}
})
r.Run(port) // listen and serve on 0.0.0.0:8080
}