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 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; private readonly BaseRestClient _client;
public DiscordApiClient(string token, ILogger logger) public DiscordApiClient(string token, ILogger logger)

View File

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

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -22,12 +23,14 @@ namespace PluralKit.Bot
private readonly IDatabase _db; private readonly IDatabase _db;
private readonly ModelRepository _repo; private readonly ModelRepository _repo;
private readonly EmbedService _embeds; 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; _db = db;
_repo = repo; _repo = repo;
_embeds = embeds; _embeds = embeds;
_client = client;
} }
public async Task CreateGroup(Context ctx) public async Task CreateGroup(Context ctx)
@ -200,7 +203,7 @@ namespace PluralKit.Bot
{ {
ctx.CheckOwnGroup(target); 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})); await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {Icon = img.Url}));
@ -262,7 +265,7 @@ namespace PluralKit.Bot
{ {
ctx.CheckOwnGroup(target); 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})); 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 public class ImportExport
{ {
private readonly DataFileService _dataFiles; private readonly DataFileService _dataFiles;
private readonly HttpClient _client;
private readonly JsonSerializerSettings _settings = new() private readonly JsonSerializerSettings _settings = new()
{ {
// Otherwise it'll mess up/reformat the ISO strings for ???some??? reason >.> // Otherwise it'll mess up/reformat the ISO strings for ???some??? reason >.>
DateParseHandling = DateParseHandling.None DateParseHandling = DateParseHandling.None
}; };
public ImportExport(DataFileService dataFiles) public ImportExport(DataFileService dataFiles, HttpClient client)
{ {
_dataFiles = dataFiles; _dataFiles = dataFiles;
_client = client;
} }
public async Task Import(Context ctx) public async Task Import(Context ctx)
@ -40,57 +42,54 @@ namespace PluralKit.Bot
await ctx.BusyIndicator(async () => await ctx.BusyIndicator(async () =>
{ {
using (var client = new HttpClient()) HttpResponseMessage response;
try
{ {
HttpResponseMessage response; response = await _client.GetAsync(url);
try }
{ catch (InvalidOperationException)
response = await client.GetAsync(url); {
} // Invalid URL throws this, we just error back out
catch (InvalidOperationException) throw Errors.InvalidImportFile;
{ }
// Invalid URL throws this, we just error back out
throw Errors.InvalidImportFile;
}
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
throw Errors.InvalidImportFile; throw Errors.InvalidImportFile;
DataFileSystem data; DataFileSystem data;
try try
{ {
var json = JsonConvert.DeserializeObject<JObject>(await response.Content.ReadAsStringAsync(), _settings); var json = JsonConvert.DeserializeObject<JObject>(await response.Content.ReadAsStringAsync(), _settings);
data = await LoadSystem(ctx, json); data = await LoadSystem(ctx, json);
} }
catch (JsonException) catch (JsonException)
{ {
throw Errors.InvalidImportFile; throw Errors.InvalidImportFile;
} }
if (!data.Valid) if (!data.Valid)
throw Errors.InvalidImportFile; throw Errors.InvalidImportFile;
if (data.LinkedAccounts != null && !data.LinkedAccounts.Contains(ctx.Author.Id)) if (data.LinkedAccounts != null && !data.LinkedAccounts.Contains(ctx.Author.Id))
{ {
var msg = $"{Emojis.Warn} You seem to importing a system profile belonging to another account. Are you sure you want to proceed?"; var msg = $"{Emojis.Warn} You seem to importing a system profile belonging to another account. Are you sure you want to proceed?";
if (!await ctx.PromptYesNo(msg, "Import")) throw Errors.ImportCancelled; if (!await ctx.PromptYesNo(msg, "Import")) throw Errors.ImportCancelled;
} }
// If passed system is null, it'll create a new one // If passed system is null, it'll create a new one
// (and that's okay!) // (and that's okay!)
var result = await _dataFiles.ImportSystem(data, ctx.System, ctx.Author.Id); var result = await _dataFiles.ImportSystem(data, ctx.System, ctx.Author.Id);
if (!result.Success) if (!result.Success)
await ctx.Reply($"{Emojis.Error} The provided system profile could not be imported. {result.Message}"); await ctx.Reply($"{Emojis.Error} The provided system profile could not be imported. {result.Message}");
else if (ctx.System == null) else if (ctx.System == null)
{ {
// We didn't have a system prior to importing, so give them the new system's ID // We didn't have a system prior to importing, so give them the new system's ID
await ctx.Reply($"{Emojis.Success} PluralKit has created a system for you based on the given file. Your system ID is `{result.System.Hid}`. Type `pk;system` for more information."); await ctx.Reply($"{Emojis.Success} PluralKit has created a system for you based on the given file. Your system ID is `{result.System.Hid}`. Type `pk;system` for more information.");
} }
else else
{ {
// We already had a system, so show them what changed // 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!"); 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) if (avatarArg != null)
{ {
try { 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 })); await _db.Execute(conn => _repo.UpdateMember(conn, member.Id, new MemberPatch { AvatarUrl = avatarArg.Url }));
} catch (Exception e) { } catch (Exception e) {
imageMatchError = e; imageMatchError = e;

View File

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

View File

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

View File

@ -1,10 +1,10 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using Myriad.Builders; using Myriad.Builders;
using Myriad.Types;
using NodaTime; using NodaTime;
using NodaTime.Text; using NodaTime.Text;
@ -18,11 +18,13 @@ namespace PluralKit.Bot
{ {
private readonly IDatabase _db; private readonly IDatabase _db;
private readonly ModelRepository _repo; private readonly ModelRepository _repo;
private readonly HttpClient _client;
public SystemEdit(IDatabase db, ModelRepository repo) public SystemEdit(IDatabase db, ModelRepository repo, HttpClient client)
{ {
_db = db; _db = db;
_repo = repo; _repo = repo;
_client = client;
} }
public async Task Name(Context ctx) public async Task Name(Context ctx)
@ -279,7 +281,7 @@ namespace PluralKit.Bot
async Task SetIcon(ParsedImage img) 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})); 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) 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})); 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.Cache;
using Myriad.Gateway; using Myriad.Gateway;
using Myriad.Rest;
using NodaTime; using NodaTime;
@ -113,7 +114,8 @@ namespace PluralKit.Bot
// Utils // Utils
builder.Register(c => new HttpClient builder.Register(c => new HttpClient
{ {
Timeout = TimeSpan.FromSeconds(5) Timeout = TimeSpan.FromSeconds(5),
DefaultRequestHeaders = {{"User-Agent", DiscordApiClient.UserAgent}}
}).AsSelf().SingleInstance(); }).AsSelf().SingleInstance();
builder.RegisterInstance(SystemClock.Instance).As<IClock>(); builder.RegisterInstance(SystemClock.Instance).As<IClock>();
builder.RegisterType<SerilogGatewayEnricherFactory>().AsSelf().SingleInstance(); builder.RegisterType<SerilogGatewayEnricherFactory>().AsSelf().SingleInstance();

View File

@ -10,7 +10,7 @@ using SixLabors.ImageSharp;
namespace PluralKit.Bot { namespace PluralKit.Bot {
public static class AvatarUtils { 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) if (url.Length > Limits.MaxUriLength)
throw Errors.UrlTooLong(url); throw Errors.UrlTooLong(url);
@ -24,35 +24,32 @@ namespace PluralKit.Bot {
// TODO: add image/webp once ImageSharp supports this // 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);
if (!PluralKit.Core.MiscUtils.TryMatchUri(url, out var uri))
throw Errors.InvalidUrl(url);
url = TryRewriteCdnUrl(url); url = TryRewriteCdnUrl(url);
var response = await client.GetAsync(url); var response = await client.GetAsync(url);
if (!response.IsSuccessStatusCode) // Check status code if (!response.IsSuccessStatusCode) // Check status code
throw Errors.AvatarServerError(response.StatusCode); throw Errors.AvatarServerError(response.StatusCode);
if (response.Content.Headers.ContentLength == null) // Check presence of content length if (response.Content.Headers.ContentLength == null) // Check presence of content length
throw Errors.AvatarNotAnImage(null); throw Errors.AvatarNotAnImage(null);
if (!acceptableMimeTypes.Contains(response.Content.Headers.ContentType.MediaType)) // Check MIME type if (!acceptableMimeTypes.Contains(response.Content.Headers.ContentType.MediaType)) // Check MIME type
throw Errors.AvatarNotAnImage(response.Content.Headers.ContentType.MediaType); throw Errors.AvatarNotAnImage(response.Content.Headers.ContentType.MediaType);
if (isFullSizeImage) if (isFullSizeImage)
// no need to do size checking on banners // no need to do size checking on banners
return; return;
if (response.Content.Headers.ContentLength > Limits.AvatarFileSizeLimit) // Check content length if (response.Content.Headers.ContentLength > Limits.AvatarFileSizeLimit) // Check content length
throw Errors.AvatarFileSizeLimit(response.Content.Headers.ContentLength.Value); throw Errors.AvatarFileSizeLimit(response.Content.Headers.ContentLength.Value);
// Parse the image header in a worker // Parse the image header in a worker
var stream = await response.Content.ReadAsStreamAsync(); var stream = await response.Content.ReadAsStreamAsync();
var image = await Task.Run(() => Image.Identify(stream)); var image = await Task.Run(() => Image.Identify(stream));
if (image == null) throw Errors.AvatarInvalid; if (image == null) throw Errors.AvatarInvalid;
if (image.Width > Limits.AvatarDimensionLimit || image.Height > Limits.AvatarDimensionLimit) // Check image size if (image.Width > Limits.AvatarDimensionLimit || image.Height > Limits.AvatarDimensionLimit) // Check image size
throw Errors.AvatarDimensionsTooLarge(image.Width, image.Height); throw Errors.AvatarDimensionsTooLarge(image.Width, image.Height);
}
} }
// Rewrite cdn.discordapp.com URLs to media.discordapp.net for jpg/png files // Rewrite cdn.discordapp.com URLs to media.discordapp.net for jpg/png files