This commit is contained in:
Łukasz Domeradzki
2025-05-02 12:56:54 +02:00
parent af6f9466a8
commit f62da0e273
5 changed files with 163 additions and 158 deletions

View File

@@ -135,12 +135,6 @@ internal static class Commands {
mobileAuthenticator.Init(bot);
MobileAuthenticatorHandler? mobileAuthenticatorHandler = bot.GetHandler<MobileAuthenticatorHandler>();
if (mobileAuthenticatorHandler == null) {
throw new InvalidOperationException(nameof(mobileAuthenticatorHandler));
}
ulong steamTime = await mobileAuthenticator.GetSteamTime().ConfigureAwait(false);
string? code = mobileAuthenticator.GenerateTokenForTime(steamTime);
@@ -149,10 +143,10 @@ internal static class Commands {
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(mobileAuthenticator.GenerateTokenForTime)));
}
CTwoFactor_FinalizeAddAuthenticator_Response? response = await mobileAuthenticatorHandler.FinalizeAuthenticator(bot.SteamID, activationCode, code, steamTime).ConfigureAwait(false);
CTwoFactor_FinalizeAddAuthenticator_Response? response = await MobileAuthenticatorWebHandler.FinalizeAuthenticator(bot, activationCode, code, steamTime).ConfigureAwait(false);
if (response == null) {
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(mobileAuthenticatorHandler.FinalizeAuthenticator)));
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(MobileAuthenticatorWebHandler.FinalizeAuthenticator)));
}
if (!response.success) {
@@ -319,18 +313,12 @@ internal static class Commands {
return bot.Commands.FormatBotResponse(Strings.BotNotConnected);
}
MobileAuthenticatorHandler? mobileAuthenticatorHandler = bot.GetHandler<MobileAuthenticatorHandler>();
if (mobileAuthenticatorHandler == null) {
throw new InvalidOperationException(nameof(mobileAuthenticatorHandler));
}
string deviceID = $"android:{Guid.NewGuid()}";
CTwoFactor_AddAuthenticator_Response? response = await mobileAuthenticatorHandler.AddAuthenticator(bot.SteamID, deviceID).ConfigureAwait(false);
CTwoFactor_AddAuthenticator_Response? response = await MobileAuthenticatorWebHandler.AddAuthenticator(bot, deviceID).ConfigureAwait(false);
if (response == null) {
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(mobileAuthenticatorHandler.AddAuthenticator)));
return bot.Commands.FormatBotResponse(Strings.FormatWarningFailedWithError(nameof(MobileAuthenticatorWebHandler.AddAuthenticator)));
}
EResult result = (EResult) response.status;

View File

@@ -1,120 +0,0 @@
// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2025 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.NLog;
using SteamKit2;
using SteamKit2.Internal;
namespace ArchiSteamFarm.OfficialPlugins.MobileAuthenticator;
internal sealed class MobileAuthenticatorHandler : ClientMsgHandler {
private readonly ArchiLogger ArchiLogger;
private readonly TwoFactor UnifiedTwoFactorService;
internal MobileAuthenticatorHandler(ArchiLogger archiLogger, SteamUnifiedMessages steamUnifiedMessages) {
ArgumentNullException.ThrowIfNull(archiLogger);
ArgumentNullException.ThrowIfNull(steamUnifiedMessages);
ArchiLogger = archiLogger;
UnifiedTwoFactorService = steamUnifiedMessages.CreateService<TwoFactor>();
}
public override void HandleMsg(IPacketMsg packetMsg) => ArgumentNullException.ThrowIfNull(packetMsg);
internal async Task<CTwoFactor_AddAuthenticator_Response?> AddAuthenticator(ulong steamID, string deviceID) {
if ((steamID == 0) || !new SteamID(steamID).IsIndividualAccount) {
throw new ArgumentOutOfRangeException(nameof(steamID));
}
ArgumentException.ThrowIfNullOrEmpty(deviceID);
if (Client == null) {
throw new InvalidOperationException(nameof(Client));
}
if (!Client.IsConnected) {
return null;
}
CTwoFactor_AddAuthenticator_Request request = new() {
authenticator_type = 1,
authenticator_time = Utilities.GetUnixTime(),
device_identifier = deviceID,
steamid = steamID
};
SteamUnifiedMessages.ServiceMethodResponse<CTwoFactor_AddAuthenticator_Response> response;
try {
response = await UnifiedTwoFactorService.AddAuthenticator(request).ToLongRunningTask().ConfigureAwait(false);
} catch (Exception e) {
ArchiLogger.LogGenericWarningException(e);
return null;
}
// We want to return the response even with failed EResult
return response.Body;
}
internal async Task<CTwoFactor_FinalizeAddAuthenticator_Response?> FinalizeAuthenticator(ulong steamID, string activationCode, string authenticatorCode, ulong authenticatorTime) {
if ((steamID == 0) || !new SteamID(steamID).IsIndividualAccount) {
throw new ArgumentOutOfRangeException(nameof(steamID));
}
ArgumentException.ThrowIfNullOrEmpty(activationCode);
ArgumentException.ThrowIfNullOrEmpty(authenticatorCode);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(authenticatorTime);
if (Client == null) {
throw new InvalidOperationException(nameof(Client));
}
if (!Client.IsConnected) {
return null;
}
CTwoFactor_FinalizeAddAuthenticator_Request request = new() {
activation_code = activationCode,
authenticator_code = authenticatorCode,
authenticator_time = authenticatorTime,
steamid = steamID
};
SteamUnifiedMessages.ServiceMethodResponse<CTwoFactor_FinalizeAddAuthenticator_Response> response;
try {
response = await UnifiedTwoFactorService.FinalizeAddAuthenticator(request).ToLongRunningTask().ConfigureAwait(false);
} catch (Exception e) {
ArchiLogger.LogGenericWarningException(e);
return null;
}
// We want to return the response even with failed EResult
return response.Body;
}
}

