private void HandleRakNetMessage(IPEndPoint senderEndpoint, UnconnectedPing incoming) { //TODO: This needs to be verified with RakNet first //response.sendpingtime = msg.sendpingtime; //response.sendpongtime = DateTimeOffset.UtcNow.Ticks / TimeSpan.TicksPerMillisecond; if (IsEdu) { var packet = UnconnectedPong.CreateObject(); packet.serverId = senderEndpoint.Address.Address + senderEndpoint.Port; packet.pingId = incoming.pingId; packet.serverName = MotdProvider.GetMotd(ServerInfo, senderEndpoint, true); var data = packet.Encode(); packet.PutPool(); TraceSend(packet); SendData(data, senderEndpoint); } { var packet = UnconnectedPong.CreateObject(); packet.serverId = senderEndpoint.Address.Address + senderEndpoint.Port; packet.pingId = incoming.pingId; packet.serverName = MotdProvider.GetMotd(ServerInfo, senderEndpoint); var data = packet.Encode(); packet.PutPool(); TraceSend(packet); SendData(data, senderEndpoint); } return; }
private void HandleQuery(byte[] receiveBytes, IPEndPoint senderEndpoint) { if (!Config.GetProperty("EnableQuery", false)) { return; } if (receiveBytes[0] != 0xFE || receiveBytes[1] != 0xFD) { return; } byte packetId = receiveBytes[2]; switch (packetId) { case 0x09: { byte[] buffer = new byte[17]; // ID buffer[0] = 0x09; // Sequence number buffer[1] = receiveBytes[3]; buffer[2] = receiveBytes[4]; buffer[3] = receiveBytes[5]; buffer[4] = receiveBytes[6]; // Textual representation of int32 (token) with null terminator string str = new Random().Next().ToString(CultureInfo.InvariantCulture) + "\x00"; Buffer.BlockCopy(str.ToCharArray(), 0, buffer, 5, 11); _listener.Send(buffer, buffer.Length, senderEndpoint); break; } case 0x00: { using (var stream = MemoryStreamManager.GetStream()) { bool isFullStatRequest = receiveBytes.Length == 15; if (Log.IsInfoEnabled) { Log.InfoFormat("Full request: {0}", isFullStatRequest); } // ID stream.WriteByte(0x00); // Sequence number stream.WriteByte(receiveBytes[3]); stream.WriteByte(receiveBytes[4]); stream.WriteByte(receiveBytes[5]); stream.WriteByte(receiveBytes[6]); //{ // string str = "splitnum\0"; // byte[] bytes = Encoding.ASCII.GetBytes(str.ToCharArray()); // stream.Write(bytes, 0, bytes.Length); //} MotdProvider.GetMotd(ServerInfo, senderEndpoint); // Force update the player counts :-) var data = new Dictionary <string, string> { { "splitnum", "" + (char)128 }, { "hostname", "Minecraft PE Server" }, { "gametype", "SMP" }, { "game_id", "MINECRAFTPE" }, { "version", "0.15.0" }, { "server_engine", "MiNET v1.0.0" }, { "plugins", "MiNET v1.0.0" }, { "map", "world" }, { "numplayers", MotdProvider.NumberOfPlayers.ToString() }, { "maxplayers", MotdProvider.MaxNumberOfPlayers.ToString() }, { "whitelist", "off" }, //{"hostip", "192.168.0.1"}, //{"hostport", "19132"} }; foreach (KeyValuePair <string, string> valuePair in data) { string key = valuePair.Key + "\x00" + valuePair.Value + "\x00"; byte[] bytes = Encoding.ASCII.GetBytes(key.ToCharArray()); stream.Write(bytes, 0, bytes.Length); } { string str = "\x00\x01player_\x00\x00"; byte[] bytes = Encoding.ASCII.GetBytes(str.ToCharArray()); stream.Write(bytes, 0, bytes.Length); } // End the stream with 0 byte stream.WriteByte(0); var buffer = stream.ToArray(); _listener.Send(buffer, buffer.Length, senderEndpoint); } break; } default: return; } }
private void HandleRakNetMessage(byte[] receiveBytes, IPEndPoint senderEndpoint, byte msgId) { DefaultMessageIdTypes msgIdType = (DefaultMessageIdTypes)msgId; Package message = PackageFactory.CreatePackage(msgId, receiveBytes); if (message == null) { if (!_badPacketBans.ContainsKey(senderEndpoint.Address)) { _badPacketBans.Add(senderEndpoint.Address, true); } Log.ErrorFormat("Receive bad packet with ID: {0} (0x{0:x2}) {2} from {1}", msgId, senderEndpoint.Address, (DefaultMessageIdTypes)msgId); return; } message.Source = "RakNet"; TraceReceive(message); switch (msgIdType) { case DefaultMessageIdTypes.ID_UNCONNECTED_PING: case DefaultMessageIdTypes.ID_UNCONNECTED_PING_OPEN_CONNECTIONS: { UnconnectedPing incoming = (UnconnectedPing)message; //TODO: This needs to be verified with RakNet first //response.sendpingtime = msg.sendpingtime; //response.sendpongtime = DateTimeOffset.UtcNow.Ticks / TimeSpan.TicksPerMillisecond; var packet = UnconnectedPong.CreateObject(); packet.serverId = 22345; packet.pingId = incoming.pingId; packet.serverName = MotdProvider.GetMotd(ServerInfo); var data = packet.Encode(); packet.PutPool(); TraceSend(packet); SendData(data, senderEndpoint, new object()); break; } case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REQUEST_1: { OpenConnectionRequest1 incoming = (OpenConnectionRequest1)message; Log.DebugFormat("New connection from: {0} {1}", senderEndpoint.Address, senderEndpoint.Port); lock (_playerSessions) { // Already connecting, then this is just a duplicate if (_connectionAttemps.ContainsKey(senderEndpoint)) { DateTime created; _connectionAttemps.TryGetValue(senderEndpoint, out created); if (DateTime.UtcNow < created + TimeSpan.FromSeconds(3)) { return; } } //PlayerNetworkSession session; //if (_playerSessions.TryGetValue(senderEndpoint, out session)) //{ // Log.DebugFormat("Reconnection detected from {0}. Removing old session and disconnecting old player.", senderEndpoint.Address); // Player oldPlayer = session.Player; // if (oldPlayer != null) // { // oldPlayer.Disconnect("Reconnecting."); // } // else // { // _playerSessions.TryRemove(session.EndPoint, out session); // } //} if (!_connectionAttemps.ContainsKey(senderEndpoint)) { _connectionAttemps.Add(senderEndpoint, DateTime.UtcNow); } } var packet = OpenConnectionReply1.CreateObject(); packet.serverGuid = 12345; packet.mtuSize = incoming.mtuSize; packet.serverHasSecurity = 0; var data = packet.Encode(); packet.PutPool(); TraceSend(packet); SendData(data, senderEndpoint, new object()); break; } case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REQUEST_2: { OpenConnectionRequest2 incoming = (OpenConnectionRequest2)message; PlayerNetworkSession session; lock (_playerSessions) { if (_connectionAttemps.ContainsKey(senderEndpoint)) { _connectionAttemps.Remove(senderEndpoint); } else { Log.ErrorFormat("Unexpected connection request packet from {0}.", senderEndpoint.Address); return; } //PlayerNetworkSession session; if (_playerSessions.TryGetValue(senderEndpoint, out session)) { Log.WarnFormat("Reconnection detected from {0}. Removing old session and disconnecting old player.", senderEndpoint.Address); Player oldPlayer = session.Player; if (oldPlayer != null) { oldPlayer.Disconnect("Reconnecting."); } else { _playerSessions.TryRemove(session.EndPoint, out session); } } if (_playerSessions.TryGetValue(senderEndpoint, out session)) { // Already connecting, then this is just a duplicate if (session.State == ConnectionState.Connecting /* && DateTime.UtcNow < session.LastUpdatedTime + TimeSpan.FromSeconds(2)*/) { return; } Log.ErrorFormat("Unexpected session from {0}. Removing old session and disconnecting old player.", senderEndpoint.Address); Player oldPlayer = session.Player; if (oldPlayer != null) { oldPlayer.Disconnect("Reconnecting."); } else { _playerSessions.TryRemove(session.EndPoint, out session); } } session = new PlayerNetworkSession(null, senderEndpoint) { State = ConnectionState.Connecting, LastUpdatedTime = DateTime.UtcNow, Mtuize = incoming.mtuSize }; _playerSessions.TryAdd(senderEndpoint, session); } Player player = PlayerFactory.CreatePlayer(this, senderEndpoint, incoming.mtuSize); player.ClientGuid = incoming.clientGuid; player.NetworkSession = session; session.Player = player; var reply = OpenConnectionReply2.CreateObject(); reply.serverGuid = 12345; reply.mtuSize = incoming.mtuSize; reply.doSecurityAndHandshake = new byte[0]; var data = reply.Encode(); reply.PutPool(); TraceSend(reply); SendData(data, senderEndpoint, session.SyncRoot); break; } default: if (!_badPacketBans.ContainsKey(senderEndpoint.Address)) { _badPacketBans.Add(senderEndpoint.Address, true); } Log.ErrorFormat("Receive unexpected packet with ID: {0} (0x{0:x2}) {2} from {1}", msgId, senderEndpoint.Address, (DefaultMessageIdTypes)msgId); break; } message.PutPool(); }