From f564cc5ebbf231da948ec7c592ddc9f4d1ebf502 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Fri, 21 Jul 2017 02:13:03 +0200 Subject: [PATCH] Update ASF base code to SK 2.0 alpha5 There is a lot of changes in this one, I like how we got rid of SteamProtocol and got SteamProtocols instead. --- ArchiSteamFarm/ASF.cs | 4 +- ArchiSteamFarm/ArchiHandler.cs | 54 ------------------- ArchiSteamFarm/ArchiSteamFarm.csproj | 2 +- ArchiSteamFarm/Bot.cs | 43 ++++++--------- ArchiSteamFarm/GlobalConfig.cs | 15 +----- ArchiSteamFarm/GlobalDatabase.cs | 12 ++--- ArchiSteamFarm/IPEndPointConverter.cs | 51 ------------------ ArchiSteamFarm/InMemoryServerListProvider.cs | 13 +++-- .../Localization/Strings.Designer.cs | 9 ---- ArchiSteamFarm/Localization/Strings.resx | 4 -- ArchiSteamFarm/config/ASF.json | 2 +- 11 files changed, 34 insertions(+), 175 deletions(-) delete mode 100644 ArchiSteamFarm/IPEndPointConverter.cs diff --git a/ArchiSteamFarm/ASF.cs b/ArchiSteamFarm/ASF.cs index 9a00c0de3..7779ba1bc 100644 --- a/ArchiSteamFarm/ASF.cs +++ b/ArchiSteamFarm/ASF.cs @@ -219,8 +219,8 @@ namespace ArchiSteamFarm { return; } - // Before attempting to connect, initialize our list of CMs - await Bot.InitializeCMs(Program.GlobalDatabase.CellID, Program.GlobalDatabase.ServerListProvider).ConfigureAwait(false); + // Before attempting to connect, initialize our configuration + await Bot.InitializeSteamConfiguration(Program.GlobalConfig.SteamProtocols, Program.GlobalDatabase.CellID, Program.GlobalDatabase.ServerList).ConfigureAwait(false); foreach (string botName in Directory.EnumerateFiles(SharedInfo.ConfigDirectory, "*.json").Select(Path.GetFileNameWithoutExtension).Where(botName => !string.IsNullOrEmpty(botName) && IsValidBotName(botName)).OrderBy(botName => botName)) { Bot.RegisterBot(botName); diff --git a/ArchiSteamFarm/ArchiHandler.cs b/ArchiSteamFarm/ArchiHandler.cs index e3ec90a0f..ed8bda81e 100644 --- a/ArchiSteamFarm/ArchiHandler.cs +++ b/ArchiSteamFarm/ArchiHandler.cs @@ -96,60 +96,6 @@ namespace ArchiSteamFarm { Client.Send(request); } - // TODO: Remove me once https://github.com/SteamRE/SteamKit/issues/305 is fixed - internal void LogOnWithoutMachineID(SteamUser.LogOnDetails details) { - if (details == null) { - throw new ArgumentNullException(nameof(details)); - } - - if (string.IsNullOrEmpty(details.Username) || (string.IsNullOrEmpty(details.Password) && string.IsNullOrEmpty(details.LoginKey))) { - throw new ArgumentException("LogOn requires a username and password to be set in 'details'."); - } - - if (!string.IsNullOrEmpty(details.LoginKey) && !details.ShouldRememberPassword) { - // Prevent consumers from screwing this up. - // If should_remember_password is false, the login_key is ignored server-side. - // The inverse is not applicable (you can log in with should_remember_password and no login_key). - throw new ArgumentException("ShouldRememberPassword is required to be set to true in order to use LoginKey."); - } - - ClientMsgProtobuf logon = new ClientMsgProtobuf(EMsg.ClientLogon); - - SteamID steamId = new SteamID(details.AccountID, details.AccountInstance, Client.ConnectedUniverse, EAccountType.Individual); - - if (details.LoginID.HasValue) { - logon.Body.obfustucated_private_ip = details.LoginID.Value; - } - - logon.ProtoHeader.client_sessionid = 0; - logon.ProtoHeader.steamid = steamId.ConvertToUInt64(); - - logon.Body.account_name = details.Username; - logon.Body.password = details.Password; - logon.Body.should_remember_password = details.ShouldRememberPassword; - - logon.Body.protocol_version = MsgClientLogon.CurrentProtocol; - logon.Body.client_os_type = (uint) details.ClientOSType; - logon.Body.client_language = details.ClientLanguage; - logon.Body.cell_id = details.CellID; - - logon.Body.steam2_ticket_request = details.RequestSteam2Ticket; - - logon.Body.client_package_version = 1771; - logon.Body.supports_rate_limit_response = true; - - // steam guard - logon.Body.auth_code = details.AuthCode; - logon.Body.two_factor_code = details.TwoFactorCode; - - logon.Body.login_key = details.LoginKey; - - logon.Body.sha_sentryfile = details.SentryFileHash; - logon.Body.eresult_sentryfile = (int) (details.SentryFileHash != null ? EResult.OK : EResult.FileNotFound); - - Client.Send(logon); - } - internal void PlayGames(IEnumerable gameIDs, string gameName = null) { if (gameIDs == null) { ArchiLogger.LogNullError(nameof(gameIDs)); diff --git a/ArchiSteamFarm/ArchiSteamFarm.csproj b/ArchiSteamFarm/ArchiSteamFarm.csproj index 6b20d7bef..f93bfbc1e 100644 --- a/ArchiSteamFarm/ArchiSteamFarm.csproj +++ b/ArchiSteamFarm/ArchiSteamFarm.csproj @@ -31,7 +31,7 @@ - + diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index a102943c9..75548ef46 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -28,7 +28,6 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; -using System.Net; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; @@ -38,7 +37,7 @@ using ArchiSteamFarm.JSON; using ArchiSteamFarm.Localization; using Newtonsoft.Json; using SteamKit2; -using SteamKit2.Internal; +using SteamKit2.Discovery; namespace ArchiSteamFarm { internal sealed class Bot : IDisposable { @@ -56,6 +55,7 @@ namespace ArchiSteamFarm { private static readonly SemaphoreSlim GiftsSemaphore = new SemaphoreSlim(1); private static readonly SemaphoreSlim LoginSemaphore = new SemaphoreSlim(1); + private static readonly SteamConfiguration SteamConfiguration = new SteamConfiguration(); internal readonly ArchiLogger ArchiLogger; internal readonly ArchiWebHandler ArchiWebHandler; @@ -161,7 +161,7 @@ namespace ArchiSteamFarm { } // Initialize - SteamClient = new SteamClient(Program.GlobalConfig.SteamProtocol); + SteamClient = new SteamClient(SteamConfiguration); if (Program.GlobalConfig.Debug && Directory.Exists(SharedInfo.DebugDirectory)) { string debugListenerPath = Path.Combine(SharedInfo.DebugDirectory, botName); @@ -416,22 +416,23 @@ namespace ArchiSteamFarm { return appID; } - internal static async Task InitializeCMs(uint cellID, InMemoryServerListProvider serverListProvider) { + internal static async Task InitializeSteamConfiguration(ProtocolTypes protocolTypes, uint cellID, InMemoryServerListProvider serverListProvider) { if (serverListProvider == null) { ASF.ArchiLogger.LogNullError(nameof(serverListProvider)); return; } - CMClient.Servers.CellID = cellID; - CMClient.Servers.ServerListProvider = serverListProvider; + SteamConfiguration.ProtocolTypes = protocolTypes; + SteamConfiguration.CellID = cellID; + SteamConfiguration.ServerListProvider = serverListProvider; // Ensure that we ask for a list of servers if we don't have any saved servers available - IEnumerable servers = await serverListProvider.FetchServerListAsync().ConfigureAwait(false); + IEnumerable servers = await SteamConfiguration.ServerListProvider.FetchServerListAsync().ConfigureAwait(false); if (servers?.Any() != true) { ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.Initializing, nameof(SteamDirectory))); try { - await SteamDirectory.Initialize(cellID).ConfigureAwait(false); + await SteamDirectory.LoadAsync(SteamConfiguration).ConfigureAwait(false); ASF.ArchiLogger.LogGenericInfo(Strings.Success); } catch { ASF.ArchiLogger.LogGenericWarning(Strings.BotSteamDirectoryInitializationFailed); @@ -561,7 +562,7 @@ namespace ArchiSteamFarm { return false; } - if (await ArchiWebHandler.Init(SteamID, SteamClient.ConnectedUniverse, callback.Nonce, BotConfig.SteamParentalPIN).ConfigureAwait(false)) { + if (await ArchiWebHandler.Init(SteamID, SteamClient.Universe, callback.Nonce, BotConfig.SteamParentalPIN).ConfigureAwait(false)) { return true; } @@ -1344,11 +1345,6 @@ namespace ArchiSteamFarm { HeartBeatFailures = 0; StopConnectionFailureTimer(); - if (callback.Result != EResult.OK) { - ArchiLogger.LogGenericError(string.Format(Strings.BotUnableToConnect, callback.Result)); - return; - } - ArchiLogger.LogGenericInfo(Strings.BotConnected); if (!KeepRunning) { @@ -1393,7 +1389,9 @@ namespace ArchiSteamFarm { TwoFactorCode = await BotDatabase.MobileAuthenticator.GenerateToken().ConfigureAwait(false); } - SteamUser.LogOnDetails logOnDetails = new SteamUser.LogOnDetails { + InitConnectionFailureTimer(); + + SteamUser.LogOn(new SteamUser.LogOnDetails { AuthCode = AuthCode, CellID = Program.GlobalDatabase.CellID, LoginID = LoginID, @@ -1403,16 +1401,7 @@ namespace ArchiSteamFarm { ShouldRememberPassword = true, TwoFactorCode = TwoFactorCode, Username = BotConfig.SteamLogin - }; - - InitConnectionFailureTimer(); - - try { - SteamUser.LogOn(logOnDetails); - } catch { - // TODO: Remove me once https://github.com/SteamRE/SteamKit/issues/305 is fixed - ArchiHandler.LogOnWithoutMachineID(logOnDetails); - } + }); } private async void OnDisconnected(SteamClient.DisconnectedCallback callback) { @@ -1683,7 +1672,7 @@ namespace ArchiSteamFarm { if ((callback.CellID != 0) && (Program.GlobalDatabase.CellID != callback.CellID)) { Program.GlobalDatabase.CellID = callback.CellID; - CMClient.Servers.CellID = callback.CellID; + SteamConfiguration.CellID = callback.CellID; } if (!HasMobileAuthenticator) { @@ -1704,7 +1693,7 @@ namespace ArchiSteamFarm { SetUserInput(ASF.EUserInputType.SteamParentalPIN, steamParentalPIN); } - if (!await ArchiWebHandler.Init(callback.ClientSteamID, SteamClient.ConnectedUniverse, callback.WebAPIUserNonce, BotConfig.SteamParentalPIN).ConfigureAwait(false)) { + if (!await ArchiWebHandler.Init(callback.ClientSteamID, SteamClient.Universe, callback.WebAPIUserNonce, BotConfig.SteamParentalPIN).ConfigureAwait(false)) { if (!await RefreshSession().ConfigureAwait(false)) { break; } diff --git a/ArchiSteamFarm/GlobalConfig.cs b/ArchiSteamFarm/GlobalConfig.cs index b4f938302..6c0321561 100644 --- a/ArchiSteamFarm/GlobalConfig.cs +++ b/ArchiSteamFarm/GlobalConfig.cs @@ -26,9 +26,9 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; -using System.Net.Sockets; using ArchiSteamFarm.Localization; using Newtonsoft.Json; +using SteamKit2; namespace ArchiSteamFarm { [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] @@ -102,7 +102,7 @@ namespace ArchiSteamFarm { internal readonly bool Statistics = true; [JsonProperty(Required = Required.DisallowNull)] - internal readonly ProtocolType SteamProtocol = ProtocolType.Tcp; + internal readonly ProtocolTypes SteamProtocols = ProtocolTypes.All; [JsonProperty(Required = Required.DisallowNull)] internal readonly EUpdateChannel UpdateChannel = EUpdateChannel.Stable; @@ -152,17 +152,6 @@ namespace ArchiSteamFarm { return null; } - // SK2 supports only TCP and UDP steam protocols - // Ensure that user can't screw this up - switch (globalConfig.SteamProtocol) { - case ProtocolType.Tcp: - case ProtocolType.Udp: - break; - default: - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.SteamProtocol), globalConfig.SteamProtocol)); - return null; - } - // User might not know what he's doing // Ensure that he can't screw core ASF variables if (globalConfig.MaxFarmingTime == 0) { diff --git a/ArchiSteamFarm/GlobalDatabase.cs b/ArchiSteamFarm/GlobalDatabase.cs index 42c216770..ff89c7139 100644 --- a/ArchiSteamFarm/GlobalDatabase.cs +++ b/ArchiSteamFarm/GlobalDatabase.cs @@ -30,9 +30,8 @@ using Newtonsoft.Json; namespace ArchiSteamFarm { internal sealed class GlobalDatabase : IDisposable { private static readonly JsonSerializerSettings CustomSerializerSettings = new JsonSerializerSettings { - Converters = new List(2) { - new IPAddressConverter(), - new IPEndPointConverter() + Converters = new List(1) { + new IPAddressConverter() } }; @@ -40,7 +39,8 @@ namespace ArchiSteamFarm { internal readonly Guid Guid = Guid.NewGuid(); [JsonProperty(Required = Required.DisallowNull)] - internal readonly InMemoryServerListProvider ServerListProvider = new InMemoryServerListProvider(); + [JsonIgnore] // TODO: Remove me once https://github.com/SteamRE/SteamKit/issues/416 is solved + internal readonly InMemoryServerListProvider ServerList = new InMemoryServerListProvider(); private readonly object FileLock = new object(); @@ -72,9 +72,9 @@ namespace ArchiSteamFarm { } // This constructor is used only by deserializer - private GlobalDatabase() => ServerListProvider.ServerListUpdated += OnServerListUpdated; + private GlobalDatabase() => ServerList.ServerListUpdated += OnServerListUpdated; - public void Dispose() => ServerListProvider.ServerListUpdated -= OnServerListUpdated; + public void Dispose() => ServerList.ServerListUpdated -= OnServerListUpdated; internal static GlobalDatabase Load(string filePath) { if (string.IsNullOrEmpty(filePath)) { diff --git a/ArchiSteamFarm/IPEndPointConverter.cs b/ArchiSteamFarm/IPEndPointConverter.cs deleted file mode 100644 index 8f89d2e28..000000000 --- a/ArchiSteamFarm/IPEndPointConverter.cs +++ /dev/null @@ -1,51 +0,0 @@ -/* - _ _ _ ____ _ _____ - / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ - / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ - / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | -/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| - - Copyright 2015-2017 Ł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.Net; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; - -namespace ArchiSteamFarm { - internal sealed class IPEndPointConverter : JsonConverter { - public override bool CanConvert(Type objectType) => objectType == typeof(IPEndPoint); - - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - JObject jo = JObject.Load(reader); - IPAddress address = jo["Address"].ToObject(serializer); - ushort port = jo["Port"].Value(); - return new IPEndPoint(address, port); - } - - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { - IPEndPoint ep = (IPEndPoint) value; - writer.WriteStartObject(); - writer.WritePropertyName("Address"); - serializer.Serialize(writer, ep.Address); - writer.WritePropertyName("Port"); - writer.WriteValue(ep.Port); - writer.WriteEndObject(); - } - } -} \ No newline at end of file diff --git a/ArchiSteamFarm/InMemoryServerListProvider.cs b/ArchiSteamFarm/InMemoryServerListProvider.cs index 9eecb58b5..9d9eca5f3 100644 --- a/ArchiSteamFarm/InMemoryServerListProvider.cs +++ b/ArchiSteamFarm/InMemoryServerListProvider.cs @@ -24,7 +24,6 @@ using System; using System.Collections.Generic; -using System.Net; using System.Threading.Tasks; using Newtonsoft.Json; using SteamKit2.Discovery; @@ -32,17 +31,17 @@ using SteamKit2.Discovery; namespace ArchiSteamFarm { internal sealed class InMemoryServerListProvider : IServerListProvider { [JsonProperty(Required = Required.DisallowNull)] - private readonly ConcurrentHashSet Servers = new ConcurrentHashSet(); + private readonly ConcurrentHashSet Servers = new ConcurrentHashSet(); - public Task> FetchServerListAsync() => Task.FromResult>(Servers); + public Task> FetchServerListAsync() => Task.FromResult>(Servers); - public Task UpdateServerListAsync(IEnumerable endPoints) { - if (endPoints == null) { - ASF.ArchiLogger.LogNullError(nameof(endPoints)); + public Task UpdateServerListAsync(IEnumerable endpoints) { + if (endpoints == null) { + ASF.ArchiLogger.LogNullError(nameof(endpoints)); return Task.CompletedTask; } - HashSet newServers = new HashSet(endPoints); + HashSet newServers = new HashSet(endpoints); if (!Servers.ReplaceIfNeededWith(newServers)) { return Task.CompletedTask; diff --git a/ArchiSteamFarm/Localization/Strings.Designer.cs b/ArchiSteamFarm/Localization/Strings.Designer.cs index be6c2b140..3566633ff 100644 --- a/ArchiSteamFarm/Localization/Strings.Designer.cs +++ b/ArchiSteamFarm/Localization/Strings.Designer.cs @@ -654,15 +654,6 @@ namespace ArchiSteamFarm.Localization { } } - /// - /// Wyszukuje zlokalizowany ciąg podobny do ciągu Unable to connect to Steam: {0}. - /// - internal static string BotUnableToConnect { - get { - return ResourceManager.GetString("BotUnableToConnect", resourceCulture); - } - } - /// /// Wyszukuje zlokalizowany ciąg podobny do ciągu Unable to login to Steam: {0}/{1}. /// diff --git a/ArchiSteamFarm/Localization/Strings.resx b/ArchiSteamFarm/Localization/Strings.resx index 28c45e6a0..e7d5db974 100644 --- a/ArchiSteamFarm/Localization/Strings.resx +++ b/ArchiSteamFarm/Localization/Strings.resx @@ -565,10 +565,6 @@ StackTrace: Bot is currently being used. - - Unable to connect to Steam: {0} - {0} will be replaced by failure reason (string) - Unable to login to Steam: {0}/{1} {0} will be replaced by failure reason (string), {1} will be replaced by extended failure reason (string) diff --git a/ArchiSteamFarm/config/ASF.json b/ArchiSteamFarm/config/ASF.json index 11151ec3b..c33993c5e 100644 --- a/ArchiSteamFarm/config/ASF.json +++ b/ArchiSteamFarm/config/ASF.json @@ -18,6 +18,6 @@ "OptimizationMode": 0, "Statistics": true, "SteamOwnerID": 0, - "SteamProtocol": 6, + "SteamProtocols": 7, "UpdateChannel": 1 } \ No newline at end of file