Ejemplo n.º 1
0
        void Get(string[] args)
        {
            // new http get request with conntionID, uri, and optional "auto-convert response from UTF8 to UTF16" arguments
            try
            {
                int connectionID = Int32.Parse(args[2]);
                // Un-base64 the uri into a byte array, then convert it from Unicode to a string
                string uriDecoded = Encoding.Unicode.GetString(Convert.FromBase64String(args[3]));
                Uri    webUri;
                try
                {
                    webUri = new Uri(uriDecoded);
                }
                catch (UriFormatException e)
                {
                    Console.WriteLine("URI incorrectly formatted: " + e.Message);
                    midiManager.SendWebRequestFailedResponse(connectionID, WEB_REQUEST_FAILED_ERROR_CODE);
                    return;
                }
                bool autoConvertResponse = false;
                if (args.Length > 4)
                {
                    autoConvertResponse = args[4] == "UTF16";
                }

                Console.WriteLine("Performing web request (" + connectionID + "): " + uriDecoded);
                webManager.GetWebRequest(connectionID, webUri, autoConvertResponse);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error parsing web request: " + e.Message);
            }
        }
Ejemplo n.º 2
0
        public async void GetWebRequest(int connectionID, Uri webUri, bool autoConvertResponse)
        {
            // Block all non-internet IP address
            if (HostnameIsPrivateIPAddress(webUri))
            {
                midiManager.SendWebRequestFailedResponse(connectionID, WEB_REQUEST_FAILED_ERROR_CODE);
                return;
            }

            // Block all non http/https traffic
            if (webUri.Scheme != Uri.UriSchemeHttp && webUri.Scheme != Uri.UriSchemeHttps)
            {
                Console.WriteLine("Error: World attempted to open unsupported URI: " + webUri.Scheme);
                midiManager.SendWebRequestFailedResponse(connectionID, WEB_REQUEST_FAILED_ERROR_CODE);
                return;
            }

            // Block all rate limited domain+path combos
            // Temporarily changed to include entire host
            var hostAndPath = new HostnameAndPath(webUri.Host, "");

            if (rateLimitedURIs.ContainsKey(hostAndPath))
            {
                if (DateTime.Now < rateLimitedURIs[hostAndPath])
                {
                    Console.WriteLine("ERROR: Could not make web request, currently rate limited.");
                    midiManager.SendWebRequestFailedResponse(connectionID, RATE_LIMITED_ERROR_CODE);
                    return;
                }
                else
                {
                    rateLimitedURIs.Remove(hostAndPath);
                }
            }

            HttpResponseMessage response;

            try
            {
                response = await httpClient.GetAsync(webUri, ctSource.Token);
            }
            catch (Exception e)
            {
                Console.WriteLine("HTTP request failed: " + e.Message);
                rateLimitedURIs.Add(hostAndPath, DateTime.Now.AddSeconds(RATE_LIMIT_TIMEOUT_SECONDS));
                midiManager.SendWebRequestFailedResponse(connectionID, WEB_REQUEST_FAILED_ERROR_CODE);
                return;
            }

            // Rate limit unsuccessful requests
            if (!response.IsSuccessStatusCode)
            {
                rateLimitedURIs.Add(hostAndPath, DateTime.Now.AddSeconds(RATE_LIMIT_TIMEOUT_SECONDS));
            }
            Console.WriteLine("Received web response (" + connectionID + "): " + webUri.AbsoluteUri);
            AddWebResponse(response, connectionID, autoConvertResponse);
        }