void OnData(OnReceivePacket args) { if (args.Data.Length < 0) return; if (args.Data[0] != (byte)'G') return; args.Cancel(); var netStream = new NetworkStream(args.Player.Socket); using(var Reader = new StreamReader(netStream)) //Not used but it likes it... using (var Writer = new StreamWriter(netStream)) { var line = Encoding.UTF8.GetString(args.Data, 0, args.Data.Length).Split('\n')[0]; var match = Parser.Match(line); if (match.Success) { var lvl = Level.FindLevel(match.Groups[1].Value); var userNameLine = Encoding.UTF8.GetString(args.Data, 0, args.Data.Length).Split('\n')[3]; var username = userNameLine.Remove(0, "X-WoM-Username: "******"\r", ""); Thread.CurrentThread.Join(2000); var player = Player.Find(username); if (player != null) player.ExtraData.Add("UsingWom", true); if (lvl == null) { Writer.Write("HTTP/1.1 404 Not Found"); Writer.Flush(); } else { if (!lvl.ExtraData.ContainsKey("WomConfig")) { Writer.Write("HTTP/1.1 Internal Server Error"); Writer.Flush(); } else { var Config = (string)lvl.ExtraData["WomConfig"]; var bytes = Encoding.UTF8.GetBytes(Config); Writer.Write("HTTP/1.1 200 OK"); Writer.WriteLine("Date: " + DateTime.UtcNow.ToString("R")); Writer.WriteLine("Server: Apache/2.2.21 (CentOS)"); Writer.WriteLine("Last-Modified: " + DateTime.UtcNow.ToString("R")); Writer.WriteLine("Accept-Ranges: bytes"); Writer.WriteLine("Content-Length: " + bytes.Length); Writer.WriteLine("Connection: close"); Writer.WriteLine("Content-Type: text/plain"); Writer.WriteLine(); Writer.WriteLine(Config); } } } else { Writer.Write("HTTP/1.1 400 Bad Request"); Writer.Flush(); } } args.Player.Kick(""); }
protected byte[] HandlePacket(byte[] buffer) { try { int length = 0; byte msg = buffer[0]; // Get the length of the message by checking the first byte switch (msg) { case 0: length = 130; break; // login case 2: SMPKick("This is not an SMP Server!"); break; // SMP Handshake packet case 5: length = 8; break; // blockchange case 8: length = 9; break; // input case 13: length = 65; break; // chat default: { OnReceivePacket args = new OnReceivePacket(this, buffer); if (args.IsCanceled) return new byte[1]; Kick("Unhandled message id \"" + msg + "\"!"); return new byte[0]; } } if (buffer.Length > length) { byte[] message = new byte[length]; Buffer.BlockCopy(buffer, 1, message, 0, length); byte[] tempbuffer = new byte[buffer.Length - length - 1]; Buffer.BlockCopy(buffer, length + 1, tempbuffer, 0, buffer.Length - length - 1); buffer = tempbuffer; ThreadPool.QueueUserWorkItem(delegate { switch (msg) { case 0: HandleLogin(message); break; case 5: HandleBlockchange(message); break; case 8: HandleIncomingPos(message); break; case 13: HandleChat(message); break; } }); if (buffer.Length > 0) buffer = HandlePacket(buffer); else return new byte[0]; } } catch (Exception e) { Kick("CONNECTION ERROR: (0x03)"); Logger.Log("[ERROR]: PLAYER MESSAGE RECIEVE ERROR (0x03)", Color.Red, Color.Black); Logger.LogError(e); } return buffer; }