protected override void NetMsgRconAuth(Chunk packet, UnPacker unPacker, int clientId) { if (IsAuthed(clientId)) { return; } var password = unPacker.GetString(SanitizeType.SanitizeCC); if (!packet.Flags.HasFlag(SendFlags.Vital) || unPacker.Error) { return; } // TODO send map list if (string.IsNullOrEmpty(Config["SvRconPassword"]) && string.IsNullOrEmpty(Config["SvRconModPassword"])) { SendRconLine(clientId, "No rcon password set on server. Set sv_rcon_password and/or sv_rcon_mod_password to enable the remote console."); return; } var authed = false; var format = string.Empty; var authLevel = 0; if (!string.IsNullOrEmpty(Config["SvRconPassword"]) && Config["SvRconPassword"] == password) { authed = true; format = $"clientId={clientId} authed 'admin'"; authLevel = BaseServerClient.AuthedAdmin; SendRconLine(clientId, "Admin authentication successful. Full remote console access granted."); } else if (!string.IsNullOrEmpty(Config["SvRconModPassword"]) && Config["SvRconModPassword"] == password) { authed = true; format = $"clientId={clientId} authed 'moderator'"; authLevel = BaseServerClient.AuthedModerator; SendRconLine(clientId, "Moderator authentication successful. Limited remote console access granted."); } else if (Config["SvRconMaxTries"]) { Clients[clientId].AuthTries++; SendRconLine(clientId, $"Wrong password {Clients[clientId].AuthTries}/{Config["SvRconMaxTries"]}."); if (Clients[clientId].AuthTries >= Config["SvRconMaxTries"]) { if (Config["SvRconBantime"]) { NetworkBan.BanAddr(NetworkServer.ClientEndPoint(clientId), Config["SvRconBantime"] * 60, "Too many remote console authentication tries"); } else { Kick(clientId, "Too many remote console authentication tries"); } } } else { SendRconLine(clientId, "Wrong password"); } if (authed) { var msg = new MsgPacker((int)NetworkMessages.ServerRconAuthOn, true); SendMsg(msg, MsgFlags.Vital, clientId); Console.Print(OutputLevel.Standard, "server", format); Clients[clientId].AccessLevel = authLevel; Clients[clientId].SendCommandsEnumerator = Console.GetCommands(authLevel, ConfigFlags.Server).GetEnumerator(); SendRconCommandsClients.Enqueue(clientId); } }
protected override void ProcessClientPacket(NetworkChunk packet) { var clientId = packet.ClientId; var unpacker = new Unpacker(); unpacker.Reset(packet.Data, packet.DataSize); var msg = unpacker.GetInt(); var isSystemMsg = (msg & 1) != 0; msg >>= 1; if (unpacker.Error) { return; } if (Config["SvNetlimit"] && !(isSystemMsg && msg == (int)NetworkMessages.CL_REQUEST_MAP_DATA)) { var now = Time.Get(); var diff = now - Clients[clientId].TrafficSince; var alpha = Config["SvNetlimitAlpha"] / 100f; var limit = (float)Config["SvNetlimit"] * 1024 / Time.Freq(); if (Clients[clientId].Traffic > limit) { NetworkBan.BanAddr(packet.EndPoint, 600, "Stressing network"); return; } if (diff > 100) { Clients[clientId].Traffic = (int)(alpha * ((float)packet.DataSize / diff) + (1.0f - alpha) * Clients[clientId].Traffic); Clients[clientId].TrafficSince = now; } } if (isSystemMsg) { var networkMsg = (NetworkMessages)msg; switch (networkMsg) { case NetworkMessages.CL_INFO: NetMsgInfo(packet, unpacker, clientId); break; case NetworkMessages.CL_REQUEST_MAP_DATA: NetMsgRequestMapData(packet, unpacker, clientId); break; case NetworkMessages.CL_READY: NetMsgReady(packet, unpacker, clientId); break; case NetworkMessages.CL_ENTERGAME: NetMsgEnterGame(packet, unpacker, clientId); break; case NetworkMessages.CL_INPUT: NetMsgInput(packet, unpacker, clientId); break; case NetworkMessages.CL_RCON_CMD: NetMsgRconCmd(packet, unpacker, clientId); break; case NetworkMessages.CL_RCON_AUTH: NetMsgRconAuth(packet, unpacker, clientId); break; case NetworkMessages.PING: NetMsgPing(packet, unpacker, clientId); break; default: Console.Print(OutputLevel.DEBUG, "server", $"strange message clientId={clientId} msg={msg} data_size={packet.DataSize}"); break; } } else if (Clients[clientId].State >= ServerClientState.READY) { GameContext.OnMessage(msg, unpacker, clientId); } }