Skip to content

Commit 781243e

Browse files
authored
Merge pull request #135 from jackbuehner/get-rdp-and-get-image-to-api
Add API endpoints for getting RDP files and associated image files
2 parents 2ef21e7 + d6fd1e6 commit 781243e

File tree

14 files changed

+615
-568
lines changed

14 files changed

+615
-568
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Web.Http;
2+
3+
namespace RAWebServer.Api
4+
{
5+
[RoutePrefix("api/resources")]
6+
public partial class ResourceController : ApiController
7+
{
8+
}
9+
}

aspx/wwwroot/App_Code/api/resources/GetImage.cs

Lines changed: 451 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Drawing;
3+
using System.Drawing.Imaging;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Net;
7+
using System.Net.Http;
8+
using System.Net.Http.Headers;
9+
using System.Web;
10+
using System.Web.Http;
11+
12+
namespace RAWebServer.Api
13+
{
14+
public partial class ResourceController : ApiController
15+
{
16+
/// <summary>
17+
///
18+
/// </summary>
19+
/// <param name="path">relative path to the rdp file, or the name of the registry key</param>
20+
/// <param name="from">rdp or registry</param>
21+
/// <returns></returns>
22+
[HttpGet]
23+
[Route("{*path}")]
24+
[Route("~/get-rdp.aspx")]
25+
[RequireAuthentication]
26+
public IHttpActionResult GetImage(string path, string from = "rdp")
27+
{
28+
int permissionHttpStatus = 200;
29+
bool hasPermission = false;
30+
31+
// ensure the parameters are valid formats
32+
if (from != "rdp" && from != "registry")
33+
{
34+
throw new ArgumentException("Parameter 'from' must be either 'rdp' or 'registry'.");
35+
}
36+
37+
// if the path starts with App_Data/, remove that part
38+
if (path.StartsWith("App_Data/", StringComparison.OrdinalIgnoreCase))
39+
{
40+
path = path.Substring("App_Data/".Length);
41+
}
42+
43+
// get authentication information
44+
var authCookieHandler = new AuthUtilities.AuthCookieHandler();
45+
var userInfo = authCookieHandler.GetUserInformationSafe(HttpContext.Current.Request);
46+
47+
// if it is an RDP file, serve it from the file system
48+
if (from == "rdp")
49+
{
50+
string root = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
51+
string filePath = Path.Combine(root, string.Format("{0}", path));
52+
if (!filePath.EndsWith(".rdp", StringComparison.OrdinalIgnoreCase))
53+
{
54+
filePath += ".rdp";
55+
}
56+
if (!File.Exists(filePath))
57+
{
58+
return ResponseMessage(Request.CreateErrorResponse(
59+
HttpStatusCode.NotFound,
60+
"The specified RDP file does not exist."
61+
));
62+
}
63+
64+
// check that the user has permission to access the RDP file
65+
hasPermission = FileSystemUtilities.Reader.CanAccessPath(filePath, userInfo, out permissionHttpStatus);
66+
if (!hasPermission)
67+
{
68+
return ResponseMessage(Request.CreateResponse((HttpStatusCode)permissionHttpStatus));
69+
}
70+
71+
// serve the RDP file
72+
var response = new HttpResponseMessage(HttpStatusCode.OK);
73+
response.Content = new ByteArrayContent(File.ReadAllBytes(filePath));
74+
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-rdp");
75+
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = Path.GetFileName(filePath) };
76+
return ResponseMessage(response);
77+
}
78+
79+
// ensure the path is a valid registry key name
80+
if (path.Contains("\\") || path.Contains("/"))
81+
{
82+
return BadRequest("When 'from' is 'registry', 'path' must be the name of the registry key, not a file path.");
83+
}
84+
85+
// check that the user has permission Wto access the remoteapp in the registry
86+
hasPermission = RegistryUtilities.Reader.CanAccessRemoteApp(path, userInfo, out permissionHttpStatus);
87+
if (!hasPermission)
88+
{
89+
return ResponseMessage(Request.CreateResponse((HttpStatusCode)permissionHttpStatus));
90+
}
91+
92+
// construct an RDP file from the values in the registry and serve it
93+
string rdpFileContents = RegistryUtilities.Reader.ConstructRdpFileFromRegistry(path);
94+
var response2 = new HttpResponseMessage(HttpStatusCode.OK);
95+
response2.Content = new StringContent(rdpFileContents);
96+
response2.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-rdp");
97+
response2.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = path + ".rdp" };
98+
return ResponseMessage(response2); ;
99+
}
100+
}
101+
}

aspx/wwwroot/Web.config

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
</system.web>
1616
<system.webServer>
1717
<staticContent>
18-
<mimeMap fileExtension=".rdp" mimeType="application/x-rdp" />
1918
<mimeMap fileExtension=".vue" mimeType="application/javascript" />
2019
<mimeMap fileExtension=".mjs" mimeType="application/javascript" />
2120
<mimeMap fileExtension=".webp" mimeType="image/webp" />
2221
<mimeMap fileExtension=".webmanifest" mimeType="application/manifest+json" />
2322
</staticContent>
23+
<handlers>
24+
<add name="AllowAllRequestsToReachApi" path="api/*" verb="GET" type="System.Web.Handlers.TransferRequestHandler" />
25+
</handlers>
2426
<caching>
2527
<profiles>
2628
<add extension=".aspx" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="00:00:01" />

aspx/wwwroot/get-image.aspx

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)