Use a proper user agent when fetching images

This commit is contained in:
Ske 2021-08-23 22:53:58 +02:00
parent 55c56c4e58
commit 41427db178
10 changed files with 106 additions and 87 deletions

View File

@ -12,7 +12,7 @@ namespace Myriad.Rest
{
public class DiscordApiClient
{
private const string UserAgent = "DiscordBot (https://github.com/xSke/PluralKit/tree/main/Myriad/, vMyriad)";
public const string UserAgent = "PluralKit (https://github.com/xSke/PluralKit/tree/main/Myriad/, v1)";
private readonly BaseRestClient _client;
public DiscordApiClient(string token, ILogger logger)

View File

@ -180,11 +180,21 @@ namespace PluralKit.Bot
await using var serviceScope = _services.BeginLifetimeScope();
// Find an event handler that can handle the type of event (<T>) we're given
var handler = serviceScope.Resolve<IEventHandler<T>>();
var queue = serviceScope.ResolveOptional<HandlerQueue<T>>();
IEventHandler<T> handler;
try
{
handler = serviceScope.Resolve<IEventHandler<T>>();
}
catch (Exception e)
{
_logger.Error(e, "Error instantiating handler class");
return;
}
try
{
var queue = serviceScope.ResolveOptional<HandlerQueue<T>>();
using var _ = LogContext.PushProperty("EventId", Guid.NewGuid());
using var __ = LogContext.Push(serviceScope.Resolve<SerilogGatewayEnricherFactory>().GetEnricher(shard, evt));
_logger.Verbose("Received gateway event: {@Event}", evt);

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@ -22,12 +23,14 @@ namespace PluralKit.Bot
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly EmbedService _embeds;
private readonly HttpClient _client;
public Groups(IDatabase db, ModelRepository repo, EmbedService embeds)
public Groups(IDatabase db, ModelRepository repo, EmbedService embeds, HttpClient client)
{
_db = db;
_repo = repo;
_embeds = embeds;
_client = client;
}
public async Task CreateGroup(Context ctx)
@ -200,7 +203,7 @@ namespace PluralKit.Bot
{
ctx.CheckOwnGroup(target);
await AvatarUtils.VerifyAvatarOrThrow(img.Url);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url);
await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {Icon = img.Url}));
@ -262,7 +265,7 @@ namespace PluralKit.Bot
{
ctx.CheckOwnGroup(target);
await AvatarUtils.VerifyAvatarOrThrow(img.Url, isFullSizeImage: true);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, isFullSizeImage: true);
await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {BannerImage = img.Url}));

View File

@ -22,15 +22,17 @@ namespace PluralKit.Bot
public class ImportExport
{
private readonly DataFileService _dataFiles;
private readonly HttpClient _client;
private readonly JsonSerializerSettings _settings = new()
{
// Otherwise it'll mess up/reformat the ISO strings for ???some??? reason >.>
DateParseHandling = DateParseHandling.None
};
public ImportExport(DataFileService dataFiles)
public ImportExport(DataFileService dataFiles, HttpClient client)
{
_dataFiles = dataFiles;
_client = client;
}
public async Task Import(Context ctx)
@ -39,13 +41,11 @@ namespace PluralKit.Bot
if (url == null) throw Errors.NoImportFilePassed;
await ctx.BusyIndicator(async () =>
{
using (var client = new HttpClient())
{
HttpResponseMessage response;
try
{
response = await client.GetAsync(url);
response = await _client.GetAsync(url);
}
catch (InvalidOperationException)
{
@ -91,7 +91,6 @@ namespace PluralKit.Bot
// We already had a system, so show them what changed
await ctx.Reply($"{Emojis.Success} Updated {result.ModifiedNames.Count} members, created {result.AddedNames.Count} members. Type `pk;system list` to check!");
}
}
});
}

View File

