private void CreateLicenseListeningSockets() { if (_initialized) { return; } _initialized = true; if (string.IsNullOrEmpty(_userName)) { _userName = Environment.UserName; } if (string.IsNullOrEmpty(_machineId)) { _machineId = _identificationService.GetMachineId(); } Log.Debug("Creating local license service and registering license sockets on local network"); var ipAddresses = GetIpAddresses(); foreach (var ipAddress in ipAddresses) { var thread = new Thread(HandleIncomingRequests); thread.IsBackground = true; thread.Start(ipAddress); _listeningThreads.Add(thread); } }
public IValidationContext Validate(string machineIdToValidate) { Argument.IsNotNullOrWhitespace(() => machineIdToValidate); var validationContext = new ValidationContext(); Log.Debug("Retrieving machine id"); var machineId = _identificationService.GetMachineId(); Log.Debug("Validating machine id '{0}' against expected machine id '{1}'", machineId, machineIdToValidate); var machineSplitted = machineId.Split(new[] { LicenseElements.IdentificationSeparator }, StringSplitOptions.None); var expectedSplitter = machineIdToValidate.Split(new[] { LicenseElements.IdentificationSeparator }, StringSplitOptions.None); if (machineSplitted.Length != expectedSplitter.Length) { var error = "The number of items inside the license differ too much, assuming machine ids do not match"; Log.Error(error); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(error)); return(validationContext); } var invalidEntries = 0; for (var i = 0; i < machineSplitted.Length; i++) { if (!string.Equals(expectedSplitter[i], machineSplitted[i], StringComparison.OrdinalIgnoreCase)) { invalidEntries++; } } if (invalidEntries > Threshold) { var error = string.Format("{0} values are not equal, not accepting the machine id, maximum threshold is '{1}'", invalidEntries, Threshold); Log.Error(error); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(error)); return(validationContext); } if (invalidEntries > 0) { var warning = string.Format("One of the values is not equal, but we have a threshold of {0} so accepting machine id", Threshold); Log.Warning(warning); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(warning)); } return(validationContext); }
/// <summary> /// Validates the license on the server. /// </summary> /// <param name="license">The license.</param> /// <param name="serverUrl">The server URL.</param> /// <param name="assembly">The assembly to get the information from. If <c>null</c>, the entry assembly will be used.</param> /// <returns><c>true</c> if the license is valid, <c>false</c> otherwise.</returns> public LicenseValidationResult ValidateLicenseOnServer(string license, string serverUrl, Assembly assembly = null) { Argument.IsNotNullOrWhitespace(() => license); Argument.IsNotNullOrWhitespace(() => serverUrl); if (assembly == null) { assembly = AssemblyHelper.GetEntryAssembly(); } LicenseValidationResult validationResult = null; try { var webRequest = WebRequest.Create(serverUrl); webRequest.ContentType = "application/json"; webRequest.Method = "POST"; using (var sw = new StreamWriter(webRequest.GetRequestStream())) { var version = "unknown version"; if (assembly != null) { try { version = assembly.InformationalVersion(); if (string.IsNullOrWhiteSpace(version)) { version = assembly.Version(); } } catch (Exception ex) { Log.Error(ex, "Failed to retrieve the product version"); } } var serviceLicenseValidation = new ServerLicenseValidation { MachineId = _identificationService.GetMachineId(), ProductName = (assembly != null) ? assembly.Product() : "unknown product", ProductVersion = version, License = license }; var json = JsonConvert.SerializeObject(serviceLicenseValidation); sw.Write(json); } using (var httpWebResponse = webRequest.GetResponse()) { using (var responseStream = httpWebResponse.GetResponseStream()) { using (var streamReader = new StreamReader(responseStream)) { var json = streamReader.ReadToEnd(); validationResult = JsonConvert.DeserializeObject <LicenseValidationResult>(json); } } } } catch (Exception ex) { Log.Error(ex, "Failed to validate the license on the server"); } if (validationResult == null) { validationResult = new LicenseValidationResult() { // We return success if we can't validate on the server, then it's up to the client to validate IsValid = true, AdditionalInfo = "Failed to check the license on the server" }; } return(validationResult); }