Skip to content

Commit 38d1a26

Browse files
committed
Merge branch 'v2.0' of https://github.com/coderrapp/codeRR.Server into v2.0
2 parents cb352b9 + 4be0d71 commit 38d1a26

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;
@@ -23,6 +24,8 @@ namespace Coderr.Server.Web.Controllers
2324
public class ReportReceiverController : Controller
2425
{
2526
private const int CompressedReportSizeLimit = 1000000;
27+
28+
private static int _currentReportCount;
2629
private readonly ConfigurationStore _configStore;
2730
private readonly ILog _logger = LogManager.GetLogger(typeof(ReportReceiverController));
2831
private readonly IMessageQueue _messageQueue;
@@ -53,6 +56,8 @@ public async Task<IActionResult> Post(string appKey, string sig)
5356
if (contentLength == null || contentLength < 1)
5457
return BadRequest("Content required.");
5558

59+
if (!IsBelowReportLimit()) return NoContent();
60+
5661
try
5762
{
5863
var buffer = new byte[contentLength.Value];
@@ -85,7 +90,8 @@ public async Task<IActionResult> Post(string appKey, string sig)
8590
}
8691
catch (Exception exception)
8792
{
88-
_logger.Error("Failed to handle request from " + appKey + " / " + Request.HttpContext.Connection.RemoteIpAddress,
93+
_logger.Error(
94+
"Failed to handle request from " + appKey + " / " + Request.HttpContext.Connection.RemoteIpAddress,
8995
exception);
9096
return new ContentResult
9197
{
@@ -105,13 +111,37 @@ internal static ClaimsPrincipal CreateReporterPrincipal()
105111
}));
106112
return principal;
107113
}
114+
115+
private bool IsBelowReportLimit()
116+
{
117+
if (!string.IsNullOrEmpty(License.LicensedTo))
118+
return true;
119+
120+
var count = License.Count / 5;
121+
count = count / 10;
122+
123+
if (_currentReportCount == 0)
124+
using (var cmd = _unitOfWork.CreateDbCommand())
125+
{
126+
var from = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
127+
cmd.CommandText =
128+
"SELECT count(*) FROM ErrorReports WHERE CreatedAtUtc >= @from AND CreatedAtUtc <= @to";
129+
cmd.AddParameter("from", from);
130+
cmd.AddParameter("to", DateTime.Now);
131+
_currentReportCount = (int) cmd.ExecuteScalar();
132+
}
133+
else
134+
_currentReportCount++;
135+
136+
return _currentReportCount < count;
137+
}
138+
108139
private Task<IActionResult> KillLargeReportAsync(string appKey)
109140
{
110-
_logger.Error(appKey + "Too large report: " + Request.ContentLength+ " from " +
141+
_logger.Error(appKey + "Too large report: " + Request.ContentLength + " from " +
111142
Request.HttpContext.Connection.RemoteIpAddress);
112143
//TODO: notify
113144
return Task.FromResult<IActionResult>(NoContent());
114145
}
115-
116146
}
117-
}
147+
}
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)