//Run #region run public Maincs() { //Set default settings #region Default Settings int LSPort = 15779; int IPCPort = 15779; string LSIP = ""; string IPCIP = "0.0.0.0"; Systems.DownloadServer = ""; #endregion //Initialize main InitializeComponent(); //Set ini LoginServer.Systems.Ini ini = null; //Load settings #region Load Settings try { if (File.Exists(Environment.CurrentDirectory + @"\Settings\Settings.ini")) { ini = new LoginServer.Systems.Ini(Environment.CurrentDirectory + @"\Settings\Settings.ini"); LSPort = Convert.ToInt32(ini.GetValue("Server", "port", 15779)); LSIP = ini.GetValue("Server", "ip", "").ToString(); IPCPort = Convert.ToInt32(ini.GetValue("IPC", "port", 15779)); IPCIP = ini.GetValue("IPC", "ip", "").ToString(); Systems.DownloadServer = ini.GetValue("Patch_Server", "ip", ""); Systems.DownloadPort = Convert.ToInt16(ini.GetValue("Patch_Server", "port", "")); ini = null; Activity("[INFO] Loaded your ip settings successfully"); } else { Activity("Could not find your settings.ini using defaults."); } } catch (Exception) { return; } #endregion #region get local ip Network.multihomed = false; if (Network.LocalIP == "") { IPAddress[] lIpList = Dns.GetHostAddresses(Dns.GetHostName()); foreach (IPAddress aIP in lIpList) { if (aIP.AddressFamily.Equals(AddressFamily.InterNetwork)) { if (!aIP.Equals(IPAddress.Loopback)) { if (Network.LocalIP != "") { Network.multihomed = true; } else { Network.LocalIP = aIP.ToString(); } } } } } #endregion Systems.Server net = new Systems.Server(); net.OnConnect += new Systems.Server.dConnect(_OnClientConnect); net.OnError += new Systems.Server.dError(_ServerError); Systems.Client.OnReceiveData += new Systems.Client.dReceive(_OnReceiveData); Systems.Client.OnDisconnect += new Systems.Client.dDisconnect(_OnClientDisconnect); //Content.Patches.LoadPatches(this); Systems.Load_NewsList(this, newsform); try { net.Start(LSIP, LSPort); } catch (Exception ex) { Activity("Starting Server error " + ex + ""); } #region Load GameServers Systems.LoadServers("GameServers.ini", 15780, this, servers); #endregion #region IPC Listener IPCServer = new Servers.IPCServer(); IPCServer.OnReceive += new Servers.IPCServer.dOnReceive(OnIPC); try { IPCServer.Start(IPCIP, IPCPort); foreach (KeyValuePair <int, Systems.SRX_Serverinfo> GS in Systems.GSList) { byte[] rqPacket = IPCServer.PacketRequestServerInfo(IPCPort); Servers.IPCenCode(ref rqPacket, GS.Value.code); IPCServer.Send(GS.Value.ip, GS.Value.ipcport, rqPacket); rqPacket = null; } } catch (Exception ex) { Activity("IPC start error " + ex + ""); } #endregion Activity("[NET] Listening for gameserver connection..."); this.ShowDialog(); while (true) { Thread.Sleep(100); foreach (KeyValuePair <int, Systems.SRX_Serverinfo> SSI in Systems.GSList) { if (SSI.Value.status != 0 && SSI.Value.lastPing.AddMinutes(5) < DateTime.Now) // server unavailable { SSI.Value.status = 0; Activity("[ERROR] Server " + SSI.Value.name + " has timed out"); } } } }
//IPC & Data #region IPC & Data public void OnIPC(System.Net.Sockets.Socket aSocket, System.Net.EndPoint ep, byte[] data) { try { if (data.Length >= 6) { UInt16 pServer = (UInt16)(data[0] + (data[1] << 8)); Systems.SRX_Serverinfo remoteGameServer = Systems.GetServerByEndPoint(((IPEndPoint)ep).Address.ToString(), pServer); if (remoteGameServer != null) { // decode data Servers.IPCdeCode(ref data, remoteGameServer.code); byte pCmd = data[3]; int dLen = (int)(data[4] + (data[5] << 8)); byte crc = Servers.BCRC(data, data.Length - 1); if (data[data.Length - 1] != crc) // wrong CRC { Activity("[ERROR] Code for: " + remoteGameServer.name + " does not match"); return; } if (data.Length >= (dLen + 6)) { if (pCmd == (byte)IPCCommand.IPC_INFO_SERVER) { if (data.Length >= 11) { remoteGameServer.maxSlots = (UInt16)(data[7] + (data[8] << 8)); remoteGameServer.usedSlots = (UInt16)(data[9] + (data[10] << 8)); remoteGameServer.lastPing = DateTime.Now; Activity("[NET] " + remoteGameServer.name + ": players online " + remoteGameServer.usedSlots + "/" + remoteGameServer.maxSlots + ""); if (remoteGameServer.status == 0 && data[6] != 0) { Activity("[NET] Server: " + remoteGameServer.name + " is now online"); } if (remoteGameServer.status != 0 && data[6] == 0) { Activity("[NET] Server: " + remoteGameServer.name + " is now in check state"); } remoteGameServer.status = data[6]; } else { } } else if (pCmd == (byte)IPCCommand.IPC_INFO_LOGIN) { if (dLen >= 4) { UInt16 IPCid = (UInt16)(data[6] + (data[7] << 8)); UInt16 IPCResult = (UInt16)(data[8] + (data[9] << 8)); byte sLen = data[10]; lock (IPCResultList) { if (IPCResultList.ContainsKey(IPCid)) { IPCResultList[IPCid].resultCode = IPCResult; if (sLen > 0) { IPCResultList[IPCid].banReason = System.Text.ASCIIEncoding.ASCII.GetString(data, 11, sLen); } } else { Activity("[ERROR] ResultList mismatch"); } } } } else { Activity("[ERROR] unknown command recevied"); } } else { Activity("[ERROR] data to short"); } } else { Activity("[ERROR] can't find the GameServer " + ((IPEndPoint)ep).Address.ToString() + ""); } } else { Activity("[ERROR] packet to short from " + ep.ToString() + ""); } } catch (Exception ex) { Activity("[ERROR] " + ex + ""); } }
public void Connect() { try { PacketReader Reader = new PacketReader(PacketInformation.buffer); if (Reader.Byte() == 18) { string ID = Reader.Text(); string PW = Reader.Text(); byte ukn = Reader.Byte(); // 0xff UInt16 ServerID = Reader.UInt16(); int lResult = 99; //Console.WriteLine("Id:{0} Pass:{1} ServerID:{2}",ID,PW,ServerID); SRX_Serverinfo SSI = GSList[ServerID]; if (SSI != null) { UInt16 myKey = 0; string sReason = ""; lock (Program.IPCResultList) { myKey = Program.IPCNewId++; } byte[] rqp = Program.IPCServer.PacketRequestLogin(Program.IPCPort, ID, PW, myKey); Servers.IPCenCode(ref rqp, SSI.code); lock (Program.IPCResultList) { Program.IPCResultList.Add(myKey, new IPCItem()); Program.IPCResultList[myKey].resultCode = 0x8000; } Program.IPCServer.Send(SSI.ip, SSI.ipcport, rqp); DateTime tOut = DateTime.Now.AddSeconds(30); while ((tOut >= DateTime.Now) && (Program.IPCResultList[myKey].resultCode == 0x8000) && (client.clientSocket.Connected)) { System.Threading.Thread.Sleep(10); } lResult = Program.IPCResultList[myKey].resultCode; sReason = Program.IPCResultList[myKey].banReason; lock (Program.IPCResultList) { Program.IPCResultList[myKey] = null; Program.IPCResultList.Remove(myKey); } rqp = null; //Console.WriteLine("Resultado de login: {0}",lResult); switch (lResult) { case 0: if (SSI.lan_wan) { client.Send(ConnectSucces(SSI.wan, SSI.port, 1)); } else { client.Send(ConnectSucces(SSI.ip, SSI.port, 1)); } return; case 1: if (WrongPassword < 3) { client.Send(WrongInformation()); WrongPassword++; return; } else { client.Disconnect(PacketInformation.Client); return; } case 2: client.Send(ServerIsFull()); client.Disconnect(PacketInformation.Client); return; case 3: client.Send(AllreadyConnected()); client.Disconnect(PacketInformation.Client); return; case 4: client.Send(BannedUser(sReason)); client.Disconnect(PacketInformation.Client); return; default: if (lResult == 0x8000) { Console.WriteLine("[IPC] Timeout"); } else { Console.WriteLine("[IPC] Result unknown {0}", lResult); } return; } } else { Console.WriteLine("F**k No fun.. SSI"); } } } catch (Exception ex) { Console.WriteLine("Connection Error: " + ex); } }
public static void Main(string[] args) { Program pro = new Program(); Definitions.Bootlogo._Load(); LoginServer.Systems.Ini ini = null; #region Default Settings int LSPort = 15779; int IPCPort = 15790; string LSIP = "127.0.0.1"; string IPCIP = "127.0.0.1"; Systems.DownloadServer = "127.0.0.1"; #endregion #region Load Settings try { if (File.Exists(Environment.CurrentDirectory + @"\Settings\Settings.ini")) { ini = new LoginServer.Systems.Ini(Environment.CurrentDirectory + @"\Settings\Settings.ini"); LSPort = Convert.ToInt32(ini.GetValue("Server", "port", 15779)); LSIP = ini.GetValue("Server", "ip", "127.0.0.1").ToString(); IPCPort = Convert.ToInt32(ini.GetValue("IPC", "port", 15790)); IPCIP = ini.GetValue("IPC", "ip", "127.0.0.1").ToString(); Systems.DownloadServer = ini.GetValue("Patch_Server", "ip", "127.0.0.1"); Systems.DownloadPort = Convert.ToInt16(ini.GetValue("Patch_Server", "port", "")); ini = null; Console.WriteLine("{0}Has loaded your ip settings successfully", Product.Prefix); } else { Console.ForegroundColor = ConsoleColor.Red; Console.BackgroundColor = ConsoleColor.Yellow; Console.WriteLine("{0}Settings.ini could not be found, using default setting", Product.Prefix); } } catch (Exception) { return; } #endregion #region get local ip if (args.Length > 0) { if (args[0] == "extip") { HttpWebRequest HWR = (HttpWebRequest)WebRequest.Create("http://checkip.dyndns.org/"); HWR.Method = "GET"; WebResponse MWR = HWR.GetResponse(); StreamReader Hsr = new StreamReader(MWR.GetResponseStream(), System.Text.Encoding.UTF8); string theip = Hsr.ReadToEnd(); } } Network.multihomed = false; if (Network.LocalIP == "") { IPAddress[] lIpList = Dns.GetHostAddresses(Dns.GetHostName()); foreach (IPAddress aIP in lIpList) { if (aIP.AddressFamily.Equals(AddressFamily.InterNetwork)) { if (!aIP.Equals(IPAddress.Loopback)) { if (Network.LocalIP != "") { Network.multihomed = true; } else { Network.LocalIP = aIP.ToString(); } } } } } #endregion Systems.Server net = new Systems.Server(); net.OnConnect += new Systems.Server.dConnect(pro._OnClientConnect); net.OnError += new Systems.Server.dError(pro._ServerError); Systems.Client.OnReceiveData += new Systems.Client.dReceive(pro._OnReceiveData); Systems.Client.OnDisconnect += new Systems.Client.dDisconnect(pro._OnClientDisconnect); //Content.Patches.LoadPatches(); Systems.Load_NewsList(); try { net.Start(LSIP, LSPort); } catch (Exception ex) { Console.WriteLine("{1}Starting Server error: {0}", ex, Product.Prefix); } #region Load GameServers Systems.LoadServers("GameServers.ini", 15780); #endregion #region IPC Listener IPCServer = new Servers.IPCServer(); IPCServer.OnReceive += new Servers.IPCServer.dOnReceive(pro.OnIPC); try { IPCServer.Start(IPCIP, IPCPort); foreach (KeyValuePair <int, Systems.SRX_Serverinfo> GS in Systems.GSList) { byte[] rqPacket = IPCServer.PacketRequestServerInfo(IPCPort); Servers.IPCenCode(ref rqPacket, GS.Value.code); IPCServer.Send(GS.Value.ip, GS.Value.ipcport, rqPacket); rqPacket = null; } } catch (Exception ex) { Console.WriteLine("Error start ICP: {0}", ex); } #endregion Console.WriteLine("{0}Ready for gameserver connection...", Global.Product.Prefix); while (true) { Thread.Sleep(100); foreach (KeyValuePair <int, Systems.SRX_Serverinfo> SSI in Systems.GSList) { if (SSI.Value.status != 0 && SSI.Value.lastPing.AddMinutes(5) < DateTime.Now) // server unavailable { SSI.Value.status = 0; Console.WriteLine("{2}Server: ({1}) has timed out, status changed to check", SSI.Value.id, SSI.Value.name, Product.Prefix); } } } }
public void OnIPC(System.Net.Sockets.Socket aSocket, System.Net.EndPoint ep, byte[] data) { try { if (data.Length >= 6) { UInt16 pServer = (UInt16)(data[0] + (data[1] << 8)); Systems.SRX_Serverinfo remoteGameServer = Systems.GetServerByEndPoint(((IPEndPoint)ep).Address.ToString(), pServer); if (remoteGameServer != null) { // decode data Servers.IPCdeCode(ref data, remoteGameServer.code); byte pCmd = data[3]; int dLen = (int)(data[4] + (data[5] << 8)); byte crc = Servers.BCRC(data, data.Length - 1); if (data[data.Length - 1] != crc) // wrong CRC { Console.WriteLine("{1} Wrong Checksum for Server {0}", remoteGameServer.name, Product.Prefix); return; } if (data.Length >= (dLen + 6)) { if (pCmd == (byte)IPCCommand.IPC_INFO_SERVER) { if (data.Length >= 11) { remoteGameServer.maxSlots = (UInt16)(data[7] + (data[8] << 8)); remoteGameServer.usedSlots = (UInt16)(data[9] + (data[10] << 8)); remoteGameServer.lastPing = DateTime.Now; //Console.WriteLine("[IPC] Received SERVER-INFO from GameServer {1} ({0}): S={2}, MAX={3}, CUR={4}", remoteGameServer.name, remoteGameServer.id, data[6], remoteGameServer.maxSlots, remoteGameServer.usedSlots); if (remoteGameServer.status == 0 && data[6] != 0) { Console.WriteLine("{2}GameServer {0} ({1}) status changed to online", remoteGameServer.id, remoteGameServer.name, Product.Prefix); } if (remoteGameServer.status != 0 && data[6] == 0) { Console.WriteLine("{2}GameServer {0} ({1}) status changed to check", remoteGameServer.id, remoteGameServer.name, Product.Prefix); } remoteGameServer.status = data[6]; } else { } } else if (pCmd == (byte)IPCCommand.IPC_INFO_LOGIN) { if (dLen >= 4) { UInt16 IPCid = (UInt16)(data[6] + (data[7] << 8)); UInt16 IPCResult = (UInt16)(data[8] + (data[9] << 8)); byte sLen = data[10]; lock (IPCResultList) { if (IPCResultList.ContainsKey(IPCid)) { IPCResultList[IPCid].resultCode = IPCResult; if (sLen > 0) { IPCResultList[IPCid].banReason = System.Text.ASCIIEncoding.ASCII.GetString(data, 11, sLen); } } else { Console.WriteLine("[IPC] ResultList mismatch"); } } } } else { Console.WriteLine("[IPC] unknown command recevied"); } } else { Console.WriteLine("[IPC] data to short"); } } else { Console.WriteLine("[IPC] can't find the GameServer {0}:{1}", ((IPEndPoint)ep).Address.ToString(), pServer); } } else { Console.WriteLine("[IPC] packet to short from {0}", ep.ToString()); } } catch (Exception ex) { Console.WriteLine("[IPC.OnIPC] {0}", ex); } }
/// <summary> /// Builds internal server list from serverlist.xml /// </summary> /// <returns></returns> private static void PopulateList() { Servers servers = new Servers(); foreach (ServerInfo newServer in servers) { server_list.AddServer(newServer); } }