private Url BuildUrl(string url)
        {
            var uri = new Flurl.Url(_baseUri + url);

            uri.SetQueryParam("dsid", _dsid);
            return(uri);
        }
 public override HttpClient CreateClient(Url url, HttpMessageHandler handler)
 {
     return new HttpClient(handler)
     {
         Timeout = FlurlHttp.Configuration.DefaultTimeout
     };
 }
 private static async Task SubmitError(Exception exception)
 {
     var mainsrv = DetectLinux.LinuxDetected() ? URLs.mainserver.Replace("https", "http") : URLs.mainserver;
     Url url     = new Url(mainsrv + "/error-report");
     await url.PostJsonAsync(new
     {
         message    = exception.Message ?? "no message",
         stackTrace = exception.StackTrace ?? "no stack trace"
     });
 }
        public async Task <T> GetAsync <T>(string url)
        {
            var uri = new Flurl.Url(_baseUri + url);

            uri.SetQueryParam("dsid", _dsid);
            var response = await _client.GetAsync(uri);

            response.EnsureSuccessStatusCode();

            return(await response.Content.ReadAsAsync <T>());
        }
Beispiel #5
0
        private async Task <Response> ProxyRequest(NancyContext context, CancellationToken cancellationToken)
        {
            string path   = context.Request.Path;
            string method = context.Request.Method.ToUpperInvariant();

            if (!path.StartsWith("/nfsw/Engine.svc"))
            {
                throw new ProxyException("Invalid request path: " + path);
            }

            path = path.Substring("/nfsw/Engine.svc".Length);

            Url resolvedUrl = new Url(ServerProxy.Instance.GetServerUrl()).AppendPathSegment(path);

            foreach (var queryParamName in context.Request.Query)
            {
                resolvedUrl = resolvedUrl.SetQueryParam(queryParamName, context.Request.Query[queryParamName],
                                                        NullValueHandling.Ignore);
            }

            IFlurlRequest request = resolvedUrl.AllowAnyHttpStatus();

            foreach (var header in context.Request.Headers)
            {
                request = request.WithHeader(header.Key,
                                             header.Key == "Host" ? resolvedUrl.ToUri().Host : header.Value.First());
            }

            var requestBody = context.Request.Method != "GET" ? context.Request.Body.AsString(Encoding.UTF8) : "";

            CommunicationLog.RecordEntry(ServerProxy.Instance.GetServerName(), "SERVER",
                                         CommunicationLogEntryType.Request,
                                         new CommunicationLogRequest(requestBody, resolvedUrl.ToString(), method));

            HttpResponseMessage responseMessage;

            var POSTContent = String.Empty;

            var queryParams = new Dictionary <string, object>();

            foreach (var param in context.Request.Query)
            {
                var value = context.Request.Query[param];
                queryParams[param] = value;
            }

            var GETContent = string.Join(";", queryParams.Select(x => x.Key + "=" + x.Value).ToArray());

            // ReSharper disable once LocalizableElement
            //Console.WriteLine($"[LOG] [{method}] ProxyHandler: {path}");

            switch (method)
            {
            case "GET":
                responseMessage = await request.GetAsync(cancellationToken);

                break;

            case "POST":
                responseMessage = await request.PostAsync(new CapturedStringContent(requestBody, Encoding.UTF8),
                                                          cancellationToken);

                POSTContent = context.Request.Body.AsString();
                break;

            case "PUT":
                responseMessage = await request.PutAsync(new CapturedStringContent(requestBody, Encoding.UTF8),
                                                         cancellationToken);

                break;

            case "DELETE":
                responseMessage = await request.DeleteAsync(cancellationToken);

                break;

            default:
                throw new ProxyException("Cannot handle request method: " + method);
            }

            var responseBody = await responseMessage.Content.ReadAsStringAsync();

            if (path == "/User/GetPermanentSession")
            {
                responseBody = Self.CleanFromUnknownChars(responseBody);
            }

            int statusCode = (int)responseMessage.StatusCode;

            try
            {
                DiscordGamePresence.HandleGameState(path, responseBody, POSTContent, GETContent);
            }
            catch (Exception e)
            {
                Log.Error($"DISCORD RPC ERROR [handling {context.Request.Path}]");
                Log.Error($"\tMESSAGE: {e.Message}");
                Log.Error($"\t{e.StackTrace}");
                await Self.SubmitError(e);
            }

            TextResponse textResponse = new TextResponse(responseBody,
                                                         responseMessage.Content.Headers.ContentType?.MediaType ?? "application/xml;charset=UTF-8")
            {
                StatusCode = (HttpStatusCode)statusCode
            };

            queryParams.Clear();

            CommunicationLog.RecordEntry(ServerProxy.Instance.GetServerName(), "SERVER",
                                         CommunicationLogEntryType.Response, new CommunicationLogResponse(
                                             responseBody, resolvedUrl.ToString(), method));

            return(textResponse);
        }
