2021-02-08 22:52:35 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
2021-11-11 05:04:17 +00:00
|
|
|
using System.Collections.Generic;
|
2022-04-21 06:16:39 +00:00
|
|
|
using System.Net;
|
2022-12-14 14:01:05 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2021-02-08 22:52:35 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using osu.Framework;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
|
|
|
namespace osu.Game.Online
|
|
|
|
{
|
2022-11-02 02:43:22 +00:00
|
|
|
public class HubClientConnector : PersistentEndpointClientConnector, IHubClientConnector
|
2021-02-08 22:52:35 +00:00
|
|
|
{
|
2022-05-26 09:03:41 +00:00
|
|
|
public const string SERVER_SHUTDOWN_MESSAGE = "Server is shutting down.";
|
|
|
|
|
2024-07-17 14:21:33 +00:00
|
|
|
public const string VERSION_HASH_HEADER = @"X-Osu-Version-Hash";
|
Send client-generated session GUID for identification purposes
This is the first half of a change that *may* fix
https://github.com/ppy/osu/issues/26338 (it definitely fixes *one case*
where the issue happens, but I'm not sure if it will cover all of them).
As described in the issue thread, using the `jti` claim from the JWT
used for authorisation seemed like a decent idea. However, upon closer
inspection the scheme falls over badly in a specific scenario where:
1. A client instance connects to spectator server using JWT A.
2. At some point, JWT A expires, and is silently rotated by the game in
exchange for JWT B.
The spectator server knows nothing of this, and continues to only
track JWT A, including the old `jti` claim in said JWT.
3. At some later point, the client's connection to one of the spectator
server hubs drops out. A reconnection is automatically attempted,
*but* it is attempted using JWT B.
The spectator server was not aware of JWT B until now, and said JWT
has a different `jti` claim than the old one, so to the spectator
server, it looks like a completely different client connecting, which
boots the user out of their account.
This PR adds a per-session GUID which is sent in a HTTP header on every
connection attempt to spectator server. This GUID will be used instead
of the `jti` claim in JWTs as a persistent identifier of a single user's
single lazer session, which bypasses the failure scenario described
above.
I don't think any stronger primitive than this is required. As far as I
can tell this is as strong a protection as the JWT was (which is to say,
not *very* strong), and doing this removes a lot of weird complexity
that would be otherwise incurred by attempting to have client ferry all
of its newly issued JWTs to the server so that it can be aware of them.
2024-07-17 12:11:35 +00:00
|
|
|
public const string CLIENT_SESSION_ID_HEADER = @"X-Client-Session-ID";
|
|
|
|
|
2021-02-08 22:52:35 +00:00
|
|
|
/// <summary>
|
2021-02-11 09:39:06 +00:00
|
|
|
/// Invoked whenever a new hub connection is built, to configure it before it's started.
|
2021-02-08 22:52:35 +00:00
|
|
|
/// </summary>
|
2021-02-15 07:31:00 +00:00
|
|
|
public Action<HubConnection>? ConfigureConnection { get; set; }
|
2021-02-08 22:52:35 +00:00
|
|
|
|
|
|
|
private readonly string endpoint;
|
2021-02-14 14:31:57 +00:00
|
|
|
private readonly string versionHash;
|
2021-08-02 05:44:51 +00:00
|
|
|
private readonly bool preferMessagePack;
|
2021-02-08 22:52:35 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The current connection opened by this connector.
|
|
|
|
/// </summary>
|
2022-10-27 05:34:24 +00:00
|
|
|
public new HubConnection? CurrentConnection => ((HubClient?)base.CurrentConnection)?.Connection;
|
2021-02-08 22:52:35 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new <see cref="HubClientConnector"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="clientName">The name of the client this connector connects for, used for logging.</param>
|
|
|
|
/// <param name="endpoint">The endpoint to the hub.</param>
|
2021-02-11 09:32:54 +00:00
|
|
|
/// <param name="api"> An API provider used to react to connection state changes.</param>
|
2021-02-14 14:31:57 +00:00
|
|
|
/// <param name="versionHash">The hash representing the current game version, used for verification purposes.</param>
|
2021-08-02 05:44:51 +00:00
|
|
|
/// <param name="preferMessagePack">Whether to use MessagePack for serialisation if available on this platform.</param>
|
|
|
|
public HubClientConnector(string clientName, string endpoint, IAPIProvider api, string versionHash, bool preferMessagePack = true)
|
2022-10-27 05:34:24 +00:00
|
|
|
: base(api)
|
2021-02-08 22:52:35 +00:00
|
|
|
{
|
2022-10-27 05:34:24 +00:00
|
|
|
ClientName = clientName;
|
2021-02-08 22:52:35 +00:00
|
|
|
this.endpoint = endpoint;
|
2021-02-14 14:31:57 +00:00
|
|
|
this.versionHash = versionHash;
|
2021-08-02 05:44:51 +00:00
|
|
|
this.preferMessagePack = preferMessagePack;
|
2022-11-04 10:36:24 +00:00
|
|
|
|
|
|
|
// Automatically start these connections.
|
|
|
|
Start();
|
2021-07-28 11:46:02 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 02:43:22 +00:00
|
|
|
protected override Task<PersistentEndpointClient> BuildConnectionAsync(CancellationToken cancellationToken)
|
2021-02-08 22:52:35 +00:00
|
|
|
{
|
|
|
|
var builder = new HubConnectionBuilder()
|
2021-02-14 14:31:57 +00:00
|
|
|
.WithUrl(endpoint, options =>
|
|
|
|
{
|
2022-12-11 22:30:49 +00:00
|
|
|
// Configuring proxies is not supported on iOS, see https://github.com/xamarin/xamarin-macios/issues/14632.
|
|
|
|
if (RuntimeInfo.OS != RuntimeInfo.Platform.iOS)
|
|
|
|
{
|
|
|
|
// Use HttpClient.DefaultProxy once on net6 everywhere.
|
|
|
|
// The credential setter can also be removed at this point.
|
|
|
|
options.Proxy = WebRequest.DefaultWebProxy;
|
|
|
|
if (options.Proxy != null)
|
|
|
|
options.Proxy.Credentials = CredentialCache.DefaultCredentials;
|
|
|
|
}
|
2022-04-21 06:16:39 +00:00
|
|
|
|
Send client-generated session GUID for identification purposes
This is the first half of a change that *may* fix
https://github.com/ppy/osu/issues/26338 (it definitely fixes *one case*
where the issue happens, but I'm not sure if it will cover all of them).
As described in the issue thread, using the `jti` claim from the JWT
used for authorisation seemed like a decent idea. However, upon closer
inspection the scheme falls over badly in a specific scenario where:
1. A client instance connects to spectator server using JWT A.
2. At some point, JWT A expires, and is silently rotated by the game in
exchange for JWT B.
The spectator server knows nothing of this, and continues to only
track JWT A, including the old `jti` claim in said JWT.
3. At some later point, the client's connection to one of the spectator
server hubs drops out. A reconnection is automatically attempted,
*but* it is attempted using JWT B.
The spectator server was not aware of JWT B until now, and said JWT
has a different `jti` claim than the old one, so to the spectator
server, it looks like a completely different client connecting, which
boots the user out of their account.
This PR adds a per-session GUID which is sent in a HTTP header on every
connection attempt to spectator server. This GUID will be used instead
of the `jti` claim in JWTs as a persistent identifier of a single user's
single lazer session, which bypasses the failure scenario described
above.
I don't think any stronger primitive than this is required. As far as I
can tell this is as strong a protection as the JWT was (which is to say,
not *very* strong), and doing this removes a lot of weird complexity
that would be otherwise incurred by attempting to have client ferry all
of its newly issued JWTs to the server so that it can be aware of them.
2024-07-17 12:11:35 +00:00
|
|
|
options.Headers.Add(@"Authorization", @$"Bearer {API.AccessToken}");
|
2024-07-17 14:21:33 +00:00
|
|
|
// non-standard header name kept for backwards compatibility, can be removed after server side has migrated to `VERSION_HASH_HEADER`
|
|
|
|
options.Headers.Add(@"OsuVersionHash", versionHash);
|
Send client-generated session GUID for identification purposes
This is the first half of a change that *may* fix
https://github.com/ppy/osu/issues/26338 (it definitely fixes *one case*
where the issue happens, but I'm not sure if it will cover all of them).
As described in the issue thread, using the `jti` claim from the JWT
used for authorisation seemed like a decent idea. However, upon closer
inspection the scheme falls over badly in a specific scenario where:
1. A client instance connects to spectator server using JWT A.
2. At some point, JWT A expires, and is silently rotated by the game in
exchange for JWT B.
The spectator server knows nothing of this, and continues to only
track JWT A, including the old `jti` claim in said JWT.
3. At some later point, the client's connection to one of the spectator
server hubs drops out. A reconnection is automatically attempted,
*but* it is attempted using JWT B.
The spectator server was not aware of JWT B until now, and said JWT
has a different `jti` claim than the old one, so to the spectator
server, it looks like a completely different client connecting, which
boots the user out of their account.
This PR adds a per-session GUID which is sent in a HTTP header on every
connection attempt to spectator server. This GUID will be used instead
of the `jti` claim in JWTs as a persistent identifier of a single user's
single lazer session, which bypasses the failure scenario described
above.
I don't think any stronger primitive than this is required. As far as I
can tell this is as strong a protection as the JWT was (which is to say,
not *very* strong), and doing this removes a lot of weird complexity
that would be otherwise incurred by attempting to have client ferry all
of its newly issued JWTs to the server so that it can be aware of them.
2024-07-17 12:11:35 +00:00
|
|
|
options.Headers.Add(VERSION_HASH_HEADER, versionHash);
|
|
|
|
options.Headers.Add(CLIENT_SESSION_ID_HEADER, API.SessionIdentifier.ToString());
|
2021-02-14 14:31:57 +00:00
|
|
|
});
|
2021-02-08 22:52:35 +00:00
|
|
|
|
2022-12-14 14:01:05 +00:00
|
|
|
if (RuntimeFeature.IsDynamicCodeCompiled && preferMessagePack)
|
2021-08-19 08:41:50 +00:00
|
|
|
{
|
|
|
|
builder.AddMessagePackProtocol(options =>
|
|
|
|
{
|
|
|
|
options.SerializerOptions = SignalRUnionWorkaroundResolver.OPTIONS;
|
|
|
|
});
|
|
|
|
}
|
2021-02-08 22:52:35 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// eventually we will precompile resolvers for messagepack, but this isn't working currently
|
|
|
|
// see https://github.com/neuecc/MessagePack-CSharp/issues/780#issuecomment-768794308.
|
2021-08-02 05:37:47 +00:00
|
|
|
builder.AddNewtonsoftJsonProtocol(options =>
|
|
|
|
{
|
|
|
|
options.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
2021-11-11 05:04:17 +00:00
|
|
|
options.PayloadSerializerSettings.Converters = new List<JsonConverter>
|
|
|
|
{
|
|
|
|
new SignalRDerivedTypeWorkaroundJsonConverter(),
|
|
|
|
};
|
2021-08-02 05:37:47 +00:00
|
|
|
});
|
2021-02-08 22:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var newConnection = builder.Build();
|
|
|
|
|
2021-02-11 07:49:16 +00:00
|
|
|
ConfigureConnection?.Invoke(newConnection);
|
2021-02-08 22:52:35 +00:00
|
|
|
|
2022-11-02 02:43:22 +00:00
|
|
|
return Task.FromResult((PersistentEndpointClient)new HubClient(newConnection));
|
2021-02-11 07:49:16 +00:00
|
|
|
}
|
2021-02-08 22:52:35 +00:00
|
|
|
|
2023-11-21 05:39:33 +00:00
|
|
|
async Task IHubClientConnector.Disconnect()
|
|
|
|
{
|
|
|
|
await Disconnect().ConfigureAwait(false);
|
|
|
|
API.Logout();
|
|
|
|
}
|
2023-11-13 05:35:07 +00:00
|
|
|
|
2022-10-27 05:34:24 +00:00
|
|
|
protected override string ClientName { get; }
|
2021-02-08 22:52:35 +00:00
|
|
|
}
|
|
|
|
}
|