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_sane(cvar_string("g_ban_sync_uri"));
35 if(n >= MAX_IPBAN_URIS)
37 for(i = 0; i < n; ++i)
38 uri_get(strcat(argv(i), uri), URI_GET_DISCARD); // 0 = "discard" callback target
41 void OnlineBanList_SendUnban(string ip)
46 uri = strcat( "?action=unban&hostname=", uri_escape(cvar_string("hostname")));
47 uri = strcat(uri, "&ip=", uri_escape(ip));
49 n = tokenize_sane(cvar_string("g_ban_sync_uri"));
50 if(n >= MAX_IPBAN_URIS)
52 for(i = 0; i < n; ++i)
53 uri_get(strcat(argv(i), uri), URI_GET_DISCARD); // 0 = "discard" callback target
56 string OnlineBanList_Servers;
57 float OnlineBanList_Timeout;
58 float OnlineBanList_RequestWaiting[MAX_IPBAN_URIS];
60 void OnlineBanList_URI_Get_Callback(float id, float status, string data)
72 if(id >= MAX_IPBAN_URIS)
74 print("Received ban list for invalid ID\n");
78 tokenize_sane(cvar_string("g_ban_sync_uri"));
81 print("Received ban list from ", uri, ": ");
83 if(OnlineBanList_RequestWaiting[id] == 0)
85 print("rejected (unexpected)\n");
89 OnlineBanList_RequestWaiting[id] = 0;
91 if(time > OnlineBanList_Timeout)
93 print("rejected (too late)\n");
97 syncinterval = cvar("g_ban_sync_interval");
100 print("rejected (syncing disabled)\n");
108 print("error: status is ", ftos(status), "\n");
112 if(substring(data, 0, 1) == "<")
114 print("error: received HTML instead of a ban list: ");
118 if(strstrofs(data, "\r", 0) != -1)
120 print("error: received carriage returns: ");
127 n = tokenizebyseparator(data, "\n");
131 print("error: received invalid item count: ", ftos(n), "\n");
137 for(i = 0; i < n; i += 4)
140 timeleft = stof(argv(i + 1));
141 reason = argv(i + 2);
142 serverip = argv(i + 3);
144 timeleft -= 1.5 * cvar("g_ban_sync_timeout");
149 for(j = 0; j < l; ++j)
150 if(strstrofs("0123456789.", substring(ip, j, 1), 0) == -1)
152 print("Invalid character ", substring(ip, j, 1), " in IP address ", ip, ". Skipping this ban.\n");
156 if(cvar("g_ban_sync_trusted_servers_verify"))
157 if((strstrofs(strcat(";", OnlineBanList_Servers, ";"), strcat(";", serverip, ";"), 0) == -1))
161 timeleft = min(syncinterval + (OnlineBanList_Timeout - time) + 5, timeleft);
162 // the ban will be prolonged on the next sync
163 // or expire 5 seconds after the next timeout
164 Ban_Insert(ip, timeleft, strcat("ban synced from ", serverip, " at ", uri), 0);
165 print("Ban list syncing: accepted ban of ", ip, " by ", serverip, " at ", uri, ": ");
173 void OnlineBanList_Think()
179 if(cvar_string("g_ban_sync_uri") == "")
181 if(cvar("g_ban_sync_interval") == 0) // < 0 is okay, it means "sync on level start only"
183 argc = tokenize_sane(cvar_string("g_ban_sync_trusted_servers"));
187 if(OnlineBanList_Servers)
188 strunzone(OnlineBanList_Servers);
189 OnlineBanList_Servers = argv(0);
190 for(i = 1; i < argc; ++i)
191 OnlineBanList_Servers = strcat(OnlineBanList_Servers, ";", argv(i));
192 OnlineBanList_Servers = strzone(OnlineBanList_Servers);
194 uri = strcat( "?action=list&hostname=", uri_escape(cvar_string("hostname")));
195 uri = strcat(uri, "&servers=", uri_escape(OnlineBanList_Servers));
197 OnlineBanList_Timeout = time + cvar("g_ban_sync_timeout");
199 n = tokenize_sane(cvar_string("g_ban_sync_uri"));
200 if(n >= MAX_IPBAN_URIS)
202 for(i = 0; i < n; ++i)
204 if(OnlineBanList_RequestWaiting[i])
206 OnlineBanList_RequestWaiting[i] = 1;
207 uri_get(strcat(argv(i), uri), URI_GET_IPBAN + i); // 1000 = "banlist" callback target
210 if(cvar("g_ban_sync_interval") > 0)
211 self.nextthink = time + max(60, cvar("g_ban_sync_interval") * 60);
216 string ban_ip[BAN_MAX];
217 float ban_expire[BAN_MAX];
235 for(i = 0; i < ban_count; ++i)
237 if(time > ban_expire[i])
239 out = strcat(out, " ", ban_ip[i]);
240 out = strcat(out, " ", ftos(ban_expire[i] - time));
242 if(strlen(out) <= 1) // no real entries
243 cvar_set("g_banned_list", "");
245 cvar_set("g_banned_list", out);
248 float Ban_Delete(float i)
254 if(ban_expire[i] == 0)
256 if(ban_expire[i] > 0)
258 OnlineBanList_SendUnban(ban_ip[i]);
259 strunzone(ban_ip[i]);
270 for(i = 0; i < ban_count; ++i)
274 n = tokenize_sane(cvar_string("g_banned_list"));
275 if(stof(argv(0)) == 1)
277 ban_count = (n - 1) / 2;
278 for(i = 0; i < ban_count; ++i)
280 ban_ip[i] = strzone(argv(2*i+1));
281 ban_expire[i] = time + stof(argv(2*i+2));
287 e.classname = "bansyncer";
288 e.think = OnlineBanList_Think;
289 e.nextthink = time + 1;
296 for(i = 0; i < ban_count; ++i)
298 if(time > ban_expire[i])
300 msg = strcat("#", ftos(i), ": ");
301 msg = strcat(msg, ban_ip[i], " is still banned for ");
302 msg = strcat(msg, ftos(ban_expire[i] - time), " seconds");
307 float Ban_GetClientIP(entity client)
310 n = tokenizebyseparator(client.netaddress, ".");
313 ban_ip1 = strcat1(argv(0));
314 ban_ip2 = strcat(ban_ip1, ".", argv(1));
315 ban_ip3 = strcat(ban_ip2, ".", argv(2));
316 ban_ip4 = strcat(ban_ip3, ".", argv(3));
320 float Ban_IsClientBanned(entity client, float idx)
325 if(!Ban_GetClientIP(client))
337 for(i = b; i < e; ++i)
340 if(time > ban_expire[i])
343 if(ban_ip1 == s) return TRUE;
344 if(ban_ip2 == s) return TRUE;
345 if(ban_ip3 == s) return TRUE;
346 if(ban_ip4 == s) return TRUE;
351 float Ban_MaybeEnforceBan(entity client)
353 if(Ban_IsClientBanned(client, -1))
356 s = strcat("^1NOTE:^7 banned client ", client.netaddress, " just tried to enter\n");
364 string Ban_Enforce(float i, string reason)
369 // Enforce our new ban
371 FOR_EACH_REALCLIENT(e)
372 if(Ban_IsClientBanned(e, i))
377 reason = strcat(reason, ": affects ");
379 reason = strcat(reason, ", ");
380 reason = strcat(reason, e.netname);
382 s = strcat(s, "^1NOTE:^7 banned client ", e.netname, "^7 has to go\n");
390 float Ban_Insert(string ip, float bantime, string reason, float dosync)
397 for(i = 0; i < ban_count; ++i)
401 if(time + bantime > ban_expire[i])
403 ban_expire[i] = time + bantime;
404 print(ip, "'s ban has been prolonged to ", ftos(bantime), " seconds from now\n");
408 reason = Ban_Enforce(i, reason);
413 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
414 OnlineBanList_SendBan(ip, bantime, reason);
419 // do we have a free slot?
420 for(i = 0; i < ban_count; ++i)
421 if(time > ban_expire[i])
423 // no free slot? Then look for the one who would get unbanned next
427 bestscore = ban_expire[i];
428 for(j = 1; j < ban_count; ++j)
430 if(ban_expire[j] < bestscore)
433 bestscore = ban_expire[i];
437 // if we replace someone, will we be banned longer than him (so long-term
438 // bans never get overridden by short-term bans)
439 if(ban_expire[i] > time + bantime)
441 // okay, insert our new victim as i
443 print(ip, " has been banned for ", ftos(bantime), " seconds\n");
444 ban_expire[i] = time + bantime;
445 ban_ip[i] = strzone(ip);
446 ban_count = max(ban_count, i + 1);
450 reason = Ban_Enforce(i, reason);
455 if(substring(reason, 0, 1) != "~") // like IRC: unauthenticated banner
456 OnlineBanList_SendBan(ip, bantime, reason);
461 void Ban_KickBanClient(entity client, float bantime, float masksize, string reason)
463 if(!Ban_GetClientIP(client))
465 sprint(client, strcat("Kickbanned: ", reason, "\n"));
473 Ban_Insert(ban_ip1, bantime, reason, 1);
476 Ban_Insert(ban_ip2, bantime, reason, 1);
479 Ban_Insert(ban_ip3, bantime, reason, 1);
482 Ban_Insert(ban_ip4, bantime, reason, 1);
486 * not needed, as we enforce the ban in Ban_Insert anyway
488 sprint(client, strcat("Kickbanned: ", reason, "\n"));
493 float GameCommand_Ban(string command)
503 argc = tokenize_sane(command);
504 if(argv(0) == "help")
506 print(" kickban # n m p reason - kickban player n for m seconds, using mask size p (1 to 4)\n");
507 print(" ban ip m reason - ban an IP or range (incomplete IP, like 1.2.3) for m seconds\n");
508 print(" bans - list all existing bans\n");
509 print(" unban n - delete the entry #n from the bans list\n");
512 if(argv(0) == "kickban")
514 #define INITARG(c) reasonarg = c
515 #define GETARG(v,d) if((argc > reasonarg) && ((v = stof(argv(reasonarg))) != 0)) ++reasonarg; else v = d
516 #define RESTARG(v) if(argc > reasonarg) v = substring(command, argv_start_index(reasonarg), strlen(command) - argv_start_index(reasonarg)); else v = ""
519 entno = stof(argv(2));
520 if(entno > maxclients || entno < 1)
522 client = edict_num(entno);
525 GETARG(bantime, cvar("g_ban_default_bantime"));
526 GETARG(masksize, cvar("g_ban_default_masksize"));
529 Ban_KickBanClient(client, bantime, masksize, reason);
533 else if(argv(0) == "ban")
541 GETARG(bantime, cvar("g_ban_default_bantime"));
544 Ban_Insert(ip, bantime, reason, 1);
551 else if(argv(0) == "bans")
556 else if(argv(0) == "unban")