@@ -43,6 +43,8 @@ std::string ssprintf(const char *fmt, ...) {
4343# include < strings.h>
4444#endif
4545
46+ // TODO: Add unban, and save bans/whitelist to disk
47+
4648void handle_cmd (NetworkManager *network, char *line) {
4749 while (isspace (*line)) ++line;
4850
@@ -83,8 +85,10 @@ void handle_cmd(NetworkManager *network, char *line) {
8385 LINE (" countdown start a countdown" );
8486 LINE (" disconnect disconnect a client by name" );
8587 LINE (" disconnect_id disconnect a client by ID" );
88+ LINE (" disconnect_ip disconnect a client by IP address" );
8689 LINE (" ban ban connections from a certain IP by ghost name" );
8790 LINE (" ban_id ban connections from a certain IP by ghost ID" );
91+ LINE (" ban_ip ban connections from a certain IP by IP address" );
8892 LINE (" accept_players start accepting connections from players" );
8993 LINE (" refuse_players stop accepting connections from players" );
9094 LINE (" accept_spectators start accepting connections from spectators" );
@@ -151,6 +155,12 @@ void handle_cmd(NetworkManager *network, char *line) {
151155 return ;
152156 }
153157
158+ if (!strcmp (line, " disconnect_ip" )) {
159+ g_current_cmd = CMD_DISCONNECT_IP;
160+ LINE_NONL (" IP of player to disconnect: " );
161+ return ;
162+ }
163+
154164 if (!strcmp (line, " ban" )) {
155165 g_current_cmd = CMD_BAN;
156166 LINE_NONL (" Name of player to ban: " );
@@ -163,6 +173,12 @@ void handle_cmd(NetworkManager *network, char *line) {
163173 return ;
164174 }
165175
176+ if (!strcmp (line, " ban_ip" )) {
177+ g_current_cmd = CMD_BAN_IP;
178+ LINE_NONL (" IP of player to ban: " );
179+ return ;
180+ }
181+
166182 if (!strcmp (line, " accept_players" )) {
167183 network->ScheduleServerThread ([=]() {
168184 network->acceptingPlayers = true ;
@@ -315,6 +331,24 @@ void handle_cmd(NetworkManager *network, char *line) {
315331 LINE (" No player ID given; not disconnecting anyone." );
316332 }
317333 return ;
334+
335+ case CMD_DISCONNECT_IP:
336+ g_current_cmd = CMD_NONE;
337+ if (len != 0 ) {
338+ sf::IpAddress ip (_line);
339+ if (ip == sf::IpAddress::None) {
340+ LINE (" Invalid IP address: %s" , line);
341+ return ;
342+ }
343+ network->ScheduleServerThread ([=]() {
344+ auto clients = network->GetClientByIP (ip);
345+ for (auto cl : clients) network->DisconnectPlayer (*cl, " kicked" );
346+ });
347+ LINE (" Disconnected player IP %s" , line);
348+ } else {
349+ LINE (" No player IP given; not disconnecting anyone." );
350+ }
351+ return ;
318352
319353 case CMD_BAN:
320354 g_current_cmd = CMD_NONE;
@@ -343,6 +377,25 @@ void handle_cmd(NetworkManager *network, char *line) {
343377 }
344378 return ;
345379
380+ case CMD_BAN_IP:
381+ g_current_cmd = CMD_NONE;
382+ if (len != 0 ) {
383+ sf::IpAddress ip (_line);
384+ if (ip == sf::IpAddress::None) {
385+ LINE (" Invalid IP address: %s" , line);
386+ return ;
387+ }
388+ network->ScheduleServerThread ([=]() {
389+ auto clients = network->GetClientByIP (ip);
390+ for (auto cl : clients) network->BanClientIP (*cl);
391+ });
392+ network->bannedIps .push_back (ip);
393+ LINE (" Banned player IP %s" , line);
394+ } else {
395+ LINE (" No player IP given; not banning anyone." );
396+ }
397+ return ;
398+
346399 case CMD_SERVER_MSG:
347400 g_current_cmd = CMD_NONE;
348401 network->ServerMessage (line);
0 commit comments