View File

@@ -22,7 +22,6 @@
// limitations under the License.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
@@ -39,7 +38,7 @@ namespace ArchiSteamFarm.OfficialPlugins.MobileAuthenticator;
[Export(typeof(IPlugin))]
[SuppressMessage("ReSharper", "MemberCanBeFileLocal")]
internal sealed class MobileAuthenticatorPlugin : OfficialPlugin, IBotCommand2, IBotSteamClient {
internal sealed class MobileAuthenticatorPlugin : OfficialPlugin, IBotCommand2 {
[JsonInclude]
public override string Name => nameof(MobileAuthenticatorPlugin);
@@ -66,25 +65,6 @@ internal sealed class MobileAuthenticatorPlugin : OfficialPlugin, IBotCommand2,
return await Commands.OnBotCommand(bot, access, message, args, steamID).ConfigureAwait(false);
}
public Task OnBotSteamCallbacksInit(Bot bot, CallbackManager callbackManager) {
ArgumentNullException.ThrowIfNull(bot);
ArgumentNullException.ThrowIfNull(callbackManager);
return Task.CompletedTask;
}
public Task<IReadOnlyCollection<ClientMsgHandler>?> OnBotSteamHandlersInit(Bot bot) {
ArgumentNullException.ThrowIfNull(bot);
SteamUnifiedMessages? steamUnifiedMessages = bot.GetHandler<SteamUnifiedMessages>();
if (steamUnifiedMessages == null) {
throw new InvalidOperationException(nameof(steamUnifiedMessages));
}
return Task.FromResult<IReadOnlyCollection<ClientMsgHandler>?>(new HashSet<ClientMsgHandler>(1) { new MobileAuthenticatorHandler(bot.ArchiLogger, steamUnifiedMessages) });
}
public override Task OnLoaded() {
Utilities.WarnAboutIncompleteTranslation(Strings.ResourceManager);

View File

@@ -0,0 +1,156 @@
// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2025 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Steam;
using ArchiSteamFarm.Steam.Integration;
using ArchiSteamFarm.Web;
using SteamKit2;
using SteamKit2.Internal;
namespace ArchiSteamFarm.OfficialPlugins.MobileAuthenticator;
internal static class MobileAuthenticatorWebHandler {
private const string TwoFactorService = "ITwoFactorService";
internal static async Task<CTwoFactor_AddAuthenticator_Response?> AddAuthenticator(Bot bot, string deviceID) {
ArgumentNullException.ThrowIfNull(bot);
ArgumentException.ThrowIfNullOrEmpty(deviceID);
if (!bot.IsConnectedAndLoggedOn) {
return null;
}
string? accessToken = bot.AccessToken;
if (string.IsNullOrEmpty(accessToken)) {
return null;
}
Dictionary<string, object?> arguments = new(5, StringComparer.Ordinal) {
{ "access_token", accessToken },
{ "authenticator_time", Utilities.GetUnixTime() },
{ "authenticator_type", 1 },
{ "device_identifier", deviceID },
{ "steamid", bot.SteamID }
};
CTwoFactor_AddAuthenticator_Response? response = null;
for (byte i = 0; (i < WebBrowser.MaxTries) && (response == null); i++) {
if ((i > 0) && (ArchiWebHandler.WebLimiterDelay > 0)) {
await Task.Delay(ArchiWebHandler.WebLimiterDelay).ConfigureAwait(false);
}
using WebAPI.AsyncInterface twoFactorService = bot.SteamConfiguration.GetAsyncWebAPIInterface(TwoFactorService);
twoFactorService.Timeout = bot.ArchiWebHandler.WebBrowser.Timeout;
try {
// TODO: Move to CallProtobufAsync<TResponse, TRequest> when we update to SK2 3.2.0+ <https://github.com/SteamRE/SteamKit/pull/1537>
response = await ArchiWebHandler.WebLimitRequest(
WebAPI.DefaultBaseAddress,
// ReSharper disable once AccessToDisposedClosure
async () => await twoFactorService.CallProtobufAsync<CTwoFactor_AddAuthenticator_Response>(HttpMethod.Post, "AddAuthenticator", args: arguments).ConfigureAwait(false)
).ConfigureAwait(false);
} catch (TaskCanceledException e) {
bot.ArchiLogger.LogGenericDebuggingException(e);
} catch (Exception e) {
bot.ArchiLogger.LogGenericWarningException(e);
}
}
if (response == null) {
bot.ArchiLogger.LogGenericWarning(Strings.FormatErrorRequestFailedTooManyTimes(WebBrowser.MaxTries));
return null;
}
return response;
}
internal static async Task<CTwoFactor_FinalizeAddAuthenticator_Response?> FinalizeAuthenticator(Bot bot, string activationCode, string authenticatorCode, ulong authenticatorTime) {
ArgumentNullException.ThrowIfNull(bot);
ArgumentException.ThrowIfNullOrEmpty(activationCode);
ArgumentException.ThrowIfNullOrEmpty(authenticatorCode);
ArgumentOutOfRangeException.ThrowIfZero(authenticatorTime);
if (!bot.IsConnectedAndLoggedOn) {
return null;
}
string? accessToken = bot.AccessToken;
if (string.IsNullOrEmpty(accessToken)) {
return null;
}
Dictionary<string, object?> arguments = new(5, StringComparer.Ordinal) {
{ "access_token", accessToken },
{ "activation_code", activationCode },
{ "authenticator_code", authenticatorCode },
{ "authenticator_time", authenticatorTime },
{ "steamid", bot.SteamID }
};
CTwoFactor_FinalizeAddAuthenticator_Response? response = null;
for (byte i = 0; (i < WebBrowser.MaxTries) && (response == null); i++) {
if ((i > 0) && (ArchiWebHandler.WebLimiterDelay > 0)) {
await Task.Delay(ArchiWebHandler.WebLimiterDelay).ConfigureAwait(false);
}
using WebAPI.AsyncInterface twoFactorService = bot.SteamConfiguration.GetAsyncWebAPIInterface(TwoFactorService);
twoFactorService.Timeout = bot.ArchiWebHandler.WebBrowser.Timeout;
try {
// TODO: Move to CallProtobufAsync<TResponse, TRequest> when we update to SK2 3.2.0+ <https://github.com/SteamRE/SteamKit/pull/1537>
response = await ArchiWebHandler.WebLimitRequest(
WebAPI.DefaultBaseAddress,
// ReSharper disable once AccessToDisposedClosure
async () => await twoFactorService.CallProtobufAsync<CTwoFactor_FinalizeAddAuthenticator_Response>(HttpMethod.Post, "FinalizeAddAuthenticator", args: arguments).ConfigureAwait(false)
).ConfigureAwait(false);
} catch (TaskCanceledException e) {
bot.ArchiLogger.LogGenericDebuggingException(e);
} catch (Exception e) {
bot.ArchiLogger.LogGenericWarningException(e);
}
}
if (response == null) {
bot.ArchiLogger.LogGenericWarning(Strings.FormatErrorRequestFailedTooManyTimes(WebBrowser.MaxTries));
return null;
}
return response;
}
}

View File

@@ -72,7 +72,8 @@ public sealed class ArchiWebHandler : IDisposable {
[PublicAPI]
public static Uri SteamStoreURL => new("https://store.steampowered.com");
private static ushort WebLimiterDelay => ASF.GlobalConfig?.WebLimiterDelay ?? GlobalConfig.DefaultWebLimiterDelay;
[PublicAPI]
public static ushort WebLimiterDelay => ASF.GlobalConfig?.WebLimiterDelay ?? GlobalConfig.DefaultWebLimiterDelay;
[PublicAPI]
public WebBrowser WebBrowser { get; }