Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

48 linhas
1.8 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using IronPython.Runtime;
  4. using Microsoft.Extensions.Logging;
  5. using MQTTnet.Protocol;
  6. using MQTTnet.Server.Scripting;
  7. namespace MQTTnet.Server.Mqtt
  8. {
  9. public class MqttConnectionValidator : IMqttServerConnectionValidator
  10. {
  11. private readonly PythonScriptHostService _pythonScriptHostService;
  12. private readonly ILogger _logger;
  13. public MqttConnectionValidator(PythonScriptHostService pythonScriptHostService, ILogger<MqttConnectionValidator> logger)
  14. {
  15. _pythonScriptHostService = pythonScriptHostService ?? throw new ArgumentNullException(nameof(pythonScriptHostService));
  16. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  17. }
  18. public Task ValidateConnectionAsync(MqttConnectionValidatorContext context)
  19. {
  20. try
  21. {
  22. var pythonContext = new PythonDictionary
  23. {
  24. { "client_id", context.ClientId },
  25. { "endpoint", context.Endpoint },
  26. { "is_secure_connection", context.IsSecureConnection },
  27. { "username", context.Username },
  28. { "password", context.Password },
  29. { "result", PythonConvert.Pythonfy(context.ReturnCode) }
  30. };
  31. _pythonScriptHostService.InvokeOptionalFunction("on_validate_client_connection", pythonContext);
  32. context.ReturnCode = PythonConvert.ParseEnum<MqttConnectReturnCode>((string)pythonContext["result"]);
  33. }
  34. catch (Exception exception)
  35. {
  36. _logger.LogError(exception, "Error while validating client connection.");
  37. }
  38. return Task.CompletedTask;
  39. }
  40. }
  41. }