SslConfigLoader.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.IO;
  2. using UnityEngine;
  3. namespace Mirror.SimpleWeb
  4. {
  5. internal class SslConfigLoader
  6. {
  7. internal struct Cert
  8. {
  9. public string path;
  10. public string password;
  11. }
  12. internal static SslConfig Load(SimpleWebTransport transport)
  13. {
  14. // don't need to load anything if ssl is not enabled
  15. if (!transport.sslEnabled)
  16. return default;
  17. string certJsonPath = transport.sslCertJson;
  18. Cert cert = LoadCertJson(certJsonPath);
  19. return new SslConfig(
  20. enabled: transport.sslEnabled,
  21. sslProtocols: transport.sslProtocols,
  22. certPath: cert.path,
  23. certPassword: cert.password
  24. );
  25. }
  26. internal static Cert LoadCertJson(string certJsonPath)
  27. {
  28. string json = File.ReadAllText(certJsonPath);
  29. Cert cert = JsonUtility.FromJson<Cert>(json);
  30. if (string.IsNullOrEmpty(cert.path))
  31. {
  32. throw new InvalidDataException("Cert Json didn't not contain \"path\"");
  33. }
  34. if (string.IsNullOrEmpty(cert.password))
  35. {
  36. // password can be empty
  37. cert.password = string.Empty;
  38. }
  39. return cert;
  40. }
  41. }
  42. }