@ -62,7 +62,7 @@ namespace PluralKit.Bot
if (avatarArg != null)
{
try {
await AvatarUtils.VerifyAvatarOrThrow(avatarArg.Url);
await AvatarUtils.VerifyAvatarOrThrow(_client, avatarArg.Url);
await _db.Execute(conn => _repo.UpdateMember(conn, member.Id, new MemberPatch { AvatarUrl = avatarArg.Url }));
} catch (Exception e) {
imageMatchError = e;

View File

@ -1,5 +1,6 @@
#nullable enable
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Myriad.Builders;
@ -12,11 +13,13 @@ namespace PluralKit.Bot
{
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly HttpClient _client;
public MemberAvatar(IDatabase db, ModelRepository repo)
public MemberAvatar(IDatabase db, ModelRepository repo, HttpClient client)
{
_db = db;
_repo = repo;
_client = client;
}
private async Task AvatarClear(AvatarLocation location, Context ctx, PKMember target, MemberGuildSettings? mgs)
@ -102,7 +105,7 @@ namespace PluralKit.Bot
}
ctx.CheckSystem().CheckOwnMember(target);
await AvatarUtils.VerifyAvatarOrThrow(avatarArg.Value.Url);
await AvatarUtils.VerifyAvatarOrThrow(_client, avatarArg.Value.Url);
await UpdateAvatar(location, ctx, target, avatarArg.Value.Url);
await PrintResponse(location, ctx, target, avatarArg.Value, guildData);
}

View File

@ -1,6 +1,7 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System;
using System.Net.Http;
using Myriad.Builders;
@ -14,11 +15,13 @@ namespace PluralKit.Bot
{
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly HttpClient _client;
public MemberEdit(IDatabase db, ModelRepository repo)
public MemberEdit(IDatabase db, ModelRepository repo, HttpClient client)
{
_db = db;
_repo = repo;
_client = client;
}
public async Task Name(Context ctx, PKMember target)
@ -148,7 +151,7 @@ namespace PluralKit.Bot
async Task SetBannerImage(ParsedImage img)
{
await AvatarUtils.VerifyAvatarOrThrow(img.Url, isFullSizeImage: true);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, isFullSizeImage: true);
await _db.Execute(c => _repo.UpdateMember(c, target.Id, new MemberPatch {BannerImage = img.Url}));

View File

@ -1,10 +1,10 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Myriad.Builders;
using Myriad.Types;
using NodaTime;
using NodaTime.Text;
@ -18,11 +18,13 @@ namespace PluralKit.Bot
{
private readonly IDatabase _db;
private readonly ModelRepository _repo;
private readonly HttpClient _client;
public SystemEdit(IDatabase db, ModelRepository repo)
public SystemEdit(IDatabase db, ModelRepository repo, HttpClient client)
{
_db = db;
_repo = repo;
_client = client;
}
public async Task Name(Context ctx)
@ -279,7 +281,7 @@ namespace PluralKit.Bot
async Task SetIcon(ParsedImage img)
{
await AvatarUtils.VerifyAvatarOrThrow(img.Url);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url);
await _db.Execute(c => _repo.UpdateSystem(c, ctx.System.Id, new SystemPatch {AvatarUrl = img.Url}));
@ -332,7 +334,7 @@ namespace PluralKit.Bot
async Task SetImage(ParsedImage img)
{
await AvatarUtils.VerifyAvatarOrThrow(img.Url, isFullSizeImage: true);
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, isFullSizeImage: true);
await _db.Execute(c => _repo.UpdateSystem(c, ctx.System.Id, new SystemPatch {BannerImage = img.Url}));

View File

@ -5,6 +5,7 @@ using Autofac;
using Myriad.Cache;
using Myriad.Gateway;
using Myriad.Rest;
using NodaTime;
@ -113,7 +114,8 @@ namespace PluralKit.Bot
// Utils
builder.Register(c => new HttpClient
{
Timeout = TimeSpan.FromSeconds(5)
Timeout = TimeSpan.FromSeconds(5),
DefaultRequestHeaders = {{"User-Agent", DiscordApiClient.UserAgent}}
}).AsSelf().SingleInstance();
builder.RegisterInstance(SystemClock.Instance).As<IClock>();
builder.RegisterType<SerilogGatewayEnricherFactory>().AsSelf().SingleInstance();

View File

@ -10,7 +10,7 @@ using SixLabors.ImageSharp;
namespace PluralKit.Bot {
public static class AvatarUtils {
public static async Task VerifyAvatarOrThrow(string url, bool isFullSizeImage = false)
public static async Task VerifyAvatarOrThrow(HttpClient client, string url, bool isFullSizeImage = false)
{
if (url.Length > Limits.MaxUriLength)
throw Errors.UrlTooLong(url);
@ -24,8 +24,6 @@ namespace PluralKit.Bot {
// TODO: add image/webp once ImageSharp supports this
};
using (var client = new HttpClient())
{
if (!PluralKit.Core.MiscUtils.TryMatchUri(url, out var uri))
throw Errors.InvalidUrl(url);
@ -53,7 +51,6 @@ namespace PluralKit.Bot {
if (image.Width > Limits.AvatarDimensionLimit || image.Height > Limits.AvatarDimensionLimit) // Check image size
throw Errors.AvatarDimensionsTooLarge(image.Width, image.Height);
}
}
// Rewrite cdn.discordapp.com URLs to media.discordapp.net for jpg/png files
// This lets us add resizing parameters to "borrow" their media proxy server to downsize the image