Beispiel #6
0
 public static Url UrlBuilder(CreepScore.Region region, string version, string endpoint)
 {
     Url url = new Url(GetBaseUrl(region)).AppendPathSegment(apiLolPart).AppendPathSegment(GetRegion(region)).AppendPathSegment(version)
         .AppendPathSegment(endpoint);
     return url;
 }
Beispiel #7
0
        private async Task <Response> ProxyRequest(NancyContext ctx, CancellationToken ct)
        {
            Debug.WriteLine("{0} - {1}", ctx.Request.Method, ctx.Request.Path);

            foreach (var requestHeader in ctx.Request.Headers)
            {
                Debug.WriteLine("\t{0}: {1}", requestHeader.Key, string.Join(" ; ", requestHeader.Value));
            }

            // Build new request
            var url = new Flurl.Url(ServerProxy.Instance.GetCurrentServer().ServerAddress)
                      .AppendPathSegment(ctx.Request.Path.Replace("/nfsw/Engine.svc", ""));

            foreach (var key in ctx.Request.Query)
            {
                url = url.SetQueryParam(key, ctx.Request.Query[key], NullValueHandling.Ignore);
            }

            IFlurlRequest request = url.WithTimeout(TimeSpan.FromSeconds(30));

            foreach (var requestHeader in ctx.Request.Headers)
            {
                request = request.WithHeader(requestHeader.Key, requestHeader.Value.First());
            }

            HttpResponseMessage responseMessage;

            switch (ctx.Request.Method)
            {
            case "GET":
                responseMessage = await request.GetAsync(ct);

                break;

            case "POST":
                responseMessage =
                    await request.PostAsync(new CapturedStringContent(ctx.Request.Body.AsString(Encoding.UTF8)), ct);

                break;

            case "PUT":
                responseMessage =
                    await request.PutAsync(new CapturedStringContent(ctx.Request.Body.AsString(Encoding.UTF8)), ct);

                break;

            case "DELETE":
                responseMessage =
                    await request.DeleteAsync(ct);

                break;

            default:
                throw new ServerProxyException("Cannot handle request method: " + ctx.Request.Method);
            }

            return(new TextResponse(await responseMessage.Content.ReadAsStringAsync(),
                                    responseMessage.Content.Headers.ContentType?.MediaType ?? "application/xml;charset=UTF-8")
            {
                StatusCode = (HttpStatusCode)(int)responseMessage.StatusCode
            });
        }
