-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoip-check.go
240 lines (220 loc) · 8.33 KB
/
geoip-check.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
package main
import (
"fmt"
"log"
"net"
"os"
"path/filepath"
"bufio"
"strings"
"github.com/oschwald/geoip2-golang"
)
func main() {
Banner := "geoip-check v1.2\n"
Banner = Banner + "Last Update: 14 Apr 2024, Alex Yang (linkedin.com/in/4yang)\n\n"
Banner = Banner + "Usage for Single IP query:\n"
Banner = Banner + " geoip-check [IPv4/v6] [Optional_Switch]\n\n"
Banner = Banner + "Optional_Switch for output format:\n"
Banner = Banner + " 0 Suppresss showing source IP\n"
Banner = Banner + " I Show only source IP, Country ISO Code\n"
Banner = Banner + " C Show only source IP, Country\n"
Banner = Banner + " c Show only source IP, City\n"
Banner = Banner + " T Show only source IP, Timezone\n"
Banner = Banner + " L Show only source IP, Latitude, Longitude\n"
Banner = Banner + " Cc Show only source IP, Country, City\n\n"
Banner = Banner + "Example:\n"
Banner = Banner + " geoip-check 74.125.200.100\n"
Banner = Banner + " geoip-check 2607:f8b0:4003:0c00:0000:0000:0000:006a\n"
Banner = Banner + " geoip-check 74.125.200.101 Cc\n"
Banner = Banner + " geoip-check 74.125.200.101 0C\n\n"
Banner = Banner + "Usage for Bulk IP query:\n"
Banner = Banner + " geoip-check [inputfile.txt] --> file extension must be .txt\n\n"
Banner = Banner + "Example:\n"
Banner = Banner + " geoip-check input.txt\n"
Banner = Banner + " geoip-check input.txt 0Cc\n\n"
var inputIP string
var isFile bool = false
var Switch string = "NIL"
defer func() {
if r := recover(); r != nil {
fmt.Println("Can't process further. Seems a bug!")
}
}()
if len(os.Args) == 1 {
fmt.Println(Banner)
return
}
if len(os.Args) == 2 { inputIP = os.Args[1] }
if len(os.Args) == 3 {
inputIP = os.Args[1]
Switch = os.Args[2]
}
if !strings.Contains(inputIP, ".") && !strings.Contains(inputIP, ":") {
fmt.Println(Banner)
return
}
if strings.Count(inputIP, ".") != 3 && strings.Count(inputIP, ":") < 4 {
if !strings.HasSuffix(inputIP, ".txt") {
fmt.Println(Banner)
return
} else {
isFile = true
}
}
homeDir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
filePath := filepath.Join(homeDir, "GeoLite2-City.mmdb")
db, err := geoip2.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
errline := 1
if isFile {
file, err := os.Open(inputIP)
if err != nil {
fmt.Println("failed opening file: %s", err)
return
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
txtlines := scanner.Text()
if len(txtlines) == 0 { continue }
if strings.Count(txtlines, ".") != 3 && strings.Count(txtlines, ":") < 4 {
fmt.Println("Error: line [", errline, "] IP: [", txtlines, "] - check if the format is correct")
errline++
continue
}
ip := net.ParseIP(txtlines)
record, err := db.City(ip)
if err != nil {
fmt.Println("Error: line [", errline, "] IP: [", txtlines, "] - check if the format is correct")
errline++
continue
}
switch Switch {
case "0":
fmt.Printf("%v,", record.Country.IsoCode)
fmt.Printf("%v,", record.Country.Names["en"])
fmt.Printf("%v,", record.City.Names["en"])
fmt.Printf("%v,", record.Location.TimeZone)
fmt.Printf("%v,", record.Location.Latitude)
fmt.Printf("%v\n", record.Location.Longitude)
case "I":
fmt.Printf("%v,", txtlines)
fmt.Printf("%v\n", record.Country.IsoCode)
case "C":
fmt.Printf("%v,", txtlines)
fmt.Printf("%v\n", record.Country.Names["en"])
case "c":
fmt.Printf("%v,", txtlines)
fmt.Printf("%v\n", record.City.Names["en"])
case "T":
fmt.Printf("%v,", txtlines)
fmt.Printf("%v\n", record.Location.TimeZone)
case "L":
fmt.Printf("%v,", txtlines)
fmt.Printf("%v,", record.Location.Latitude)
fmt.Printf("%v\n", record.Location.Longitude)
case "Cc":
fmt.Printf("%v,", txtlines)
fmt.Printf("%v,", record.Country.Names["en"])
fmt.Printf("%v\n", record.City.Names["en"])
case "0C":
fmt.Printf("%v\n", record.Country.Names["en"])
case "0I":
fmt.Printf("%v\n", record.Country.IsoCode)
case "0c":
fmt.Printf("%v\n", record.City.Names["en"])
case "0T":
fmt.Printf("%v\n", record.Location.TimeZone)
case "0L":
fmt.Printf("%v,", record.Location.Latitude)
fmt.Printf("%v\n", record.Location.Longitude)
case "0Cc":
fmt.Printf("%v,", record.Country.IsoCode)
fmt.Printf("%v\n", record.City.Names["en"])
case "NIL":
fmt.Printf("%v,", txtlines)
fmt.Printf("%v,", record.Country.IsoCode)
fmt.Printf("%v,", record.Country.Names["en"])
fmt.Printf("%v,", record.City.Names["en"])
fmt.Printf("%v,", record.Location.TimeZone)
fmt.Printf("%v,", record.Location.Latitude)
fmt.Printf("%v\n", record.Location.Longitude)
default:
fmt.Println("Unrecognized switch!")
}
errline++
}
file.Close()
} else {
ip := net.ParseIP(inputIP)
record, err := db.City(ip)
if err != nil {
log.Fatal(err)
}
switch Switch {
case "0":
fmt.Printf("%v,", record.Country.IsoCode)
fmt.Printf("%v,", record.Country.Names["en"])
fmt.Printf("%v,", record.City.Names["en"])
fmt.Printf("%v,", record.Location.TimeZone)
fmt.Printf("%v,", record.Location.Latitude)
fmt.Printf("%v\n", record.Location.Longitude)
case "I":
fmt.Printf("%v,", inputIP)
fmt.Printf("%v\n", record.Country.IsoCode)
case "C":
fmt.Printf("%v,", inputIP)
fmt.Printf("%v\n", record.Country.Names["en"])
case "c":
fmt.Printf("%v,", inputIP)
fmt.Printf("%v\n", record.City.Names["en"])
case "T":
fmt.Printf("%v,", inputIP)
fmt.Printf("%v\n", record.Location.TimeZone)
case "L":
fmt.Printf("%v,", inputIP)
fmt.Printf("%v,", record.Location.Latitude)
fmt.Printf("%v\n", record.Location.Longitude)
case "Cc":
fmt.Printf("%v,", inputIP)
fmt.Printf("%v,", record.Country.Names["en"])
fmt.Printf("%v\n", record.City.Names["en"])
case "0C":
fmt.Printf("%v\n", record.Country.Names["en"])
case "0I":
fmt.Printf("%v\n", record.Country.IsoCode)
case "0c":
fmt.Printf("%v\n", record.City.Names["en"])
case "0T":
fmt.Printf("%v\n", record.Location.TimeZone)
case "0L":
fmt.Printf("%v,", record.Location.Latitude)
fmt.Printf("%v\n", record.Location.Longitude)
case "0Cc":
fmt.Printf("%v,", record.Country.Names["en"])
fmt.Printf("%v\n", record.City.Names["en"])
case "NIL":
fmt.Printf("%v,", inputIP)
fmt.Printf("%v,", record.Country.IsoCode)
fmt.Printf("%v,", record.Country.Names["en"])
fmt.Printf("%v,", record.City.Names["en"])
fmt.Printf("%v,", record.Location.TimeZone)
fmt.Printf("%v,", record.Location.Latitude)
fmt.Printf("%v\n", record.Location.Longitude)
default:
fmt.Println("Unrecognized switch!")
}
}
}
func IsIpv4Net(host string) bool {
return net.ParseIP(host) != nil
}
func IsIpv6Net(host string) bool {
return net.ParseIP(host) != nil
}