2017-05-28 21:01:17 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : Remote.cs
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2017-05-28 21:01:17 +01:00
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Core algorithms.
|
2017-05-28 21:01:17 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Handles connections to DiscImageChef.Server.
|
2017-05-28 21:01:17 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-29 17:34:38 +00:00
|
|
|
|
// Copyright © 2011-2019 Natalia Portillo
|
2017-05-28 21:01:17 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 03:50:57 +00:00
|
|
|
|
|
2017-12-21 14:41:38 +00:00
|
|
|
|
using System;
|
2019-01-02 04:05:51 +00:00
|
|
|
|
using System.Collections.Generic;
|
2018-12-20 00:16:54 +00:00
|
|
|
|
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;
|
2019-01-04 04:10:45 +00:00
|
|
|
|
using System.Text;
|
2017-12-19 19:33:46 +00:00
|
|
|
|
using System.Threading;
|
2018-06-25 19:08:16 +01:00
|
|
|
|
using DiscImageChef.CommonTypes.Metadata;
|
2019-01-02 04:05:51 +00:00
|
|
|
|
using DiscImageChef.Console;
|
|
|
|
|
|
using DiscImageChef.Database;
|
|
|
|
|
|
using DiscImageChef.Database.Models;
|
|
|
|
|
|
using DiscImageChef.Dto;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-12-20 00:12:48 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2019-01-02 04:05:51 +00:00
|
|
|
|
using CdOffset = DiscImageChef.Database.Models.CdOffset;
|
2018-06-25 19:08:16 +01:00
|
|
|
|
using Version = DiscImageChef.CommonTypes.Metadata.Version;
|
2017-06-03 01:18:36 +01:00
|
|
|
|
|
2017-05-28 21:01:17 +01:00
|
|
|
|
namespace DiscImageChef.Core
|
|
|
|
|
|
{
|
2017-12-23 01:46:08 +00:00
|
|
|
|
/// <summary>
|
2017-12-23 17:41:23 +00:00
|
|
|
|
/// Handles connections to DiscImageChef.Server
|
2017-12-23 01:46:08 +00:00
|
|
|
|
/// </summary>
|
2017-05-28 21:01:17 +01:00
|
|
|
|
public static class Remote
|
|
|
|
|
|
{
|
2017-12-23 01:46:08 +00:00
|
|
|
|
/// <summary>
|
2017-12-23 17:41:23 +00:00
|
|
|
|
/// Submits a device report
|
2017-12-23 01:46:08 +00:00
|
|
|
|
/// </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
|
|
|
|
{
|
|
|
|
|
|
Thread submitThread = new Thread(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
System.Console.WriteLine("Uploading device report");
|
|
|
|
|
|
#else
|
|
|
|
|
|
DiscImageChef.Console.DicConsole.DebugWriteLine("Submit stats", "Uploading device report");
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2019-01-04 04:10:45 +00:00
|
|
|
|
string json = JsonConvert.SerializeObject(report, Formatting.Indented,
|
|
|
|
|
|
new JsonSerializerSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
NullValueHandling = NullValueHandling.Ignore
|
|
|
|
|
|
});
|
|
|
|
|
|
byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
|
2019-05-26 00:45:34 +01:00
|
|
|
|
WebRequest request = WebRequest.Create("https://www.discimagechef.app/api/uploadreportv2");
|
2018-12-20 00:12:48 +00:00
|
|
|
|
((HttpWebRequest)request).UserAgent = $"DiscImageChef {typeof(Version).Assembly.GetName().Version}";
|
|
|
|
|
|
request.Method = "POST";
|
2019-01-04 04:10:45 +00:00
|
|
|
|
request.ContentLength = jsonBytes.Length;
|
2018-12-20 00:12:48 +00:00
|
|
|
|
request.ContentType = "application/json";
|
|
|
|
|
|
Stream reqStream = request.GetRequestStream();
|
2019-01-04 04:10:45 +00:00
|
|
|
|
reqStream.Write(jsonBytes, 0, jsonBytes.Length);
|
2017-06-03 01:18:36 +01:00
|
|
|
|
reqStream.Close();
|
|
|
|
|
|
WebResponse response = request.GetResponse();
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK) return;
|
2017-06-03 01:18:36 +01:00
|
|
|
|
|
2017-12-29 01:32:12 +00:00
|
|
|
|
Stream data = response.GetResponseStream();
|
2017-12-21 14:41:38 +00:00
|
|
|
|
StreamReader reader = new StreamReader(data ?? throw new InvalidOperationException());
|
2017-06-03 01:18:36 +01:00
|
|
|
|
|
2017-12-21 23:00:30 +00: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
|
|
|
|
|
|
}
|
2017-12-21 07:23:33 +00:00
|
|
|
|
// ReSharper disable once RedundantCatchClause
|
2017-06-03 01:18:36 +01:00
|
|
|
|
catch
|
|
|
|
|
|
{
|
2017-12-29 01:32:12 +00:00
|
|
|
|
#if DEBUG
|
2018-12-20 00:16:54 +00:00
|
|
|
|
if(Debugger.IsAttached) throw;
|
2017-12-29 01:32:12 +00:00
|
|
|
|
#endif
|
2017-06-03 01:18:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
submitThread.Start();
|
2017-05-28 21:01:17 +01:00
|
|
|
|
}
|
2019-01-02 04:05:51 +00:00
|
|
|
|
|
|
|
|
|
|
public static void UpdateMasterDatabase(bool create)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicContext mctx = DicContext.Create(Settings.Settings.MasterDbPath);
|
|
|
|
|
|
mctx.Database.Migrate();
|
|
|
|
|
|
mctx.SaveChanges();
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
long lastUpdate = 0;
|
|
|
|
|
|
DateTime latest = DateTime.MinValue;
|
|
|
|
|
|
|
|
|
|
|
|
if(!create)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<DateTime> latestAll = new List<DateTime>();
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
|
|
if(latestAll.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
latest = latestAll.Max(t => t);
|
|
|
|
|
|
lastUpdate = (latest.ToFileTimeUtc() - new DateTime(1970, 1, 1).ToFileTimeUtc()) / 10000000;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(lastUpdate == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
create = true;
|
|
|
|
|
|
DicConsole.WriteLine("Creating master database");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.WriteLine("Updating master database");
|
|
|
|
|
|
DicConsole.WriteLine("Last update: {0}", latest);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DateTime updateStart = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
|
|
WebRequest request =
|
2019-05-26 00:45:34 +01:00
|
|
|
|
WebRequest.Create($"https://www.discimagechef.app/api/update?timestamp={lastUpdate}");
|
2019-01-02 04:05:51 +00:00
|
|
|
|
((HttpWebRequest)request).UserAgent = $"DiscImageChef {typeof(Version).Assembly.GetName().Version}";
|
|
|
|
|
|
request.Method = "GET";
|
|
|
|
|
|
request.ContentType = "application/json";
|
|
|
|
|
|
WebResponse response = request.GetResponse();
|
|
|
|
|
|
|
|
|
|
|
|
if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.ErrorWriteLine("Error {0} when trying to get updated entities.",
|
|
|
|
|
|
((HttpWebResponse)response).StatusCode);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Stream data = response.GetResponseStream();
|
|
|
|
|
|
StreamReader reader = new StreamReader(data ?? throw new InvalidOperationException());
|
|
|
|
|
|
SyncDto sync = JsonConvert.DeserializeObject<SyncDto>(reader.ReadToEnd());
|
|
|
|
|
|
|
|
|
|
|
|
if(create)
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.WriteLine("Adding USB vendors");
|
|
|
|
|
|
foreach(UsbVendorDto vendor in sync.UsbVendors)
|
|
|
|
|
|
mctx.UsbVendors.Add(new UsbVendor(vendor.VendorId, vendor.Vendor));
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Added {0} usb vendors", sync.UsbVendors.Count);
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Adding USB products");
|
|
|
|
|
|
foreach(UsbProductDto product in sync.UsbProducts)
|
|
|
|
|
|
mctx.UsbProducts.Add(new UsbProduct(product.VendorId, product.ProductId, product.Product));
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Added {0} usb products", sync.UsbProducts.Count);
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Adding CompactDisc read offsets");
|
|
|
|
|
|
foreach(CdOffsetDto offset in sync.Offsets)
|
|
|
|
|
|
mctx.CdOffsets.Add(new CdOffset(offset) {Id = offset.Id});
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Added {0} CompactDisc read offsets", sync.Offsets.Count);
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Adding known devices");
|
|
|
|
|
|
foreach(DeviceDto device in sync.Devices) mctx.Devices.Add(new Device(device) {Id = device.Id});
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Added {0} known devices", sync.Devices.Count);
|
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Updating USB vendors");
|
|
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Added {0} USB vendors", addedVendors);
|
|
|
|
|
|
DicConsole.WriteLine("Modified {0} USB vendors", modifiedVendors);
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Updating USB products");
|
|
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Added {0} USB products", addedProducts);
|
|
|
|
|
|
DicConsole.WriteLine("Modified {0} USB products", modifiedProducts);
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Updating CompactDisc read offsets");
|
|
|
|
|
|
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++;
|
|
|
|
|
|
mctx.CdOffsets.Add(new CdOffset(offset) {Id = offset.Id});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Added {0} CompactDisc read offsets", addedOffsets);
|
|
|
|
|
|
DicConsole.WriteLine("Modified {0} CompactDisc read offsets", modifiedOffsets);
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Updating known devices");
|
|
|
|
|
|
foreach(DeviceDto device in sync.Devices)
|
|
|
|
|
|
{
|
|
|
|
|
|
Device existing = mctx.Devices.FirstOrDefault(d => d.Id == device.Id);
|
|
|
|
|
|
|
|
|
|
|
|
if(existing != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
modifiedDevices++;
|
2019-01-03 16:55:15 +00:00
|
|
|
|
existing = new Device(device)
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = device.Id, OptimalMultipleSectorsRead = device.OptimalMultipleSectorsRead
|
|
|
|
|
|
};
|
2019-01-02 04:05:51 +00:00
|
|
|
|
mctx.Devices.Update(existing);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
addedDevices++;
|
2019-01-03 16:55:15 +00:00
|
|
|
|
mctx.Devices.Add(new Device(device)
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = device.Id,
|
|
|
|
|
|
OptimalMultipleSectorsRead = device.OptimalMultipleSectorsRead
|
|
|
|
|
|
});
|
2019-01-02 04:05:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DicConsole.WriteLine("Added {0} known devices", addedDevices);
|
|
|
|
|
|
DicConsole.WriteLine("Modified {0} known devices", modifiedDevices);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch(Exception ex) { DicConsole.ErrorWriteLine("Exception {0} when updating database.", ex); }
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
DicConsole.WriteLine("Saving changes...");
|
|
|
|
|
|
mctx.SaveChanges();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-05-28 21:01:17 +01:00
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|