// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { //loggerFactory.AddConsole(); //loggerFactory.AddDebug(); app.UseDeveloperExceptionPage(); app.UseWebSockets(); app.UseRouter(router => { router.MapGet("devtools/page/{pageId}", async context => { if (!context.WebSockets.IsWebSocketRequest) { context.Response.StatusCode = 400; return; } try { var proxy = new MonoProxy(); var browserUri = GetBrowserUri(context.Request.Path.ToString()); var ideSocket = await context.WebSockets.AcceptWebSocketAsync(); await proxy.Run(browserUri, ideSocket); } catch (Exception e) { Console.WriteLine("got exception {0}", e); } }); }); }
public async Task LaunchAndServe(ProcessStartInfo psi, HttpContext context, Func <string, string> extract_conn_url) { if (!context.WebSockets.IsWebSocketRequest) { context.Response.StatusCode = 400; return; } var tcs = new TaskCompletionSource <string> (); var proc = Process.Start(psi); try { proc.ErrorDataReceived += (sender, e) => { Console.WriteLine($"stderr: {e.Data}"); var res = extract_conn_url(e.Data); if (res != null) { tcs.TrySetResult(res); } }; proc.OutputDataReceived += (sender, e) => { Console.WriteLine($"stdout: {e.Data}"); }; proc.BeginErrorReadLine(); proc.BeginOutputReadLine(); if (await Task.WhenAny(tcs.Task, Task.Delay(2000)) != tcs.Task) { Console.WriteLine("Didnt get the con string after 2s."); throw new Exception("node.js timedout"); } var con_str = await tcs.Task; Console.WriteLine($"lauching proxy for {con_str}"); var proxy = new MonoProxy(); var browserUri = new Uri(con_str); var ideSocket = await context.WebSockets.AcceptWebSocketAsync(); await proxy.Run(browserUri, ideSocket); Console.WriteLine("Proxy done"); } catch (Exception e) { Console.WriteLine("got exception {0}", e.GetType().FullName); } finally { proc.CancelErrorRead(); proc.CancelOutputRead(); proc.Kill(); proc.WaitForExit(); proc.Close(); } }
public static IApplicationBuilder UseDebugProxy(this IApplicationBuilder app, DebugSettings settings) { app.Use(async(context, next) => { var rewriteFunc = settings.TabMapper; var devToolsHost = settings.DevToolsHost; var request = context.Request; var requestPath = request.Path; var endpoint = $"{devToolsHost.Scheme}://{devToolsHost.Authority}{request.Path}{request.QueryString}"; switch (requestPath.Value.ToLower(System.Globalization.CultureInfo.InvariantCulture)) { case "/": using (var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(5) }) { var response = await httpClient.GetStringAsync($"{devToolsHost}"); context.Response.ContentType = "text/html"; context.Response.ContentLength = response.Length; await context.Response.WriteAsync(response); } break; case "/json/version": context.Response.ContentType = "application/json"; await context.Response.WriteAsync(JsonConvert.SerializeObject( new Dictionary <string, string> { { "Browser", "Chrome/71.0.3578.98" }, { "Protocol-Version", "1.3" }, { "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" }, { "V8-Version", "7.1.302.31" }, { "WebKit-Version", "537.36 (@15234034d19b85dcd9a03b164ae89d04145d8368)" }, })); break; case "/json/new": var tab = await ProxyGetJsonAsync <DevToolsTab> (endpoint); var redirect = rewriteFunc?.Invoke(tab, context, devToolsHost); context.Response.ContentType = "application/json"; await context.Response.WriteAsync(JsonConvert.SerializeObject(redirect)); break; case "/json/list": case "/json": var tabs = await ProxyGetJsonAsync <DevToolsTab[]>(endpoint); var alteredTabs = tabs.Select(t => rewriteFunc?.Invoke(t, context, devToolsHost)).ToArray(); context.Response.ContentType = "application/json"; await context.Response.WriteAsync(JsonConvert.SerializeObject(alteredTabs)); break; default: await next(); break; } }) .UseRouter(router => { router.MapGet("devtools/page/{pageId}", async context => { if (!context.WebSockets.IsWebSocketRequest) { context.Response.StatusCode = 400; return; } var devToolsHost = settings.DevToolsHost; var endpoint = new Uri($"ws://{devToolsHost.Authority}{context.Request.Path.ToString()}"); try { var proxy = new MonoProxy(); var ideSocket = await context.WebSockets.AcceptWebSocketAsync(); await proxy.Run(endpoint, ideSocket); } catch (Exception e) { Console.WriteLine("got exception {0}", e); } }); }); return(app); }