Adds Network Resource Uploading for admins. (#6904)
Co-authored-by: Pieter-Jan Briers <pieterjan.briers@gmail.com> Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
committed by
GitHub
parent
5954b7668c
commit
eb54f4b224
@@ -1,11 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Administration.Managers;
|
||||
using Content.Shared.Administration;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
|
||||
90
Content.Server/Administration/NetworkResourceManager.cs
Normal file
90
Content.Server/Administration/NetworkResourceManager.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using Content.Server.Administration.Managers;
|
||||
using Content.Server.Database;
|
||||
using Content.Shared.Administration;
|
||||
using Content.Shared.CCVar;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Network;
|
||||
|
||||
namespace Content.Server.Administration;
|
||||
|
||||
public sealed class NetworkResourceManager : SharedNetworkResourceManager
|
||||
{
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IAdminManager _adminManager = default!;
|
||||
[Dependency] private readonly IServerNetManager _serverNetManager = default!;
|
||||
[Dependency] private readonly IConfigurationManager _cfgManager = default!;
|
||||
[Dependency] private readonly IServerDbManager _serverDb = default!;
|
||||
|
||||
[ViewVariables] public bool Enabled { get; private set; } = true;
|
||||
[ViewVariables] public float SizeLimit { get; private set; } = 0f;
|
||||
[ViewVariables] public bool StoreUploaded { get; set; } = true;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_serverNetManager.Connected += ServerNetManagerOnConnected;
|
||||
_cfgManager.OnValueChanged(CCVars.ResourceUploadingEnabled, value => Enabled = value, true);
|
||||
_cfgManager.OnValueChanged(CCVars.ResourceUploadingLimitMb, value => SizeLimit = value, true);
|
||||
_cfgManager.OnValueChanged(CCVars.ResourceUploadingStoreEnabled, value => StoreUploaded = value, true);
|
||||
|
||||
AutoDelete(_cfgManager.GetCVar(CCVars.ResourceUploadingStoreDeletionDays));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Callback for when a client attempts to upload a resource.
|
||||
/// </summary>
|
||||
/// <param name="msg"></param>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
protected override async void ResourceUploadMsg(NetworkResourceUploadMessage msg)
|
||||
{
|
||||
// Do not allow uploading any new resources if it has been disabled.
|
||||
// Note: Any resources uploaded before being disabled will still be kept and sent.
|
||||
if (!Enabled)
|
||||
return;
|
||||
|
||||
if (!_playerManager.TryGetSessionByChannel(msg.MsgChannel, out var session))
|
||||
return;
|
||||
|
||||
// +QUERY only for now.
|
||||
if (!_adminManager.HasAdminFlag(session, AdminFlags.Query))
|
||||
return;
|
||||
|
||||
// Ensure the data is under the current size limit, if it's currently enabled.
|
||||
if (SizeLimit > 0f && msg.Data.Length * BytesToMegabytes > SizeLimit)
|
||||
return;
|
||||
|
||||
ContentRoot.AddOrUpdateFile(msg.RelativePath, msg.Data);
|
||||
|
||||
// Now we broadcast the message!
|
||||
foreach (var channel in _serverNetManager.Channels)
|
||||
{
|
||||
channel.SendMessage(msg);
|
||||
}
|
||||
|
||||
if (!StoreUploaded)
|
||||
return;
|
||||
|
||||
await _serverDb.AddUploadedResourceLogAsync(session.UserId, DateTime.Now, msg.RelativePath.ToString(), msg.Data);
|
||||
}
|
||||
|
||||
private void ServerNetManagerOnConnected(object? sender, NetChannelArgs e)
|
||||
{
|
||||
foreach (var (path, data) in ContentRoot.GetAllFiles())
|
||||
{
|
||||
var msg = _serverNetManager.CreateNetMessage<NetworkResourceUploadMessage>();
|
||||
msg.RelativePath = path;
|
||||
msg.Data = data;
|
||||
e.Channel.SendMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
private async void AutoDelete(int days)
|
||||
{
|
||||
if (days <= 0)
|
||||
return; // auto-deletion disabled...
|
||||
|
||||
await _serverDb.PurgeUploadedResourceLogAsync(days);
|
||||
}
|
||||
}
|
||||
@@ -776,6 +776,34 @@ namespace Content.Server.Database
|
||||
|
||||
#endregion
|
||||
|
||||
#region Uploaded Resources Logs
|
||||
|
||||
public async Task AddUploadedResourceLogAsync(NetUserId user, DateTime date, string path, byte[] data)
|
||||
{
|
||||
await using var db = await GetDb();
|
||||
|
||||
db.DbContext.UploadedResourceLog.Add(new UploadedResourceLog() { UserId = user, Date = date, Path = path, Data = data });
|
||||
await db.DbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task PurgeUploadedResourceLogAsync(int days)
|
||||
{
|
||||
await using var db = await GetDb();
|
||||
|
||||
var date = DateTime.Now.Subtract(TimeSpan.FromDays(days));
|
||||
|
||||
await foreach (var log in db.DbContext.UploadedResourceLog
|
||||
.Where(l => date > l.Date)
|
||||
.AsAsyncEnumerable())
|
||||
{
|
||||
db.DbContext.UploadedResourceLog.Remove(log);
|
||||
}
|
||||
|
||||
await db.DbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
protected abstract Task<DbGuard> GetDb();
|
||||
|
||||
protected abstract class DbGuard : IAsyncDisposable
|
||||
|
||||
@@ -185,6 +185,14 @@ namespace Content.Server.Database
|
||||
Task RemoveFromWhitelistAsync(NetUserId player);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Uploaded Resources Logs
|
||||
|
||||
Task AddUploadedResourceLogAsync(NetUserId user, DateTime date, string path, byte[] data);
|
||||
|
||||
Task PurgeUploadedResourceLogAsync(int days);
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public sealed class ServerDbManager : IServerDbManager
|
||||
@@ -455,6 +463,16 @@ namespace Content.Server.Database
|
||||
return _db.RemoveFromWhitelistAsync(player);
|
||||
}
|
||||
|
||||
public Task AddUploadedResourceLogAsync(NetUserId user, DateTime date, string path, byte[] data)
|
||||
{
|
||||
return _db.AddUploadedResourceLogAsync(user, date, path, data);
|
||||
}
|
||||
|
||||
public Task PurgeUploadedResourceLogAsync(int days)
|
||||
{
|
||||
return _db.PurgeUploadedResourceLogAsync(days);
|
||||
}
|
||||
|
||||
private DbContextOptions<PostgresServerDbContext> CreatePostgresOptions()
|
||||
{
|
||||
var host = _cfg.GetCVar(CCVars.DatabasePgHost);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.IO;
|
||||
using Content.Server.Administration;
|
||||
using Content.Server.Administration.Managers;
|
||||
using Content.Server.Afk;
|
||||
using Content.Server.AI.Utility;
|
||||
@@ -86,6 +87,7 @@ namespace Content.Server.Entry
|
||||
IoCManager.Resolve<IServerPreferencesManager>().Init();
|
||||
IoCManager.Resolve<INodeGroupFactory>().Initialize();
|
||||
IoCManager.Resolve<IGamePrototypeLoadManager>().Initialize();
|
||||
IoCManager.Resolve<NetworkResourceManager>().Initialize();
|
||||
_voteManager.Initialize();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@ namespace Content.Server.IoC
|
||||
IoCManager.Register<IGamePrototypeLoadManager, GamePrototypeLoadManager>();
|
||||
IoCManager.Register<RulesManager, RulesManager>();
|
||||
IoCManager.Register<RoleBanManager, RoleBanManager>();
|
||||
IoCManager.Register<NetworkResourceManager>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user