/// <summary>
        /// Connect to a peer
        /// </summary>
        /// <param name="VirtualIpOrDns"></param>
        /// <param name="peer"></param>
        /// <returns></returns>
        public PeerErrorCode ConnectToPeer(string VirtualIpOrDns, RootPeer peer)
        {
            if (peer.Connected)
            {
                throw new Exception("Peer is already connected");
            }

            PeerResponse response = SharedRoot.ConnectToPeer(VirtualIpOrDns);

            if (response.ErrorCode == PeerErrorCode.Success)
            {
                peer.Connected    = true; //temp
                peer.VirtualIP    = response.VirtualIP;
                peer.ConnectionId = response.ConnectionId;
                peer._client      = this;
                peer.FromClient   = this;

                if (!PeerConnections.ContainsKey(response.ConnectionId))
                {
                    PeerConnections.Add(response.ConnectionId, peer);
                }
                else
                {
                    PeerConnections[response.ConnectionId] = peer;
                }

                peer.onRegisterMessages(this.MessageHandler);
                peer.onClientConnect();
                return(PeerErrorCode.Success);
            }
            return(response.ErrorCode);
        }
示例#2
0
        public bool RequestPeerConnection(string VirtualIP, decimal ConnectionId)
        {
            lock (client.PeerConnections)
            {
                RootPeer peer = client.onGetNewPeerObject();
                peer.VirtualIP    = VirtualIP;
                peer.ConnectionId = ConnectionId;
                peer._client      = client;
                peer.FromClient   = client;

                if (client.PeerConnections.ContainsKey(ConnectionId))
                {
                    client.Disconnect(); //should never happen!
                }
                bool HasPermission = client.onPeerConnectionRequest(peer);

                if (HasPermission)
                {
                    client.PeerConnections.Add(ConnectionId, peer);
                }
                else
                {
                    peer = null;
                }
                return(HasPermission);
            }
        }
 public override bool onPeerConnectionRequest(RootPeer peer)
 {
     Console.WriteLine("Allowed connect permission for peer " + peer.VirtualIP);
     return(true);
 }
示例#4
0
        public PeerResponse ConnectToPeer(string VirtualIpOrDns)
        {
            SSPClient TargetClient = null;

            //check if dns name
            if (!Regex.IsMatch(VirtualIpOrDns, RootPeer.IpValidationString))
            {
                RootDnsInfo inf = client.Server.RootSocket_DNS.GetDnsRecord(VirtualIpOrDns);
                if (inf != null)
                {
                    VirtualIpOrDns = inf.VirtualIp;
                }
            }

            TargetClient = client.Server.GetClient(VirtualIpOrDns);

            if (TargetClient == null)
            {
                return(new PeerResponse(PeerErrorCode.PeerNotFound, 0, ""));
            }

            //check if server side allows this connection
            if (!client.Server.onPeerConnectionRequest(client, TargetClient))
            {
                return(new PeerResponse(PeerErrorCode.PermissionDenied, 0, ""));
            }

            //check if target client allows this connection
            decimal ConnectionId  = 0;
            bool    HasPermission = false;

            lock (TargetClient.PeerConnections)
            {
                lock (client.PeerConnections)
                {
                    RandomDecimal rnd = new RandomDecimal(DateTime.Now.Millisecond);
                    ConnectionId = rnd.NextDecimal();
                    while (TargetClient.PeerConnections.ContainsKey(ConnectionId) || client.PeerConnections.ContainsKey(ConnectionId))
                    {
                        ConnectionId = rnd.NextDecimal();
                    }

                    try
                    {
                        HasPermission = TargetClient.SharedClientRoot.RequestPeerConnection(client.VirtualIP, ConnectionId);
                    }
                    catch { HasPermission = false; }


                    if (HasPermission)
                    {
                        RootPeer TargetPeer = client.onGetNewPeerObject();
                        TargetPeer._client      = TargetClient;
                        TargetPeer.FromClient   = TargetClient;
                        TargetPeer.ToClient     = client;
                        TargetPeer.VirtualIP    = client.VirtualIP;
                        TargetPeer.Connected    = true;
                        TargetPeer.ConnectionId = ConnectionId;

                        RootPeer FromPeer = client.onGetNewPeerObject();
                        FromPeer._client      = client;
                        FromPeer.FromClient   = client;
                        FromPeer.ToClient     = TargetClient;
                        FromPeer.VirtualIP    = TargetClient.VirtualIP;
                        FromPeer.Connected    = true;
                        FromPeer.ConnectionId = ConnectionId;

                        if (!TargetClient.PeerConnections.ContainsKey(ConnectionId))
                        {
                            TargetClient.PeerConnections.Add(ConnectionId, TargetPeer);
                        }

                        if (!client.PeerConnections.ContainsKey(ConnectionId))
                        {
                            client.PeerConnections.Add(ConnectionId, FromPeer);
                        }

                        TargetClient.SharedClientRoot.NewPeerconnection(ConnectionId);

                        return(new PeerResponse(PeerErrorCode.Success, ConnectionId, TargetClient.VirtualIP));
                    }
                    return(new PeerResponse(PeerErrorCode.PermissionDenied, 0, ""));
                }
            }
        }
        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;
                    }
                    }
                }
            }
        }
 public abstract bool onPeerConnectionRequest(RootPeer peer);