Exemplo n.º 1
0
 public void SendMessage(IPeerMessage message)
 {
     using (var ms = new MemoryStream())
     {
         BinaryWriter w = new BigEndianBinaryWriter(ms);
         message.Send(w);
         Send(ms.ToArray());
     }
 }
Exemplo n.º 2
0
        public override void onReceiveMessage(IPeerMessage message)
        {
            Received += (ulong)message.DecompressedRawSize;
            PacketsPerSec++;

            (message as TestMessage).Stuff = new byte[5];
            SendMessage(message);

            if (sw.ElapsedMilliseconds >= 1000)
            {
                Console.WriteLine("[Rootsocket] last size: " + message.DecompressedRawSize + ", pps:" + PacketsPerSec + ", data/sec:" + Received + " [" + Math.Round(((float)Received / 1000F) / 1000F, 2) + "MBps] " + (Math.Round((((float)Received / 1000F) / 1000F) / 1000F, 2) * 8F) + "Gbps");
                sw            = Stopwatch.StartNew();
                Received      = 0;
                PacketsPerSec = 0;
            }
        }
Exemplo n.º 3
0
        public void SendMessage(IPeerMessage message)
        {
            if (!Connected)
            {
                return;
            }

            message.ConnectionId = this.ConnectionId;

            Connection           conn = _client.Connection;
            NetworkPayloadWriter temp = message.WritePacket(message, conn);

            message.RawSize = temp.Length - _client.Connection.HEADER_SIZE;

            //apply compression, encryption, masks here

            conn.SendPayload(temp, conn.messageHandler.GetMessageId(message.GetType()), PacketId.RootSocket_Payload, false, null, null, VirtualIpInt);
        }
