Example #1
0
        /// <summary>
        /// Evaluates the function on a scalar argument.
        /// </summary>
        /// <param name="Argument">Function argument.</param>
        /// <param name="Variables">Variables collection.</param>
        /// <returns>Function result.</returns>
        public override IElement EvaluateScalar(IElement Argument, Variables Variables)
        {
            if (!(Argument.AssociatedObjectValue is byte[] Bin))
            {
                throw new ScriptRuntimeException("Binary data expected.", this);
            }

            return(new ObjectValue(Hashes.ComputeSHA1Hash(Bin)));
        }
Example #2
0
		/// <summary>
		/// XMPP-specific SOCKS5 connection, as described in XEP-0065:
		/// https://xmpp.org/extensions/xep-0065.html
		/// </summary>
		/// <param name="StreamID">Stream ID</param>
		/// <param name="RequesterJID">Requester JID</param>
		/// <param name="TargetJID">Target JID</param>
		public void CONNECT(string StreamID, string RequesterJID, string TargetJID)
		{
			string s = StreamID + RequesterJID + TargetJID;
			byte[] Hash = Hashes.ComputeSHA1Hash(Encoding.UTF8.GetBytes(s));
			StringBuilder sb = new StringBuilder();

			foreach (byte b in Hash)
				sb.Append(b.ToString("x2"));

			this.CONNECT(sb.ToString(), 0);
		}
Example #3
0
        /// <summary>
        /// Executes the GET method on the resource.
        /// </summary>
        /// <param name="Request">HTTP Request</param>
        /// <param name="Response">HTTP Response</param>
        /// <exception cref="HttpException">If an error occurred when processing the method.</exception>
        public async Task GET(HttpRequest Request, HttpResponse Response)
        {
            if (!Request.Header.TryGetHeaderField("Upgrade", out HttpField Upgrade) ||
                Upgrade.Value != "websocket")
            {
                throw new UpgradeRequiredException("websocket");
            }

            string Challenge;
            string WebSocketProtocol = null;
            int?   WebSocketVersion;
            int    i;

            if (Request.Header.TryGetHeaderField("Sec-WebSocket-Key", out HttpField Field))
            {
                Challenge = Field.Value;
            }
            else
            {
                throw new BadRequestException("Sec-WebSocket-Key header field missing.");
            }

            if (Request.Header.TryGetHeaderField("Sec-WebSocket-Protocol", out Field))
            {
                string[] Options = Field.Value.Split(',');

                foreach (string Option in Options)
                {
                    i = Array.IndexOf <string>(this.subProtocols, Option.Trim().ToLower());
                    if (i >= 0)
                    {
                        WebSocketProtocol = this.subProtocols[i];
                        break;
                    }
                }

                if (WebSocketProtocol is null)
                {
                    throw new NotSupportedException();
                }
            }

            if (Request.Header.TryGetHeaderField("Sec-WebSocket-Version", out Field) &&
                int.TryParse(Field.Value, out i))
            {
                if (i < 13)
                {
                    throw new PreconditionFailedException();
                }

                WebSocketVersion = i;
            }
            else
            {
                throw new BadRequestException("Sec-WebSocket-Version header field missing.");
            }

            if (Request.Header.TryGetHeaderField("Sec-WebSocket-Extensions", out Field))
            {
                // TODO: ยง9.1
            }

            if (Request.clientConnection is null)
            {
                throw new ForbiddenException("Invalid connection.");
            }

            WebSocket Socket = new WebSocket(this, Request, Response);

            this.Accept?.Invoke(this, new WebSocketEventArgs(Socket));

            string ChallengeResponse = Convert.ToBase64String(Hashes.ComputeSHA1Hash(
                                                                  Encoding.UTF8.GetBytes(Challenge.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")));

            Response.StatusCode    = 101;
            Response.StatusMessage = "Switching Protocols";
            Response.SetHeader("Upgrade", "websocket");
            Response.SetHeader("Connection", "Upgrade");
            Response.SetHeader("Sec-WebSocket-Accept", ChallengeResponse);

            if (!(WebSocketProtocol is null))
            {
                Response.SetHeader("Sec-WebSocket-Protocol", WebSocketProtocol);
            }

            Request.clientConnection.Upgrade(Socket);

            await Response.SendResponse();

            this.Connected?.Invoke(this, new WebSocketEventArgs(Socket));
        }
Example #4
0
 /// <summary>
 /// Hash function
 /// </summary>
 /// <param name="Data">Data to hash.</param>
 /// <returns>Hash of data.</returns>
 public override byte[] H(byte[] Data)
 {
     return(Hashes.ComputeSHA1Hash(Data));
 }