Esempio n. 1
0
        /// <summary>
        /// Handles a connected client. Run this method on a seperate thread.
        /// </summary>
        /// <param name="combobotClientDescriptor">A CombobotClientDescriptor object.</param>
        private void HandleClient(object combobotClientDescriptor)
        {
            CombobotClientDescriptor descriptor = (CombobotClientDescriptor)combobotClientDescriptor;

            try
            {
                TcpClient     tc      = descriptor.TcpClient;
                NetworkStream nstream = tc.GetStream();
                while (this.IsRunning)
                {
                    Objects.Packet p = Objects.Packet.GetNextPacket(nstream);
                    if (p.Length == 0)
                    {
                        break;     // disconnected
                    }
                    p.GetUInt16(); // length, not needed
                    switch ((PacketType)p.GetByte())
                    {
                    case PacketType.Ping:
                        descriptor.SetRTT((ushort)descriptor.GetElapsedMilliseconds());
                        if (this.ClientPingReceived != null)
                        {
                            this.ClientPingReceived(descriptor);
                        }
                        break;

                    case PacketType.PlayerInfo:
                        descriptor.CharacterName = p.GetString();
                        break;
                    }
                }
            }
            catch { }
            finally
            {
                if (descriptor.TcpClient != null)
                {
                    descriptor.TcpClient.Close();
                }
                this.ConnectedClients.Remove(descriptor);
                if (this.ClientDisconnected != null)
                {
                    this.ClientDisconnected(descriptor);
                }
            }
        }
