public void AddClient(LoginClient client) { lock (clients) { clients.Add(client); } }
public bool RemoveClient(LoginClient client) { lock (clients) { return clients.Remove(client); } }
public static void FileHash(LoginClient pClient, Packet pPacket) { string hash; if (!pPacket.TryReadString(out hash)) { Log.WriteLine(LogLevel.Warn, "Empty filehash received."); SendFailedLogin(pClient, ServerError.Exception); } else AllowFiles(pClient, true); }
public static void FileHash(LoginClient pClient, Packet pPacket) { string hash; if (!pPacket.TryReadString(out hash)) { Log.WriteLine(LogLevel.Warn, "Empty filehash received."); SendFailedLogin(pClient, ServerError.EXCEPTION); } else { //allowfiles here f***s shit up? } }
public static void VersionInfo(LoginClient pClient, Packet pPacket) { string year; ushort version; if (!pPacket.TryReadString(out year, 20) || !pPacket.TryReadUShort(out version)) { Log.WriteLine(LogLevel.Warn, "Invalid client version."); pClient.Disconnect(); return; } Log.WriteLine(LogLevel.Debug, "Client version {0}:{1}.", year, version); using (Packet response = new Packet(SH3Type.VersionAllowed)) { response.WriteShort(1); pClient.SendPacket(response); } }
public static void Login(LoginClient pClient, Packet pPacket) { string hash; string username; if (!pPacket.TryReadString(out username, 18) || !pPacket.TryReadString(out hash, 16)) { Log.WriteLine(LogLevel.Warn, "Could not read user token."); SendFailedLogin(pClient, ServerError.EXCEPTION); return; } User user; if (Program.Entity.Users.Count() > 0 && Program.Entity.Users.Count(u => u.Username == username) == 1) { user = Program.Entity.Users.First(u => u.Username == username); if (user.Password.ToLower() == hash.ToLower()) { Log.WriteLine(LogLevel.Debug, "{0} tries to login.", username); if (ClientManager.Instance.IsLoggedIn(user.Username)) { Log.WriteLine(LogLevel.Warn, "{0} is trying dual login. Disconnecting.", user.Username); pClient.Disconnect(); } else if (user.Banned) { SendFailedLogin(pClient, ServerError.BLOCKED); } else { pClient.IsAuthenticated = true; pClient.Username = user.Username; pClient.AccountID = user.ID; pClient.Admin = user.Admin; AllowFiles(pClient, true); WorldList(pClient, false); } } else SendFailedLogin(pClient, ServerError.INVALID_CREDENTIALS); } else SendFailedLogin(pClient, ServerError.INVALID_CREDENTIALS); }
private static void WorldList(LoginClient pClient, bool pPing) { using (var pack = new Packet(pPing ? SH3Type.WorldistResend : SH3Type.WorldlistNew)) { int max = 11; int count = 0; pack.WriteByte((byte)max); foreach (var world in WorldManager.Instance.Worlds.Values) { pack.WriteByte(world.ID); pack.WriteString(world.Name, 16); pack.WriteByte((byte)world.Status); count++; } for (int i = count; i < max; i++ ) { pack.WriteByte((byte)i); pack.WriteString("DUMMY~" + count, 16); pack.WriteByte((byte)WorldStatus.OFFLINE); } pClient.SendPacket(pack); } }
private static void SendWorldServerIP(LoginClient pClient, WorldConnection wc, string hash) { using (var pack = new Packet(SH3Type.WorldServerIP)) { pack.WriteByte((byte)wc.Status); string ip = pClient.Host == "127.0.0.1" ? "127.0.0.1" : wc.IP; pack.WriteString(wc.IP, 16); pack.WriteUShort(wc.Port); pack.WriteString(hash, 32); pack.Fill(32, 0); pClient.SendPacket(pack); } }
private static void SendFailedLogin(LoginClient pClient, ServerError pError) { using (Packet pack = new Packet(SH3Type.Error)) { pack.WriteUShort((ushort)pError); pClient.SendPacket(pack); } }
private static void WorldList(LoginClient pClient, bool pPing) { using (var pack = new Packet(pPing ? SH3Type.WorldistResend : SH3Type.WorldlistNew)) { pack.WriteByte(11);//worldmax count //pack.WriteByte((byte)WorldManager.Instance.WorldCount); foreach (var world in WorldManager.Instance.Worlds.Values) { pack.WriteByte(world.ID); pack.WriteString(world.Name, 16); pack.WriteByte((byte)world.Status); } for (int i = 0; i < (11 - WorldManager.Instance.Worlds.Count); i++) { pack.WriteByte((byte)i); pack.WriteString("DUMMY" + i, 16); pack.WriteByte((byte)WorldStatus.Offline); } pClient.SendPacket(pack); } }
private static void InvalidClientVersion(LoginClient pClient) { using (Packet pack = new Packet(SH3Type.IncorrectVersion)) { pack.Fill(10, 0); pClient.SendPacket(pack); } }
private static void AllowFiles(LoginClient pClient, bool pIsOk) { using (Packet pack = new Packet(SH3Type.FilecheckAllow)) { pack.WriteBool(pIsOk); pClient.SendPacket(pack); } }
public static void WorldSelectHandler(LoginClient pClient, Packet pPacket) { if (!pClient.IsAuthenticated || pClient.IsTransferring) { Log.WriteLine(LogLevel.Warn, "Invalid world select request."); SendFailedLogin(pClient, ServerError.EXCEPTION); return; } byte id; if (!pPacket.TryReadByte(out id)) { Log.WriteLine(LogLevel.Warn, "Invalid world select."); return; } WorldConnection world; if (WorldManager.Instance.Worlds.TryGetValue(id, out world)) { switch (world.Status) { case WorldStatus.MAINTENANCE: Log.WriteLine(LogLevel.Warn, "{0} tried to join world in maintentance.", pClient.Username); SendFailedLogin(pClient, ServerError.SERVER_MAINTENANCE); return; case WorldStatus.OFFLINE: Log.WriteLine(LogLevel.Warn, "{0} tried to join offline world.", pClient.Username); SendFailedLogin(pClient, ServerError.SERVER_MAINTENANCE); return; default: Log.WriteLine(LogLevel.Debug, "{0} joins world {1}", pClient.Username, world.Name); break; } string hash = System.Guid.NewGuid().ToString().Replace("-", ""); world.SendTransferClientFromWorld(pClient.AccountID, pClient.Username, pClient.Admin, pClient.Host, hash); Log.WriteLine(LogLevel.Debug, "Transferring login client {0}.", pClient.Username); pClient.IsTransferring = true; SendWorldServerIP(pClient, world, hash); } else { Log.WriteLine(LogLevel.Warn, "{0} selected invalid world {1}.", pClient.Username, id); return; } }
public static void WorldReRequestHandler(LoginClient pClient, Packet pPacket) { if (!pClient.IsAuthenticated) { Log.WriteLine(LogLevel.Warn, "Invalid world list request."); return; } WorldList(pClient, true); }
private static void WorldList(LoginClient pClient, bool pPing) { using (var pack = new Packet(pPing ? SH3Type.WorldistResend : SH3Type.WorldlistNew)) { pack.WriteByte((byte)WorldManager.Instance.WorldCount); foreach (var world in WorldManager.Instance.Worlds.Values) { pack.WriteByte(world.ID); pack.WriteString(world.Name, 16); pack.WriteByte((byte)world.Status); } pClient.SendPacket(pack); } }
public static void Login(LoginClient pClient, Packet pPacket) { // Initialize DB DatabaseClient dbClient = Program.DatabaseManager.GetClient(); // Define packet lengths, as these may change with client updates int packetLength = 316; int loginLength = 17 + 1; int passwordLength = 32 + 1; string md5 = pPacket.ReadStringForLogin(packetLength); char[] md5Char = md5.ToCharArray(); string username = ""; string clientPassword = ""; // Read from 0 --> 17 for (int i = 0; i < loginLength; i++) username += md5Char[i].ToString().Replace("\0", ""); // Read from 260 --> 292 for (int i = 260; i < 260 + passwordLength; i++) clientPassword += md5Char[i].ToString().Replace("\0", ""); Log.WriteLine(LogLevel.Debug, "{0} tries to login.", username); DataTable loginData = null; using (dbClient) loginData = dbClient.ReadDataTable("SELECT * FROM accounts WHERE Username= '******'"); // Auto account creation if no username found if (loginData.Rows.Count == 0) { dbClient.ExecuteQuery("INSERT INTO accounts (username, password) VALUES ('" + username + "','" + clientPassword + "')"); using (dbClient) loginData = dbClient.ReadDataTable("SELECT * FROM accounts WHERE Username= '******'"); } if (loginData != null) { if (loginData.Rows.Count > 0) { foreach (DataRow row in loginData.Rows) { string uIsername = (string)row["username"]; string password = (string)row["password"]; bool banned = Database.DataStore.ReadMethods.EnumToBool(row["banned"].ToString()); if (clientPassword == password) { if (banned) { SendFailedLogin(pClient, ServerError.Blocked); Log.WriteLine(LogLevel.Debug, "Banned user - {0} tries to login.", username); } else if (ClientManager.Instance.IsLoggedIn(uIsername)) { Log.WriteLine(LogLevel.Warn, "{0} is trying dual login. Disconnecting.", uIsername); pClient.Disconnect(); break; } else { pClient.Username = uIsername; pClient.IsAuthenticated = true; pClient.Admin = 0; /*(byte)row["Admin"];*/ pClient.AccountID = int.Parse(row["id"].ToString()); WorldList(pClient, false); } } else SendFailedLogin(pClient, ServerError.InvalidCredentials); } } } }
public override void OnClientConnect(Socket socket) { LoginClient client = new LoginClient(socket); ClientManager.Instance.AddClient(client); Log.WriteLine(LogLevel.Debug, "Client connected from {0}", client.Host); }
public static void BackToServerSelectHandler(LoginClient pClient) { WorldList(pClient, false); }