Skip to content

Commit

Permalink
Move proxy to startup
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchitmehta committed Nov 30, 2020
1 parent bf4e82e commit 0ca6f0d
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 6 deletions.
7 changes: 4 additions & 3 deletions Kudu.Services.Web/Startup.cs
Expand Up @@ -358,6 +358,8 @@ private static Uri GetAbsoluteUri(HttpContext httpContext)
app.UseKubeMiddleware();
}

ProxyRequestIfRelativeUrlMatches(@"/webssh", "http", "127.0.0.1", KuduWebUtil.GetWebSSHProxyPort(), app);

app.MapWhen(containsRelativeProvisionPath, application => application.Run(async context =>
{
FileSystemHelpers.EnsureDirectory("/home/apps/"+context.Request.Path.Value.Replace("/api/provision/", ""));
Expand Down Expand Up @@ -385,8 +387,6 @@ private static Uri GetAbsoluteUri(HttpContext httpContext)

app.UseStaticFiles();

//ProxyRequestIfRelativeUrlMatches(@"/webssh", "http", "127.0.0.1", KuduWebUtil.GetWebSSHProxyPort() , app);

var configuration = app.ApplicationServices.GetRequiredService<IServerConfiguration>();

// CORE TODO any equivalent for this? Needed?
Expand Down Expand Up @@ -714,7 +714,8 @@ private static Uri GetAbsoluteUri(HttpContext httpContext)
IApplicationBuilder app)
{
var containsRelativePath = new Func<HttpContext, bool>(i =>
i.Request.Path.Value.StartsWith(relativeUrl, StringComparison.OrdinalIgnoreCase));
i.Request.Path.Value.StartsWith("/instances/", StringComparison.OrdinalIgnoreCase)
&& i.Request.Path.Value.IndexOf("/webssh")>0);
app.MapWhen(containsRelativePath, builder => builder.RunProxy(new ProxyOptions
{
Scheme = scheme,
Expand Down
108 changes: 105 additions & 3 deletions Kudu.Services/DebugExtension/InstanceController.cs
Expand Up @@ -94,12 +94,112 @@ public IActionResult RemoteDebug(string instanceId)
}
*/

[Route("{instanceId}/webssh/{subpath}")]
/*
[Route("{instanceId}/gcdump")]
public async Task<string> GetGCDump(string instanceId)
{
var httpClientHandler = new HttpClientHandler
{
AllowAutoRedirect = false
};
var webRequest = new HttpClient(httpClientHandler);
var buffer = new byte[4 * 1024];
var localResponse = HttpContext.Response;
try
{
using (var remoteStream = await webRequest.GetStreamAsync(url))
{
var bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
localResponse.Clear();
localResponse.ContentType = "application/octet-stream";
var uri = "";
var scheme = HttpContext.Request.Scheme;
if (scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
{
scheme = "http";
}
if (HttpContext.Request.Path.StartsWithSegments($"/instances/{instanceId}", out var remainingPath))
{
Console.WriteLine("PATH STRING : " + remainingPath);
uri = $"{scheme}://localhost:3000" + remainingPath);
}
var fileName = Path.GetFileName(url);
localResponse.Headers.Add("Content-Disposition", "attachment; filename=" + fileName);
if (remoteStream.Length != -1)
localResponse.ContentLength = remoteStream.Length;
while (bytesRead > 0) // && localResponse.IsClientConnected)
{
await localResponse.Body.WriteAsync(buffer, 0, bytesRead);
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
}
}
}
catch (Exception e)
{
// Do some logging here
}
}
[Route("{instanceId}/dump")]
public async Task<string> GetDump(string instanceId)
{
var httpClientHandler = new HttpClientHandler
{
AllowAutoRedirect = false
};
var webRequest = new HttpClient(httpClientHandler);
var buffer = new byte[4 * 1024];
var localResponse = HttpContext.Response;
try
{
using (var remoteStream = await webRequest.GetStreamAsync(url))
{
var bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
localResponse.Clear();
localResponse.ContentType = "application/octet-stream";
var uri = "";
var scheme = HttpContext.Request.Scheme;
if (scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
{
scheme = "http";
}
if (HttpContext.Request.Path.StartsWithSegments($"/instances/{instanceId}", out var remainingPath))
{
Console.WriteLine("PATH STRING : " + remainingPath);
uri = $"{scheme}://localhost:3000" + remainingPath);
}
var fileName = Path.GetFileName(url);
localResponse.Headers.Add("Content-Disposition", "attachment; filename=" + fileName);
if (remoteStream.Length != -1)
localResponse.ContentLength = remoteStream.Length;
while (bytesRead > 0) // && localResponse.IsClientConnected)
{
await localResponse.Body.WriteAsync(buffer, 0, bytesRead);
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
}
}
}
catch (Exception e)
{
// Do some logging here
}
}
*/

/*
public async Task<string> SSH(string instanceId, string subpath)
{
if(K8SEDeploymentHelper.IsK8SEEnvironment())
{
/*
var instances = K8SEDeploymentHelper.GetInstances(K8SEDeploymentHelper.GetAppName(HttpContext));
PodInstance instance = null;
if (instances.Count > 0)
Expand All @@ -116,7 +216,7 @@ public async Task<string> SSH(string instanceId, string subpath)
{
return "Invalid instance";
}
*/
var instance = new PodInstance()
{
Name = "codeapp-sample-8994dbf4d-vsdr5",
Expand All @@ -133,10 +233,12 @@ public async Task<string> SSH(string instanceId, string subpath)
//CopyFromTargetResponseHeaders(HttpContext, responseMessage);
await responseMessage.Content.CopyToAsync(HttpContext.Response.Body);
}
}
return null;
}
*/
private HttpRequestMessage CreateTargetMessage(HttpContext context, Uri targetUri, PodInstance instance)
{
var requestMessage = new HttpRequestMessage();
Expand Down

0 comments on commit 0ca6f0d

Please sign in to comment.