Exemplo n.º 1
0
        private void HandleHandshake(Client client, HandshakeRequestPacket packet)
        {
            if (client.LoggedIn)
                return;

            client.Stream.WritePacket(new HandshakeResponsePacket("-"));
            Logger.Debug("New user '" + packet.Username + "' is connecting...");
        }
        public async Task <HandshakeResult> Handshake(IConnection connection, CancellationToken cancellation = default(CancellationToken))
        {
            var authRequest = new HandshakeRequestPacket("asdf")
            {
                ["userid"] = UserId
            };
            var result = await connection.Request(authRequest, cancellation);

            return(result.As <HandshakeResult>());
        }
Exemplo n.º 3
0
        public async Task <HandshakeResult> Handshake(IConnection connection, CancellationToken cancellationToken)
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }

            var sessionKey = SessionKey;
            var userId     = UserId;
            var deviceId   = DeviceId;

            if (!Validate(sessionKey, userId, deviceId, out var invalidFields))
            {
                return(new HandshakeResult()
                {
                    Successful = false,
                    ErrorCode = $"{nameof(DefaultHandshaker)}: These fields are invalid: {string.Join(",", invalidFields)}"
                });
            }

            var req = new HandshakeRequestPacket(DefaultHandshakeType)
            {
                [nameof(SessionKey)] = SessionKey,
                [nameof(UserId)]     = UserId,
                [nameof(DeviceId)]   = DeviceId
            };

            var result = await connection.Request(req, cancellationToken);

            if (result.Data == null)
            {
                return(new HandshakeResult()
                {
                    Successful = false,
                    ErrorCode = $"{nameof(DefaultHandshaker)}: Invalid response data. Expecting {nameof(HandshakeResult)}, but no data received"
                });
            }

            if (result.Data is HandshakeResult handshakeResult)
            {
                return(handshakeResult);
            }

            return(new HandshakeResult()
            {
                Successful = false,
                ErrorCode = $"{nameof(DefaultHandshaker)}: Invalid response data. Expecting {nameof(HandshakeResult)}, received {result.Data.GetType().Name}"
            });
        }
Exemplo n.º 4
0
 public Task <HandshakeResult> OnHandshakeRequest(HandshakeRequestPacket request, IConnection connection, CancellationToken cancellation)
 {
     throw new NotImplementedException();
 }
        public Task <HandshakeResult> OnHandshakeRequest(HandshakeRequestPacket request, IConnection connection, CancellationToken cancellation = default(CancellationToken))
        {
            var userId = (string)request["userid"];

            return(Task.FromResult(HandshakeResult.Success(new User(userId, userId))));
        }
Exemplo n.º 6
0
        public async Task <HandshakeResult> OnHandshakeRequest(HandshakeRequestPacket request, IConnection connection, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }

            if (request.HandshakeType != DefaultHandshakeType)
            {
                return(new HandshakeResult()
                {
                    Successful = false,
                    ErrorCode = $"{nameof(DefaultHandshaker)}: Invalid {nameof(HandshakeRequestPacket.HandshakeType)}: {request.HandshakeType}, expected {DefaultHandshakeType}"
                });
            }

            var sessionKey = (string)request[nameof(SessionKey)];
            var userId     = (string)request[nameof(UserId)];
            var deviceId   = (string)request[nameof(DeviceId)];

            if (!Validate(sessionKey, userId, deviceId, out var errors))
            {
                return(new HandshakeResult()
                {
                    Successful = false,
                    ErrorCode = $"{nameof(DefaultHandshaker)}: These fields are invalid: {string.Join(Environment.NewLine, errors)}"
                });
            }

            try {
                if (!await _apiClient.ValidateSession(userId, deviceId, sessionKey, cancellationToken))
                {
                    return(new HandshakeResult {
                        ErrorCode = "SessionInvalid",
                        Successful = false
                    });
                }

                cancellationToken.ThrowIfCancellationRequested();

                var apiUser = await _apiClient.UserFromUserId(userId, cancellationToken);

                if (apiUser == null)
                {
                    return(new HandshakeResult {
                        ErrorCode = "ApiUserNotFound",
                        Successful = false
                    });
                }

                var user = new User(userId, apiUser.UserName);

                return(new HandshakeResult {
                    Successful = true,
                    User = user
                });
            } catch (TaskCanceledException ex) {
                Log.Warn($"{nameof(OnHandshakeRequest)}", ex);

                return(new HandshakeResult {
                    ErrorCode = "Canceled",
                    Successful = false
                });
            } catch (ApiClientException ex) {
                Log.Warn($"{nameof(OnHandshakeRequest)}", ex);

                return(new HandshakeResult {
                    ErrorCode = "ApiClientError",
                    Successful = false
                });
            }
        }