Пример #1
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();
            });
        }
Пример #2
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);
            }
        }