|
| 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 | +} |
0 commit comments