The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using Teknik.Areas.Error.Controllers;
  10. using Teknik.Areas.Paste.ViewModels;
  11. using Teknik.Controllers;
  12. using Teknik.Helpers;
  13. using Teknik.Models;
  14. namespace Teknik.Areas.Paste.Controllers
  15. {
  16. public class PasteController : DefaultController
  17. {
  18. private TeknikEntities db = new TeknikEntities();
  19. [AllowAnonymous]
  20. public ActionResult Index()
  21. {
  22. ViewBag.Title = Config.Title + " Paste";
  23. PasteCreateViewModel model = new PasteCreateViewModel();
  24. return View(model);
  25. }
  26. [AllowAnonymous]
  27. public ActionResult ViewPaste(string type, string url, string password)
  28. {
  29. Models.Paste paste = db.Pastes.Where(p => p.Url == url).FirstOrDefault();
  30. if (paste != null)
  31. {
  32. ViewBag.Title = ((string.IsNullOrEmpty(paste.Title)) ? string.Empty : paste.Title + " - ") + Config.Title + " Paste";
  33. // Increment Views
  34. paste.Views += 1;
  35. db.Entry(paste).State = EntityState.Modified;
  36. db.SaveChanges();
  37. // Check Expiration
  38. if (CheckExpiration(paste))
  39. {
  40. db.Pastes.Remove(paste);
  41. db.SaveChanges();
  42. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  43. }
  44. PasteViewModel model = new PasteViewModel();
  45. model.Url = url;
  46. model.Content = paste.Content;
  47. model.Title = paste.Title;
  48. model.Syntax = paste.Syntax;
  49. model.DatePosted = paste.DatePosted;
  50. byte[] data = Encoding.Unicode.GetBytes(paste.Content);
  51. // The paste has a password set
  52. if (!string.IsNullOrEmpty(paste.HashedPassword))
  53. {
  54. if (string.IsNullOrEmpty(password) || Helpers.SHA384.Hash(paste.Key, password) != paste.HashedPassword)
  55. {
  56. PasswordViewModel passModel = new PasswordViewModel();
  57. passModel.Url = url;
  58. passModel.CallingAction = Url.SubRouteUrl("paste", "Paste.View", new { type = type });
  59. // Redirect them to the password request page
  60. return View("~/Areas/Paste/Views/Paste/PasswordNeeded.cshtml", passModel);
  61. }
  62. // Now we decrypt the content
  63. byte[] ivBytes = Encoding.Unicode.GetBytes(paste.IV);
  64. byte[] keyBytes = AES.CreateKey(password, ivBytes, paste.KeySize);
  65. data = AES.Decrypt(data, keyBytes, ivBytes);
  66. model.Content = Encoding.Unicode.GetString(data);
  67. }
  68. switch (type.ToLower())
  69. {
  70. case "full":
  71. return View("~/Areas/Paste/Views/Paste/Full.cshtml", model);
  72. case "simple":
  73. return View("~/Areas/Paste/Views/Paste/Simple.cshtml", model);
  74. case "raw":
  75. return Content(model.Content, "application/octet-stream");
  76. case "download":
  77. //Create File
  78. var cd = new System.Net.Mime.ContentDisposition
  79. {
  80. FileName = url,
  81. Inline = true
  82. };
  83. Response.AppendHeader("Content-Disposition", cd.ToString());
  84. return File(data, "text/plain");
  85. default:
  86. return View("~/Areas/Paste/Views/Paste/Full.cshtml", model);
  87. }
  88. }
  89. return Redirect(Url.SubRouteUrl("error", "Error.Http404"));
  90. }
  91. [HttpPost]
  92. [AllowAnonymous]
  93. [ValidateAntiForgeryToken]
  94. public ActionResult Paste([Bind(Include = "Content, Title, Syntax, ExpireLength, ExpireUnit, Password, Hide")]PasteCreateViewModel model)
  95. {
  96. if (ModelState.IsValid)
  97. {
  98. try
  99. {
  100. Models.Paste paste = db.Pastes.Create();
  101. paste.DatePosted = DateTime.Now;
  102. paste.Url = Utility.RandomString(Config.PasteConfig.UrlLength);
  103. paste.MaxViews = 0;
  104. paste.Views = -1;
  105. // Figure out the expire date (null if 'never' or 'visit')
  106. if (model.ExpireLength.HasValue || model.ExpireUnit == "never")
  107. {
  108. switch (model.ExpireUnit)
  109. {
  110. case "never":
  111. break;
  112. case "view":
  113. paste.MaxViews = model.ExpireLength ?? 0;
  114. break;
  115. case "minute":
  116. paste.ExpireDate = paste.DatePosted.AddMinutes(model.ExpireLength ?? 1);
  117. break;
  118. case "hour":
  119. paste.ExpireDate = paste.DatePosted.AddHours(model.ExpireLength ?? 1);
  120. break;
  121. case "day":
  122. paste.ExpireDate = paste.DatePosted.AddDays(model.ExpireLength ?? 1);
  123. break;
  124. case "month":
  125. paste.ExpireDate = paste.DatePosted.AddMonths(model.ExpireLength ?? 1);
  126. break;
  127. case "year":
  128. paste.ExpireDate = paste.DatePosted.AddYears(model.ExpireLength ?? 1);
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. // Set the hashed password if one is provided and encrypt stuff
  135. if (!string.IsNullOrEmpty(model.Password))
  136. {
  137. string key = Utility.RandomString(Config.PasteConfig.KeySize / 8);
  138. string iv = Utility.RandomString(Config.PasteConfig.BlockSize / 8);
  139. paste.HashedPassword = Helpers.SHA384.Hash(key, model.Password);
  140. // Encrypt Content
  141. byte[] data = Encoding.Unicode.GetBytes(model.Content);
  142. byte[] ivBytes = Encoding.Unicode.GetBytes(iv);
  143. byte[] keyBytes = AES.CreateKey(model.Password, ivBytes, Config.PasteConfig.KeySize);
  144. byte[] encData = AES.Encrypt(data, keyBytes, ivBytes);
  145. model.Content = Encoding.Unicode.GetString(encData);
  146. paste.Key = key;
  147. paste.KeySize = Config.PasteConfig.KeySize;
  148. paste.IV = iv;
  149. paste.BlockSize = Config.PasteConfig.BlockSize;
  150. }
  151. paste.Content = model.Content;
  152. paste.Title = model.Title;
  153. paste.Syntax = model.Syntax;
  154. paste.Hide = model.Hide;
  155. db.Pastes.Add(paste);
  156. db.SaveChanges();
  157. return Redirect(Url.SubRouteUrl("paste", "Paste.View", new { type = "Full", url = paste.Url, password = model.Password }));
  158. }
  159. catch (Exception ex)
  160. {
  161. return Redirect(Url.SubRouteUrl("error", "Error.500", new { exception = ex }));
  162. }
  163. }
  164. return View("~/Areas/Paste/Views/Paste/Index.cshtml", model);
  165. }
  166. private bool CheckExpiration(Models.Paste paste)
  167. {
  168. if (paste.ExpireDate != null && DateTime.Now >= paste.ExpireDate)
  169. return true;
  170. if (paste.MaxViews > 0 && paste.Views > paste.MaxViews)
  171. return true;
  172. return false;
  173. }
  174. }
  175. }