public static void DeregisterClient(Client client) { ConnectedClients.TryRemove(client.ConnectionId, out Client _); GameLog.InfoFormat("Deregistering {0}", client.ConnectionId); // Send a control message to clean up after World users; Lobby and Login handle themselves if (client.ServerType == ServerTypes.World) { if (!WorldClients.TryRemove(client.ConnectionId, out Client _)) { GameLog.Error("Couldn't deregister cid {id}", client.ConnectionId); } try { if (!World.ControlMessageQueue.IsCompleted) { World.ControlMessageQueue.Add(new HybrasylControlMessage(ControlOpcodes.CleanupUser, CleanupType.ByConnectionId, client.ConnectionId)); } } catch (InvalidOperationException e) { Game.ReportException(e); if (!World.ControlMessageQueue.IsCompleted) { GameLog.ErrorFormat("Connection {id}: DeregisterClient failed", client.ConnectionId); } } } }
public static void Execute(object obj, ElapsedEventArgs args) { GameLog.Debug("Job starting"); try { foreach (var connection in GlobalConnectionManifest.WorldClients) { var client = connection.Value; var connectionId = connection.Key; User user; if (Game.World.WorldData.TryGetValueByIndex(connectionId, out user)) { if (client.IsHeartbeatExpired()) { GameLog.InfoFormat("{0} (connection id {1}: heartbeat expired, disconnecting", user.Name, connectionId); GlobalConnectionManifest.DeregisterClient(client); World.ControlMessageQueue.Add(new HybrasylControlMessage(ControlOpcodes.CleanupUser, CleanupType.ByConnectionId, connectionId)); } } } } catch (Exception e) { Game.ReportException(e); GameLog.ErrorFormat("Exception occurred in job:", e); } }
public Client(Socket socket, Server server) { ClientState = new ClientState(socket); Server = server; GameLog.InfoFormat("Connection {0} from {1}:{2}", ConnectionId, ((IPEndPoint)Socket.RemoteEndPoint).Address.ToString(), ((IPEndPoint)Socket.RemoteEndPoint).Port); if (server is Lobby) { EncryptionKey = Game.Config.ApiEndpoints.EncryptionEndpoint != null?GlobalConnectionManifest.RequestEncryptionKey(Game.Config.ApiEndpoints.EncryptionEndpoint.Url, ((IPEndPoint)socket.RemoteEndPoint).Address) : Encoding.ASCII.GetBytes("UrkcnItnI"); GameLog.InfoFormat($"EncryptionKey is {Encoding.ASCII.GetString(EncryptionKey)}"); var valid = Game.Config.ApiEndpoints.ValidationEndpoint != null?GlobalConnectionManifest.ValidateEncryptionKey(Game.Config.ApiEndpoints.ValidationEndpoint.Url, new ServerToken { Ip = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(), Seed = EncryptionKey }) : true; if (!valid) { GameLog.ErrorFormat("Invalid key from {IP}", ((IPEndPoint)Socket.RemoteEndPoint).Address.ToString()); socket.Disconnect(true); } } EncryptionKeyTable = new byte[1024]; _lastReceived = DateTime.Now.Ticks; GlobalConnectionManifest.RegisterClient(this); ConnectedSince = DateTime.Now.Ticks; }
/// <summary> /// Pretty print an object, which is essentially a dump of its properties, at the moment. /// </summary> /// <param name="obj">The object to be pretty printed, using Hybrasyl.Utility.Logger.</param> public static void PrettyPrint(object obj) { GameLog.DebugFormat("object dump follows"); try { foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj)) { string name = descriptor.Name; object value = descriptor.GetValue(obj); GameLog.DebugFormat("{0} = {1}", name, value); } } catch (Exception e) { GameLog.ErrorFormat("Couldn't pretty print: {0}", e.ToString()); } }
public static void Execute(object obj, ElapsedEventArgs args) { try { GameLog.Debug("Job starting"); var now = DateTime.Now.Ticks; foreach (var mailbox in Game.World.WorldData.Values <Mailbox>().Where(mb => mb.Full)) { try { mailbox.Lock(); mailbox.Cleanup(); mailbox.Unlock(); } catch (MessageStoreLocked e) { Game.ReportException(e); GameLog.ErrorFormat("{0}: mailbox locked during cleanup...?", mailbox.Name); } } foreach (var board in Game.World.WorldData.Values <Board>().Where(mb => mb.Full)) { try { board.Lock(); board.Cleanup(); board.Unlock(); } catch (MessageStoreLocked e) { Game.ReportException(e); GameLog.ErrorFormat("{0}: board locked during cleanup...?", board.Name); } } GameLog.Debug("Job complete"); } catch (Exception e) { Game.ReportException(e); GameLog.Error("Exception occured in job:", e); } }
// Chart for all error password-related error codes were provided by kojasou@ on // https://github.com/hybrasyl/server/pull/11. private void PacketHandler_0x26_ChangePassword(Client client, ClientPacket packet) { var name = packet.ReadString8(); var currentPass = packet.ReadString8(); // Clientside validation ensures that the same string is typed twice for the new // password, and the new password is only sent to the server once. We can assume // that they matched if 0x26 request is sent from the client. var newPass = packet.ReadString8(); if (!Game.World.WorldData.TryGetAuthInfo(name, out AuthInfo login)) { client.LoginMessage(GetPasswordError(0x0E), 0x0E); GameLog.InfoFormat("cid {0}: Password change attempt on nonexistent player {1}", client.ConnectionId, name); return; } if (login.VerifyPassword(currentPass)) { // Check if the password is valid. if (ValidPassword(newPass, out byte err)) { login.PasswordHash = HashPassword(newPass); login.LastPasswordChange = DateTime.Now; login.LastPasswordChangeFrom = ((IPEndPoint)client.Socket.RemoteEndPoint).Address.ToString(); login.Save(); // Let the user know the good news. client.LoginMessage("Your password has been changed successfully.", 0x0); GameLog.InfoFormat("Password successfully changed for `{0}`", name); } else { client.LoginMessage(GetPasswordError(err), err); GameLog.ErrorFormat("Invalid new password proposed during password change attempt for `{0}`", name); } } // The current password is incorrect. Don't allow any changes to happen. else { client.LoginMessage(GetPasswordError(0x0F), 0x0F); GameLog.ErrorFormat("Invalid current password during password change attempt for `{0}`", name); } }
public void SendCallback(IAsyncResult ar) { ClientState state = (ClientState)ar.AsyncState; Client client; GameLog.DebugFormat($"EndSend: SocketConnected: {state.WorkSocket.Connected}, IAsyncResult: Completed: {ar.IsCompleted}, CompletedSynchronously: {ar.CompletedSynchronously}"); try { SocketError errorCode; var bytesSent = state.WorkSocket.EndSend(ar, out errorCode); if (!GlobalConnectionManifest.ConnectedClients.TryGetValue(state.Id, out client)) { GameLog.ErrorFormat("Send: socket should not exist: cid {0}", state.Id); state.WorkSocket.Close(); state.WorkSocket.Dispose(); return; } if (bytesSent == 0 || errorCode != SocketError.Success) { GameLog.ErrorFormat("cid {0}: disconnected"); client.Disconnect(); throw new SocketException((int)errorCode); } } catch (SocketException e) { Game.ReportException(e); GameLog.Error($"Error Code: {e.ErrorCode}, {e.Message}"); state.WorkSocket.Close(); } catch (ObjectDisposedException) { //client.Disconnect(); GameLog.Error($"ObjectDisposedException"); state.WorkSocket.Close(); } state.SendComplete.Set(); }
public IEnumerator Initialize() { yield return(GameInitializer.Instance.StartCoroutine(BytesReader.Read(mConfigFileName, bytes => { if (null == bytes) { #if UNITY_EDITOR_WIN || UNITY_STANDALONE File.WriteAllText( Path.Combine(Application.streamingAssetsPath, mConfigFileName), JsonUtility.ToJson(Instance, true), Encoding.UTF8); #endif GameLog.ErrorFormat("Read Config {0} Failed", mConfigFileName); } else { // NOTE(llisper): detect BOM mark // http://stackoverflow.com/questions/26101859/why-is-file-readallbytes-result-different-than-when-using-file-readalltext string json; if (bytes.Length > 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { json = Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3); } else { json = Encoding.UTF8.GetString(bytes); } JsonUtility.FromJsonOverwrite(json, Instance); } }))); StringBuilder log = new StringBuilder("AppConfig:\n"); LogConfig(log, "AppConfig", this); GameLog.Info(log.ToString()); }
public void FlushReceiveBuffer() { lock (ClientState.ReceiveLock) { try { ClientPacket packet; while (ClientState.ReceiveBufferTake(out packet)) { if (packet.ShouldEncrypt) { packet.Decrypt(this); } if (packet.Opcode == 0x39 || packet.Opcode == 0x3A) { packet.DecryptDialog(); } try { if (Server is Lobby) { GameLog.DebugFormat("Lobby: 0x{0:X2}", packet.Opcode); var handler = (Server as Lobby).PacketHandlers[packet.Opcode]; handler.Invoke(this, packet); GameLog.DebugFormat("Lobby packet done"); UpdateLastReceived(); } else if (Server is Login) { GameLog.Debug($"Login: 0x{packet.Opcode:X2}"); var handler = (Server as Login).PacketHandlers[packet.Opcode]; handler.Invoke(this, packet); GameLog.DebugFormat("Login packet done"); UpdateLastReceived(); } else { UpdateLastReceived(packet.Opcode != 0x45 && packet.Opcode != 0x75); GameLog.Debug($"Queuing: 0x{packet.Opcode:X2}"); //packet.DumpPacket(); // Check for throttling var throttleResult = Server.PacketThrottleCheck(this, packet); if (throttleResult == ThrottleResult.OK || throttleResult == ThrottleResult.ThrottleEnd || throttleResult == ThrottleResult.SquelchEnd) { World.MessageQueue.Add(new HybrasylClientMessage(packet, ConnectionId)); } else if (packet.Opcode == 0x06) { World.ControlMessageQueue.Add(new HybrasylControlMessage(ControlOpcodes.TriggerRefresh, ConnectionId)); } } } catch (Exception e) { Game.ReportException(e); GameLog.ErrorFormat("EXCEPTION IN HANDLING: 0x{0:X2}: {1}", packet.Opcode, e); } } } catch (Exception e) { Game.ReportException(e); Console.WriteLine(e); throw; } } }