Skip to content

Commit 4be0d71

Browse files
committed
first commit
1 parent 228cffd commit 4be0d71

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

src/Server/Coderr.Server.Web/Coderr.Server.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<PackageReference Include="Griffin.Framework" Version="2.0.0-alpha01" />
2525
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.0.3" />
2626
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.3" />
27+
<PackageReference Include="Portable.Licensing" Version="1.1.0" />
2728
</ItemGroup>
2829

2930
<ItemGroup>

src/Server/Coderr.Server.Web/Controllers/ReportReceiverController.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Coderr.Server.App.Core.Reports.Config;
1010
using Coderr.Server.ReportAnalyzer.Inbound;
1111
using Coderr.Server.Web.Infrastructure;
12+
using Coderr.Server.Web.Infrastructure.Misc;
1213
using Coderr.Server.Web.Infrastructure.Results;
1314
using DotNetCqs.Queues;
1415
using Griffin.Data;
@@ -21,6 +22,8 @@ namespace Coderr.Server.Web.Controllers
2122
public class ReportReceiverController : Controller
2223
{
2324
private const int CompressedReportSizeLimit = 1000000;
25+
26+
private static int _currentReportCount;
2427
private readonly ConfigurationStore _configStore;
2528
private readonly ILog _logger = LogManager.GetLogger(typeof(ReportReceiverController));
2629
private readonly IMessageQueue _messageQueue;
@@ -51,6 +54,8 @@ public async Task<IActionResult> Post(string appKey, string sig)
5154
if (contentLength == null || contentLength < 1)
5255
return BadRequest("Content required.");
5356

57+
if (!IsBelowReportLimit()) return NoContent();
58+
5459
try
5560
{
5661
var buffer = new byte[contentLength.Value];
@@ -83,7 +88,8 @@ public async Task<IActionResult> Post(string appKey, string sig)
8388
}
8489
catch (Exception exception)
8590
{
86-
_logger.Error("Failed to handle request from " + appKey + " / " + Request.HttpContext.Connection.RemoteIpAddress,
91+
_logger.Error(
92+
"Failed to handle request from " + appKey + " / " + Request.HttpContext.Connection.RemoteIpAddress,
8793
exception);
8894
return new ContentResult
8995
{
@@ -103,13 +109,37 @@ internal static ClaimsPrincipal CreateReporterPrincipal()
103109
}));
104110
return principal;
105111
}
112+
113+
private bool IsBelowReportLimit()
114+
{
115+
if (!string.IsNullOrEmpty(License.LicensedTo))
116+
return true;
117+
118+
var count = License.Count / 5;
119+
count = count / 10;
120+
121+
if (_currentReportCount == 0)
122+
using (var cmd = _unitOfWork.CreateDbCommand())
123+
{
124+
var from = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
125+
cmd.CommandText =
126+
"SELECT count(*) FROM ErrorReports WHERE CreatedAtUtc >= @from AND CreatedAtUtc <= @to";
127+
cmd.AddParameter("from", from);
128+
cmd.AddParameter("to", DateTime.Now);
129+
_currentReportCount = (int) cmd.ExecuteScalar();
130+
}
131+
else
132+
_currentReportCount++;
133+
134+
return _currentReportCount < count;
135+
}
136+
106137
private Task<IActionResult> KillLargeReportAsync(string appKey)
107138
{
108-
_logger.Error(appKey + "Too large report: " + Request.ContentLength+ " from " +
139+
_logger.Error(appKey + "Too large report: " + Request.ContentLength + " from " +
109140
Request.HttpContext.Connection.RemoteIpAddress);
110141
//TODO: notify
111142
return Task.FromResult<IActionResult>(NoContent());
112143
}
113-
114144
}
115-
}
145+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Security;
6+
using System.Threading.Tasks;
7+
using Portable.Licensing;
8+
using Portable.Licensing.Validation;
9+
10+
namespace Coderr.Server.Web.Infrastructure.Misc
11+
{
12+
public class License
13+
{
14+
private static SecureString _publicKey;
15+
public static int Count = 25000;
16+
private static Portable.Licensing.License _license;
17+
18+
private static readonly GeneralValidationFailure UnlicensedApplications = new GeneralValidationFailure
19+
{
20+
HowToResolve = "Delete unlicensed applications or buy additional applications",
21+
Message = "You have created more applications than your license allows."
22+
};
23+
24+
static License()
25+
{
26+
var file = Path.Combine(Environment.CurrentDirectory, "coderr.lic");
27+
_license = Portable.Licensing.License.Load(file);
28+
29+
ValidationErrors = _license.Validate()
30+
.ExpirationDate()
31+
.And()
32+
.Signature(_publicKey.ToString())
33+
.And()
34+
.AssertThat(x=>x.Quantity <= CreatedApplicationCount, UnlicensedApplications)
35+
.AssertValidLicense();
36+
}
37+
38+
public static int CreatedApplicationCount { get; set; }
39+
40+
public static IEnumerable<IValidationFailure> ValidationErrors { get; private set; }
41+
42+
public static int ApplicationCount => _license.Quantity;
43+
44+
public static string LicensedTo => _license.Customer.Company;
45+
46+
public static DateTime Expires => _license.Expiration;
47+
48+
49+
}
50+
}

src/Server/Coderr.Server.Web/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Reflection;
4+
using System.Security.Cryptography;
45
using Coderr.Server.SqlServer.ReportAnalyzer;
56
using Griffin.Data.Mapper;
67
using log4net;

0 commit comments

Comments
 (0)