Esempio n. 2
0
        private void PlaybackRecv()
        {
            try
            {
                NetworkStream nstream = TcpClientLocal.GetStream();
                int           connection = 0;
                bool          doLoop = true, obtainedXteaKey = false;

                while (doLoop)
                {
                    Objects.Packet p = Objects.Packet.GetNextPacket(nstream);
                    if (p.Length == 0)
                    {
                        break;
                    }

                    if (!obtainedXteaKey)
                    {
                        if (Client.TibiaVersion >= 770)
                        {
                            this.XteaKey = new uint[4];
                            for (int i = 0; i < 4; i++)
                            {
                                this.XteaKey[i] = Memory.ReadUInt(Addresses.Client.XTEAKey + i * 4);
                            }
                        }
                        obtainedXteaKey = true;
                    }

                    connection = Memory.ReadByte(Addresses.Client.Connection);
                    switch ((Enums.Connection)connection)
                    {
                    case Enums.Connection.WaitingForCharacterList:
                        if (this.AutoPlayback)
                        {
                            p = new Objects.Packet();
                            p.AddByte(0x64);
                            p.AddByte(1);
                            p.AddString("TibiaCam");
                            p.AddString(CurrentRecording.TibiaVersion);
                            p.AddBytes(IPAddress.Loopback.GetAddressBytes());
                            p.AddUInt16(this.ListenerPort);
                            p.AddUInt16(0);     // premium days
                            p.AddLength();
                            if (Client.TibiaVersion >= 770)
                            {
                                p.XteaEncrypt(this.XteaKey); p.AddLength();
                            }
                            p.Send(this.TcpClientLocal);
                            doLoop = false;
                            break;
                        }

                        List <string> files = new List <string>();
                        foreach (string kcam in System.IO.Directory.GetFiles(System.Windows.Forms.Application.StartupPath + "\\", "*.kcam"))
                        {
                            files.Add(kcam);
                        }
                        foreach (string iryontcam in System.IO.Directory.GetFiles(System.Windows.Forms.Application.StartupPath + "\\", "*.cam"))
                        {
                            files.Add(iryontcam);
                        }
                        if (Settings.Tibiacam.Playback.SupportTibiaMovies)
                        {
                            foreach (string tmv in System.IO.Directory.GetFiles(System.Windows.Forms.Application.StartupPath + "\\", "*.tmv"))
                            {
                                files.Add(tmv);
                            }
                        }
                        files.Sort();
                        string warning = string.Empty;
                        int    count   = files.Count;
                        if (files.Count > 255)
                        {
                            warning = "\n\nWarning! There are " + files.Count + " recordings.\nOnly the first 255 recordings are listed."; count = 255;
                        }
                        p = new Objects.Packet();
                        p.AddByte(0x14);     // motd type
                        // motd id is 0-255, which is followed by a newline (\n)
                        p.AddString((byte)new Random().Next(255) + "\nThank you for using Tibianic Tools.\nhttp://code.google.com/p/tibianic-tools/\nhttp://tibianic.org/" + warning);
                        p.AddByte(0x64);     // character list type
                        List <Recording> recordings = new List <Recording>();
                        for (int i = 0; i < count; i++)
                        {
                            Recording r = new Recording(files[i], Settings.Tibiacam.Playback.ReadMetaData, false, false);
                            if (!r.isCorrupt)
                            {
                                recordings.Add(r);
                            }
                        }
                        p.AddByte((byte)count);     // amount of characters
                        foreach (Recording r in recordings)
                        {
                            p.AddString(r.FileNameShort);                     // character name
                            p.AddString(r.TibiaVersion);                      // server name
                            p.AddBytes(IPAddress.Loopback.GetAddressBytes()); // server ipv4 (in this case: 127.0.0.1)
                            p.AddUInt16(this.ListenerPort);                   // server port
                        }
                        p.AddUInt16((ushort)count);                           // premium days
                        p.AddLength();
                        if (Client.TibiaVersion >= 770)
                        {
                            p.XteaEncrypt(this.XteaKey); p.AddLength();
                        }
                        p.Send(TcpClientLocal);
                        doLoop = false;
                        break;

                    case Enums.Connection.ConnectingGameServer:
                        if (this.AutoPlayback)
                        {
                            this.threadPlaybackSend = new Thread(new ThreadStart(PlaybackSend));
                            this.threadPlaybackSend.Start();
                            break;
                        }
                        CharacterList.Player player = CharacterList.GetPlayers()[Memory.ReadByte(Addresses.Charlist.SelectedIndex)];
                        if (!System.IO.File.Exists(player.Name))
                        {
                            System.Windows.Forms.MessageBox.Show(player.Name + "\ndoes not exist", "Error"); doLoop = false; break;
                        }
                        Recording rec = new Recording(player.Name, true, true, Settings.Tibiacam.Playback.doPlayMouse);
                        if (rec.isCorrupt || !rec.ContainsData())
                        {
                            System.Windows.Forms.MessageBox.Show(player.Name + "\nappears to be corrupt or it doesn't contain any data", "Error"); doLoop = false; break;
                        }
                        this.CurrentRecording = rec;
                        while (this.threadPlaybackSend != null && this.threadPlaybackSend.IsAlive)
                        {
                            this.threadPlaybackSend.Abort(); Thread.Sleep(100);
                        }
                        while (this.threadPlaybackSendMouse != null && this.threadPlaybackSendMouse.IsAlive)
                        {
                            this.threadPlaybackSendMouse.Abort(); Thread.Sleep(100);
                        }
                        this.threadPlaybackSend = new Thread(new ThreadStart(PlaybackSend));
                        this.threadPlaybackSend.Start();
                        break;

                    case Enums.Connection.Online:
                        ushort len = p.GetUInt16();
                        if (Client.TibiaVersion >= 770)
                        {
                            p = p.XteaDecrypt(this.XteaKey, 2);
                            if (p == null)
                            {
                                break;
                            }
                            p.GetPosition = 2;
                        }
                        byte type = p.GetByte();
                        switch (type)
                        {
                        case 0x14:         // logout
                            while (this.threadPlaybackSend != null && this.threadPlaybackSend.IsAlive)
                            {
                                this.threadPlaybackSend.Abort(); Thread.Sleep(100);
                            }
                            this.TcpClientLocal.Close();
                            return;

                        case 0x96:         // player speech
                            byte speechType = p.GetByte();
                            if (speechType < 1 || speechType > 3)
                            {
                                break;
                            }
                            string   msg      = p.GetString().ToLower();
                            string[] msgSplit = msg.Split(' ');
                            p = new Objects.Packet();
                            switch (msgSplit[0])
                            {
                            case "info":
                                p.AddByte(0xb4);
                                p.AddByte(22);
                                p.AddString("Tibia version: " + this.CurrentRecording.TibiaVersion + "\nRecorder version: " + this.CurrentRecording.RecorderVersion + "\n# of packets: " + this.CurrentRecording.Packets.Count);
                                p.AddLength();
                                if (Client.TibiaVersion >= 770)
                                {
                                    p.XteaEncrypt(this.XteaKey, p.GetPosition); p.AddLength();
                                }
                                p.Send(this.TcpClientLocal);
                                break;

                            case "goto":
                                TimeSpan ts;
                                if (!TimeSpan.TryParse(msgSplit[1], out ts))
                                {
                                    break;
                                }
                                this.FastForward(ts);
                                break;

                            case "pause":
                                this.PlaybackSpeed = 0;
                                break;

                            case "resume":
                                this.PlaybackSpeed = 1;
                                break;
                            }
                            break;
                        }
                        break;
                    }
                }
                if (TcpClientLocal != null)
                {
                    TcpClientLocal.Close();
                }
            }
            catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message + "\n" + ex.StackTrace); if (TcpClientLocal != null)
                                   {
                                       TcpClientLocal.Close();
                                   }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Handles the local Tibia client connection. Should be run on its own thread.
        /// </summary>
        private void HandleTcpLocal()
        {
            try
            {
                if (TcpClientLocal == null)
                {
                    return;
                }
                NetworkStream nstream         = TcpClientLocal.GetStream();
                int           connection      = 0;
                bool          obtainedXteaKey = false;

                while (true)
                {
                    Objects.Packet p = Objects.Packet.GetNextPacket(nstream);
                    if (p.Length == 0)
                    {
                        break;
                    }

                    if (!obtainedXteaKey)
                    {
                        if (Client.TibiaVersion >= 770)
                        {
                            this.XteaKey = new uint[4];
                            for (int i = 0; i < 4; i++)
                            {
                                this.XteaKey[i] = Memory.ReadUInt(Addresses.Client.XTEAKey + i * 4);
                            }
                        }
                        obtainedXteaKey = true;
                    }

                    if (connection != 8)
                    {
                        connection = Memory.ReadByte(Addresses.Client.Connection);
                        if (connection == 6) // connecting to gameserver
                        {
                            try
                            {
                                byte index = Memory.ReadByte(Addresses.Charlist.SelectedIndex);
                                if (index < 0 || index >= this.CachedCharacterList.Count)
                                {
                                    break;                                                       // invalid index
                                }
                                CharacterList.Player player = this.CachedCharacterList[index];
                                TcpClientServer = new TcpClient(player.IP, player.Port);
                                Thread t = new Thread(new ThreadStart(HandleTcpServer));
                                t.Start();
                                Thread.Sleep(20); // give thread time to start
                            }
                            catch { break; }
                        }
                        else if (connection > 0 && connection < 6) // connecting to login server, not sure exactly what value it should be
                        {
                            try
                            {
                                this.TcpClientServer = new TcpClient(this.ServerInfo.IP, this.ServerInfo.Port);
                                Thread t = new Thread(new ThreadStart(HandleTcpServer));
                                t.Start();
                                Thread.Sleep(20); // give thread time to start
                            }
                            catch { break; } // couldn't connect to server
                        }
                    }
                    p.Send(this.TcpClientServer);

                    try
                    {
                        if (Client.TibiaVersion >= 770)
                        {
                            p = p.XteaDecrypt(this.XteaKey, 2);
                        }
                        ushort len        = p.GetUInt16();
                        byte   packetType = p.GetByte();
                        if (!Settings.Tibiacam.Recorder.Filters.OutgoingPrivateMessages && packetType == 0x96 && Client.TibiaVersion == 740)
                        {
                            if (p.GetByte() != 0x04)
                            {
                                continue;
                            }
                            Objects.Packet pm = new Objects.Packet();
                            pm.AddByte((byte)Addresses.Enums.IncomingPacketTypes.CreatureSpeech);
                            string recipient = p.GetString();
                            pm.AddString(recipient);
                            pm.AddByte(0x04);
                            string msg = p.GetString();
                            pm.AddString("» " + msg);
                            pm.AddLength();
                            //Objects.Packet temp = new Objects.Packet();
                            //temp.AddUInt32((uint)this.RecordingStopwatch.ElapsedMilliseconds);
                            //temp.AddUInt32((uint)pm.Length);
                            //temp.AddBytes(pm.ToBytes());
                            CurrentRecording.Packets.Add(new Recording.Packet((uint)this.RecordingStopwatch.ElapsedMilliseconds, pm.ToBytes()));
                        }
                    }
                    catch { }
                }
                if (this.TcpClientLocal != null)
                {
                    this.TcpClientLocal.Close();
                }
                if (this.TcpClientServer != null)
                {
                    this.TcpClientServer.Close();
                }
            }
            catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message + "\n" + ex.StackTrace); }
        }