The Free Software Media System - Server Backend & API
Pull in security objects from external assembly
Eric Reedabed5b331bf8
diff --git a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj b/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csprojindex b2872e22a8..79344bb0db 100644--- a/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj+++ b/MediaBrowser.Common.Implementations/MediaBrowser.Common.Implementations.csproj@@ -35,9 +35,6 @@<RunPostBuildEvent>Always</RunPostBuildEvent></PropertyGroup><ItemGroup>- <Reference Include="Mediabrowser.PluginSecurity">- <HintPath>..\ThirdParty\PluginSecurity\Mediabrowser.PluginSecurity.dll</HintPath>- </Reference><Reference Include="NLog"><HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath></Reference>@@ -79,6 +76,8 @@<Compile Include="ScheduledTasks\Tasks\DeleteLogFileTask.cs" /><Compile Include="ScheduledTasks\Tasks\ReloadLoggerTask.cs" /><Compile Include="ScheduledTasks\Tasks\SystemUpdateTask.cs" />+ <Compile Include="Security\MBLicenseFile.cs" />+ <Compile Include="Security\MBRegistration.cs" /><Compile Include="Security\PluginSecurityManager.cs" /><Compile Include="Serialization\JsonSerializer.cs" /><Compile Include="Serialization\XmlSerializer.cs" />@@ -104,7 +103,6 @@<PropertyGroup><PostBuildEvent>if $(ConfigurationName) == Release (xcopy "$(TargetPath)" "$(SolutionDir)\Nuget\dlls\" /y /d /r /i-xcopy "$(TargetDir)Mediabrowser.PluginSecurity.dll" "$(SolutionDir)\Nuget\dlls\" /y /d /r /i)</PostBuildEvent></PropertyGroup><!-- To modify your build process, add your task inside one of the targets below and uncomment it.diff --git a/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs b/MediaBrowser.Common.Implementations/Security/MBLicenseFile.csnew file mode 100644index 0000000000..336ca78f03--- /dev/null+++ b/MediaBrowser.Common.Implementations/Security/MBLicenseFile.cs@@ -0,0 +1,106 @@+using MediaBrowser.Common.Configuration;+using System;+using System.Collections.Generic;+using System.IO;+using System.Security.Cryptography;+using System.Text;++namespace Mediabrowser.Common.Implementations.Security+{+ internal class MBLicenseFile+ {+ private readonly IApplicationPaths _appPaths;++ private readonly string _filename;+ public string RegKey+ {+ get { return _regKey; }+ set+ {+ if (value != _regKey)+ {+ //if key is changed - clear out our saved validations+ UpdateRecords.Clear();+ _regKey = value;+ }+ }+ }++ public string LegacyKey { get; set; }+ private Dictionary<Guid, DateTime> UpdateRecords { get; set; }+ private readonly object _lck = new object();+ private string _regKey;++ public MBLicenseFile(IApplicationPaths appPaths)+ {+ _appPaths = appPaths;++ _filename = Path.Combine(_appPaths.ConfigurationDirectoryPath, "mb.lic");++ UpdateRecords = new Dictionary<Guid, DateTime>();+ Load();+ }++ public void AddRegCheck(string featureId)+ {+ using (var provider = new MD5CryptoServiceProvider())+ {+ UpdateRecords[new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId)))] = DateTime.UtcNow;+ Save();+ }++ }++ public DateTime LastChecked(string featureId)+ {+ using (var provider = new MD5CryptoServiceProvider())+ {+ DateTime last;+ lock(_lck) UpdateRecords.TryGetValue(new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(featureId))), out last);+ return last < DateTime.UtcNow ? last : DateTime.MinValue; // guard agains people just putting a large number in the file+ }+ }++ private void Load()+ {+ string[] contents = null;+ lock (_lck)+ {+ try+ {+ contents = File.ReadAllLines(_filename);+ }+ catch (FileNotFoundException)+ {+ (File.Create(_filename)).Close();+ }+ }+ if (contents != null && contents.Length > 0)+ {+ //first line is reg key+ RegKey = contents[0];+ //next is legacy key+ if (contents.Length > 1) LegacyKey = contents[1];+ //the rest of the lines should be pairs of features and timestamps+ for (var i = 2; i < contents.Length; i = i + 2)+ {+ var feat = Guid.Parse(contents[i]);+ UpdateRecords[feat] = new DateTime(Convert.ToInt64(contents[i + 1]));+ }+ }+ }++ public void Save()+ {+ //build our array+ var lines = new List<string> {RegKey, LegacyKey};+ foreach (var pair in UpdateRecords)+ {+ lines.Add(pair.Key.ToString());+ lines.Add(pair.Value.Ticks.ToString());+ }++ lock(_lck) File.WriteAllLines(_filename, lines);+ }+ }+}diff --git a/MediaBrowser.Common.Implementations/Security/MBRegistration.cs b/MediaBrowser.Common.Implementations/Security/MBRegistration.csnew file mode 100644index 0000000000..570a0429c7--- /dev/null+++ b/MediaBrowser.Common.Implementations/Security/MBRegistration.cs@@ -0,0 +1,109 @@+using Mediabrowser.Model.Entities;+using MediaBrowser.Common.Configuration;+using MediaBrowser.Common.Net;+using MediaBrowser.Model.Serialization;+using System;+using System.Collections.Generic;+using System.Management;+using System.Threading;+using System.Threading.Tasks;++namespace Mediabrowser.Common.Implementations.Security+{+ public static class MBRegistration+ {++ private static MBLicenseFile _licenseFile;+ private const string MBValidateUrl = "http://mb3admin.com/admin/service/registration/validate";++ private static IApplicationPaths _appPaths;++ private static MBLicenseFile LicenseFile+ {+ get { return _licenseFile ?? (_licenseFile = new MBLicenseFile(_appPaths)); }+ }++ public static string SupporterKey+ {+ get { return LicenseFile.RegKey; }+ set { LicenseFile.RegKey = value; LicenseFile.Save(); }+ }++ public static string LegacyKey+ {+ get { return LicenseFile.LegacyKey; }+ set { LicenseFile.LegacyKey = value; LicenseFile.Save(); }+ }++ public static void Init(IApplicationPaths appPaths)+ {+ // Ugly alert (static init)++ _appPaths = appPaths;+ }++ public static async Task<MBRegistrationRecord> GetRegistrationStatus(IHttpClient httpClient, IJsonSerializer jsonSerializer, string feature, string mb2Equivalent = null)+ {+ var mac = GetMacAddress();+ var data = new Dictionary<string, string> {{"feature", feature}, {"key",SupporterKey}, {"mac",mac}, {"mb2equiv",mb2Equivalent}, {"legacykey", LegacyKey} };++ var reg = new RegRecord();+ try+ {+ using (var json = await httpClient.Post(MBValidateUrl, data, CancellationToken.None).ConfigureAwait(false))+ {+ reg = jsonSerializer.DeserializeFromStream<RegRecord>(json);+ }++ if (reg.registered)+ {+ LicenseFile.AddRegCheck(feature);+ }++ }+ catch (Exception)+ {+ //if we have trouble obtaining from web - allow it if we've validated in the past 30 days+ reg.registered = LicenseFile.LastChecked(feature) > DateTime.UtcNow.AddDays(-30);+ }++ return new MBRegistrationRecord {IsRegistered = reg.registered, ExpirationDate = reg.expDate, RegChecked = true};+ }++ /// <summary>+ /// Returns MAC Address from first Network Card in Computer+ /// </summary>+ /// <returns>[string] MAC Address</returns>+ public static string GetMacAddress()+ {+ var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");+ var moc = mc.GetInstances();+ var macAddress = String.Empty;+ foreach (ManagementObject mo in moc)+ {+ if (macAddress == String.Empty) // only return MAC Address from first card+ {+ try+ {+ if ((bool)mo["IPEnabled"]) macAddress = mo["MacAddress"].ToString();+ }+ catch+ {+ mo.Dispose();+ return "";+ }+ }+ mo.Dispose();+ }++ return macAddress.Replace(":", "");+ }+ }++ class RegRecord+ {+ public string featId { get; set; }+ public bool registered { get; set; }+ public DateTime expDate { get; set; }+ }+}diff --git a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.csindex c7c3b3a576..c71cf3802b 100644--- a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs+++ b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs@@ -2,8 +2,8 @@using MediaBrowser.Common.Configuration;using MediaBrowser.Common.Security;using MediaBrowser.Model.Serialization;+using Mediabrowser.Common.Implementations.Security;using Mediabrowser.Model.Entities;-using Mediabrowser.PluginSecurity;using MediaBrowser.Common.Net;using System;using System.Threading;