using AchiesUtilities.Web.Models; using AchiesUtilities.Web.Proxy; using NebulaAuth.Model.Entities; using SteamLib.Api.Mobile; using SteamLib.Authentication; using SteamLib.Core.Interfaces; using SteamLib.Exceptions; using SteamLib.ProtoCore.Services; using SteamLib.SteamMobile.Confirmations; using SteamLib.Web; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using SteamLib.Utility; namespace NebulaAuth.Model; public static class MaClient { private static SocketsHttpHandler ClientHandler { get; } private static HttpClient Client { get; } private static DynamicProxy Proxy { get; } public static ProxyData? DefaultProxy { get; set; } public static SocketsClientHandlerPair Chp => new(Client, ClientHandler); static MaClient() { Proxy = new DynamicProxy(); var pair = ClientBuilder.BuildMobileClient(Proxy, null); Client = pair.Client; ClientHandler = pair.Handler; } public static void SetAccount(Mafile? account) { ClientHandler.CookieContainer.ClearAllCookies(); if (account != null) { if (account.SessionData != null) { ClientHandler.CookieContainer.SetSteamMobileCookiesWithMobileToken(account.SessionData); } else { ClientHandler.CookieContainer.ClearSteamCookies(); ClientHandler.CookieContainer.AddMinimalMobileCookies(); AdmissionHelper.TransferCommunityCookies(ClientHandler.CookieContainer); } Proxy.SetData(account.Proxy?.Data); } } public static Task> GetConfirmations(Mafile mafile) { ValidateMafile(mafile); SetProxy(mafile); return SteamMobileConfirmationsApi.GetConfirmations(Client, mafile, mafile.SessionData!.SteamId); } public static Task LoginAgain(Mafile mafile, string password, bool savePassword, ICaptchaResolver? resolver) { SetProxy(mafile); return SessionHandler.LoginAgain(Chp, mafile, password, savePassword); } public static Task RefreshSession(Mafile mafile) { ValidateMafile(mafile, true); SetProxy(mafile); return SessionHandler.RefreshMobileToken(Chp, mafile); } public static Task SendConfirmation(Mafile mafile, Confirmation confirmation, bool confirm) { ValidateMafile(mafile); SetProxy(mafile); return SteamMobileConfirmationsApi.SendConfirmation(Client, confirmation, mafile.SessionData!.SteamId, mafile, confirm); } public static Task SendMultipleConfirmation(Mafile mafile, IEnumerable confirmations, bool confirm) { var enumerable = confirmations.ToList(); if (enumerable.Count == 0) { return Task.FromResult(result: false); } ValidateMafile(mafile); SetProxy(mafile); return SteamMobileConfirmationsApi.SendMultipleConfirmations(Client, enumerable, mafile.SessionData!.SteamId, mafile, confirm); } public static Task RemoveAuthenticator(Mafile mafile) { ValidateMafile(mafile); SetProxy(mafile); if (mafile.RevocationCode == null) { throw new InvalidOperationException("This mafile does not have R-Code"); } var token = mafile.SessionData!.GetMobileToken()!; return SteamMobileApi.RemoveAuthenticator(Client, token.Value.Token, mafile.RevocationCode); } private static void SetProxy(Mafile mafile) { Proxy.SetData(mafile.Proxy?.Data ?? DefaultProxy); } private static void ValidateMafile(Mafile mafile, bool ignoreAccessToken = false) { if (mafile.SessionData == null) throw new SessionInvalidException(); if (mafile.SessionData.RefreshToken.IsExpired) throw new SessionPermanentlyExpiredException(); if (ignoreAccessToken == false) { var access = mafile.SessionData.GetMobileToken(); if (access == null || access.Value.IsExpired) throw new SessionPermanentlyExpiredException(); } } public static async Task ConfirmLoginRequest(Mafile mafile) { ValidateMafile(mafile); SetProxy(mafile); var token = mafile.SessionData!.GetMobileToken()!.Value; var sessions = await SteamMobileAuthenticatorApi.GetAuthSessionsForAccount(Client, token.Token); if (sessions.ClientIds.Count == 0) { return new LoginConfirmationResult { Error = LoginConfirmationError.NoRequests }; } if (sessions.ClientIds.Count > 1) { return new LoginConfirmationResult { Error = LoginConfirmationError.MoreThanOneRequest }; } var clientId = sessions.ClientIds.Single(); var clientInfo = await SteamMobileAuthenticatorApi.GetAuthSessionInfo(Client, token.Token, clientId); var updateReq = new UpdateAuthSessionWithMobileConfirmation_Request { ClientId = clientId, Confirm = true, Persistence = 1, Steamid = mafile.SessionData.SteamId.Steam64.ToUlong(), Version = 1 }; await SteamMobileAuthenticatorApi.UpdateAuthSessionStatus(Client, token.Token, mafile.SharedSecret, updateReq); return new LoginConfirmationResult { Country = clientInfo.Country, IP = clientInfo.IP, Success = true }; } }