internal async Task <PurchaseResponseCallback> RedeemKey(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                Logging.LogNullError(nameof(key), Bot.BotName);
                return(null);
            }

            if (!Client.IsConnected)
            {
                return(null);
            }

            ClientMsgProtobuf <CMsgClientRegisterKey> request = new ClientMsgProtobuf <CMsgClientRegisterKey>(EMsg.ClientRegisterKey)
            {
                SourceJobID = Client.GetNextJobID()
            };

            request.Body.key = key;

            Client.Send(request);

            try {
                return(await new AsyncJob <PurchaseResponseCallback>(Client, request.SourceJobID));
            } catch (Exception e) {
                Logging.LogGenericException(e, Bot.BotName);
                return(null);
            }
        }
        internal async Task <RedeemGuestPassResponseCallback> RedeemGuestPass(ulong guestPassID)
        {
            if (guestPassID == 0)
            {
                Logging.LogNullError(nameof(guestPassID), Bot.BotName);
                return(null);
            }

            if (!Client.IsConnected)
            {
                return(null);
            }

            ClientMsgProtobuf <CMsgClientRedeemGuestPass> request = new ClientMsgProtobuf <CMsgClientRedeemGuestPass>(EMsg.ClientRedeemGuestPass)
            {
                SourceJobID = Client.GetNextJobID()
            };

            request.Body.guest_pass_id = guestPassID;

            Client.Send(request);

            try {
                return(await new AsyncJob <RedeemGuestPassResponseCallback>(Client, request.SourceJobID));
            } catch (Exception e) {
                Logging.LogGenericException(e, Bot.BotName);
                return(null);
            }
        }
Example #3
0
        internal static async Task <JObject> UrlGetToJObject(string request, Dictionary <string, string> cookies = null, string referer = null)
        {
            if (string.IsNullOrEmpty(request))
            {
                return(null);
            }

            string content = await UrlGetToContent(request, cookies, referer).ConfigureAwait(false);

            if (string.IsNullOrEmpty(content))
            {
                return(null);
            }

            JObject jObject;

            try {
                jObject = JObject.Parse(content);
            } catch (Exception e) {
                Logging.LogGenericException(e);
                return(null);
            }

            return(jObject);
        }
Example #4
0
        internal void StartServer()
        {
            if (ServiceHost != null)
            {
                return;
            }

            Logging.LogGenericInfo("Starting WCF server...");

            try {
                ServiceHost = new ServiceHost(typeof(WCF), new Uri(URL));

                ServiceHost.Description.Behaviors.Add(new ServiceMetadataBehavior {
                    HttpGetEnabled = true
                });

                ServiceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
                ServiceHost.AddServiceEndpoint(typeof(IWCF), new BasicHttpBinding(), string.Empty);

                ServiceHost.Open();
            } catch (Exception e) {
                Logging.LogGenericException(e);
                return;
            }

            Logging.LogGenericInfo("WCF server ready!");
        }
Example #5
0
        internal static async Task <HtmlDocument> UrlToHtmlDocument(string websiteAddress, Dictionary <string, string> cookieVariables = null)
        {
            if (string.IsNullOrEmpty(websiteAddress))
            {
                return(null);
            }

            HtmlDocument result = null;

            try {
                HttpResponseMessage responseMessage = await UrlToHttpResponse(websiteAddress, cookieVariables).ConfigureAwait(false);

                if (responseMessage != null)
                {
                    string source = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

                    if (!string.IsNullOrEmpty(source))
                    {
                        source = WebUtility.HtmlDecode(source);
                        result = new HtmlDocument();
                        result.LoadHtml(source);
                    }
                }
            } catch (Exception e) {
                Logging.LogGenericException("Utilities", e);
            }

            return(result);
        }
Example #6
0
        internal async Task <JObject> UrlGetToJObject(string request, string referer = null)
        {
            if (string.IsNullOrEmpty(request))
            {
                return(null);
            }

            string content = await UrlGetToContent(request, referer).ConfigureAwait(false);

            if (string.IsNullOrEmpty(content))
            {
                return(null);
            }

            JObject jObject;

            try {
                jObject = JObject.Parse(content);
            } catch (JsonException e) {
                Logging.LogGenericException(e, Identifier);
                return(null);
            }

            return(jObject);
        }
