-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from bdach/mirrors
Implement required interactions with beatmap mirrors
- Loading branch information
Showing
7 changed files
with
116 additions
and
1 deletion.
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; | ||
} | ||
} |