public void Init(string host, string user, string password, string database, int port) { Connection = new MySqlConnection("Server=" + host + ";User Id=" + user + ";Port=" + port + ";" + "Password="******";Database=" + database + ";Allow Zero Datetime=True"); try { Connection.Open(); LogConsole.Show("MYSQL Connected to Host: [{0}] at Port: [{1}] using DB: [{2}]", host, port, database); } catch (MySqlException ex) { LogDebug.Show("{0}", ex.Message); // Try auto reconnect on error (every 5 seconds) LogDebug.Show("Try reconnect in 5 seconds..."); Thread.Sleep(5000); Init(host, user, password, database, port); } catch (OverflowException exp) { LogDebug.Show("Error MYSQL: {0}", exp.ToString()); } }
public static void HandleOpCodes(Decode decode) { try { Systems sys = (Systems)decode.Packet; sys.PacketInformation = decode; PacketReader reader = new PacketReader(sys.PacketInformation.buffer); LogDebug.Show("Opcode: {0}", decode.opcode); OpCodes opc = (OpCodes)decode.opcode; if (opc == OpCodes._MSG_LOGIN) { { string usernameShift = reader.String(32); string passwordMd5 = reader.String(32); reader.Skip(4); string clientMac = reader.String(32); reader.Skip(32); uint unk3 = reader.UInt32(); StringShift shift = new StringShift(); string username = shift.Parser(usernameShift); LogDebug.Show("username: {0}", username); LogDebug.Show("password_md5: {0}", passwordMd5); LogDebug.Show("MAC: {0}", clientMac); LogDebug.Show("unk3: {0}", unk3); int res = UserLogin(username, passwordMd5, clientMac); switch (res) { case (int)AuthenticationStatus.OK: { sys.client.SendC(ServerList()); break; } case (int)AuthenticationStatus.BANNED: { sys.client.SendC(UserFail(0xF0, Reason.BANNED)); break; } default: { sys.client.SendC(UserFail(0xF0, Reason.AUTH_FAILED)); break; } } } } else { LogConsole.Show("Default Opcode: {0:X} - {1}", decode.opcode, opc); LogDebug.HexDump(sys.PacketInformation.buffer, 16, true, false); } } catch (Exception) { // ignored } }
public static int LoadServers(string serverFile, UInt16 defaultPort) { try { if (File.Exists(Environment.CurrentDirectory + @"\Settings\" + serverFile)) { Systems.Ini ini = new Systems.Ini(Environment.CurrentDirectory + @"\Settings\" + serverFile); string[] sList = null; sList = ini.GetEntryNames("SERVERS"); if (sList != null && sList.Length > 0) { foreach (string sectname in sList) { string sName = ini.GetValue("SERVERS", sectname, ""); Systems.SRX_Serverinfo SServerInfo = new Systems.SRX_Serverinfo(); SServerInfo.id = Convert.ToUInt16(ini.GetValue(sName, "id", 0)); SServerInfo.ip = ini.GetValue(sName, "ip", "192.168.1.5"); SServerInfo.wan = ini.GetValue(sName, "wan", "192.168.1.5"); SServerInfo.name = ini.GetValue(sName, "name", sName); SServerInfo.port = Convert.ToUInt16(ini.GetValue(sName, "port", defaultPort)); SServerInfo.ipcport = Convert.ToUInt16(ini.GetValue(sName, "ipcport", "6001")); SServerInfo.code = ini.GetValue(sName, "code", ""); SServerInfo.lan_wan = ini.GetValue(sName, "lan_wan", "0") == "1" ? true : false; SServerInfo.Version = Convert.ToInt32(ini.GetValue(sName, "version", 0)); if (SServerInfo.ip == "" || SServerInfo.port == 0 || SServerInfo.id == 0 || SServerInfo.ipcport == 0 || GSList.ContainsKey(SServerInfo.id)) { LogDebug.Show("IPC: Error on Server " + sName + " in " + serverFile + ": field missing or id already in use!"); SServerInfo = null; } else { GSList.Add(SServerInfo.id, SServerInfo); } } } if (GSList.Count() > 0) { string servers = "Server"; if (GSList.Count > 1) { servers = "Servers"; } LogConsole.Show("Loaded " + GSList.Count() + " " + servers + " from server settings"); } else { Systems.SRX_Serverinfo GServer = new Systems.SRX_Serverinfo(); GServer.id = 1; GServer.ip = "192.168.1.5"; if (Global.Network.multihomed) { //Multihomed } else { GServer.extip = Global.Network.LocalIP; } GServer.name = "[SERVER] Default"; GServer.port = defaultPort; GServer.ipcport = 6001; GServer.code = "xdxdxdxdxdxd"; GSList.Add(GServer.id, GServer); } sList = null; ini = null; return(GSList.Count()); } else { Systems.SRX_Serverinfo GServer = new Systems.SRX_Serverinfo(); GServer.id = 1; GServer.ip = "192.168.1.5"; if (Global.Network.multihomed) { //Multihomed } else { //No servers GServer.extip = Global.Network.LocalIP; } GServer.name = "[SERVER " + Global.Versions.appVersion + "]"; GServer.port = defaultPort; GServer.ipcport = 6001; GServer.code = ""; GSList.Add(GServer.id, GServer); return(-1); } } catch (Exception ex) { LogConsole.Show("Error loading GameServer settings " + ex + ""); return(-2); } }
public void ReceiveData(IAsyncResult ar) { Socket wSocket = (Socket)ar.AsyncState; try { if (wSocket.Connected) { int recvSize = wSocket.EndReceive(ar); // get the count of received bytes bool checkData = true; if (recvSize > 0) { if ((recvSize + bufCount) > MAX_BUFFER) // that may be a try to force buffer overflow, we don't allow that ;) { checkData = false; LocalDisconnect(wSocket); } else { // we have something in input buffer and it is not beyond our limits Buffer.BlockCopy(tmpbuf, 0, buffer, bufCount, recvSize); // copy the new data to our buffer bufCount += recvSize; // increase our buffer-counter } } else { // 0 bytes received, this should be a disconnect checkData = false; LocalDisconnect(wSocket); } while (checkData) // repeat while we have data { checkData = false; if (bufCount >= 4) // a minimum of 4 byte is required for us { byte[] newtmp = new byte[bufCount]; Buffer.BlockCopy(buffer, 0, newtmp, 0, bufCount); LogDebug.HexDump(newtmp, 16, true, true); Decode de = new Decode(buffer); // only get the size first. Decode tmp_de = new Decode(tmpbuf); LogConsole.Show("TMP BUFFER OPCODE: {0}", tmp_de.opcode); LogConsole.Show("bufCount: {0} dataSize: {1}", bufCount, de.dataSize); if (bufCount >= (de.dataSize - 2)) // It's a complete packet. Call the handler. { de = new Decode(wSocket, de.tempbuff, this, Packets); // build up the Decode structure for next step OnReceiveData(de); // call the handling routine bufCount -= (de.dataSize); // decrease buffer-counter if (bufCount >= 0) // was the buffer greater than what the packet needs? then it may be the next packet. { Buffer.BlockCopy(buffer, 2 + de.dataSize, buffer, 0, bufCount); // move the rest to buffer start checkData = true; // loop for next packet } } else { byte[] _tempddd = new byte[bufCount]; EncDec c = new EncDec(); byte[] dddxx = c.Crypt(buffer); Buffer.BlockCopy(dddxx, 0, _tempddd, 0, de.dataSize); LogConsole.Show("bufCount: {0} dataSize: {1}", bufCount, de.dataSize); LogDebug.HexDump(_tempddd, 16, true, true); } de = null; } } // start the next async read if (wSocket.Connected) { wSocket.BeginReceive(tmpbuf, 0, tmpbuf.Length, SocketFlags.None, new AsyncCallback(ReceiveData), wSocket); } } else { LocalDisconnect(wSocket); } } catch (SocketException) // explicit handling of SocketException { LocalDisconnect(wSocket); } catch (Exception ex) // other exceptions { LogConsole.Show("Exception Occurred! {0}", ex.Message); LocalDisconnect(wSocket); } }
static void Main(string[] args) { Program pro = new Program(); Bootlogo._Load(); Systems.Ini ini = null; LogConsole.Init(); new EncDec().Load_Hash1(); new EncDec().Load_Hash2(); #region Default Settings int LSPort = 5999; int IPCPort = 5999; string LSIP = "127.0.0.1"; string IPCIP = "127.0.0.1"; #endregion #region Load Settings try { if (File.Exists(Environment.CurrentDirectory + @"\Settings\Settings.ini")) { ini = new Systems.Ini(Environment.CurrentDirectory + @"\Settings\Settings.ini"); LSPort = Convert.ToInt32(ini.GetValue("SERVER", "port", 5999)); LSIP = ini.GetValue("SERVER", "ip", "127.0.0.1"); IPCPort = Convert.ToInt32(ini.GetValue("IPC", "port", 5999)); IPCIP = ini.GetValue("IPC", "ip", "127.0.0.1"); debug = ini.GetValue("CONSOLE", "debug", false); MYSQL_USER = ini.GetValue("MYSQL", "user", "root"); MYSQL_PASS = ini.GetValue("MYSQL", "pass", "123456"); MYSQL_DATA = ini.GetValue("MYSQL", "data", "godswar"); MYSQL_IP = ini.GetValue("MYSQL", "ip", "127.0.0.1"); MYSQL_PORT = ini.GetValue("MYSQL", "port", 3306); ini = null; LogConsole.Show("MYSQL Connection Settings... LOADED!"); } else { LogConsole.Show("Settings.ini could not be found, doing fallback to default settings!"); } } catch (Exception) { return; } #endregion _SQL.Init(MYSQL_IP, MYSQL_USER, MYSQL_PASS, MYSQL_DATA, MYSQL_PORT); Systems.Server net = new Systems.Server(); net.OnConnect += pro._OnClientConnect; net.OnError += pro._ServerError; Systems.Client.OnReceiveData += pro._OnReceiveData; Systems.Client.OnDisconnect += pro._OnClientDisconnect; try { net.Start(LSIP, LSPort); } catch (Exception ex) { LogConsole.Show("Error Initializing Server: {0}", ex); } #region Load GameServers //Systems.LoadServers("GameServers.ini", 6001); #endregion #region IPC Listener /*IPCServer = new Network.Servers.IPCServer(); * IPCServer.OnReceive += new Network.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); * Network.Servers.IPCenCode(ref rqPacket, GS.Value.code); * IPCServer.Send(GS.Value.ip, GS.Value.ipcport, rqPacket); * rqPacket = null; * } * } * catch (Exception ex) * { * LogConsole.Show("Error start ICP: {0}", ex); * }*/ #endregion //LogConsole.Show("Ready for gameserver connection..."); #region Loop Update GameServers while (true) { Thread.Sleep(100); } #endregion }
public void _OnClientConnect(ref object de, Systems.Client net) { LogConsole.Show("New Client Connection Received!"); de = new Systems(net); }
public void _OnReceiveData(Systems.Decode de) { LogConsole.Show("[OPCODE FROM RECV] {0}", de.opcode); Systems.HandleOpCodes(de); }