Example #7
0
        internal static async Task <XmlDocument> UrlGetToXML(string request, Dictionary <string, string> cookies = null, string referer = null)
        {
            if (string.IsNullOrEmpty(request))
            {
                return(null);
            }

            string content = await UrlGetToContent(request, cookies, referer).ConfigureAwait(false);

            if (string.IsNullOrEmpty(content))
            {
                return(null);
            }

            XmlDocument xmlDocument = new XmlDocument();

            try {
                xmlDocument.LoadXml(content);
            } catch (XmlException e) {
                Logging.LogGenericException(e);
                return(null);
            }

            return(xmlDocument);
        }
        private async Task <XmlDocument> UrlGetToXML(string request, string referer = null)
        {
            if (string.IsNullOrEmpty(request))
            {
                Logging.LogNullError(nameof(request), Identifier);
                return(null);
            }

            string xml = await UrlGetToContent(request, referer).ConfigureAwait(false);

            if (string.IsNullOrEmpty(xml))
            {
                return(null);
            }

            XmlDocument xmlDocument = new XmlDocument();

            try {
                xmlDocument.LoadXml(xml);
            } catch (XmlException e) {
                Logging.LogGenericException(e, Identifier);
                return(null);
            }

            return(xmlDocument);
        }
        internal uint GetServerTime()
        {
            KeyValue response = null;

            for (byte i = 0; (i < WebBrowser.MaxRetries) && (response == null); i++)
            {
                using (dynamic iTwoFactorService = WebAPI.GetInterface("ITwoFactorService")) {
                    iTwoFactorService.Timeout = Timeout;

                    try {
                        response = iTwoFactorService.QueryTime(
                            method: WebRequestMethods.Http.Post,
                            secure: !Program.GlobalConfig.ForceHttp
                            );
                    } catch (Exception e) {
                        Logging.LogGenericException(e, Bot.BotName);
                    }
                }
            }

            if (response != null)
            {
                return(response["server_time"].AsUnsignedInteger());
            }

            Logging.LogGenericWarning("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName);
            return(0);
        }
Example #10
0
        internal static BotDatabase Load(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                Logging.LogNullError(nameof(filePath));
                return(null);
            }

            if (!File.Exists(filePath))
            {
                return(new BotDatabase(filePath));
            }

            BotDatabase botDatabase;

            try {
                botDatabase = JsonConvert.DeserializeObject <BotDatabase>(File.ReadAllText(filePath));
            } catch (Exception e) {
                Logging.LogGenericException(e);
                return(null);
            }

            if (botDatabase == null)
            {
                Logging.LogNullError(nameof(botDatabase));
                return(null);
            }

            botDatabase.FilePath = filePath;
            return(botDatabase);
        }
