예제 #1
0
        /// <summary>
        /// Sends a webservice to the miniserver
        /// </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)
        {
            WebserviceRequest encRequest = await GetEncryptedRequest(request);

            Uri url = new UriBuilder()
            {
                Scheme = "http",
                Host   = IP,
                Port   = Port,
                Path   = encRequest.Command,
                Query  = encRequest.Queries.ToString()
            }.Uri;

            CancellationTokenSource?.Dispose();
            CancellationTokenSource = new CancellationTokenSource(request.Timeout);
            HttpResponseMessage httpResponse = await HttpClient?.GetAsync(url.OriginalString, CancellationTokenSource.Token);

            byte[] responseContent = await httpResponse?.Content.ReadAsByteArrayAsync();

            CancellationTokenSource?.Dispose();
            if (httpResponse.IsSuccessStatusCode && request.Encryption == EncryptionType.RequestAndResponse)              //decypt response if needed
            {
                responseContent = Encoding.UTF8.GetBytes(Cryptography.AesDecrypt(Encoding.UTF8.GetString(responseContent), Session));
            }
            WebserviceResponse response = new WebserviceResponse(null, responseContent, (int)httpResponse.StatusCode);

            encRequest.TryValidateResponse(response);
            return(response);
        }
예제 #2
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());
            }
        }
예제 #4
0
 /// <summary>
 /// Sends a webservice to the miniserver
 /// </summary>
 /// <param name="request">The Request that should be sent</param>
 /// <returns>The Response the miniserver returns</returns>
 public virtual async Task <WebserviceResponse> SendWebservice(WebserviceRequest request)
 {
     return(await Task.FromResult <WebserviceResponse>(null));
 }
예제 #5
0
 /// <summary>
 /// Sends a webservice to the miniserver
 /// </summary>
 /// <typeparam name="T">The object that should be returned in Value</typeparam>
 /// <param name="request">The Request that should be sent</param>
 /// <returns>The Response the miniserver returns</returns>
 public async Task <WebserviceContent <T> > SendWebservice <T>(WebserviceRequest <T> request)
 {
     return((await SendWebservice((WebserviceRequest)request))?.GetAsWebserviceContent <T>());
 }