-
Notifications
You must be signed in to change notification settings - Fork 18
/
LicenseHandler.cs
387 lines (344 loc) · 12.4 KB
/
LicenseHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#region Copyright (c) 2011-2022 Technosoftware GmbH. All rights reserved
//-----------------------------------------------------------------------------
// Copyright (c) 2011-2022 Technosoftware GmbH. All rights reserved
// Web: https://technosoftware.com
//
// Purpose:
//
//
// The Software is subject to the Technosoftware GmbH Source Code License Agreement,
// which can be found here:
// https://technosoftware.com/documents/Source_License_Agreement.pdf
//-----------------------------------------------------------------------------
#endregion Copyright (c) 2011-2022 Technosoftware GmbH. All rights reserved
#region Using Directives
using System;
using System.Linq;
using static System.String;
using Technosoftware.DaAeHdaClient.Utilities;
#endregion
namespace Technosoftware.DaAeHdaClient
{
/// <summary>
/// Manages the license to enable the different product versions.
/// </summary>
public class LicenseHandler
{
#region Nested Enums
/// <summary>
/// The possible products.
/// </summary>
[Flags]
public enum ProductLicense : uint
{
/// <summary>
/// No product selected
/// </summary>
None = 0,
/// <summary>
/// OPC DA/AE/HDA Client .NET
/// </summary>
Client = 1,
/// <summary>
/// OPC DA/AE Server .NET
/// </summary>
Server = 2,
/// <summary>
/// Evaluation
/// </summary>
Evaluation = 4,
/// <summary>
/// Expired Evaluation or License
/// </summary>
Expired = 8,
}
/// <summary>
/// The possible products.
/// </summary>
[Flags]
public enum ProductFeature : uint
{
/// <summary>
/// Basic OPC Features enabled
/// </summary>
None = 0,
/// <summary>
/// OPC DataAccess enabled
/// </summary>
DataAccess = 1,
/// <summary>
/// OPC Alarms and Events enabled
/// </summary>
AlarmsConditions = 2,
/// <summary>
/// OPC Historical Dara Access enabled
/// </summary>
HistoricalAccess = 4,
/// <summary>
/// All supported OPC DA/AE/HDA Features enabled
/// </summary>
AllFeatures = uint.MaxValue,
}
#endregion
#region Constants
/// <summary>
/// License Validation Parameters String for the OPC UA Solution .NET
/// </summary>
private const string LicenseParameter =
@"";
#endregion
#region Internal Fields
internal static bool LicenseTraceDone;
#endregion
#region Properties
/// <summary>
/// This flag suppresses the conversion to local time done during marshalling.
/// </summary>
[Obsolete("Replaced by the ApplicationInstance.TimeAsUtc property")]
public bool TimeAsUtc
{
get => Com.Interop.PreserveUtc;
set => Com.Interop.PreserveUtc = value;
}
/// <summary>
/// Returns whether the product is a licensed product.
/// </summary>
/// <returns>Returns true if the product is licensed; false if it is used in evaluation mode or license is expired.</returns>
public static bool IsLicensed
{
get
{
if ((LicensedProduct == ProductLicense.None) ||
((LicensedProduct & ProductLicense.Expired) == ProductLicense.Expired) ||
((LicensedProduct & ProductLicense.Evaluation) == ProductLicense.Evaluation))
{
return false;
}
return true;
}
}
/// <summary>
/// Returns whether the product is an evaluation version.
/// </summary>
/// <returns>Returns true if the product is an evaluation; false if it is a product or license is expired.</returns>
public static bool IsEvaluation
{
get
{
if ((LicensedProduct & ProductLicense.Evaluation) == ProductLicense.Evaluation)
{
return true;
}
return false;
}
}
/// <summary>
/// Indicates whether the evaluation period and a restart is required or not.
/// </summary>
public static bool IsExpired
{
get
{
if ((LicensedProduct & ProductLicense.Expired) == ProductLicense.Expired)
{
return true;
}
return false;
}
}
/// <summary>
/// Returns the Version of the product.
/// </summary>
public static string Version
{
get
{
var version = (typeof(LicenseHandler).Assembly.GetName().Version);
var major = version.Major;
var minor = version.Minor;
var build = version.Build;
var revision = version.Revision;
var versionString = revision == 0 ? $"{major}.{minor} ( Build {build} )" : $"{major}.{minor} ( Build {build}.{revision} )";
return versionString;
}
}
/// <summary>
/// Returns the licensed products.
/// </summary>
public static ProductLicense LicensedProduct { get; set; } = ProductLicense.Client;
/// <summary>
/// Returns the licensed OPC UA Features.
/// </summary>
public static ProductFeature LicensedFeatures { get; set; } = ProductFeature.AllFeatures;
/// <summary>
/// Returns the licensed product name.
/// </summary>
public static string Product
{
get
{
var product = "Expired Evaluation or License";
if ((LicensedProduct & ProductLicense.Expired) == ProductLicense.Expired)
{
// It's an expired evaluation or license
if (((LicensedProduct & ProductLicense.Client) == ProductLicense.Client) &&
((LicensedProduct & ProductLicense.Server) == ProductLicense.Server))
{
product = "Expired OPC DA/AE/HDA Bundle .NET license";
}
else if ((LicensedProduct & ProductLicense.Client) == ProductLicense.Client)
{
product = "Expired OPC DA/AE/HDA Client .NET license";
}
else if ((LicensedProduct & ProductLicense.Server) == ProductLicense.Server)
{
product = "Expired OPC DA/HDA Server .NET license";
}
else if ((LicensedProduct & ProductLicense.Evaluation) == ProductLicense.Evaluation)
{
product = "Expired OPC DA/AE/HDA Bundle .NET Evaluation";
}
return product;
}
// It's a license or evaluation
if (((LicensedProduct & ProductLicense.Client) == ProductLicense.Client) &&
((LicensedProduct & ProductLicense.Server) == ProductLicense.Server))
{
product = "OPC UA Bundle .NET";
}
else if ((LicensedProduct & ProductLicense.Client) == ProductLicense.Client)
{
product = "OPC UA Client .NET";
}
else if ((LicensedProduct & ProductLicense.Server) == ProductLicense.Server)
{
product = "OPC UA Server .NET";
}
else if ((LicensedProduct & ProductLicense.Evaluation) == ProductLicense.Evaluation)
{
product = "OPC UA Bundle .NET Evaluation";
}
return product;
}
}
/// <summary>
/// Returns the product information.
/// </summary>
public static string ProductInformation
{
get
{
if (IsLicensed)
{
return Product;
}
if (!Check())
{
return Product + " EVALUATION EXPIRED !!!";
}
return Product + " EVALUATION";
}
}
internal static bool Checked { get; private set; }
#endregion
#region Public Methods
/// <summary>
/// Validate the license.
/// </summary>
/// <param name="serialNumber">Serial Number</param>
public static bool Validate(string serialNumber)
{
return CheckLicense(serialNumber);
}
#endregion
#region Protected Methods
/// <summary>
/// Validate the license.
/// </summary>
/// <param name="serialNumber">Serial Number</param>
protected static bool CheckLicense(string serialNumber)
{
var check = false;
check = CheckLicenseClient(serialNumber);
CheckProductFeature(serialNumber);
if (check)
{
return true;
}
return false;
}
/// <summary>
/// Check if the licensed product provided through ValidateLicense qualifies for the given application type and license edition.
/// </summary>
/// <returns>True if the license qualifies for the requested application and edition or if the evaluation period is still running; otherwise False.</returns>
protected static bool CheckLicense()
{
if (Checked && LicensedProduct == ProductLicense.None)
{
return false;
}
if (Checked &&
((LicensedProduct & ProductLicense.Client) == ProductLicense.Client))
{
return true;
}
CheckProductFeature("");
return Check();
}
#endregion
#region Internal Methods
/// <summary>
/// Core Feature validation
/// </summary>
/// <returns>True if valid; false otherwise</returns>
/// <exception cref="BadInternalErrorException"></exception>
internal static void ValidateFeatures(ProductFeature requiredProductFeature = ProductFeature.None, bool silent = false)
{
var valid = CheckLicense();
if (!LicenseTraceDone)
{
LicenseTraceDone = true;
Utils.Trace("Used Product = {0}, Features = {1}, Version = {2}.", Product, LicensedFeatures, Version);
}
if (!valid && !IsLicensed && !silent)
{
throw new BadInternalErrorException("Evaluation time expired! You need to restart the application.");
}
if (!valid && !silent)
{
throw new BadInternalErrorException("License required! You can't use this feature.");
}
if (requiredProductFeature != ProductFeature.None && LicensedFeatures != ProductFeature.AllFeatures)
{
if (((requiredProductFeature & LicensedFeatures) != requiredProductFeature) && !silent)
{
var message =
$"Feature {requiredProductFeature} required but only {LicensedFeatures} licensed! You can't use this feature.";
throw new BadInternalErrorException(message);
}
}
}
#endregion
#region Private Methods
/// <summary>
/// Validate the license.
/// </summary>
/// <param name="licenseKey">The license key</param>
protected static void CheckProductFeature(string licenseKey)
{
}
/// <summary>
/// Validate the license.
/// </summary>
/// <param name="licenseKey">The license key</param>
protected static bool CheckLicenseClient(string licenseKey)
{
return true;
}
internal static bool Check()
{
return true;
}
#endregion
}
}