public EncryptedMessage(NodeDssMessage msg, byte[] Key, byte[] IV)
            {
                var jsonMessage = JsonUtility.ToJson(msg);

                Cipher = System.Convert.ToBase64String(AES.EncryptStringToBytes_Aes(jsonMessage, Key, IV));
                Hmac   = System.Convert.ToBase64String(HMAC.Sign(Key, jsonMessage));
            }
        /// <summary>
        /// Internal helper for sending HTTP data to the node-dss server using POST
        /// </summary>
        /// <param name="msg">the message to send</param>
        private IEnumerator PostToServer(NodeDssMessage msg)
        {
            if (RemotePeerId.Length == 0)
            {
                throw new InvalidOperationException("Cannot send SDP message to remote peer; invalid empty remote peer ID.");
            }

            byte[] data;

            if (Encrypt)
            {
                data = System.Text.Encoding.UTF8.GetBytes(JsonUtility.ToJson(new EncryptedMessage(msg, this.Key, this.IV)));
            }
            else
            {
                data = System.Text.Encoding.UTF8.GetBytes(JsonUtility.ToJson(msg));
            }
            var www = new UnityWebRequest($"{HttpServerAddress}data/{RemotePeerId}", UnityWebRequest.kHttpVerbPOST);

            www.uploadHandler = new UploadHandlerRaw(data);

            yield return(www.SendWebRequest());

            if (AutoLogErrors && (www.isNetworkError || www.isHttpError))
            {
                Debug.Log($"Failed to send message to remote peer {RemotePeerId}: {www.error}");
            }
        }
示例#3
0
        /// <summary>
        /// Internal helper to wrap a coroutine into a synchronous call for use inside
        /// a <see cref="Task"/> object.
        /// </summary>
        /// <param name="msg">the message to send</param>
        private IEnumerator PostToServerAndWait(NodeDssMessage message, TaskCompletionSource <bool> tcs)
        {
            yield return(StartCoroutine(PostToServer(message)));

            const bool dummy = true; // unused

            tcs.SetResult(dummy);
        }
示例#4
0
        private Task SendMessageImplAsync(NodeDssMessage message)
        {
            // This method needs to return a Task object which gets completed once the signaler message
            // has been sent. Because the implementation uses a Unity coroutine, use a reset event to
            // signal the task to complete from the coroutine after the message is sent.
            // Note that the coroutine is a Unity object so needs to be started from the main Unity app thread.
            // Also note that TaskCompletionSource<bool> is used as a no-result variant; there is no meaning
            // to the bool value.
            // https://stackoverflow.com/questions/11969208/non-generic-taskcompletionsource-or-alternative
            var tcs = new TaskCompletionSource <bool>();

            _mainThreadWorkQueue.Enqueue(() => StartCoroutine(PostToServerAndWait(message, tcs)));
            return(tcs.Task);
        }
            public static NodeDssMessage DecryptMessage(string cipherText, string hmac, byte[] Key, byte[] IV)
            {
                byte[] cipherBytes   = System.Convert.FromBase64String(cipherText);
                byte[] hmacBytes     = System.Convert.FromBase64String(hmac);
                var    jsonPlainText = AES.DecryptStringFromBytes_Aes(cipherBytes, Key, IV);

                if (HMAC.Verify(Key, jsonPlainText, hmacBytes))
                {
                    NodeDssMessage nodeMessage = JsonUtility.FromJson <NodeDssMessage>(jsonPlainText);
                    return(nodeMessage);
                }
                else
                {
                    throw new InvalidOperationException("Cannot verify the sender of the SDP message; invalid HMAC");
                }
            }