-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoip.cc
201 lines (163 loc) · 5.27 KB
/
geoip.cc
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
#include <iostream>
#include <vector>
#include <string>
#include "maxmind.hpp"
#include "CLI/CLI.hpp"
#define VERSION "v0.2.1"
#define AUTHOR "xbol"
void lookup(std::string ip, std::string file, std::vector<std::vector<std::string>> fields, bool dump = false) {
maxmind db{std::filesystem::path {file}};
if (!db) {
std::cerr << "ERROR: " << db.status_msg() << std::endl;
return;
}
LookupResult result{db.lookup(ip)};
if (!result) {
std::cout << "ip " << ip << " not found" << std::endl;
return;
}
if (dump) {
db.print();
return;
}
std::cout << ip << "/" << result.netmask;
for (auto& frags : fields) {
Entry entry{result.value(frags)};
if (!entry) continue;
if (entry.str()) {
std::cout << " " << entry.str().value();
} else if (entry.num()) {
std::cout << " " << entry.num().value();
} else if (entry.numf()) {
std::cout << " " << entry.numf().value();
}
}
std::cout << std::endl;
}
void lookupName(std::string host, std::string file, std::vector<std::vector<std::string>> fields, bool dump = false) {
maxmind db{std::filesystem::path {file}};
if (!db) {
std::cerr << "ERROR: " << db.status_msg() << std::endl;
return;
}
std::vector<LookupResult> results{db.lookupName(host)};
if (results.size() == 0) {
std::cout << "host " << host << " not found" << std::endl;
return;
}
for (auto& result : results) {
if (!result) {
std::cout << "host " << host << " not found" << std::endl;
continue;
}
if (dump) {
db.print();
continue;
}
std::cout << host << " " << result.canonical_ip << "/" << result.netmask;
for (auto& frags : fields) {
Entry entry{result.value(frags)};
if (!entry) continue;
if (entry.str()) {
std::cout << " " << entry.str().value();
} else if (entry.num()) {
std::cout << " " << entry.num().value();
} else if (entry.numf()) {
std::cout << " " << entry.numf().value();
}
}
std::cout << std::endl;
}
}
// Three default and free maxmind db
std::string ASN{"/usr/share/GeoIP/GeoLite2-ASN.mmdb"};
std::string CITY{"/usr/share/GeoIP/GeoLite2-City.mmdb"};
std::string COUNTRY{"/usr/share/GeoIP/GeoLite2-Country.mmdb"};
using fragments = std::vector<std::vector<std::string>>;
int main(int argc, char** argv) {
// Variables
std::vector<std::string> ARGentries;
std::vector<std::string> ARGfields{"continent", "country"};
std::string ARGlang{"en"};
std::optional<std::string> ARGfile{std::nullopt};
bool FLAGasn{false};
bool FLAGcity{false};
bool FLAGcountry{false};
bool FLAGdump{false};
bool FLAGinfo{false};
CLI::App app{"A tool to inspect GeoIP, " AUTHOR " " VERSION};
app.add_option("entries", ARGentries, "domain name or ip address (4/6) to lookup")->required();
auto OPTformat = app.add_option("-F,--format", ARGfields, "result entry formats"); // TODO
app.add_option("--lang", ARGlang, "language to display");
app.add_flag("-s,--asn", FLAGasn, "show ASN number, aka. Autonomous System Numbers");
app.add_option("-f", ARGfile, "use the specific mmdb file")->needs(OPTformat); // TODO
auto OPTcity = app.add_flag("-c,--city", FLAGcity, "show city information (more verbose, country information is included)");
auto OPTcountry = app.add_flag("-C,--country", FLAGcountry, "show country information");
app.add_flag("--dump,-d", FLAGdump, "dump queried result content, json format");
app.add_flag("-i,--info", FLAGinfo, "query addrinfo for a host, support both ipv4/6 and domain name but there is a network request");
CLI11_PARSE(app, argc, argv);
// SETTINGS, TODO
OPTcity->excludes(OPTcountry);
/* OPTcountry->excludes(OPTcity); */
// PROGRAMS
if (!(FLAGcity || FLAGcountry || FLAGdump || FLAGasn))
FLAGcountry = true;
for (auto& entry : ARGentries) {
if (FLAGinfo) {
if (FLAGasn) {
lookupName(entry, ASN,
fragments {
{"autonomous_system_number"}
, {"autonomous_system_organization"}
}, FLAGdump
);
}
if (FLAGcity) {
lookupName(entry, CITY,
fragments {
{"continent", "names", ARGlang}
, {"country", "names", ARGlang}
, {"location", "time_zone"}
, {"location", "latitude"}
, {"location", "longitude"}
}, FLAGdump
);
} else if (FLAGcountry) {
lookupName(entry, COUNTRY,
fragments {
{"continent", "names", ARGlang}
, {"country", "names", ARGlang}
}, FLAGdump
);
}
} else {
if (FLAGasn) {
lookup(entry, ASN,
fragments {
{"autonomous_system_number"}
, {"autonomous_system_organization"}
}, FLAGdump
);
}
if (FLAGcity) {
lookup(entry, CITY,
fragments {
{"continent", "names", ARGlang}
, {"country", "names", ARGlang}
, {"location", "time_zone"}
, {"location", "latitude"}
, {"location", "longitude"}
}, FLAGdump
);
} else if (FLAGcountry) {
lookup(entry, COUNTRY,
fragments {
{"continent", "names", ARGlang}
, {"country", "names", ARGlang}
}, FLAGdump
);
}
}
}
return 0;
}