This repository has been archived by the owner on Jul 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
geoip.inc
223 lines (174 loc) · 5.45 KB
/
geoip.inc
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
// built-in include guard removal
// just in case the user has a local dependency with the same file name
#if defined _inc_geoip
#undef _inc_geoip
#endif
// custom include-guard to ensure we don't duplicate
#if defined _geoip_included
#endinput
#endif
#define _geoip_included
#include <a_samp>
#include <logger>
#include <requests>
#include <map>
#include <YSI_Coding\y_hooks>
#if !defined IPHUB_KEY
#error Must define `IPHUB_KEY` see iphub.info/api for details
#endif
#if !defined MAX_GEOIP_COUNTRY_CODE
#define MAX_GEOIP_COUNTRY_CODE (4)
#endif
#if !defined MAX_GEOIP_COUNTRY_NAME
#define MAX_GEOIP_COUNTRY_NAME (128)
#endif
#if !defined MAX_GEOIP_ISP_NAME
#define MAX_GEOIP_ISP_NAME (128)
#endif
// E_GEO_DATA matches the iphub response object, minus the IP address itself
enum E_GEO_DATA {
bool:E_GEO_READY, // false if response hasn't arrived yet
E_GEO_CODE[MAX_GEOIP_COUNTRY_CODE], // "countryCode": "US",
E_GEO_NAME[MAX_GEOIP_COUNTRY_NAME], // "countryName": "United States",
E_GEO_ASN, // "asn": 15169,
E_GEO_ISP[MAX_GEOIP_ISP_NAME], // "isp": "GOOGLE - Google Inc.",
E_GEO_BLOCK, // "block": 1
}
static GeoData[MAX_PLAYERS][E_GEO_DATA];
static RequestsClient:Client;
static Map:RequestToPlayer;
forward OnGeoResponse(Request:id, E_HTTP_STATUS:status, Node:node);
forward OnGeoDataReady(playerid);
// -
// API
// -
// IsGeoDataReady checks if geo IP data for a player is ready to read yet.
stock bool:IsGeoDataReady(playerid) {
if(!IsPlayerConnected(playerid)) {
return false;
}
return GeoData[playerid][E_GEO_READY];
}
stock GetPlayerCountryCode(playerid, output[], len = sizeof output) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
if(!GeoData[playerid][E_GEO_READY]) {
return 2;
}
strcat(output, GeoData[playerid][E_GEO_CODE], len);
return 0;
}
stock GetPlayerCountryName(playerid, output[], len = sizeof output) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
if(!GeoData[playerid][E_GEO_READY]) {
return 2;
}
strcat(output, GeoData[playerid][E_GEO_NAME], len);
return 0;
}
stock GetPlayerASN(playerid, &asn) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
if(!GeoData[playerid][E_GEO_READY]) {
return 2;
}
asn = GeoData[playerid][E_GEO_ASN];
return 0;
}
stock GetPlayerISP(playerid, output[], len = sizeof output) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
if(!GeoData[playerid][E_GEO_READY]) {
return 2;
}
strcat(output, GeoData[playerid][E_GEO_ISP], len);
return 0;
}
stock GetPlayerIPBlock(playerid, &block) {
if(!IsPlayerConnected(playerid)) {
return 1;
}
if(!GeoData[playerid][E_GEO_READY]) {
return 2;
}
block = GeoData[playerid][E_GEO_BLOCK];
return 0;
}
// -
// Internal
// -
hook OnScriptInit() {
new key[] = IPHUB_KEY;
if(strlen(key) == 0) {
Logger_Fatal("IPHUB_KEY must be set for geoip library");
}
Client = RequestsClient("http://v2.api.iphub.info/ip/", RequestHeaders("X-Key", key));
}
hook OnPlayerConnect(playerid) {
new addr[16];
GetPlayerIp(playerid, addr, sizeof addr);
_geoip_doRequest(addr, playerid);
}
hook OnPlayerDisconnect(playerid, reason) {
GeoData[playerid][E_GEO_READY] = false;
GeoData[playerid][E_GEO_CODE][0] = EOS;
GeoData[playerid][E_GEO_NAME][0] = EOS;
GeoData[playerid][E_GEO_ASN] = 0;
GeoData[playerid][E_GEO_ISP][0] = EOS;
GeoData[playerid][E_GEO_BLOCK] = 0;
}
_geoip_doRequest(const addr[16], playerid) {
Logger_Dbg("geoip", "requesting ip data",
Logger_S("addr", addr),
Logger_I("playerid", playerid));
new Request:r = RequestJSON(
Client,
addr,
HTTP_METHOD_GET,
"OnGeoResponse"
);
MAP_insert_val_val(RequestToPlayer, _:r, playerid);
}
public OnGeoResponse(Request:id, E_HTTP_STATUS:status, Node:node) {
Logger_Dbg("geoip", "received ip data",
Logger_I("request", _:id),
Logger_I("status", _:status));
if(status != HTTP_STATUS_OK) {
Logger_Err("iphub response status code was not OK",
Logger_I("status", _:status));
return;
}
new playerid = MAP_get_val_val(RequestToPlayer, _:id);
if(!IsPlayerConnected(playerid)) {
return;
}
Logger_Dbg("geoip", "determined target player for ip data",
Logger_I("playerid", playerid));
// {
// "ip": "8.8.8.8",
// "countryCode": "US",
// "countryName": "United States",
// "asn": 15169,
// "isp": "GOOGLE - Google Inc.",
// "block": 1
// }
GeoData[playerid][E_GEO_READY] = true;
JsonGetString(node, "countryCode", GeoData[playerid][E_GEO_CODE], 4);
JsonGetString(node, "countryName", GeoData[playerid][E_GEO_NAME], 128);
JsonGetInt(node, "asn", GeoData[playerid][E_GEO_ASN]);
JsonGetString(node, "isp", GeoData[playerid][E_GEO_ISP], 128);
JsonGetInt(node, "block", GeoData[playerid][E_GEO_BLOCK]);
Logger_Dbg("geoip", "extracted ip data for player",
Logger_S("code", GeoData[playerid][E_GEO_CODE]),
Logger_S("name", GeoData[playerid][E_GEO_NAME]),
Logger_I("asn", GeoData[playerid][E_GEO_ASN]),
Logger_S("isp", GeoData[playerid][E_GEO_ISP]),
Logger_I("block", GeoData[playerid][E_GEO_BLOCK])
);
CallLocalFunction("OnGeoDataReady", "%d", playerid);
}