Files
Aaru/Aaru.Core/Remote.cs

368 lines
15 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Remote.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles connections to Aaru.Server.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
using System;
2019-01-02 04:05:51 +00:00
using System.Collections.Generic;
using System.Diagnostics;
2017-06-03 01:18:36 +01:00
using System.IO;
2019-01-02 04:05:51 +00:00
using System.Linq;
2017-06-03 01:18:36 +01:00
using System.Net;
using System.Text;
2017-12-19 19:33:46 +00:00
using System.Threading;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Metadata;
using Aaru.Console;
using Aaru.Database;
using Aaru.Database.Models;
using Aaru.Dto;
2019-01-02 04:05:51 +00:00
using Microsoft.EntityFrameworkCore;
2018-12-20 00:12:48 +00:00
using Newtonsoft.Json;
2020-02-27 00:33:26 +00:00
using CdOffset = Aaru.Database.Models.CdOffset;
using Version = Aaru.CommonTypes.Metadata.Version;
2017-06-03 01:18:36 +01:00
2020-02-27 00:33:26 +00:00
namespace Aaru.Core
{
2020-02-29 18:03:35 +00:00
/// <summary>Handles connections to Aaru.Server</summary>
public static class Remote
{
2020-02-29 18:03:35 +00:00
/// <summary>Submits a device report</summary>
/// <param name="report">Device report</param>
2019-01-04 04:16:44 +00:00
public static void SubmitReport(DeviceReportV2 report)
2018-12-20 00:12:48 +00:00
{
2020-02-29 18:03:35 +00:00
var submitThread = new Thread(() =>
2018-12-20 00:12:48 +00:00
{
try
{
2020-02-29 18:03:35 +00:00
#if DEBUG
2018-12-20 00:12:48 +00:00
System.Console.WriteLine("Uploading device report");
2020-02-29 18:03:35 +00:00
#else
2020-02-27 23:48:41 +00:00
Aaru.Console.AaruConsole.DebugWriteLine("Submit stats", "Uploading device report");
2020-02-29 18:03:35 +00:00
#endif
string json = JsonConvert.SerializeObject(report, Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
var request = WebRequest.Create("https://www.aaru.app/api/uploadreportv2");
((HttpWebRequest)request).UserAgent = $"Aaru {typeof(Version).Assembly.GetName().Version}";
2018-12-20 00:12:48 +00:00
request.Method = "POST";
request.ContentLength = jsonBytes.Length;
2018-12-20 00:12:48 +00:00
request.ContentType = "application/json";
Stream reqStream = request.GetRequestStream();
reqStream.Write(jsonBytes, 0, jsonBytes.Length);
2017-06-03 01:18:36 +01:00
reqStream.Close();
WebResponse response = request.GetResponse();
2020-02-29 18:03:35 +00:00
if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK)
return;
2017-06-03 01:18:36 +01:00
2020-02-29 18:03:35 +00:00
Stream data = response.GetResponseStream();
var reader = new StreamReader(data ?? throw new InvalidOperationException());
2017-06-03 01:18:36 +01:00
reader.ReadToEnd();
2017-06-03 01:18:36 +01:00
data.Close();
response.Close();
}
catch(WebException)
{
// Can't connect to the server, do nothing
}
2020-02-29 18:03:35 +00:00
// ReSharper disable once RedundantCatchClause
2017-06-03 01:18:36 +01:00
catch
{
2020-02-29 18:03:35 +00:00
#if DEBUG
if(Debugger.IsAttached)
throw;
#endif
2017-06-03 01:18:36 +01:00
}
});
2020-02-29 18:03:35 +00:00
2017-06-03 01:18:36 +01:00
submitThread.Start();
}
2019-01-02 04:05:51 +00:00
public static void UpdateMasterDatabase(bool create)
{
2020-02-29 18:03:35 +00:00
var mctx = AaruContext.Create(Settings.Settings.MasterDbPath);
2020-07-10 17:38:52 +01:00
if(create)
{
mctx.Database.EnsureCreated();
mctx.Database.
ExecuteSqlRaw("CREATE TABLE IF NOT EXISTS \"__EFMigrationsHistory\" (\"MigrationId\" TEXT PRIMARY KEY, \"ProductVersion\" TEXT)");
foreach(string migration in mctx.Database.GetPendingMigrations())
{
mctx.Database.
ExecuteSqlRaw($"INSERT INTO \"__EFMigrationsHistory\" (MigrationId, ProductVersion) VALUES ('{migration}', '0.0.0')");
}
}
else
mctx.Database.Migrate();
2019-01-02 04:05:51 +00:00
mctx.SaveChanges();
try
{
long lastUpdate = 0;
DateTime latest = DateTime.MinValue;
if(!create)
{
List<DateTime> latestAll = new List<DateTime>();
2020-02-29 18:03:35 +00:00
if(mctx.UsbVendors.Any())
latestAll.Add(mctx.UsbVendors.Max(v => v.ModifiedWhen));
if(mctx.UsbProducts.Any())
latestAll.Add(mctx.UsbProducts.Max(p => p.ModifiedWhen));
if(mctx.CdOffsets.Any())
latestAll.Add(mctx.CdOffsets.Max(o => o.ModifiedWhen));
if(mctx.Devices.Any())
latestAll.Add(mctx.Devices.Max(d => d.LastSynchronized));
2019-01-02 04:05:51 +00:00
if(latestAll.Any())
{
latest = latestAll.Max(t => t);
lastUpdate = (latest.ToFileTimeUtc() - new DateTime(1970, 1, 1).ToFileTimeUtc()) / 10000000;
}
}
if(lastUpdate == 0)
{
create = true;
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Creating master database");
2019-01-02 04:05:51 +00:00
}
else
{
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Updating master database");
AaruConsole.WriteLine("Last update: {0}", latest);
2019-01-02 04:05:51 +00:00
}
DateTime updateStart = DateTime.UtcNow;
2020-02-29 18:03:35 +00:00
var request = WebRequest.Create($"https://www.aaru.app/api/update?timestamp={lastUpdate}");
((HttpWebRequest)request).UserAgent = $"Aaru {typeof(Version).Assembly.GetName().Version}";
2019-01-02 04:05:51 +00:00
request.Method = "GET";
request.ContentType = "application/json";
WebResponse response = request.GetResponse();
if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK)
{
2020-02-27 23:48:41 +00:00
AaruConsole.ErrorWriteLine("Error {0} when trying to get updated entities.",
2020-02-29 18:03:35 +00:00
((HttpWebResponse)response).StatusCode);
2019-01-02 04:05:51 +00:00
return;
}
2020-02-29 18:03:35 +00:00
Stream data = response.GetResponseStream();
var reader = new StreamReader(data ?? throw new InvalidOperationException());
SyncDto sync = JsonConvert.DeserializeObject<SyncDto>(reader.ReadToEnd());
2019-01-02 04:05:51 +00:00
if(create)
{
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Adding USB vendors");
2020-02-29 18:03:35 +00:00
2019-01-02 04:05:51 +00:00
foreach(UsbVendorDto vendor in sync.UsbVendors)
mctx.UsbVendors.Add(new UsbVendor(vendor.VendorId, vendor.Vendor));
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Added {0} usb vendors", sync.UsbVendors.Count);
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Adding USB products");
2020-02-29 18:03:35 +00:00
2019-01-02 04:05:51 +00:00
foreach(UsbProductDto product in sync.UsbProducts)
mctx.UsbProducts.Add(new UsbProduct(product.VendorId, product.ProductId, product.Product));
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Added {0} usb products", sync.UsbProducts.Count);
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Adding CompactDisc read offsets");
2020-02-29 18:03:35 +00:00
2019-01-02 04:05:51 +00:00
foreach(CdOffsetDto offset in sync.Offsets)
2020-02-29 18:03:35 +00:00
mctx.CdOffsets.Add(new CdOffset(offset)
{
Id = offset.Id
});
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Added {0} CompactDisc read offsets", sync.Offsets.Count);
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Adding known devices");
2020-02-29 18:03:35 +00:00
foreach(DeviceDto device in sync.Devices)
mctx.Devices.Add(new Device(device)
{
Id = device.Id
});
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Added {0} known devices", sync.Devices.Count);
2019-01-02 04:05:51 +00:00
}
else
{
long addedVendors = 0;
long addedProducts = 0;
long addedOffsets = 0;
long addedDevices = 0;
long modifiedVendors = 0;
long modifiedProducts = 0;
long modifiedOffsets = 0;
long modifiedDevices = 0;
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Updating USB vendors");
2020-02-29 18:03:35 +00:00
2019-01-02 04:05:51 +00:00
foreach(UsbVendorDto vendor in sync.UsbVendors)
{
UsbVendor existing = mctx.UsbVendors.FirstOrDefault(v => v.Id == vendor.VendorId);
if(existing != null)
{
modifiedVendors++;
existing.Vendor = vendor.Vendor;
existing.ModifiedWhen = updateStart;
mctx.UsbVendors.Update(existing);
}
else
{
addedVendors++;
mctx.UsbVendors.Add(new UsbVendor(vendor.VendorId, vendor.Vendor));
}
}
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("Added {0} USB vendors", addedVendors);
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Modified {0} USB vendors", modifiedVendors);
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Updating USB products");
2020-02-29 18:03:35 +00:00
2019-01-02 04:05:51 +00:00
foreach(UsbProductDto product in sync.UsbProducts)
{
UsbProduct existing =
mctx.UsbProducts.FirstOrDefault(p => p.VendorId == product.VendorId &&
p.ProductId == product.ProductId);
if(existing != null)
{
modifiedProducts++;
existing.Product = product.Product;
existing.ModifiedWhen = updateStart;
mctx.UsbProducts.Update(existing);
}
else
{
addedProducts++;
mctx.UsbProducts.Add(new UsbProduct(product.VendorId, product.ProductId, product.Product));
}
}
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("Added {0} USB products", addedProducts);
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Modified {0} USB products", modifiedProducts);
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Updating CompactDisc read offsets");
2020-02-29 18:03:35 +00:00
2019-01-02 04:05:51 +00:00
foreach(CdOffsetDto offset in sync.Offsets)
{
CdOffset existing = mctx.CdOffsets.FirstOrDefault(o => o.Id == offset.Id);
if(existing != null)
{
modifiedOffsets++;
existing.Agreement = offset.Agreement;
existing.Manufacturer = offset.Manufacturer;
existing.Model = offset.Model;
existing.Submissions = offset.Submissions;
existing.Offset = offset.Offset;
existing.ModifiedWhen = updateStart;
mctx.CdOffsets.Update(existing);
}
else
{
addedOffsets++;
2020-02-29 18:03:35 +00:00
mctx.CdOffsets.Add(new CdOffset(offset)
{
Id = offset.Id
});
2019-01-02 04:05:51 +00:00
}
}
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("Added {0} CompactDisc read offsets", addedOffsets);
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Modified {0} CompactDisc read offsets", modifiedOffsets);
2019-01-02 04:05:51 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Updating known devices");
2020-02-29 18:03:35 +00:00
2019-01-02 04:05:51 +00:00
foreach(DeviceDto device in sync.Devices)
{
Device existing = mctx.Devices.FirstOrDefault(d => d.Id == device.Id);
if(existing != null)
{
modifiedDevices++;
mctx.Remove(existing);
existing = new Device(device)
{
Id = device.Id, OptimalMultipleSectorsRead = device.OptimalMultipleSectorsRead,
CanReadGdRomUsingSwapDisc = device.CanReadGdRomUsingSwapDisc
};
mctx.Devices.Add(existing);
2019-01-02 04:05:51 +00:00
}
else
{
addedDevices++;
2020-02-29 18:03:35 +00:00
mctx.Devices.Add(new Device(device)
{
Id = device.Id, OptimalMultipleSectorsRead = device.OptimalMultipleSectorsRead,
CanReadGdRomUsingSwapDisc = device.CanReadGdRomUsingSwapDisc
});
2019-01-02 04:05:51 +00:00
}
}
2020-02-29 18:03:35 +00:00
AaruConsole.WriteLine("Added {0} known devices", addedDevices);
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Modified {0} known devices", modifiedDevices);
2019-01-02 04:05:51 +00:00
}
}
2020-02-29 18:03:35 +00:00
catch(Exception ex)
{
AaruConsole.ErrorWriteLine("Exception {0} when updating database.", ex);
}
2019-01-02 04:05:51 +00:00
finally
{
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Saving changes...");
2019-01-02 04:05:51 +00:00
mctx.SaveChanges();
}
}
}
2017-12-19 20:33:03 +00:00
}