The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PasteController.cs 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Teknik.Areas.Paste.ViewModels;
  6. using Teknik.Areas.Users.Utility;
  7. using Teknik.Controllers;
  8. using Teknik.Filters;
  9. using Teknik.Utilities;
  10. using Teknik.Models;
  11. using Teknik.Attributes;
  12. using Teknik.Utilities.Cryptography;
  13. using Microsoft.Extensions.Logging;
  14. using Teknik.Configuration;
  15. using Teknik.Data;
  16. using Microsoft.AspNetCore.Authorization;
  17. using Microsoft.AspNetCore.Mvc;
  18. using Microsoft.EntityFrameworkCore;
  19. using Microsoft.AspNetCore.Http;
  20. using Teknik.Logging;
  21. namespace Teknik.Areas.Paste.Controllers
  22. {
  23. [TeknikAuthorize]
  24. [Area("Paste")]
  25. public class PasteController : DefaultController
  26. {
  27. public PasteController(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { }
  28. [AllowAnonymous]
  29. public IActionResult Index()
  30. {
  31. ViewBag.Title = "Paste - " + _config.Title;
  32. ViewBag.Description = "Paste your code or text easily and securely. Set an expiration, set a password, or leave it open for the world to see.";
  33. PasteCreateViewModel model = new PasteCreateViewModel();
  34. return View(model);
  35. }
  36. [AllowAnonymous]
  37. public IActionResult ViewPaste(string type, string url, string password)
  38. {
  39. Models.Paste paste = _dbContext.Pastes.Where(p => p.Url == url).FirstOrDefault();
  40. if (paste != null)
  41. {
  42. ViewBag.Title = ((string.IsNullOrEmpty(paste.Title)) ? string.Empty : paste.Title + " - ") + _config.Title + " Paste";
  43. ViewBag.Description = "Paste your code or text easily and securely. Set an expiration, set a password, or leave it open for the world to see.";
  44. // Increment Views
  45. paste.Views += 1;
  46. _dbContext.Entry(paste).State = EntityState.Modified;
  47. _dbContext.SaveChanges();
  48. // Check Expiration
  49. if (PasteHelper.CheckExpiration(paste))
  50. {
  51. _dbContext.Pastes.Remove(paste);
  52. _dbContext.SaveChanges();
  53. return new StatusCodeResult(StatusCodes.Status404NotFound);
  54. }
  55. PasteViewModel model = new PasteViewModel();
  56. model.Url = url;
  57. model.Content = paste.Content;
  58. model.Title = paste.Title;
  59. model.Syntax = paste.Syntax;
  60. model.DatePosted = paste.DatePosted;
  61. byte[] data = Encoding.UTF8.GetBytes(paste.Content);
  62. if (User.Identity.IsAuthenticated && type.ToLower() == "full")
  63. {
  64. Users.Models.User user = UserHelper.GetUser(_dbContext, User.Identity.Name);
  65. if (user != null)
  66. {
  67. model.Vaults = user.Vaults.ToList();
  68. }
  69. }
  70. // The paste has a password set
  71. if (!string.IsNullOrEmpty(paste.HashedPassword))
  72. {
  73. string hash = string.Empty;
  74. if (!string.IsNullOrEmpty(password))
  75. {
  76. byte[] passBytes = SHA384.Hash(paste.Key, password);
  77. hash = passBytes.ToHex();
  78. // We need to convert old pastes to the new password scheme
  79. if (paste.Transfers.ToList().Exists(t => t.Type == TransferTypes.ASCIIPassword))
  80. {
  81. hash = Encoding.ASCII.GetString(passBytes);
  82. // Remove the transfer types
  83. paste.Transfers.Clear();
  84. _dbContext.Entry(paste).State = EntityState.Modified;
  85. _dbContext.SaveChanges();
  86. }
  87. }
  88. if (string.IsNullOrEmpty(password) || hash != paste.HashedPassword)
  89. {
  90. PasswordViewModel passModel = new PasswordViewModel();
  91. passModel.Url = url;
  92. passModel.Type = type;
  93. if (!string.IsNullOrEmpty(password) && hash != paste.HashedPassword)
  94. {
  95. passModel.Error = true;
  96. passModel.ErrorMessage = "Invalid Password";
  97. }
  98. // Redirect them to the password request page
  99. return View("~/Areas/Paste/Views/Paste/PasswordNeeded.cshtml", passModel);
  100. }
  101. data = Convert.FromBase64String(paste.Content);
  102. // Now we decrypt the content
  103. byte[] ivBytes = Encoding.Unicode.GetBytes(paste.IV);
  104. byte[] keyBytes = AesCounterManaged.CreateKey(password, ivBytes, paste.KeySize);
  105. data = AesCounterManaged.Decrypt(data, keyBytes, ivBytes);
  106. model.Content = Encoding.Unicode.GetString(data);
  107. }
  108. switch (type.ToLower())
  109. {
  110. case "full":
  111. return View("~/Areas/Paste/Views/Paste/Full.cshtml", model);
  112. case "simple":
  113. return View("~/Areas/Paste/Views/Paste/Simple.cshtml", model);
  114. case "raw":
  115. return Content(model.Content, "text/plain");
  116. case "download":
  117. //Create File
  118. var cd = new System.Net.Mime.ContentDisposition
  119. {
  120. FileName = url + ".txt",
  121. Inline = true
  122. };
  123. Response.Headers.Add("Content-Disposition", cd.ToString());
  124. return File(data, "application/octet-stream");
  125. default:
  126. return View("~/Areas/Paste/Views/Paste/Full.cshtml", model);
  127. }
  128. }
  129. return new StatusCodeResult(StatusCodes.Status404NotFound);
  130. }
  131. [HttpPost]
  132. [AllowAnonymous]
  133. public IActionResult Paste([Bind("Content, Title, Syntax, ExpireLength, ExpireUnit, Password, Hide")]PasteCreateViewModel model)
  134. {
  135. if (ModelState.IsValid)
  136. {
  137. if (_config.PasteConfig.Enabled)
  138. {
  139. try
  140. {
  141. Models.Paste paste = PasteHelper.CreatePaste(_config, _dbContext, model.Content, model.Title, model.Syntax, model.ExpireUnit, model.ExpireLength ?? 1, model.Password, model.Hide);
  142. if (model.ExpireUnit == "view")
  143. {
  144. paste.Views = -1;
  145. }
  146. if (User.Identity.IsAuthenticated)
  147. {
  148. Users.Models.User user = UserHelper.GetUser(_dbContext, User.Identity.Name);
  149. if (user != null)
  150. {
  151. paste.UserId = user.UserId;
  152. }
  153. }
  154. _dbContext.Pastes.Add(paste);
  155. _dbContext.SaveChanges();
  156. return Redirect(Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url }));
  157. }
  158. catch (Exception ex)
  159. {
  160. return Redirect(Url.SubRouteUrl("error", "Error.500", new { exception = ex }));
  161. }
  162. }
  163. return new StatusCodeResult(StatusCodes.Status403Forbidden);
  164. }
  165. return View("~/Areas/Paste/Views/Paste/Index.cshtml", model);
  166. }
  167. }
  168. }