|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using UnityEngine; |
| 4 | + |
| 5 | +namespace Unity.Netcode.Transports.UTP |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Component to add to a NetworkManager if you want the certificates to be loaded from files. |
| 9 | + /// Mostly helpful to ease development and testing, especially with self-signed certificates |
| 10 | + /// |
| 11 | + /// Shipping code should make the calls to |
| 12 | + /// - SetServerSecrets |
| 13 | + /// - SetClientSecrets |
| 14 | + /// directly, instead of relying on this. |
| 15 | + /// </summary> |
| 16 | + public class SecretsLoaderHelper : MonoBehaviour |
| 17 | + { |
| 18 | + internal struct ServerSecrets |
| 19 | + { |
| 20 | + public string ServerPrivate; |
| 21 | + public string ServerCertificate; |
| 22 | + }; |
| 23 | + |
| 24 | + internal struct ClientSecrets |
| 25 | + { |
| 26 | + public string ServerCommonName; |
| 27 | + public string ClientCertificate; |
| 28 | + }; |
| 29 | + |
| 30 | + private void Awake() |
| 31 | + { |
| 32 | + var serverSecrets = new ServerSecrets(); |
| 33 | + |
| 34 | + try |
| 35 | + { |
| 36 | + serverSecrets.ServerCertificate = ServerCertificate; |
| 37 | + } |
| 38 | + catch (Exception exception) |
| 39 | + { |
| 40 | + Debug.Log(exception); |
| 41 | + } |
| 42 | + |
| 43 | + try |
| 44 | + { |
| 45 | + serverSecrets.ServerPrivate = ServerPrivate; |
| 46 | + } |
| 47 | + catch (Exception exception) |
| 48 | + { |
| 49 | + Debug.Log(exception); |
| 50 | + } |
| 51 | + |
| 52 | + var clientSecrets = new ClientSecrets(); |
| 53 | + try |
| 54 | + { |
| 55 | + clientSecrets.ClientCertificate = ClientCA; |
| 56 | + } |
| 57 | + catch (Exception exception) |
| 58 | + { |
| 59 | + Debug.Log(exception); |
| 60 | + } |
| 61 | + |
| 62 | + try |
| 63 | + { |
| 64 | + clientSecrets.ServerCommonName = ServerCommonName; |
| 65 | + } |
| 66 | + catch (Exception exception) |
| 67 | + { |
| 68 | + Debug.Log(exception); |
| 69 | + } |
| 70 | + |
| 71 | + var unityTransportComponent = GetComponent<UnityTransport>(); |
| 72 | + |
| 73 | + if (unityTransportComponent == null) |
| 74 | + { |
| 75 | + Debug.LogError($"You need to select the UnityTransport protocol, in the NetworkManager, in order for the SecretsLoaderHelper component to be useful."); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + unityTransportComponent.SetServerSecrets(serverSecrets.ServerCertificate, serverSecrets.ServerPrivate); |
| 80 | + unityTransportComponent.SetClientSecrets(clientSecrets.ServerCommonName, clientSecrets.ClientCertificate); |
| 81 | + } |
| 82 | + |
| 83 | + [Tooltip("Hostname")] |
| 84 | + [SerializeField] |
| 85 | + private string m_ServerCommonName = "localhost"; |
| 86 | + public string ServerCommonName |
| 87 | + { |
| 88 | + get => m_ServerCommonName; |
| 89 | + set => m_ServerCommonName = value; |
| 90 | + } |
| 91 | + |
| 92 | + [Tooltip("Client CA filepath. Useful with self-signed certificates")] |
| 93 | + [SerializeField] |
| 94 | + private string m_ClientCAFilePath = "Assets/Secure/myGameClientCA.pem"; |
| 95 | + public string ClientCAFilePath |
| 96 | + { |
| 97 | + get => m_ClientCAFilePath; |
| 98 | + set => m_ClientCAFilePath = value; |
| 99 | + } |
| 100 | + |
| 101 | + [Tooltip("Client CA Override. Only useful for development with self-signed certificates. Certificate content, for platforms that lack file access (WebGL)")] |
| 102 | + [SerializeField] |
| 103 | + private string m_ClientCAOverride = ""; |
| 104 | + public string ClientCAOverride |
| 105 | + { |
| 106 | + get => m_ClientCAOverride; |
| 107 | + set => m_ClientCAOverride = value; |
| 108 | + } |
| 109 | + |
| 110 | + [Tooltip("Server Certificate filepath")] |
| 111 | + [SerializeField] |
| 112 | + private string m_ServerCertificateFilePath = "Assets/Secure/myGameServerCertificate.pem"; |
| 113 | + public string ServerCertificateFilePath |
| 114 | + { |
| 115 | + get => m_ServerCertificateFilePath; |
| 116 | + set => m_ServerCertificateFilePath = value; |
| 117 | + } |
| 118 | + [Tooltip("Server Private Keyfilepath")] |
| 119 | + [SerializeField] |
| 120 | + private string m_ServerPrivateFilePath = "Assets/Secure/myGameServerPrivate.pem"; |
| 121 | + public string ServerPrivateFilePath |
| 122 | + { |
| 123 | + get => m_ServerPrivateFilePath; |
| 124 | + set => m_ServerPrivate = value; |
| 125 | + } |
| 126 | + |
| 127 | + private string m_ClientCA; |
| 128 | + public string ClientCA |
| 129 | + { |
| 130 | + get |
| 131 | + { |
| 132 | + if (m_ClientCAOverride != "") |
| 133 | + { |
| 134 | + return m_ClientCAOverride; |
| 135 | + } |
| 136 | + return ReadFile(m_ClientCAFilePath, "Client Certificate"); |
| 137 | + } |
| 138 | + set => m_ClientCA = value; |
| 139 | + } |
| 140 | + private string m_ServerCertificate; |
| 141 | + public string ServerCertificate |
| 142 | + { |
| 143 | + get => ReadFile(m_ServerCertificateFilePath, "Server Certificate"); |
| 144 | + set => m_ServerCertificate = value; |
| 145 | + } |
| 146 | + private string m_ServerPrivate; |
| 147 | + public string ServerPrivate |
| 148 | + { |
| 149 | + get => ReadFile(m_ServerPrivateFilePath, "Server Key"); |
| 150 | + set => m_ServerPrivate = value; |
| 151 | + } |
| 152 | + |
| 153 | + private static string ReadFile(string path, string label) |
| 154 | + { |
| 155 | + var reader = new StreamReader(path); |
| 156 | + string fileContent = reader.ReadToEnd(); |
| 157 | + Debug.Log((fileContent.Length > 1) ? ("Successfully loaded " + fileContent.Length + " byte(s) from " + label) : ("Could not read " + label + " file")); |
| 158 | + return fileContent; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments