2 * Protocol of online ban list:
5 * GET g_ban_sync_uri?action=ban&hostname=...&ip=xxx.xxx.xxx&duration=nnnn&reason=...................
6 * (IP 1, 2, 3, or 4 octets, 3 octets for example is a /24 mask)
8 * GET g_ban_sync_uri?action=unban&hostname=...&ip=xxx.xxx.xxx
9 * - Querying the ban list
10 * GET g_ban_sync_uri?action=list&hostname=...&servers=xxx.xxx.xxx.xxx;xxx.xxx.xxx.xxx;...
12 * shows the bans from the listed servers, and possibly others.
13 * Format of a ban is ASCII plain text, four lines per ban, delimited by
14 * newline ONLY (no carriage return):
16 * IP address (also 1, 2, 3, or 4 octets, delimited by dot)
17 * time left in seconds
19 * server IP that registered the ban
22 float Ban_Insert(string ip, float bantime, string reason, float dosync);
24 void OnlineBanList_SendBan(string ip, float bantime, string reason)
29 uri = strcat( "action=ban&hostname=", uri_escape(cvar_string("hostname")));
30 uri = strcat(uri, "&ip=", uri_escape(ip));
31 uri = strcat(uri, "&duration=", ftos(bantime));
32 uri = strcat(uri, "&reason=", uri_escape(reason));
34 n = tokenize_console(cvar_string("g_ban_sync_uri"));
35 if(n >= MAX_IPBAN_URIS)
37 for(i = 0; i < n; ++i)
39 if(strstrofs(argv(i), "?", 0) >= 0)
40 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
42 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
46 void OnlineBanList_SendUnban(string ip)
51 uri = strcat( "action=unban&hostname=", uri_escape(cvar_string("hostname")));
52 uri = strcat(uri, "&ip=", uri_escape(ip));
54 n = tokenize_console(cvar_string("g_ban_sync_uri"));
55 if(n >= MAX_IPBAN_URIS)
57 for(i = 0; i < n; ++i)
59 if(strstrofs(argv(i), "?", 0) >= 0)
60 uri_get(strcat(argv(i), "&", uri), URI_GET_DISCARD); // 0 = "discard" callback target
62 uri_get(strcat(argv(i), "?", uri), URI_GET_DISCARD); // 0 = "discard" callback target
66 string OnlineBanList_Servers;
67 float OnlineBanList_Timeout;
68 float OnlineBanList_RequestWaiting[MAX_IPBAN_URIS];
70 void OnlineBanList_URI_Get_Callback(float id, float status, string data)
82 if(id >= MAX_IPBAN_URIS)
84 print("Received ban list for invalid ID\n");
88 tokenize_console(cvar_string("g_ban_sync_uri"));
91 print("Received ban list from ", uri, ": ");
93 if(OnlineBanList_RequestWaiting[id] == 0)
95 print("rejected (unexpected)\n");
99 OnlineBanList_RequestWaiting[id] = 0;
101 if(time > OnlineBanList_Timeout)
103 print("rejected (too late)\n");
107 syncinterval = cvar("g_ban_sync_interval");
108 if(syncinterval == 0)
110 print("rejected (syncing disabled)\n");
118 print("error: status is ", ftos(status), "\n");
122 if(substring(data, 0, 1) == "<")
124 print("error: received HTML instead of a ban list\n");
128 if(strstrofs(data, "\r", 0) != -1)
130 print("error: received carriage returns\n");
137 n = tokenizebyseparator(data, "\n");
141 print("error: received invalid item count: ", ftos(n), "\n");
145 print("OK, ", ftos(n / 4), " items\n");
147 for(i = 0; i < n; i += 4)
150 timeleft = stof(argv(i + 1));
151 reason = argv(i + 2);
152 serverip = argv(i + 3);
154 dprint("received ban list item ", ftos(i / 4), ": ip=", ip);
155 dprint(" timeleft=", ftos(timeleft), " reason=", reason);
156 dprint(" serverip=", serverip, "\n");
158 timeleft -= 1.5 * cvar("g_ban_sync_timeout");
163 for(j = 0; j < l; ++j)
164 if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
166 print("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.\n");
170 if(cvar("g_ban_sync_trusted_servers_verify"))
171 if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
175 timeleft = min(syncinterval + (OnlineBanList_Timeout - time) + 5, timeleft);
176 // the ban will be prolonged on the next sync
177 // or expire 5 seconds after the next timeout
178 Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip, " at ", uri), 0);
179 print("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ");
186 void OnlineBanList_Think()
192 if(cvar_string("g_ban_sync_uri") == "")
194 if(cvar("g_ban_sync_interval") == 0) // < 0 is okay, it means "sync on level start only"
196 argc = tokenize_console(cvar_string("g_ban_sync_trusted_servers"));
200 if(OnlineBanList_Servers)
201 strunzone(OnlineBanList_Servers);
202 OnlineBanList_Servers = argv(0);
203 for(i = 1; i < argc; ++i)
204 OnlineBanList_Servers = strcat(OnlineBanList_Servers, ";", argv(i));
205 OnlineBanList_Servers = strzone(OnlineBanList_Servers);
207 uri = strcat( "action=list&hostname=", uri_escape(cvar_string("hostname")));
208 uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
210 OnlineBanList_Timeout = time + cvar("g_ban_sync_timeout");
212 n = tokenize_console(cvar_string("g_ban_sync_uri"));
213 if(n >= MAX_IPBAN_URIS)
215 for(i = 0; i < n; ++i)
217 if(OnlineBanList_RequestWaiting[i])
219 OnlineBanList_RequestWaiting[i] = 1;
220 if(strstrofs(argv(i), "?", 0) >= 0)
221 uri_get(strcat(argv(i), "&", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
223 uri_get(strcat(argv(i), "?", uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
226 if(cvar("g_ban_sync_interval") > 0)
227 self.nextthink = time + max(60, cvar("g_ban_sync_interval") * 60);
238 string ban_ip[BAN_MAX];
239 float ban_expire[BAN_MAX];
260 for(i = 0; i < ban_count; ++i)
262 if(time > ban_expire[i])
264 out = strcat(out, " ", ban_ip[i]);
265 out = strcat(out, " ", ftos(ban_expire[i] - time));
267 if(strlen(out) <= 1) // no real entries
268 cvar_set("g_banned_list", "");
270 cvar_set("g_banned_list", out);
273 float Ban_Delete(float i)
279 if(ban_expire[i] == 0)
281 if(ban_expire[i] > 0)
283 OnlineBanList_SendUnban(ban_ip[i]);
284 strunzone(ban_ip[i]);
295 for(i = 0; i < ban_count; ++i)
299 n = tokenize_console(cvar_string("g_banned_list"));
300 if(stof(argv(0)) == 1)
302 ban_count = (n - 1) / 2;
303 for(i = 0; i < ban_count; ++i)
305 ban_ip[i] = strzone(argv(2*i+1));
306 ban_expire[i] = time + stof(argv(2*i+2));
312 e.classname = "bansyncer";
313 e.think = OnlineBanList_Think;
314 e.nextthink = time + 1;
321 for(i = 0; i < ban_count; ++i)
323 if(time > ban_expire[i])
325 msg = strcat("#", ftos(i), ": ");
326 msg = strcat(msg, ban_ip[i], " is still banned for ");
327 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
332 float Ban_GetClientIP(entity client)
334 // we can't use tokenizing here, as this is called during ban list parsing
335 float i1, i2, i3, i4;
339 ban_uid = client.uid;
342 s = client.netaddress;
344 i1 = strstrofs(s, ".", 0);
347 i2 = strstrofs(s, ".", i1 + 1);
350 i3 = strstrofs(s, ".", i2 + 1);
353 i4 = strstrofs(s, ".", i3 + 1);
355 s = substring(s, 0, i4);
357 ban_ip1 = substring(s, 0, i1); // 8
358 ban_ip2 = substring(s, 0, i2); // 16
359 ban_ip3 = substring(s, 0, i3); // 24
360 ban_ip4 = strcat1(s); // 32
364 i1 = strstrofs(s, ":", 0);
367 i1 = strstrofs(s, ":", i1 + 1);
370 i2 = strstrofs(s, ":", i1 + 1);
373 i3 = strstrofs(s, ":", i2 + 1);
377 ban_ip1 = strcat(substring(s, 0, i1), "::/32"); // 32
378 ban_ip2 = strcat(substring(s, 0, i2), "::/48"); // 48
379 ban_ip4 = strcat(substring(s, 0, i3), "::/64"); // 64
381 if(i3 - i2 > 3) // means there is more than 2 digits and a : in the range
382 ban_ip3 = strcat(substring(s, 0, i2), ":", substring(s, i2 + 1, i3 - i2 - 3), "00::/56");
384 ban_ip3 = strcat(substring(s, 0, i2), ":0::/56");
389 float Ban_IsClientBanned(entity client, float idx)
394 if(!Ban_GetClientIP(client))
406 for(i = b; i < e; ++i)
409 if(time > ban_expire[i])
412 if(ban_ip1 == s) return TRUE;
413 if(ban_ip2 == s) return TRUE;
414 if(ban_ip3 == s) return TRUE;
415 if(ban_ip4 == s) return TRUE;
417 if(ban_uid == s) return TRUE;
423 float Ban_MaybeEnforceBan(entity client)
425 if(Ban_IsClientBanned(client, -1))
428 s = strcat("^1NOTE:^7 banned client ", client.netaddress, " just tried to enter\n");
436 string Ban_Enforce(float i, string reason)
441 // Enforce our new ban
443 FOR_EACH_REALCLIENT(e)
444 if(Ban_IsClientBanned(e, i))
449 reason = strcat(reason, ": affects ");
451 reason = strcat(reason, ", ");
452 reason = strcat(reason, e.netname);
454 s = strcat(s, "^1NOTE:^7 banned client ", e.netname, "^7 has to go\n");
462 float Ban_Insert(string ip, float bantime, string reason, float dosync)
469 for(i = 0; i < ban_count; ++i)
473 if(time + bantime > ban_expire[i])
475 ban_expire[i] = time + bantime;
476 dprint(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now\n");
479 dprint(ip, "'s ban is still active until ", ftos(ban_expire[i] - time), " seconds from now\n");
482 reason = Ban_Enforce(i, reason);
487 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
488 OnlineBanList_SendBan(ip, bantime, reason);
493 // do we have a free slot?
494 for(i = 0; i < ban_count; ++i)
495 if(time > ban_expire[i])
497 // no free slot? Then look for the one who would get unbanned next
501 bestscore = ban_expire[i];
502 for(j = 1; j < ban_count; ++j)
504 if(ban_expire[j] < bestscore)
507 bestscore = ban_expire[i];
511 // if we replace someone, will we be banned longer than him (so long-term
512 // bans never get overridden by short-term bans)
514 if(ban_expire[i] > time + bantime)
516 print(ip, " could not get banned due to no free ban slot\n");
519 // okay, insert our new victim as i
521 dprint(ip, " has been banned for ", ftos(bantime), " seconds\n");
522 ban_expire[i] = time + bantime;
523 ban_ip[i] = strzone(ip);
524 ban_count = max(ban_count, i + 1);
528 reason = Ban_Enforce(i, reason);
533 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
534 OnlineBanList_SendBan(ip, bantime, reason);
539 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
541 if(!Ban_GetClientIP(client))
543 sprint(client, strcat("Kickbanned: ", reason, "\n"));
551 Ban_Insert(ban_ip1, bantime, reason, 1);
554 Ban_Insert(ban_ip2, bantime, reason, 1);
557 Ban_Insert(ban_ip3, bantime, reason, 1);
561 Ban_Insert(ban_ip4, bantime, reason, 1);
565 Ban_Insert(ban_uid, bantime, reason, 1);
570 * not needed, as we enforce the ban in Ban_Insert anyway
572 sprint(client, strcat("Kickbanned: ", reason, "\n"));
577 float GameCommand_Ban(string command)
587 argc = tokenize_console(command);
588 if(argv(0) == "help")
590 print(" kickban # n m p reason - kickban player n for m seconds, using mask size p (1 to 4)\n");
591 print(" ban ip m reason - ban an IP or range (incomplete IP, like 1.2.3) for m seconds\n");
592 print(" bans - list all existing bans\n");
593 print(" unban n - delete the entry #n from the bans list\n");
596 if(argv(0) == "kickban")
598 #define INITARG(c) reasonarg = c
599 #define GETARG(v,d) if((argc > reasonarg) && ((v = stof(argv(reasonarg))) != 0)) ++reasonarg; else v = d
600 #define RESTARG(v) if(argc > reasonarg) v = substring(command, argv_start_index(reasonarg), strlen(command) - argv_start_index(reasonarg)); else v = ""
603 entno = stof(argv(2));
604 if(entno > maxclients || entno < 1)
606 client = edict_num(entno);
609 GETARG(bantime, cvar("g_ban_default_bantime"));
610 GETARG(masksize, cvar("g_ban_default_masksize"));
613 Ban_KickBanClient(client, bantime, masksize, reason);
617 else if(argv(0) == "ban")
625 GETARG(bantime, cvar("g_ban_default_bantime"));
628 Ban_Insert(ip, bantime, reason, 1);
635 else if(argv(0) == "bans")
640 else if(argv(0) == "unban")