The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PasteController.cs 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. using System.IO;
  22. using System.Threading.Tasks;
  23. namespace Teknik.Areas.Paste.Controllers
  24. {
  25. [Authorize]
  26. [Area("Paste")]
  27. public class PasteController : DefaultController
  28. {
  29. public PasteController(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { }
  30. [AllowAnonymous]
  31. public IActionResult Index()
  32. {
  33. ViewBag.Title = "Paste - " + _config.Title;
  34. 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.";
  35. PasteCreateViewModel model = new PasteCreateViewModel();
  36. return View(model);
  37. }
  38. [AllowAnonymous]
  39. public async Task<IActionResult> ViewPaste(string type, string url, string password)
  40. {
  41. Models.Paste paste = _dbContext.Pastes.Where(p => p.Url == url).FirstOrDefault();
  42. if (paste != null)
  43. {
  44. ViewBag.Title = ((string.IsNullOrEmpty(paste.Title)) ? string.Empty : paste.Title + " - ") + _config.Title + " Paste";
  45. 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.";
  46. // Increment Views
  47. paste.Views += 1;
  48. _dbContext.Entry(paste).State = EntityState.Modified;
  49. _dbContext.SaveChanges();
  50. // Check Expiration
  51. if (PasteHelper.CheckExpiration(paste))
  52. {
  53. _dbContext.Pastes.Remove(paste);
  54. _dbContext.SaveChanges();
  55. return new StatusCodeResult(StatusCodes.Status404NotFound);
  56. }
  57. PasteViewModel model = new PasteViewModel();
  58. model.Url = url;
  59. model.Title = paste.Title;
  60. model.Syntax = paste.Syntax;
  61. model.DatePosted = paste.DatePosted;
  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. byte[] ivBytes = Encoding.Unicode.GetBytes(paste.IV);
  71. byte[] keyBytes = AesCounterManaged.CreateKey(paste.Key, ivBytes, paste.KeySize);
  72. // The paste has a password set
  73. if (!string.IsNullOrEmpty(paste.HashedPassword))
  74. {
  75. string hash = string.Empty;
  76. if (!string.IsNullOrEmpty(password))
  77. {
  78. hash = PasteHelper.HashPassword(paste.Key, password);
  79. keyBytes = AesCounterManaged.CreateKey(password, ivBytes, paste.KeySize);
  80. }
  81. if (string.IsNullOrEmpty(password) || hash != paste.HashedPassword)
  82. {
  83. PasswordViewModel passModel = new PasswordViewModel();
  84. passModel.Url = url;
  85. passModel.Type = type;
  86. if (!string.IsNullOrEmpty(password) && hash != paste.HashedPassword)
  87. {
  88. passModel.Error = true;
  89. passModel.ErrorMessage = "Invalid Password";
  90. }
  91. // Redirect them to the password request page
  92. return View("~/Areas/Paste/Views/Paste/PasswordNeeded.cshtml", passModel);
  93. }
  94. }
  95. // Read in the file
  96. string subDir = paste.FileName[0].ToString();
  97. string filePath = Path.Combine(_config.PasteConfig.PasteDirectory, subDir, paste.FileName);
  98. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
  99. using (AesCounterStream cs = new AesCounterStream(fs, false, keyBytes, ivBytes))
  100. using (StreamReader sr = new StreamReader(cs, Encoding.Unicode))
  101. {
  102. model.Content = await sr.ReadToEndAsync();
  103. }
  104. switch (type.ToLower())
  105. {
  106. case "full":
  107. return View("~/Areas/Paste/Views/Paste/Full.cshtml", model);
  108. case "simple":
  109. return View("~/Areas/Paste/Views/Paste/Simple.cshtml", model);
  110. case "raw":
  111. return Content(model.Content, "text/plain");
  112. case "download":
  113. //Create File
  114. var cd = new System.Net.Mime.ContentDisposition
  115. {
  116. FileName = url + ".txt",
  117. Inline = true
  118. };
  119. Response.Headers.Add("Content-Disposition", cd.ToString());
  120. FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  121. return new BufferedFileStreamResult("application/octet-stream", async (response) => await ResponseHelper.StreamToOutput(response, true, new AesCounterStream(fs, false, keyBytes, ivBytes), (int)fs.Length, _config.PasteConfig.ChunkSize), false);
  122. default:
  123. return View("~/Areas/Paste/Views/Paste/Full.cshtml", model);
  124. }
  125. }
  126. return new StatusCodeResult(StatusCodes.Status404NotFound);
  127. }
  128. [HttpPost]
  129. [AllowAnonymous]
  130. [DisableRequestSizeLimit]
  131. public IActionResult Paste([Bind("Content, Title, Syntax, ExpireLength, ExpireUnit, Password, Hide")]PasteCreateViewModel model)
  132. {
  133. if (ModelState.IsValid)
  134. {
  135. if (_config.PasteConfig.Enabled)
  136. {
  137. try
  138. {
  139. Models.Paste paste = PasteHelper.CreatePaste(_config, _dbContext, model.Content, model.Title, model.Syntax, model.ExpireUnit, model.ExpireLength ?? 1, model.Password, model.Hide);
  140. if (model.ExpireUnit == "view")
  141. {
  142. paste.Views = -1;
  143. }
  144. if (User.Identity.IsAuthenticated)
  145. {
  146. Users.Models.User user = UserHelper.GetUser(_dbContext, User.Identity.Name);
  147. if (user != null)
  148. {
  149. paste.UserId = user.UserId;
  150. }
  151. }
  152. _dbContext.Pastes.Add(paste);
  153. _dbContext.SaveChanges();
  154. return Redirect(Url.SubRouteUrl("p", "Paste.View", new { type = "Full", url = paste.Url }));
  155. }
  156. catch (Exception ex)
  157. {
  158. return Redirect(Url.SubRouteUrl("error", "Error.500", new { exception = ex }));
  159. }
  160. }
  161. return new StatusCodeResult(StatusCodes.Status403Forbidden);
  162. }
  163. return View("~/Areas/Paste/Views/Paste/Index.cshtml", model);
  164. }
  165. }
  166. }