Exemplo n.º 4
0
 public override void onReceiveMessage(IPeerMessage message)
 {
 }
 private void onRootSocketQueue(IPeerMessage message)
 {
     message.Peer.onReceiveMessage(message);
 }
        private unsafe void networkStreamCallback(Network.NetworkStream stream)
        {
            lock (stream)
            {
                bool DataAvailable = true;
                while (DataAvailable)
                {
                    switch (this.stream.ReceiveState)
                    {
                    case ReceiveType.Header:
                    {
                        if (stream.CanRead(HEADER_SIZE))
                        {
                            byte[] headerData = new byte[HEADER_SIZE];
                            if (stream.Read(ref headerData, 0, headerData.Length) > 0)
                            {
                                //wopEncryption.Decrypt(header, 0, HEADER_SIZE);
                                stream.NetworkPayload.Header = new PacketHeader(headerData, 0, this);

                                if (!DPI.Inspect(stream.NetworkPayload.Header))
                                {
                                    Client.Disconnect(DisconnectReason.DeepPacketInspectionDisconnection);
                                    return;
                                }
                                this.stream.ReceiveState = ReceiveType.Payload;
                            }
                        }
                        else
                        {
                            DataAvailable = false;
                        }
                        break;
                    }

                    case ReceiveType.Payload:
                    {
                        if (stream.CanRead(stream.NetworkPayload.Header.PacketSize))
                        {
                            int          receivedSize = stream.NetworkPayload.Header.PacketSize;
                            uint         packetSize   = (uint)stream.NetworkPayload.Header.PacketSize;
                            byte[]       payload      = stream.Buffer;
                            uint         offset       = (uint)stream.Position;
                            PacketHeader header       = stream.NetworkPayload.Header;
                            IPlugin      plugin       = null;
                            uint         GenHash      = 0;

                            if (stream.NetworkPayload.Header.PacketID == PacketId.PluginPacket)
                            {
                                plugin = pluginSystem.GetPlugin(stream.NetworkPayload.Header.PluginId);

                                if (plugin == null)
                                {
                                    throw new Exception("Plugin not found");
                                }
                            }

                            if (Client.Certificate != null)
                            {
                                GenHash = stream.NetworkPayload.Header.HashPayload(payload, (int)offset, (int)packetSize, Client.Certificate.Checksum);

                                if (stream.NetworkPayload.Header.Hash != GenHash)
                                {
                                    Client.Disconnect(DisconnectReason.DataModificationDetected);
                                    return;
                                }
                            }

                            //peer code
                            if (stream.NetworkPayload.Header.PeerId != 0 && stream.NetworkPayload.Header.PacketID == PacketId.RootSocket_Payload)
                            {
                                if (stream.NetworkPayload.Header.PeerId == Client.VirtualIpInt && !Client.ServerSided)
                                {
                                    //we arrived at the target peer !
                                    IPeerMessage PeerMsg = null;
                                    try
                                    {
                                        PeerMsg = (IPeerMessage)messageHandler.HandleMessage(new PayloadReader(payload)
                                            {
                                                Offset = (int)offset
                                            }, stream.NetworkPayload.Header.MessageId);

                                        if (PeerMsg != null)
                                        {
                                            PeerMsg.RawSize             = stream.NetworkPayload.Header.PacketSize;
                                            PeerMsg.DecompressedRawSize = payload.Length;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Client.onException(ex, ErrorType.Core);
                                        return;
                                    }

                                    lock (Client.PeerConnections)
                                    {
                                        RootPeer peer = null;
                                        if (Client.PeerConnections.TryGetValue(PeerMsg.ConnectionId, out peer))
                                        {
                                            /*try
                                             * {
                                             *  if (!peer.DPI.Inspect(stream.NetworkPayload.Header, PeerMsg))
                                             *  {
                                             *      Client.Disconnect(DisconnectReason.DeepPacketInspectionDisconnection);
                                             *      return;
                                             *  }
                                             * }
                                             * catch { }*/

                                            PeerMsg.Peer = peer;
                                            RootSocketQueue.Enqueue(PeerMsg);
                                        }
                                    }
                                }
                                else if (Client.ServerSided)
                                {
                                    SSPClient TargetClient = null;

                                    lock (Client.PeerConnections)
                                    {
                                        for (int i = 0; i < Client.PeerConnections.Count; i++)
                                        {
                                            RootPeer rootPeer = Client.PeerConnections.Values[i];
                                            if (rootPeer.FromClient != null && rootPeer.FromClient.VirtualIpInt == stream.NetworkPayload.Header.PeerId)
                                            {
                                                TargetClient = rootPeer.FromClient;
                                                break;
                                            }
                                            if (rootPeer.ToClient != null && rootPeer.ToClient.VirtualIpInt == stream.NetworkPayload.Header.PeerId)
                                            {
                                                TargetClient = rootPeer.ToClient;
                                                break;
                                            }
                                        }
                                    }

                                    if (TargetClient != null)
                                    {
                                        //no protection is being applied to the payload
                                        //the protection should already have been applied
                                        //when the client sended this data to the server
                                        NetworkPayloadWriter pw = new NetworkPayloadWriter(this);
                                        pw.WriteBytes(payload, (int)offset, receivedSize);
                                        TargetClient.Connection.SendPayload(pw, stream.NetworkPayload.Header.MessageId,
                                                                            PacketId.RootSocket_Payload, false, null, null,
                                                                            stream.NetworkPayload.Header.PeerId);
                                    }
                                }
                                else
                                {
                                    //strange...
                                }

                                this.stream.ReceiveState = ReceiveType.Header;
                                stream.Position         += receivedSize;
                                payload = null;
                                break;
                            }

                            //decrypt, decompress, de-cache the data we received
                            if (plugin != null && plugin.UseOwnProtection)
                            {
                                payload = plugin.protection.RemoveProtection(payload, ref offset, ref packetSize, ref stream.NetworkPayload.Header);
                            }
                            else
                            {
                                payload = protection.RemoveProtection(payload, ref offset, ref packetSize, ref stream.NetworkPayload.Header);
                            }

                            /*
                             * if (stream.NetworkPayload.isCached)
                             * {
                             *  payload = ReceiveCache.DeCache(payload, offset, packetSize);
                             *  offset = 0;
                             *  packetSize = payload.Length;
                             * }*/

                            IMessage message = null;
                            try
                            {
                                message = messageHandler.HandleMessage(new PayloadReader(payload)
                                    {
                                        Offset = (int)offset
                                    }, stream.NetworkPayload.Header.MessageId);

                                if (message != null)
                                {
                                    message.RawSize             = stream.NetworkPayload.Header.PacketSize;
                                    message.DecompressedRawSize = payload.Length;
                                }
                            }
                            catch (Exception ex)
                            {
                                Client.onException(ex, ErrorType.Core);
                                return;
                            }

                            if (!Client.DPI.Inspect(null, message))
                            {
                                Client.Disconnect(DisconnectReason.DeepPacketInspectionDisconnection);
                                return;
                            }

                            /*if(ProcessSpeed == ReceivePerformance.Unsafe)
                             * {
                             *  if(!MovedPayload && plugin != null && (plugin.UseOwnSystem))
                             *  {
                             *      //payload is not moved from memory and plugin is using his own system
                             *      //We'll just copy the memory...
                             *      payload = new byte[packetSize];
                             *      fixed (byte* dataPtr = payload, streamPtr = stream.Buffer)
                             *      {
                             *          NativeMethods.memcpy(dataPtr, streamPtr, (uint)packetSize);
                             *      }
                             *  }
                             * }*/

                            switch (stream.NetworkPayload.Header.PacketID)
                            {
                            case PacketId.PacketQueue:
                            {
                                PacketTaskQueue.Enqueue(message);
                                break;
                            }

                            case PacketId.ChannelPayload:
                            {
                                ChannelPayloadQueue.Enqueue(new ChannelRecvInfo(message, stream.NetworkPayload.Header.ChannelId));
                                break;
                            }

                            case PacketId.CloseChannel:
                            {
                                CloseChannelQueue.Enqueue(message);
                                break;
                            }

                            case PacketId.Disconnected:
                            {
                                DisconnectedQueue.Enqueue(message);
                                break;
                            }

                            case PacketId.OpenChannel:
                            {
                                OpenChannelQueue.Enqueue(message);
                                break;
                            }

                            case PacketId.OpenChannelResponse:
                            case PacketId.LiteCodeResponse:
                            case PacketId.KeepAlive:
                            {
                                ResponseQueue.Enqueue(message);
                                break;
                            }

                            case PacketId.Payload:
                            {
                                PayloadQueue.Enqueue(message);
                                break;
                            }

                            case PacketId.PluginPacket:
                            {
                                PluginDataQueue.Enqueue(new PluginRecvInfo(plugin, message, payload));
                                break;
                            }

                            case PacketId.StreamMessages:
                            {
                                StreamQueue.Enqueue(message);
                                break;
                            }

                            case PacketId.LiteCode:
                            {
                                LiteCodeQueue.Enqueue(message);
                                break;
                            }

                            case PacketId.LiteCode_Delegates:
                            {
                                LiteCodeDelegateQueue.Enqueue(message);
                                break;
                            }

                            case PacketId.RequestMessages:
                            {
                                RequestQueue.Enqueue(message);
                                break;
                            }
                            }

                            this.stream.ReceiveState = ReceiveType.Header;
                            stream.Position         += receivedSize;
                            payload = null;
                        }
                        else
                        {
                            DataAvailable = false;
                        }
                        break;
                    }
                    }
                }
            }
        }
Exemplo n.º 7
0
 public abstract void onReceiveMessage(IPeerMessage message);