Example #11
0
        internal bool DeclineTradeOffer(ulong tradeID)
        {
            if (tradeID == 0 || ApiKey == null)
            {
                return(false);
            }

            KeyValue response = null;

            using (dynamic iEconService = WebAPI.GetInterface("IEconService", ApiKey)) {
                iEconService.Timeout = Timeout;

                for (byte i = 0; i < WebBrowser.MaxRetries && response == null; i++)
                {
                    try {
                        response = iEconService.DeclineTradeOffer(
                            tradeofferid: tradeID.ToString(),
                            method: WebRequestMethods.Http.Post,
                            secure: true
                            );
                    } catch (Exception e) {
                        Logging.LogGenericException(e, Bot.BotName);
                    }
                }
            }

            if (response == null)
            {
                Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName);
                return(false);
            }

            return(true);
        }
        internal bool DeclineTradeOffer(ulong tradeID)
        {
            if (ApiKey == null)
            {
                return(false);
            }

            if (tradeID == 0)
            {
                return(false);
            }

            KeyValue response;

            using (dynamic iEconService = WebAPI.GetInterface("IEconService")) {
                // Timeout
                iEconService.Timeout = Timeout;

                try {
                    response = iEconService.DeclineTradeOffer(
                        key: ApiKey,
                        tradeofferid: tradeID.ToString(),
                        method: WebRequestMethods.Http.Post,
                        secure: true
                        );
                } catch (Exception e) {
                    Logging.LogGenericException(Bot.BotName, e);
                    return(false);
                }
            }

            return(response != null);            // Steam API doesn't respond with any error code, assume any response is a success
        }
        internal void DeclineTradeOffer(ulong tradeID)
        {
            if ((tradeID == 0) || string.IsNullOrEmpty(Bot.BotConfig.SteamApiKey))
            {
                Logging.LogNullError(nameof(tradeID) + " || " + nameof(Bot.BotConfig.SteamApiKey), Bot.BotName);
                return;
            }

            KeyValue response = null;

            for (byte i = 0; (i < WebBrowser.MaxRetries) && (response == null); i++)
            {
                using (dynamic iEconService = WebAPI.GetInterface("IEconService", Bot.BotConfig.SteamApiKey)) {
                    iEconService.Timeout = Timeout;

                    try {
                        response = iEconService.DeclineTradeOffer(
                            tradeofferid: tradeID.ToString(),
                            method: WebRequestMethods.Http.Post,
                            secure: !Program.GlobalConfig.ForceHttp
                            );
                    } catch (Exception e) {
                        Logging.LogGenericException(e, Bot.BotName);
                    }
                }
            }

            if (response == null)
            {
                Logging.LogGenericWarning("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName);
            }
        }
        internal static GlobalConfig Load()
        {
            string filePath = Path.Combine(Program.ConfigDirectory, Program.GlobalConfigFile);

            if (!File.Exists(filePath))
            {
                return(null);
            }

            GlobalConfig globalConfig;

            try {
                globalConfig = JsonConvert.DeserializeObject <GlobalConfig>(File.ReadAllText(filePath));
            } catch (Exception e) {
                Logging.LogGenericException(e);
                return(null);
            }

            // SK2 supports only TCP and UDP steam protocols
            // Make sure that user can't screw this up
            switch (globalConfig.SteamProtocol)
            {
            case ProtocolType.Tcp:
            case ProtocolType.Udp:
                break;

            default:
                Logging.LogGenericWarning("Configured SteamProtocol is invalid: " + globalConfig.SteamProtocol + ", default TCP protocol will be used instead");
                globalConfig.SteamProtocol = ProtocolType.Tcp;
                break;
            }

            return(globalConfig);
        }
        internal static GlobalDatabase Load(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                Logging.LogNullError(nameof(filePath));
                return(null);
            }

            if (!File.Exists(filePath))
            {
                return(new GlobalDatabase(filePath));
            }

            GlobalDatabase globalDatabase;

            try {
                globalDatabase = JsonConvert.DeserializeObject <GlobalDatabase>(File.ReadAllText(filePath), CustomSerializerSettings);
            } catch (Exception e) {
                Logging.LogGenericException(e);
                return(null);
            }

            if (globalDatabase == null)
            {
                Logging.LogNullError(nameof(globalDatabase));
                return(null);
            }

            globalDatabase.FilePath = filePath;
            return(globalDatabase);
        }
        private async Task <HttpResponseMessage> UrlRequest(string request, HttpMethod httpMethod, Dictionary <string, string> data = null, string referer = null)
        {
            if (string.IsNullOrEmpty(request) || (httpMethod == null))
            {
                Logging.LogNullError(nameof(request) + " || " + nameof(httpMethod));
                return(null);
            }

            if (request.StartsWith("https://", StringComparison.Ordinal) && Program.GlobalConfig.ForceHttp)
            {
                return(null);
            }

            HttpResponseMessage responseMessage;

            using (HttpRequestMessage requestMessage = new HttpRequestMessage(httpMethod, request)) {
                if ((data != null) && (data.Count > 0))
                {
                    try {
                        requestMessage.Content = new FormUrlEncodedContent(data);
                    } catch (UriFormatException e) {
                        Logging.LogGenericException(e, Identifier);
                        return(null);
                    }
                }

                if (!string.IsNullOrEmpty(referer))
                {
                    requestMessage.Headers.Referrer = new Uri(referer);
                }

                try {
                    responseMessage = await HttpClient.SendAsync(requestMessage).ConfigureAwait(false);
                } catch {                 // Request failed, we don't need to know the exact reason, swallow exception
                    return(null);
                }
            }

            if (responseMessage == null)
            {
                return(null);
            }

            if (responseMessage.IsSuccessStatusCode)
            {
                return(responseMessage);
            }

            if (Debugging.IsDebugBuild || Program.GlobalConfig.Debug)
            {
                Logging.LogGenericError("Request: " + request + " failed!", Identifier);
                Logging.LogGenericError("Status code: " + responseMessage.StatusCode, Identifier);
                Logging.LogGenericError("Content: " + Environment.NewLine + await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false), Identifier);
            }

            responseMessage.Dispose();
            return(null);
        }
Example #17
0
        private static void UnobservedTaskExceptionHandler(object sender, UnobservedTaskExceptionEventArgs args)
        {
            if (sender == null || args == null)
            {
                return;
            }

            Logging.LogGenericException(args.Exception);
        }
Example #18
0
 internal static void Restart()
 {
     try {
         Process.Start(ExecutableFile, string.Join(" ", Environment.GetCommandLineArgs().Skip(1)));
         Environment.Exit(0);
     } catch (Exception e) {
         Logging.LogGenericException(e);
     }
 }
