using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Net; using System.Net.Sockets; using System.Threading; using Blizzard.HabboHotel.Users; namespace Blizzard.Connections { public class MUS { public static Socket SocketListen; public string musIp; public int musPort; private byte[] dataBuffer = new byte[1024]; private static bool Connected = false; public HashSet allowedIps; public static void End() { Connected = false; SocketListen.Close(); } internal void InitConnection() { musIp = "localhost"; GetData ConfigD = new GetData("Settings/configuration.cnf"); string musIP = ConfigD.data["net.mus.ip"]; musPort = int.Parse(ConfigD.data["net.mus.port"]); string[] AllowedIPS = ConfigD.data["net.mus.allowedips"].Split(';'); allowedIps = new HashSet(); foreach (String ip in AllowedIPS) { allowedIps.Add(ip); } try { string IPv = ConfigD.data["ip.type"]; if (IPv == "IPv6") // We need to process the IPNetwork 6 Sockets D: ¡use this! SocketListen = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); else // or not... USE THIS NORMAL WAY! SocketListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Connected = true; IPEndPoint LocalHost = new IPEndPoint(IPAddress.Any, musPort); SocketListen.ReceiveTimeout = 6000; SocketListen.SendTimeout = 6000; SocketListen.NoDelay = true; SocketListen.LingerState = new LingerOption(true, 1); SocketListen.Bind(LocalHost); SocketListen.Listen(10000); BlizzardSystem.WriteLine("MUS is Initialized on IP: " + musIP + "; Port: " + musPort); SocketListen.BeginAccept(new AsyncCallback(NewConnection), SocketListen); } catch { BlizzardSystem.WriteLine("The MUS port is full now, play off the program that using it!"); } } public void NewConnection(IAsyncResult iAr) { try { if (!Connected) return; Socket FromClient = ((Socket)iAr.AsyncState).EndAccept(iAr); string ClientIP = FromClient.RemoteEndPoint.ToString().Split(':')[0]; new MUSProcess(FromClient); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.ToString()); BlizzardSystem.WriteLine("The MUS port is full now, turn off the program that using it!"); } finally { if (Connected) SocketListen.BeginAccept(new AsyncCallback(NewConnection), SocketListen); } } } internal class MUSProcess { private Socket ClientSocket; private byte[] dataBuffer = new byte[1024]; private byte[] buffer = new byte[1024]; internal MUSProcess(Socket _socket) { ClientSocket = _socket; AsyncCallback CallBack = new AsyncCallback(RecieveData); WaitForData(); } internal void WaitForData() { try { AsyncCallback CallBack = new AsyncCallback(RecieveData); ClientSocket.BeginReceive(dataBuffer, 0, dataBuffer.Length, SocketFlags.None, CallBack, null); // Linea 44 } catch (Exception e) { //Console.WriteLine("WaitForData Error: " + e.ToString()); } } internal void RecieveData(IAsyncResult iAr) { int bytesBuffer = ClientSocket.EndReceive(iAr); StringBuilder Data = new StringBuilder(); Data.Append(Encoding.Default.GetString(dataBuffer, 0, bytesBuffer)); this.ProcessCommands(Data.ToString()); } internal void ProcessCommands(string Data) { UsersData UserGame; if (Data.Length >= 1) { BlizzardSystem.WriteMUSLine("[MUSSOCKET] Received:" + Data); string[] mData = Data.Split(' '); switch (mData[0]) { case "UpdateUsersList": UsersData.GetAllUsers(); BlizzardSystem.UpdateRegisteredUsers(); break; case "UpdateUserTicket": int ssUserId = int.Parse(mData[1]); string ssoTicket = mData[2]; UsersData ssData = UsersData.UsersRegister[ssUserId]; ssData.ConnectionTicket = ssoTicket; break; case "SendAlertToUser": string User = mData[1]; int AlertType = int.Parse(mData[2]); string Message = this.MergeParams(mData, 3); Message = Message.Replace("@", " "); int UserId = UsersData.GetDataFromName(User).Id; if (UserId == 0) { BlizzardSystem.WriteMUSLine("VPN Failed on packet:" + Data); return; } else if (StaticGame.VirtualUser.ContainsKey(UserId)) { Habbo TheUser = StaticGame.VirtualUser[UserId]; TheUser.SendAlert(Message, AlertType); } break; case "SendGigaAlert": int sAlertType = int.Parse(mData[1]); string sMessage = this.MergeParams(mData, 2); sMessage = sMessage.Replace("@", " "); foreach (KeyValuePair VirtualUser in StaticGame.VirtualUser) { int UserID = VirtualUser.Key; Habbo MyUser = VirtualUser.Value; MyUser.SendAlert("Alerta General:\n\n" + sMessage, sAlertType); } break; case "UpdateCredits": string sUser = mData[1]; int NewCredits = int.Parse(mData[2]); int AddedCredits = int.Parse(mData[3]); int sUserId = 0; foreach (UsersData D in UsersData.UsersRegistered) { if (D.UserName.Contains(sUser)) sUserId = D.Id; } if (sUserId == 0) { BlizzardSystem.WriteMUSLine("VPN Failed on packet:" + Data); return; } if (StaticGame.VirtualUser.ContainsKey(sUserId)) { Habbo TheUser = StaticGame.VirtualUser[sUserId]; TheUser.UpdateCredits(NewCredits, false); TheUser.SendAlert("Has recibido " + AddedCredits + " Créditos del Staff!", 1); } break; case "MassiveBan": foreach (KeyValuePair VirtualUser in StaticGame.VirtualUser) { int UserID = VirtualUser.Key; Habbo MyUser = VirtualUser.Value; if (MyUser.Rank < 9) { MyUser.SendAlert("¡Baneo de Seguridad!", 2); MyUser.DisconnectMe(); } } break; case "MassiveKick": foreach (KeyValuePair VirtualUser in StaticGame.VirtualUser) { int UserID = VirtualUser.Key; Habbo MyUser = VirtualUser.Value; if (MyUser.Rank < 9) { MyUser.SendAlert("¡Has sido expulsado/a de la sala por seguridad!", 2); //MyUser.KickMe(); } } break; case "RemoteBan": foreach (KeyValuePair VirtualUser in StaticGame.VirtualUser) { string qUser = mData[1]; int qUserId = UsersData.GetDataFromName(qUser).Id; if (StaticGame.VirtualUser.ContainsKey(qUserId)) { Habbo TheUser = StaticGame.VirtualUser[qUserId]; TheUser.SendAlert("¡Has sido baneado/a!", 2); TheUser.DisconnectMe(); } } break; case "RemoteKick": string fUser = mData[1]; int fUserId = UsersData.GetDataFromName(fUser).Id; if (StaticGame.VirtualUser.ContainsKey(fUserId)) { Habbo TheUser = StaticGame.VirtualUser[fUserId]; TheUser.SendAlert("¡Has sido expulsado/a de la sala!", 2); //TheUser.KickMe(); } break; } } } internal void SendData(string Data) { try { //Console.WriteLine("Sended Data: " + Data); byte[] dataBytes = Encoding.Default.GetBytes(Data); ClientSocket.BeginSend(dataBytes, 0, dataBytes.Length, 0, new AsyncCallback(sentData), null); } catch (Exception e) { Console.WriteLine(e.ToString()); } } private void sentData(IAsyncResult iAr) { try { ClientSocket.EndSend(iAr); } catch { } } internal string MergeParams(string[] Params, int Start) { StringBuilder MergedParams = new StringBuilder(); for (int i = 0; i < Params.Length; i++) { if (i < Start) { continue; } if (i > Start) { MergedParams.Append("@"); } MergedParams.Append(Params[i]); } return MergedParams.ToString(); } } }