예제 #1
0
        public static IApplicationBuilder UseDebugProxy(
            this IApplicationBuilder app,
            ProxyOptions options,
            Func <Dictionary <string, string>, HttpContext, Uri, Dictionary <string, string> > mapFunc)
        {
            var devToolsHost = options.DevToolsUrl;

            app.UseRouter(router => {
                router.MapGet("json", RewriteArray);
                router.MapGet("json/list", RewriteArray);
                router.MapGet("json/version", RewriteSingle);
                router.MapGet("json/new", RewriteSingle);
                router.MapGet("devtools/page/{pageId}", ConnectProxy);
                router.MapGet("devtools/browser/{pageId}", ConnectProxy);

                string GetEndpoint(HttpContext context)
                {
                    var request     = context.Request;
                    var requestPath = request.Path;
                    return($"{devToolsHost.Scheme}://{devToolsHost.Authority}{request.Path}{request.QueryString}");
                }

                async Task Copy(HttpContext context)
                {
                    using (var httpClient = new HttpClient {
                        Timeout = TimeSpan.FromSeconds(5)
                    })
                    {
                        var response = await httpClient.GetAsync(GetEndpoint(context));
                        context.Response.ContentType = response.Content.Headers.ContentType.ToString();
                        if ((response.Content.Headers.ContentLength ?? 0) > 0)
                        {
                            context.Response.ContentLength = response.Content.Headers.ContentLength;
                        }

                        var bytes = await response.Content.ReadAsByteArrayAsync();
                        await context.Response.Body.WriteAsync(bytes);
                    }
                }

                async Task RewriteSingle(HttpContext context)
                {
                    var version = await ProxyGetJsonAsync <Dictionary <string, string> >(GetEndpoint(context));
                    context.Response.ContentType = "application/json";
                    await context.Response.WriteAsync(
                        JsonSerializer.Serialize(mapFunc(version, context, devToolsHost)));
                }

                async Task RewriteArray(HttpContext context)
                {
                    var tabs        = await ProxyGetJsonAsync <Dictionary <string, string>[]>(GetEndpoint(context));
                    var alteredTabs = tabs.Select(t => mapFunc(t, context, devToolsHost)).ToArray();
                    context.Response.ContentType = "application/json";
                    await context.Response.WriteAsync(JsonSerializer.Serialize(alteredTabs));
                }

                async Task ConnectProxy(HttpContext context)
                {
                    if (!context.WebSockets.IsWebSocketRequest)
                    {
                        context.Response.StatusCode = 400;
                        return;
                    }

                    var endpoint = new Uri($"ws://{devToolsHost.Authority}{context.Request.Path.ToString()}");
                    try
                    {
                        using var loggerFactory = LoggerFactory.Create(
                                  builder => builder.AddConsole().AddFilter(null, LogLevel.Trace));
                        var proxy     = new DebuggerProxy(loggerFactory);
                        var ideSocket = await context.WebSockets.AcceptWebSocketAsync();

                        await proxy.Run(endpoint, ideSocket);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("got exception {0}", e);
                    }
                }
            });
            return(app);
        }
예제 #2
0
 public static IApplicationBuilder UseDebugProxy(this IApplicationBuilder app, ProxyOptions options) =>
 UseDebugProxy(app, options, MapValues);