Example #19
0
 public string HandleCommand(string input)
 {
     try {
         return(Channel.HandleCommand(input));
     } catch (Exception e) {
         Logging.LogGenericException(e);
         return(null);
     }
 }
Example #20
0
        private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
        {
            if (sender == null || args == null)
            {
                return;
            }

            Logging.LogGenericException((Exception)args.ExceptionObject);
        }
Example #21
0
 internal void Save()
 {
     lock (FilePath) {
         try {
             File.WriteAllText(FilePath, JsonConvert.SerializeObject(this));
         } catch (Exception e) {
             Logging.LogGenericException(e);
         }
     }
 }
Example #22
0
        private static void UnobservedTaskExceptionHandler(object sender, UnobservedTaskExceptionEventArgs args)
        {
            if ((sender == null) || (args == null) || (args.Exception == null))
            {
                Logging.LogNullError(nameof(sender) + " || " + nameof(args) + " || " + nameof(args.Exception));
                return;
            }

            Logging.LogGenericException(args.Exception);
        }
Example #23
0
        private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
        {
            if ((sender == null) || (args == null) || (args.ExceptionObject == null))
            {
                Logging.LogNullError(nameof(sender) + " || " + nameof(args) + " || " + nameof(args.ExceptionObject));
                return;
            }

            Logging.LogGenericException((Exception)args.ExceptionObject);
        }
Example #24
0
 public void WriteLine(string category, string msg)
 {
     lock (FilePath) {
         try {
             File.AppendAllText(FilePath, category + " | " + msg + Environment.NewLine);
         } catch (Exception e) {
             Logging.LogGenericException(e);
         }
     }
 }
Example #25
0
        // TODO: This should be removed soon
        internal bool Convert(string path)
        {
            try {
                File.WriteAllText(path, JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented));
            } catch (Exception e) {
                Logging.LogGenericException(e);
                return(false);
            }

            Logging.LogGenericWarning("Your config was converted to new ASF V2.0 format");
            return(true);
        }
Example #26
0
        internal static void Restart()
        {
            InitShutdownSequence();

            try {
                Process.Start(Assembly.GetEntryAssembly().Location, string.Join(" ", Environment.GetCommandLineArgs().Skip(1)));
            } catch (Exception e) {
                Logging.LogGenericException(e);
            }

            Environment.Exit(0);
        }
Example #27
0
        internal static BotConfig Load(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                Logging.LogNullError(nameof(filePath));
                return(null);
            }

            if (!File.Exists(filePath))
            {
                return(null);
            }

            BotConfig botConfig;

            try {
                botConfig = JsonConvert.DeserializeObject <BotConfig>(File.ReadAllText(filePath));
            } catch (Exception e) {
                Logging.LogGenericException(e);
                return(null);
            }

            if (botConfig == null)
            {
                Logging.LogNullError(nameof(botConfig));
                return(null);
            }

            // Support encrypted passwords
            if ((botConfig.PasswordFormat != CryptoHelper.ECryptoMethod.PlainText) && !string.IsNullOrEmpty(botConfig.SteamPassword))
            {
                // In worst case password will result in null, which will have to be corrected by user during runtime
                botConfig.SteamPassword = CryptoHelper.Decrypt(botConfig.PasswordFormat, botConfig.SteamPassword);
            }

            // User might not know what he's doing
            // Ensure that he can't screw core ASF variables
            if (botConfig.GamesPlayedWhileIdle.Count <= CardsFarmer.MaxGamesPlayedConcurrently)
            {
                return(botConfig);
            }

            Logging.LogGenericWarning("Playing more than " + CardsFarmer.MaxGamesPlayedConcurrently + " games concurrently is not possible, only first " + CardsFarmer.MaxGamesPlayedConcurrently + " entries from GamesPlayedWhileIdle will be used");

            HashSet <uint> validGames = new HashSet <uint>(botConfig.GamesPlayedWhileIdle.Take(CardsFarmer.MaxGamesPlayedConcurrently));

            botConfig.GamesPlayedWhileIdle.IntersectWith(validGames);
            botConfig.GamesPlayedWhileIdle.TrimExcess();

            return(botConfig);
        }
