Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 29 additions & 4 deletions src/RustServerMetrics/MetricsLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -631,13 +645,14 @@ private void LoadConfiguration()
{
var configStr = File.ReadAllText(ConfigurationPath);
Configuration = JsonConvert.DeserializeObject<ConfigData>(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))
{
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/RustServerMetrics/ReportUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down