diff --git a/README.md b/README.md index febf7bf..c060882 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ The username of a user created in step 5. ### Influx Database Password The password for the user created in step 5. +Credentials are sent via HTTP Basic authentication (`Authorization` header), not as URL query parameters, so they are not written to reverse-proxy or CDN access logs. + ### Server Tag This is a static tag that is added to all records submitted by your rust server to the database, this tag should be different for each rust server diff --git a/src/RustServerMetrics/MetricsLogger.cs b/src/RustServerMetrics/MetricsLogger.cs index c2faa92..cf48807 100644 --- a/src/RustServerMetrics/MetricsLogger.cs +++ b/src/RustServerMetrics/MetricsLogger.cs @@ -61,6 +61,7 @@ public NetworkUpdateData(int count, long bytes) internal ConfigData Configuration { get; private set; } private Uri _baseUri; + private string _authorizationHeader; private readonly int _performanceReportRequestId = UnityEngine.Random.Range(-2147483648, 2147483647); private ReportUploader _reportUploader; private Message.Type _lastMessageType; @@ -77,12 +78,25 @@ public Uri BaseUri return _baseUri; } - _baseUri = new Uri(new Uri(Configuration.DatabaseUrl), - $"/write?db={Configuration.DatabaseName}&precision=ms&u={Configuration.DatabaseUser}&p={Configuration.DatabasePassword}"); + EnsureInfluxEndpoint(); return _baseUri; } } + internal string AuthorizationHeader + { + get + { + if (_authorizationHeader != null) + { + return _authorizationHeader; + } + + EnsureInfluxEndpoint(); + return _authorizationHeader; + } + } + #region Initialization internal static void Initialize() @@ -631,13 +645,14 @@ private void LoadConfiguration() { var configStr = File.ReadAllText(ConfigurationPath); Configuration = JsonConvert.DeserializeObject(configStr) ?? new ConfigData(); - var uri = new Uri(Configuration.DatabaseUrl); - _baseUri = new Uri(uri, $"/write?db={Configuration.DatabaseName}&precision=ms&u={Configuration.DatabaseUser}&p={Configuration.DatabasePassword}"); + EnsureInfluxEndpoint(); } catch { Debug.LogError("[ServerMetrics]: The configuration seems to be missing or malformed. Defaults will be loaded."); Configuration = new ConfigData(); + _baseUri = null; + _authorizationHeader = null; if (File.Exists(ConfigurationPath)) { @@ -647,6 +662,16 @@ private void LoadConfiguration() SaveConfiguration(); } + private void EnsureInfluxEndpoint() + { + var uri = new Uri(Configuration.DatabaseUrl); + _baseUri = new Uri(uri, $"/write?db={Configuration.DatabaseName}&precision=ms"); + + var credentials = Convert.ToBase64String( + Encoding.UTF8.GetBytes($"{Configuration.DatabaseUser}:{Configuration.DatabasePassword}")); + _authorizationHeader = $"Basic {credentials}"; + } + private void SaveConfiguration() { try diff --git a/src/RustServerMetrics/ReportUploader.cs b/src/RustServerMetrics/ReportUploader.cs index 677cbe2..0313a1f 100644 --- a/src/RustServerMetrics/ReportUploader.cs +++ b/src/RustServerMetrics/ReportUploader.cs @@ -120,6 +120,11 @@ private IEnumerator SendRequest() useHttpContinue = true, redirectLimit = 5 }; + var authorizationHeader = _metricsLogger.AuthorizationHeader; + if (!string.IsNullOrEmpty(authorizationHeader)) + { + request.SetRequestHeader("Authorization", authorizationHeader); + } yield return request.SendWebRequest(); if (request.isNetworkError)