1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Added cacheing of the config.

Added file extensions to cdn url's to allow Azure to work properly.
Configured Uploads to use new CDN.
Added httpCompression to allow compression over proxy.
This commit is contained in:
Uncled1023 2016-10-07 16:49:46 -07:00
parent 4ec159e753
commit 8b22b22d23
8 changed files with 126 additions and 85 deletions

View File

@ -3,6 +3,7 @@ using System.Web.Mvc;
using System.Web.Optimization; using System.Web.Optimization;
using Teknik.Configuration; using Teknik.Configuration;
using Teknik.Controllers; using Teknik.Controllers;
using Teknik.Helpers;
namespace Teknik.Areas.Upload namespace Teknik.Areas.Upload
{ {
@ -61,18 +62,18 @@ namespace Teknik.Areas.Upload
); );
// Register Script Bundles // Register Script Bundles
BundleTable.Bundles.Add(new ScriptBundle("~/bundles/upload").Include( BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/upload", config.CdnHost).Include(
"~/Scripts/Dropzone/dropzone.js", "~/Scripts/Dropzone/dropzone.js",
"~/Areas/Upload/Scripts/Upload.js", "~/Areas/Upload/Scripts/Upload.js",
"~/Scripts/bootstrap-switch.js", "~/Scripts/bootstrap-switch.js",
"~/Scripts/bootbox/bootbox.min.js")); "~/Scripts/bootbox/bootbox.min.js"));
BundleTable.Bundles.Add(new ScriptBundle("~/bundles/download").Include( BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/download", config.CdnHost).Include(
"~/Scripts/Blob.js", "~/Scripts/Blob.js",
"~/Scripts/FileSaver.js", "~/Scripts/FileSaver.js",
"~/Areas/Upload/Scripts/Download.js")); "~/Areas/Upload/Scripts/Download.js"));
BundleTable.Bundles.Add(new ScriptBundle("~/bundles/cryptoWorker").Include( BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/cryptoWorker", config.CdnHost).Include(
"~/Areas/Upload/Scripts/EncryptionWorker.js")); "~/Areas/Upload/Scripts/EncryptionWorker.js"));
BundleTable.Bundles.Add(new ScriptBundle("~/bundles/crypto").Include( BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/crypto", config.CdnHost).Include(
"~/Scripts/Crypto-js/aes.js", "~/Scripts/Crypto-js/aes.js",
"~/Scripts/Crypto-js/enc-base64.js", "~/Scripts/Crypto-js/enc-base64.js",
"~/Scripts/Crypto-js/mode-ctr.js", "~/Scripts/Crypto-js/mode-ctr.js",
@ -80,7 +81,7 @@ namespace Teknik.Areas.Upload
"~/Scripts/Crypto-js/pad-nopadding.js")); "~/Scripts/Crypto-js/pad-nopadding.js"));
// Register Style Bundles // Register Style Bundles
BundleTable.Bundles.Add(new StyleBundle("~/Content/upload").Include( BundleTable.Bundles.Add(new CdnStyleBundle("~/Content/upload", config.CdnHost).Include(
"~/Content/dropzone.css", "~/Content/dropzone.css",
"~/Content/bootstrap-switch/bootstrap3/bootstrap-switch.css", "~/Content/bootstrap-switch/bootstrap3/bootstrap-switch.css",
"~/Areas/Upload/Content/Upload.css")); "~/Areas/Upload/Content/Upload.css"));

View File