Example #28
0
        internal Dictionary <uint, string> GetOwnedGames(ulong steamID)
        {
            if ((steamID == 0) || string.IsNullOrEmpty(Bot.BotConfig.SteamApiKey))
            {
                // TODO: Correct this when Mono 4.4+ will be a latest stable one | https://bugzilla.xamarin.com/show_bug.cgi?id=39455
                Logging.LogNullError("steamID || SteamApiKey", Bot.BotName);
                //Logging.LogNullError(nameof(steamID) + " || " + nameof(Bot.BotConfig.SteamApiKey), Bot.BotName);
                return(null);
            }

            KeyValue response = null;

            using (dynamic iPlayerService = WebAPI.GetInterface("IPlayerService", Bot.BotConfig.SteamApiKey)) {
                iPlayerService.Timeout = Timeout;

                for (byte i = 0; (i < WebBrowser.MaxRetries) && (response == null); i++)
                {
                    try {
                        response = iPlayerService.GetOwnedGames(
                            steamid: steamID,
                            include_appinfo: 1,
                            secure: !Program.GlobalConfig.ForceHttp
                            );
                    } catch (Exception e) {
                        Logging.LogGenericException(e, Bot.BotName);
                    }
                }
            }

            if (response == null)
            {
                Logging.LogGenericWTF("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName);
                return(null);
            }

            Dictionary <uint, string> result = new Dictionary <uint, string>(response["games"].Children.Count);

            foreach (KeyValue game in response["games"].Children)
            {
                uint appID = (uint)game["appid"].AsUnsignedLong();
                if (appID == 0)
                {
                    Logging.LogNullError(nameof(appID));
                    continue;
                }

                result[appID] = game["name"].Value;
            }

            return(result);
        }
Example #29
0
        private static async Task <HttpResponseMessage> UrlRequest(string request, HttpMethod httpMethod, Dictionary <string, string> data = null, Dictionary <string, string> cookies = null, string referer = null)
        {
            if (string.IsNullOrEmpty(request) || httpMethod == null)
            {
                return(null);
            }

            HttpResponseMessage responseMessage;

            using (HttpRequestMessage requestMessage = new HttpRequestMessage(httpMethod, request)) {
                if (data != null && data.Count > 0)
                {
                    try {
                        requestMessage.Content = new FormUrlEncodedContent(data);
                    } catch (UriFormatException e) {
                        Logging.LogGenericException(e);
                        return(null);
                    }
                }

                if (cookies != null && cookies.Count > 0)
                {
                    StringBuilder cookieHeader = new StringBuilder();
                    foreach (KeyValuePair <string, string> cookie in cookies)
                    {
                        cookieHeader.Append(cookie.Key + "=" + cookie.Value + ";");
                    }
                    requestMessage.Headers.Add("Cookie", cookieHeader.ToString());
                }

                if (!string.IsNullOrEmpty(referer))
                {
                    requestMessage.Headers.Referrer = new Uri(referer);
                }

                try {
                    responseMessage = await HttpClient.SendAsync(requestMessage).ConfigureAwait(false);
                } catch {                 // Request failed, we don't need to know the exact reason, swallow exception
                    return(null);
                }
            }

            if (responseMessage == null || !responseMessage.IsSuccessStatusCode)
            {
                return(null);
            }

            return(responseMessage);
        }
        internal Dictionary <uint, string> GetOwnedGames(ulong steamID)
        {
            if ((steamID == 0) || string.IsNullOrEmpty(Bot.BotConfig.SteamApiKey))
            {
                Logging.LogNullError(nameof(steamID) + " || " + nameof(Bot.BotConfig.SteamApiKey), Bot.BotName);
                return(null);
            }

            KeyValue response = null;

            for (byte i = 0; (i < WebBrowser.MaxRetries) && (response == null); i++)
            {
                using (dynamic iPlayerService = WebAPI.GetInterface("IPlayerService", Bot.BotConfig.SteamApiKey)) {
                    iPlayerService.Timeout = Timeout;

                    try {
                        response = iPlayerService.GetOwnedGames(
                            steamid: steamID,
                            include_appinfo: 1,
                            secure: !Program.GlobalConfig.ForceHttp
                            );
                    } catch (Exception e) {
                        Logging.LogGenericException(e, Bot.BotName);
                    }
                }
            }

            if (response == null)
            {
                Logging.LogGenericWarning("Request failed even after " + WebBrowser.MaxRetries + " tries", Bot.BotName);
                return(null);
            }

            Dictionary <uint, string> result = new Dictionary <uint, string>(response["games"].Children.Count);

            foreach (KeyValue game in response["games"].Children)
            {
                uint appID = game["appid"].AsUnsignedInteger();
                if (appID == 0)
                {
                    Logging.LogNullError(nameof(appID), Bot.BotName);
                    return(null);
                }

                result[appID] = game["name"].Value;
            }

            return(result);
        }