Exemplo n.º 1
0
        public void DecryptAES(IReadStream readStream, string sharedSecret)
        {
            if (!IsEncrypt)
            {
                return;
            }
            using (var aesAlg = new RijndaelManaged()) {
                var key = new Rfc2898DeriveBytes(sharedSecret, _salt);
                aesAlg.Padding = PaddingMode.None;

                aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);

                var iv    = readStream.ShiftRight(readStream.ShiftRight <ushort>());
                var bytes = new byte[iv.Count];
                Buffer.BlockCopy(iv.Buffer, iv.Offset, bytes, 0, iv.Count);
                aesAlg.IV = bytes;

                var length       = readStream.ShiftRight <ushort>();
                var byteFragment = readStream.ToByteFragment();

                using (var msDecrypt = new MemoryStream(byteFragment.Buffer, byteFragment.Offset, byteFragment.Count)) {
                    var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                    using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
                        csDecrypt.Read(byteFragment.Buffer, byteFragment.Offset, length);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void Connected(IPeer peer, IReadStream readStream)
        {
            if (peer == null)
            {
                Logger.Info($"连接失败!");
                Callback?.ConnectEvent?.Invoke(false, readStream);
                return;
            }

            Logger.Info("连接到: " + peer.Connection.RemoteAddress);
            var ids = readStream.ShiftRight <string>();

            Logger.Info(ids.Length);
            if (ids != null && ids.Length != 0)
            {
                var rpcMethodIds = ids.Split(';');

                foreach (var item in rpcMethodIds)
                {
                    Ssui.Connections[item] = peer;
                }

                Logger.Info(ids);
            }

            Callback?.ConnectEvent?.Invoke(true, readStream);
        }
Exemplo n.º 3
0
        private void AcceptConnected(IUser peer, IReadStream readStream,
                                     IWriteStream writeStream)
        {
            var publicKey  = readStream.ShiftRight <string>();
            var key        = ID + "_" + SystemUtil.CreateRandomString(Ssfi.CryptoConfig.CryptonKeyLength);
            var encryptKey = Ssfi.Security.EncryptAesKey(key, publicKey);

            var securityComponent = SecurityComponent.GetObject();

            securityComponent.AesKey     = key;
            securityComponent.Encryptkey = encryptKey;

            peer.AddComponent <ISecurityComponent>(securityComponent);
            Logger.Debug($"添加{nameof(ISecurityComponent)}组件!");

            writeStream.ShiftRight((ushort)encryptKey.Length);
            Logger.Debug("EncryptKey Length:" + encryptKey.Length);
            writeStream.ShiftRight(encryptKey);
            writeStream.ShiftRight(string.Join(";", RpcMethodIds));

            var tmp = readStream.Clone();

            _serverSocket.AddEvent(() => {
                try {
                    _acceptAction.Invoke(peer, readStream);
                } catch (Exception e) {
                    Logger.Error(e);
                }
                tmp.Dispose();
            });
        }
Exemplo n.º 4
0
        private void PeerConnected(IUser peer, IReadStream readStream)
        {
            var length = readStream.ShiftRight <ushort>();
            var data   = readStream.ShiftRight(length);
            var bytes  = new byte[data.Count];

            Buffer.BlockCopy(data.Buffer, data.Offset, bytes, 0, bytes.Length);
            var buffer = Ssfi.Security.DecryptAesKey(bytes, _keys);
            var aesKey = buffer.toString();

            var securityComponent = SecurityComponent.GetObject();

            securityComponent.AesKey = aesKey;
            peer.AddComponent <ISecurityComponent>(securityComponent);
            Logger.Debug($"添加{nameof(ISecurityComponent)}组件!");

            try {
                _connectedAction?.Invoke(peer, readStream);
            } catch (Exception e) {
                Logger.Error(e);
            }
        }
Exemplo n.º 5
0
 public void FromBytes(IReadStream readStream)
 {
     Length         = readStream.ShiftRight <ushort>();
     PacketId       = readStream.ShiftRight <ulong>();
     SendOption     = (SendOption)readStream.ShiftRight <byte>();
     TotalBytes     = readStream.ShiftRight <ushort>();
     FragmentId     = readStream.ShiftRight <ushort>();
     TotalFragments = readStream.ShiftRight <ushort>();
 }
Exemplo n.º 6
0
        public static void Invoke(ulong remoteMessageId, IReadStream readStream, IPeer peer)
        {
            var index = (MessageType)readStream.ShiftRight <short>();

            if (!_messages.TryGetValue(index, out var func))
            {
                Logger.Warn($"没有添加消息:{index}");
                return;
            }

            var message = func();

            message.DeserializeFields(readStream);
            message.Handle(remoteMessageId, peer, readStream);
        }
Exemplo n.º 7
0
 public void DeserializeFields(IReadStream readStream)
 {
     ID        = readStream.ShiftRight <short>();
     Exception = readStream.ShiftRight <string>();
     Status    = readStream.ShiftRight <bool>();
 }
Exemplo n.º 8
0
        public IConvertible Read()
        {
            if (_readStream.ShiftRight <bool>())
            {
                var typeCode = (TypeCode)_readStream.ShiftRight <byte>();
                var value    = _readStream.ShiftRight(typeCode);
                return(value);
            }

            return(null);
        }
Exemplo n.º 9
0
        public object[] Deserialize(object[] objects, int length)
        {
            for (var i = 0; i < length; ++i)
            {
                var type = (FieldType)_readStream.ShiftRight <byte>();
                switch (type)
                {
                case FieldType.ArrayBase: {
                    objects[i] = _endianBinaryReader.ReadArray();
                }
                break;

                case FieldType.ArrayPacket: {
                    objects[i] = _endianBinaryReader.ReadPacketArray();
                }
                break;

                case FieldType.DictKBVB: {
                    objects[i] = _endianBinaryReader.ReadDictionaryBB();
                }
                break;

                case FieldType.PacketType: {
                    objects[i] = _endianBinaryReader.ReadPacket();
                }
                break;

                case FieldType.DictKBVP: {
                    objects[i] = _endianBinaryReader.ReadDictionaryBP();
                }
                break;

                case FieldType.DictKPVP: {
                    objects[i] = _endianBinaryReader.ReadDictionaryPP();
                }
                break;

                case FieldType.DictKPVB: {
                    objects[i] = _endianBinaryReader.ReadDictionaryPB();
                }
                break;

                case FieldType.BaseType: {
                    objects[i] = _endianBinaryReader.Read();
                }
                break;

                case FieldType.NullType: {
                    objects[i] = null;
                }
                break;

                default: {
                    Logger.Warn($"{type}类型不支持!");
                    throw new NotSupportedException(type.ToString());
                }
                }
            }

            return(objects);
        }
Exemplo n.º 10
0
 public void DeserializeFields(IReadStream readStream)
 {
     Exception = readStream.ShiftRight <string>();
     MethodId  = readStream.ShiftRight <string>();
     MessageId = readStream.ShiftRight <ulong>();
 }
Exemplo n.º 11
0
        public void Handle(ulong remoteMessageId, IPeer peer, IReadStream readStream)
        {
            if (peer == null)
            {
                Logger.Error($"{nameof(peer)} 为 null");
                return;
            }

            var method = RpcResponseManager.GetRpcMethod(MethodId);

            if (method == null)
            {
                Logger.Warn($"{MethodId}未注册!");
                return;
            }

            var length = readStream.ShiftRight <byte>();

            Logger.Debug($"MethodId:{MethodId} 参数个数:{length}");

            var deserializable = new Deserializable(readStream);
            var values         = deserializable.Deserialize(new object[length + 2], length);

            values[values.Length - 2] = peer;
            Action          temp   = () => { };
            Action <Action> action = callback => temp += callback;

            values[values.Length - 1] = action;

            ResponseCallback responseCallback = (rm, sd) => {
                temp?.Invoke();
            };

            object result          = null;
            var    lateBoundMethod = RpcResponseManager.GetRpcMethod(MethodId);
            var    exception       = "";

            if (lateBoundMethod == null)
            {
                exception = $"没有方法:{MethodId}";
                Logger.Warn(exception);
            }
            else
            {
                var stopwatch = Stopwatch.StartNew();
                try {
                    result = lateBoundMethod.Invoke(values);
                } catch (Exception e) {
                    Logger.Error($"方法:[{MethodId}] " + e);
                    exception = $"方法:[{MethodId}] 发生异常!";
                } finally {
                    stopwatch.Stop();
                }

                Logger.Debug($"当前[{MethodId}]方法执行时间:{stopwatch.ElapsedMilliseconds}毫秒");
            }

            var rpcResponse = new RpcResponse(exception, MethodId, remoteMessageId);

            using (var serializable = new Serializable(null)) {
                serializable.SerializableObject(result);
                RpcProxy.Invoke(MessageType.RpcResponse, rpcResponse,
                                serializable.WriteStream, peer, responseCallback);
            }
        }