@ -8,6 +8,9 @@ namespace Teknik.Configuration
{ {
public class Config public class Config
{ {
private static Config _Config { get; set; }
private static string _FileHash { get; set; }
private ReaderWriterLockSlim _ConfigRWLock; private ReaderWriterLockSlim _ConfigRWLock;
private ReaderWriterLockSlim _ConfigFileRWLock; private ReaderWriterLockSlim _ConfigFileRWLock;
private JsonSerializerSettings _JsonSettings; private JsonSerializerSettings _JsonSettings;
@ -152,7 +155,17 @@ namespace Teknik.Configuration
public static Config Load() public static Config Load()
{ {
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString(); string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
return Load(path); string newHash = string.Empty;
if (File.Exists(Path.Combine(path, "Config.json")))
{
newHash = Helpers.MD5.FileHash(Path.Combine(path, "Config.json"));
}
if (_Config == null || _FileHash == null || newHash != _FileHash)
{
_Config = Load(path);
_FileHash = newHash;
}
return _Config;
} }
public static Config Load(string path) public static Config Load(string path)

View File

@ -53,7 +53,6 @@ namespace Teknik
protected void Application_EndRequest(object sender, EventArgs e) protected void Application_EndRequest(object sender, EventArgs e)
{ {
Config config = Config.Load();
HttpContext context = HttpContext.Current; HttpContext context = HttpContext.Current;
// Set the generation time in the header // Set the generation time in the header

View File

@ -13,7 +13,7 @@ namespace Teknik.Helpers
public class CdnScriptBundle : Bundle public class CdnScriptBundle : Bundle
{ {
public CdnScriptBundle(string virtualPath, string cdnHost) public CdnScriptBundle(string virtualPath, string cdnHost)
: base(virtualPath, null, new IBundleTransform[] { new JsMinify(), new CdnBundleTransform { CdnHost = cdnHost } }) : base(virtualPath, null, new IBundleTransform[] { new JsMinify(), new CdnBundleTransform(cdnHost, ".js") })
{ {
ConcatenationToken = ";"; ConcatenationToken = ";";
} }
@ -22,7 +22,7 @@ namespace Teknik.Helpers
public class CdnStyleBundle : Bundle public class CdnStyleBundle : Bundle
{ {
public CdnStyleBundle(string virtualPath, string cdnHost) public CdnStyleBundle(string virtualPath, string cdnHost)
: base(virtualPath, null, new IBundleTransform[] { new CssMinify(), new CdnBundleTransform { CdnHost = cdnHost } }) : base(virtualPath, null, new IBundleTransform[] { new CssMinify(), new CdnBundleTransform(cdnHost, ".css") })
{ {
} }
} }
@ -31,13 +31,16 @@ namespace Teknik.Helpers
{ {
public string CdnHost { get; set; } public string CdnHost { get; set; }
static CdnBundleTransform() public string Ext { get; set; }
public CdnBundleTransform(string cdnHost, string ext)
{ {
CdnHost = cdnHost;
Ext = ext;
} }
public virtual void Process(BundleContext context, BundleResponse response) public virtual void Process(BundleContext context, BundleResponse response)
{ {
// Don't continue if we aren't using a CDN // Don't continue if we aren't using a CDN
if (!context.BundleCollection.UseCdn) if (!context.BundleCollection.UseCdn)
{ {
@ -47,6 +50,7 @@ namespace Teknik.Helpers
// Get the directory and filename for the bundle // Get the directory and filename for the bundle
var dir = VirtualPathUtility.GetDirectory(context.BundleVirtualPath).TrimStart('/').TrimStart('~').TrimStart('/').TrimEnd('/'); var dir = VirtualPathUtility.GetDirectory(context.BundleVirtualPath).TrimStart('/').TrimStart('~').TrimStart('/').TrimEnd('/');
var file = VirtualPathUtility.GetFileName(context.BundleVirtualPath); var file = VirtualPathUtility.GetFileName(context.BundleVirtualPath);
var group = string.Format("{0}{1}", file, Ext);
if (string.IsNullOrEmpty(CdnHost)) if (string.IsNullOrEmpty(CdnHost))
{ {
@ -56,7 +60,7 @@ namespace Teknik.Helpers
using (var hashAlgorithm = SHA256.CreateHashAlgorithm()) using (var hashAlgorithm = SHA256.CreateHashAlgorithm())
{ {
var hash = HttpServerUtility.UrlTokenEncode(hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(response.Content))); var hash = HttpServerUtility.UrlTokenEncode(hashAlgorithm.ComputeHash(Encoding.Unicode.GetBytes(response.Content)));
context.BundleCollection.GetBundleFor(context.BundleVirtualPath).CdnPath = string.Format("{0}/{1}/{2}?v={3}", CdnHost.TrimEnd('/'), dir, file, hash); context.BundleCollection.GetBundleFor(context.BundleVirtualPath).CdnPath = string.Format("{0}/{1}/{2}?v={3}&group={4}", CdnHost.TrimEnd('/'), dir, file, hash, group);
} }
} }
} }

View File

@ -39,6 +39,44 @@ namespace Teknik.Helpers
return sBuilder.ToString(); return sBuilder.ToString();
} }
public static string FileHash(string filename)
{
try
{
using (var md5 = System.Security.Cryptography.MD5.Create())
{
using (var stream = File.OpenRead(filename))
{
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
}
}
}
catch (Exception)
{
return string.Empty;
}
}
public static string DataHash(string data)
{
try
{
using (var md5 = System.Security.Cryptography.MD5.Create())
{
// convert string to stream
byte[] byteArray = Encoding.UTF8.GetBytes(data);
using (MemoryStream stream = new MemoryStream(byteArray))
{
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
}
}
}
catch (Exception)
{
return string.Empty;
}
}
} }
public class SHA384 public class SHA384
@ -55,6 +93,16 @@ namespace Teknik.Helpers
public class SHA256 public class SHA256
{ {
public static string Hash(string value)
{
byte[] valueBytes = Encoding.Unicode.GetBytes(value);
HashAlgorithm hash = new SHA256CryptoServiceProvider();
byte[] hashBytes = hash.ComputeHash(valueBytes);
return Convert.ToBase64String(hashBytes);
}
public static string Hash(string value, string salt1, string salt2) public static string Hash(string value, string salt1, string salt2)
{ {
SHA256Managed hash = new SHA256Managed(); SHA256Managed hash = new SHA256Managed();

View File

@ -78,22 +78,6 @@
<Private>True</Private> <Private>True</Private>
</Reference> </Reference>
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Data.Edm, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Data.Edm.5.6.4\lib\net40\Microsoft.Data.Edm.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Data.OData, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Data.OData.5.6.4\lib\net40\Microsoft.Data.OData.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Data.Services.Client, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Data.Services.Client.5.6.4\lib\net40\Microsoft.Data.Services.Client.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Storage, Version=7.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\WindowsAzure.Storage.7.2.1\lib\net40\Microsoft.WindowsAzure.Storage.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL"> <Reference Include="MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
<HintPath>..\packages\MySql.Data.6.9.9\lib\net45\MySql.Data.dll</HintPath> <HintPath>..\packages\MySql.Data.6.9.9\lib\net45\MySql.Data.dll</HintPath>
<Private>True</Private> <Private>True</Private>
@ -126,10 +110,6 @@
<Reference Include="System.Runtime.Serialization" /> <Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Security" /> <Reference Include="System.Security" />
<Reference Include="System.ServiceModel" /> <Reference Include="System.ServiceModel" />
<Reference Include="System.Spatial, Version=5.6.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\System.Spatial.5.6.4\lib\net40\System.Spatial.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.DynamicData" /> <Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" /> <Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" /> <Reference Include="System.Web.ApplicationServices" />

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0"?>
<!-- <!--
For more information on how to configure your ASP.NET application, please visit For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301880 http://go.microsoft.com/fwlink/?LinkId=301880
@ -46,6 +46,7 @@
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" cacheControlCustom="public"/> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" cacheControlCustom="public"/>
</staticContent> </staticContent>
<urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/> <urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/>
<httpCompression cacheControlHeader="max-age=86400" noCompressionForHttp10="false" noCompressionForProxies="false" sendCacheHeaders="true" />
<security> <security>
<requestFiltering> <requestFiltering>
<requestLimits maxAllowedContentLength="1073741824"/> <requestLimits maxAllowedContentLength="1073741824"/>

View File

@ -21,9 +21,6 @@
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net46" userInstalled="true" /> <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net46" userInstalled="true" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net46" userInstalled="true" /> <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net46" userInstalled="true" />
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net452" /> <package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Data.Edm" version="5.6.4" targetFramework="net452" />
<package id="Microsoft.Data.OData" version="5.6.4" targetFramework="net452" />
<package id="Microsoft.Data.Services.Client" version="5.6.4" targetFramework="net452" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net46" userInstalled="true" /> <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net46" userInstalled="true" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" userInstalled="true" /> <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" userInstalled="true" />
<package id="Modernizr" version="2.8.3" targetFramework="net452" userInstalled="true" /> <package id="Modernizr" version="2.8.3" targetFramework="net452" userInstalled="true" />
@ -33,8 +30,6 @@
<package id="Piwik.Tracker" version="2.16.0.0" targetFramework="net452" /> <package id="Piwik.Tracker" version="2.16.0.0" targetFramework="net452" />
<package id="QRCoder" version="1.2.2" targetFramework="net452" /> <package id="QRCoder" version="1.2.2" targetFramework="net452" />
<package id="Respond" version="1.4.2" targetFramework="net452" userInstalled="true" /> <package id="Respond" version="1.4.2" targetFramework="net452" userInstalled="true" />
<package id="System.Spatial" version="5.6.4" targetFramework="net452" />
<package id="TwoStepsAuthenticator" version="1.2.0" targetFramework="net452" /> <package id="TwoStepsAuthenticator" version="1.2.0" targetFramework="net452" />
<package id="WebGrease" version="1.6.0" targetFramework="net46" userInstalled="true" /> <package id="WebGrease" version="1.6.0" targetFramework="net46" userInstalled="true" />
<package id="WindowsAzure.Storage" version="7.2.1" targetFramework="net452" />
</packages> </packages>