Beispiel #8
0
        private async Task <Response> ProxyRequest(NancyContext context, CancellationToken cancellationToken)
        {
            string path   = Strings.Encode(context.Request.Path);
            string method = Strings.Encode(context.Request.Method.ToUpperInvariant());

            if (!path.StartsWith("/nfsw/Engine.svc"))
            {
                Log.Error("PROXY HANDLER: Invalid Request: " + path);
                return("SBRW Launcher Version: " + Theming.PrivacyRPCBuild + "\nBuild Date: " + InsiderInfo.BuildNumberOnly());
            }
            else
            {
                path = path.Substring("/nfsw/Engine.svc".Length);

                UrlFlurl resolvedUrl = new UrlFlurl(ServerProxy.Instance.GetServerUrl()).AppendPathSegment(path, false);

                foreach (var queryParamName in context.Request.Query)
                {
                    resolvedUrl = resolvedUrl.SetQueryParam(queryParamName, context.Request.Query[queryParamName],
                                                            NullValueHandling.Ignore);
                }

                IFlurlRequest request = resolvedUrl.AllowAnyHttpStatus();

                foreach (var header in context.Request.Headers)
                {
                    /* Don't send Content-Length for GET requests - HeyItsLeo */
                    if (method == "GET" && header.Key.ToLowerInvariant() == "content-length")
                    {
                        continue;
                    }

                    request = request.WithHeader
                                  (header.Key, (header.Key == "Host") ? resolvedUrl.ToUri().Host : ((header.Value != null) ? header.Value.First() : string.Empty));
                }

                string requestBody = (method != "GET") ? context.Request.Body.AsString(UTF8) : string.Empty;

                CommunicationLog.RecordEntry(ServerProxy.Instance.GetServerName(), "SERVER", CommunicationLogEntryType.Request,
                                             new CommunicationLogRequest(requestBody, resolvedUrl.ToString(), method));

                IFlurlResponse responseMessage;

                if (path == "/event/arbitration" && !string.IsNullOrWhiteSpace(requestBody))
                {
                    requestBody = Strings.Encode(
                        requestBody.Replace("</TopSpeed>", "</TopSpeed><Konami>" + AntiCheat.Get_Cheat_Status() + "</Konami>"));
                    foreach (var header in context.Request.Headers)
                    {
                        if (header.Key.ToLowerInvariant() == "content-length")
                        {
                            int KonamiCode = Convert.ToInt32(header.Value.First()) +
                                             ("<Konami>" + AntiCheat.Get_Cheat_Status() + "</Konami>").Length;
                            request = request.WithHeader(header.Key, KonamiCode);
                        }
                    }
                }

                switch (method)
                {
                case "GET":
                    responseMessage = await request.GetAsync(cancellationToken);

                    break;

                case "POST":
                    responseMessage = await request.PostAsync(new CapturedStringContent(requestBody),
                                                              cancellationToken);

                    break;

                case "PUT":
                    responseMessage = await request.PutAsync(new CapturedStringContent(requestBody),
                                                             cancellationToken);

                    break;

                case "DELETE":
                    responseMessage = await request.DeleteAsync(cancellationToken);

                    break;

                default:
                    Log.Error("PROXY HANDLER: Cannot handle Request Method " + method);
                    responseMessage = null;
                    break;
                }

                string responseBody = Strings.Encode(await responseMessage.GetStringAsync());

                int statusCode = responseMessage.StatusCode;

                DiscordGamePresence.HandleGameState(path, responseBody, context.Request.Query);

                TextResponse Response = new TextResponse(responseBody,
                                                         responseMessage.ResponseMessage.Content.Headers.ContentType?.MediaType ?? "application/xml;charset=UTF-8")
                {
                    StatusCode = (HttpStatusCode)statusCode
                };

                CommunicationLog.RecordEntry(ServerProxy.Instance.GetServerName(), "SERVER", CommunicationLogEntryType.Response,
                                             new CommunicationLogResponse(responseBody, resolvedUrl.ToString(), method));

                return(Response);
            }
        }
Beispiel #9
0
 public static IFlurlRequest WithBcHeaders(this Url url, IDocument originating, string csrf = null) => url
 .WithCookie("csrftoken", csrf ?? originating.CsrfToken())
 //.WithCookies(originating.GetCookies().Select(c => (c.Name, c.Value)))
 .WithHeader("referer", originating.Url);
Beispiel #10
0
        public static Flurl.Url ParseUrl(this string urlPath)
        {
            var url = new Flurl.Url(urlPath);

            return(url);
        }
 public HttpClient CreateClient(Url url, HttpMessageHandler handler)
 {
     return _authenticatedClientFactory.CreateClient(url, handler);
 }