-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement required interactions with beatmap mirrors #14
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
osu.Server.BeatmapSubmission/Models/Database/osu_mirror.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
// ReSharper disable InconsistentNaming | ||
|
||
namespace osu.Server.BeatmapSubmission.Models.Database | ||
{ | ||
public class osu_mirror | ||
{ | ||
public ushort mirror_id { get; set; } | ||
public string base_url { get; set; } = string.Empty; | ||
public decimal version { get; set; } | ||
public bool perform_updates { get; set; } | ||
public string secret_key { get; set; } = string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using MySqlConnector; | ||
|
||
namespace osu.Server.BeatmapSubmission.Services | ||
{ | ||
public interface IMirrorService | ||
{ | ||
Task PurgeBeatmapSetAsync(MySqlConnection db, uint beatmapSetId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using MySqlConnector; | ||
using osu.Framework.Extensions; | ||
using osu.Server.BeatmapSubmission.Models.Database; | ||
|
||
namespace osu.Server.BeatmapSubmission.Services | ||
{ | ||
public class MirrorService : IMirrorService | ||
{ | ||
private readonly HttpClient client; | ||
|
||
public MirrorService(HttpClient client) | ||
{ | ||
this.client = client; | ||
} | ||
|
||
public async Task PurgeBeatmapSetAsync(MySqlConnection db, uint beatmapSetId) | ||
{ | ||
osu_mirror[] mirrors = (await db.GetMirrorsRequiringUpdateAsync()).ToArray(); | ||
|
||
foreach (var mirror in mirrors) | ||
{ | ||
if (await performMirrorAction(mirror, "purge", new Dictionary<string, string> { ["s"] = beatmapSetId.ToString() }) != "1") | ||
await db.MarkPendingPurgeAsync(mirror, beatmapSetId); | ||
} | ||
} | ||
|
||
private async Task<string?> performMirrorAction(osu_mirror mirror, string action, Dictionary<string, string> data) | ||
{ | ||
data["ts"] = DateTimeOffset.Now.ToUnixTimeSeconds().ToString(); | ||
data["action"] = action; | ||
data["cs"] = ($"{data.GetValueOrDefault("s")}{data.GetValueOrDefault("fd")}{data.GetValueOrDefault("fs")}{data.GetValueOrDefault("ts")}" | ||
+ $"{data.GetValueOrDefault("nv")}{data.GetValueOrDefault("action")}{mirror.secret_key}").ComputeMD5Hash(); | ||
|
||
var request = new HttpRequestMessage(HttpMethod.Post, mirror.base_url); | ||
request.Content = new FormUrlEncodedContent(data); | ||
|
||
try | ||
{ | ||
var response = await client.SendAsync(request); | ||
return await response.Content.ReadAsStringAsync(); | ||
} | ||
catch (Exception) | ||
{ | ||
// TODO: log error | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
public class NoOpMirrorService : IMirrorService | ||
{ | ||
public Task PurgeBeatmapSetAsync(MySqlConnection db, uint beatmapSetId) => Task.CompletedTask; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would ask to look away for now, my next item on the list (one of the very few remaining, actually) is an extensive global pass on observability, reliability, and logging.