Exemple #1
0
        /// <summary>
        /// Creates a clone of a request and encrypts it
        /// </summary>
        /// <param name="request">The request that should be encrypted</param>
        /// <returns>the encrypted clone of the given request</returns>
        private async Task <WebserviceRequest> GetEncryptedRequest(WebserviceRequest request)
        {
            if (request == null)
            {
                return(null);
            }
            WebserviceRequest encRequest = (WebserviceRequest)request.Clone();

            if (request.NeedAuthentication && TokenHandler != null)              //add authentication if needed
            {
                if (TokenHandler.Token == null)
                {
                    await TokenHandler.RequestNewToken();
                }
                encRequest.Queries.Add("autht", await TokenHandler.GetTokenHash());
                encRequest.Queries.Add("user", TokenHandler.Username);
                encRequest.NeedAuthentication = false;
                if (encRequest.Encryption == EncryptionType.None)
                {
                    encRequest.Encryption = EncryptionType.Request;
                }
            }
            switch (encRequest.Encryption)
            {
            case EncryptionType.Request:
                encRequest.Command = "jdev/sys/enc/";
                break;

            case EncryptionType.RequestAndResponse:
                encRequest.Command = "jdev/sys/fenc/";
                break;

            case EncryptionType.None:
            default:
                return(encRequest);
            }
            string query = encRequest.Queries.HasKeys() ? $"?{encRequest.Queries.ToString()}" : "";

            encRequest.Command += Uri.EscapeDataString(Cryptography.AesEncrypt($"salt/{Session.Salt}/{request.Command}{query}", Session));
            encRequest.Queries.Clear();
            encRequest.Queries.Add("sk", await Session.GetSessionKey());
            encRequest.Encryption = EncryptionType.None;
            return(encRequest);
        }
        /// <summary>
        /// Sends a webservice to the miniserver and waits until its receives an answer
        /// </summary>
        /// <param name="request">The Request that should be sent</param>
        /// <returns>The Response the miniserver returns</returns>
        public async override Task <WebserviceResponse> SendWebservice(WebserviceRequest request)
        {
            switch (request?.Encryption)
            {
            case EncryptionType.Request:
                request.Command    = Uri.EscapeDataString(Cryptography.AesEncrypt($"salt/{Session.Salt}/{request.Command}", Session));
                request.Command    = $"jdev/sys/enc/{request.Command}";
                request.Encryption = EncryptionType.None;
                return(await SendWebservice(request));

            case EncryptionType.RequestAndResponse:
                string command = Uri.EscapeDataString(Cryptography.AesEncrypt($"salt/{Session.Salt}/{request.Command}", Session));
                command = $"jdev/sys/fenc/{command}";
                WebserviceRequest  encryptedRequest = new WebserviceRequest(command, EncryptionType.None);
                WebserviceResponse encrypedResponse = await SendWebservice(encryptedRequest);

                if (encrypedResponse == null)
                {
                    request.TryValidateResponse(new WebserviceResponse(null, null, (int?)WebSocket?.CloseStatus));
                }
                else
                {
                    request.TryValidateResponse(new WebserviceResponse(encrypedResponse.Header, Encoding.UTF8.GetBytes(Cryptography.AesDecrypt(encrypedResponse.GetAsStringContent(), Session)), (int?)WebSocket?.CloseStatus));
                }
                return(request.WaitForResponse());

            default:
            case EncryptionType.None:
                if (WebSocket == null || WebSocket.State != WebSocketState.Open)
                {
                    return(null);
                }
                lock (Requests) {
                    Requests.Add(request);
                }
                byte[] input = Encoding.UTF8.GetBytes(request.Command);
                await WebSocket.SendAsync(new ArraySegment <byte>(input, 0, input.Length), WebSocketMessageType.Text, true, CancellationToken.None);

                return(request.WaitForResponse());
            }
        }