Files
Aaru.Server/DiscImageChef.Server/Controllers/UploadReportController.cs

202 lines
7.5 KiB
C#
Raw Normal View History

2019-11-02 01:40:41 +00:00
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : UploadReportController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : DiscImageChef Server.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles report uploads.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2019 Natalia Portillo
// ****************************************************************************/
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
2019-11-02 21:01:25 +00:00
using System.Threading.Tasks;
2019-11-02 01:40:41 +00:00
using System.Xml.Serialization;
using Cinchoo.PGP;
using DiscImageChef.CommonTypes.Metadata;
using DiscImageChef.Server.Models;
using MailKit.Net.Smtp;
2019-11-02 21:01:25 +00:00
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
2019-11-02 01:40:41 +00:00
using MimeKit;
using Newtonsoft.Json;
namespace DiscImageChef.Server.Controllers
{
2019-11-02 21:01:25 +00:00
public class UploadReportController : Controller
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
private readonly IWebHostEnvironment _environment;
private readonly DicServerContext ctx;
2019-11-02 21:01:25 +00:00
public UploadReportController(IWebHostEnvironment environment, DicServerContext _ctx)
{
_environment = environment;
ctx = _ctx;
}
2019-11-02 01:40:41 +00:00
/// <summary>
/// Receives a report from DiscImageChef.Core, verifies it's in the correct format and stores it on the server
/// </summary>
/// <returns>HTTP response</returns>
[Route("api/uploadreport")]
[HttpPost]
2019-11-02 21:01:25 +00:00
public async Task<IActionResult> UploadReport()
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var response = new ContentResult {StatusCode = (int) HttpStatusCode.OK, ContentType = "text/plain"};
2019-11-02 01:40:41 +00:00
try
{
2019-11-02 21:37:09 +00:00
var newReport = new DeviceReport();
var request = HttpContext.Request;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var xs = new XmlSerializer(newReport.GetType());
newReport = (DeviceReport) xs.Deserialize(
new StringReader(await new StreamReader(request.Body).ReadToEndAsync()));
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (newReport == null)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:01:25 +00:00
response.Content = "notstats";
2019-11-02 01:40:41 +00:00
return response;
}
2019-11-02 21:37:09 +00:00
var reportV2 = new DeviceReportV2(newReport);
var jsonSw = new StringWriter();
2019-11-02 01:40:41 +00:00
jsonSw.Write(JsonConvert.SerializeObject(reportV2, Formatting.Indented,
2019-11-02 21:37:09 +00:00
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}));
var reportV2String = jsonSw.ToString();
2019-11-02 01:40:41 +00:00
jsonSw.Close();
ctx.Reports.Add(new UploadedReport(reportV2));
ctx.SaveChanges();
2019-11-02 21:37:09 +00:00
var pgpIn = new MemoryStream(Encoding.UTF8.GetBytes(reportV2String));
var pgpOut = new MemoryStream();
var pgp = new ChoPGPEncryptDecrypt();
2019-11-02 01:40:41 +00:00
pgp.Encrypt(pgpIn, pgpOut,
2019-11-02 21:37:09 +00:00
Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
"public.asc"));
2019-11-02 01:40:41 +00:00
pgpOut.Position = 0;
2019-11-02 21:37:09 +00:00
reportV2String = Encoding.UTF8.GetString(pgpOut.ToArray());
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var message = new MimeMessage
2019-11-02 01:40:41 +00:00
{
Subject = "New device report (old version)",
2019-11-02 21:37:09 +00:00
Body = new TextPart("plain") {Text = reportV2String}
2019-11-02 01:40:41 +00:00
};
2019-11-02 21:37:09 +00:00
message.From.Add(new MailboxAddress("DiscImageChef", "dic@claunia.com"));
2019-11-02 01:40:41 +00:00
message.To.Add(new MailboxAddress("Natalia Portillo", "claunia@claunia.com"));
2019-11-02 21:37:09 +00:00
using (var client = new SmtpClient())
2019-11-02 01:40:41 +00:00
{
client.Connect("mail.claunia.com", 25, false);
client.Send(message);
client.Disconnect(true);
}
2019-11-02 21:01:25 +00:00
response.Content = "ok";
2019-11-02 01:40:41 +00:00
return response;
}
// ReSharper disable once RedundantCatchClause
catch
{
2019-11-02 21:37:09 +00:00
#if DEBUG
if (Debugger.IsAttached) throw;
#endif
2019-11-02 21:01:25 +00:00
response.Content = "error";
2019-11-02 01:40:41 +00:00
return response;
}
}
/// <summary>
/// Receives a report from DiscImageChef.Core, verifies it's in the correct format and stores it on the server
/// </summary>
/// <returns>HTTP response</returns>
[Route("api/uploadreportv2")]
[HttpPost]
2019-11-02 21:01:25 +00:00
public async Task<IActionResult> UploadReportV2()
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:37:09 +00:00
var response = new ContentResult {StatusCode = (int) HttpStatusCode.OK, ContentType = "text/plain"};
2019-11-02 01:40:41 +00:00
try
{
2019-11-02 21:37:09 +00:00
var request = HttpContext.Request;
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var sr = new StreamReader(request.Body);
var reportJson = await sr.ReadToEndAsync();
var newReport = JsonConvert.DeserializeObject<DeviceReportV2>(reportJson);
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
if (newReport == null)
2019-11-02 01:40:41 +00:00
{
2019-11-02 21:01:25 +00:00
response.Content = "notstats";
2019-11-02 01:40:41 +00:00
return response;
}
ctx.Reports.Add(new UploadedReport(newReport));
ctx.SaveChanges();
2019-11-02 21:37:09 +00:00
var pgpIn = new MemoryStream(Encoding.UTF8.GetBytes(reportJson));
var pgpOut = new MemoryStream();
var pgp = new ChoPGPEncryptDecrypt();
2019-11-02 01:40:41 +00:00
pgp.Encrypt(pgpIn, pgpOut,
2019-11-02 21:37:09 +00:00
Path.Combine(_environment.ContentRootPath ?? throw new InvalidOperationException(),
"public.asc"));
2019-11-02 01:40:41 +00:00
pgpOut.Position = 0;
2019-11-02 21:37:09 +00:00
reportJson = Encoding.UTF8.GetString(pgpOut.ToArray());
2019-11-02 01:40:41 +00:00
2019-11-02 21:37:09 +00:00
var message = new MimeMessage
2019-11-02 01:40:41 +00:00
{
Subject = "New device report", Body = new TextPart("plain") {Text = reportJson}
};
2019-11-02 21:37:09 +00:00
message.From.Add(new MailboxAddress("DiscImageChef", "dic@claunia.com"));
2019-11-02 01:40:41 +00:00
message.To.Add(new MailboxAddress("Natalia Portillo", "claunia@claunia.com"));
2019-11-02 21:37:09 +00:00
using (var client = new SmtpClient())
2019-11-02 01:40:41 +00:00
{
client.Connect("mail.claunia.com", 25, false);
client.Send(message);
client.Disconnect(true);
}
2019-11-02 21:01:25 +00:00
response.Content = "ok";
2019-11-02 01:40:41 +00:00
return response;
}
// ReSharper disable once RedundantCatchClause
catch
{
2019-11-02 21:37:09 +00:00
#if DEBUG
if (Debugger.IsAttached) throw;
#endif
response.Content = "error";
2019-11-02 01:40:41 +00:00
return response;
}
}
}
}