diff --git a/ArchiSteamFarm/ArchiSteamFarm.csproj b/ArchiSteamFarm/ArchiSteamFarm.csproj index 254f95743..54b67b4cc 100644 --- a/ArchiSteamFarm/ArchiSteamFarm.csproj +++ b/ArchiSteamFarm/ArchiSteamFarm.csproj @@ -99,6 +99,7 @@ + diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index f8bbdc668..02b0be827 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -131,7 +131,7 @@ namespace ArchiSteamFarm { {"pin", parentalPin} }; - HttpResponseMessage response = await Utilities.UrlPostRequestWithResponse("https://steamcommunity.com/parental/ajaxunlock", postData, SteamCookieDictionary, "https://steamcommunity.com/").ConfigureAwait(false); + HttpResponseMessage response = await WebBrowser.UrlPost("https://steamcommunity.com/parental/ajaxunlock", postData, SteamCookieDictionary, "https://steamcommunity.com/").ConfigureAwait(false); if (response != null && response.IsSuccessStatusCode) { Logging.LogGenericInfo(Bot.BotName, "Success!"); @@ -241,7 +241,7 @@ namespace ArchiSteamFarm { {"action", "join"} }; - await Utilities.UrlPostRequest(request, postData, SteamCookieDictionary).ConfigureAwait(false); + await WebBrowser.UrlPost(request, postData, SteamCookieDictionary).ConfigureAwait(false); } internal async Task LeaveClan(ulong clanID) { @@ -260,7 +260,8 @@ namespace ArchiSteamFarm { {"action", "leaveGroup"}, {"groupId", clanID.ToString()} }; - await Utilities.UrlPostRequest(request, postData, SteamCookieDictionary).ConfigureAwait(false); + + await WebBrowser.UrlPost(request, postData, SteamCookieDictionary).ConfigureAwait(false); } internal async Task AcceptTradeOffer(ulong tradeID) { @@ -282,7 +283,7 @@ namespace ArchiSteamFarm { {"tradeofferid", tradeID.ToString()} }; - HttpResponseMessage result = await Utilities.UrlPostRequestWithResponse(request, postData, SteamCookieDictionary, referer).ConfigureAwait(false); + HttpResponseMessage result = await WebBrowser.UrlPost(request, postData, SteamCookieDictionary, referer).ConfigureAwait(false); if (result == null) { return false; } @@ -338,7 +339,7 @@ namespace ArchiSteamFarm { return null; } - return await Utilities.UrlToHtmlDocument("http://steamcommunity.com/profiles/" + SteamID + "/badges?p=" + page, SteamCookieDictionary).ConfigureAwait(false); + return await WebBrowser.UrlGetToHtmlDocument("http://steamcommunity.com/profiles/" + SteamID + "/badges?p=" + page, SteamCookieDictionary).ConfigureAwait(false); } internal async Task GetGameCardsPage(ulong appID) { @@ -346,7 +347,7 @@ namespace ArchiSteamFarm { return null; } - return await Utilities.UrlToHtmlDocument("http://steamcommunity.com/profiles/" + SteamID + "/gamecards/" + appID, SteamCookieDictionary).ConfigureAwait(false); + return await WebBrowser.UrlGetToHtmlDocument("http://steamcommunity.com/profiles/" + SteamID + "/gamecards/" + appID, SteamCookieDictionary).ConfigureAwait(false); } } } diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index ea5753684..d9785219c 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -50,12 +50,12 @@ namespace ArchiSteamFarm { private static readonly string ExecutablePath = Assembly.Location; private static readonly AssemblyName AssemblyName = Assembly.GetName(); //private static readonly string ExeName = AssemblyName.Name + ".exe"; - private static readonly string Version = AssemblyName.Version.ToString(); + internal static readonly string Version = AssemblyName.Version.ToString(); internal static readonly object ConsoleLock = new object(); private static async Task CheckForUpdate() { - JObject response = await Utilities.UrlToJObject(LatestGithubReleaseURL).ConfigureAwait(false); + JObject response = await WebBrowser.UrlGetToJObject(LatestGithubReleaseURL).ConfigureAwait(false); if (response == null) { return; } @@ -133,9 +133,15 @@ namespace ArchiSteamFarm { } } + private static void InitServices() { + WebBrowser.Init(); + } + private static void Main(string[] args) { Logging.LogGenericInfo("Main", "Archi's Steam Farm, version " + Version); + InitServices(); + Task.Run(async () => await CheckForUpdate().ConfigureAwait(false)).Wait(); // Allow loading configs from source tree if it's a debug build diff --git a/ArchiSteamFarm/Utilities.cs b/ArchiSteamFarm/Utilities.cs index 7e1646016..9655b60aa 100644 --- a/ArchiSteamFarm/Utilities.cs +++ b/ArchiSteamFarm/Utilities.cs @@ -57,161 +57,5 @@ namespace ArchiSteamFarm { return ulong.Parse(resultString, CultureInfo.InvariantCulture); } - - internal static async Task UrlToHttpResponse(string websiteAddress, Dictionary cookieVariables = null) { - if (string.IsNullOrEmpty(websiteAddress)) { - return null; - } - - HttpResponseMessage result = null; - - try { - using (HttpClientHandler clientHandler = new HttpClientHandler { UseCookies = false }) { - using (HttpClient client = new HttpClient(clientHandler)) { - client.Timeout = TimeSpan.FromSeconds(30); - client.DefaultRequestHeaders.UserAgent.ParseAdd("ArchiSteamFarm/1.0"); - HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, websiteAddress); - if (cookieVariables != null) { - StringBuilder cookie = new StringBuilder(); - foreach (KeyValuePair cookieVariable in cookieVariables) { - cookie.Append(cookieVariable.Key + "=" + cookieVariable.Value + ";"); - } - requestMessage.Headers.Add("Cookie", cookie.ToString()); - } - HttpResponseMessage responseMessage = await client.SendAsync(requestMessage).ConfigureAwait(false); - if (responseMessage != null) { - responseMessage.EnsureSuccessStatusCode(); - result = responseMessage; - } - } - } - } catch (Exception e) { - Logging.LogGenericException("Utilities", e); - } - - return result; - } - - internal static async Task UrlToHtmlDocument(string websiteAddress, Dictionary 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; - } - - internal static async Task UrlPostRequest(string request, Dictionary postData, Dictionary cookieVariables = null, string referer = null) { - if (string.IsNullOrEmpty(request) || postData == null) { - return false; - } - - bool result = false; - - try { - using (HttpClientHandler clientHandler = new HttpClientHandler { UseCookies = false }) { - using (HttpClient client = new HttpClient(clientHandler)) { - client.Timeout = TimeSpan.FromSeconds(30); - client.DefaultRequestHeaders.UserAgent.ParseAdd("ArchiSteamFarm/1.0"); - HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, request); - requestMessage.Content = new FormUrlEncodedContent(postData); - if (cookieVariables != null && cookieVariables.Count > 0) { - StringBuilder cookie = new StringBuilder(); - foreach (KeyValuePair cookieVariable in cookieVariables) { - cookie.Append(cookieVariable.Key + "=" + cookieVariable.Value + ";"); - } - requestMessage.Headers.Add("Cookie", cookie.ToString()); - } - if (referer != null) { - requestMessage.Headers.Referrer = new Uri(referer); - } - HttpResponseMessage responseMessage = await client.SendAsync(requestMessage).ConfigureAwait(false); - if (responseMessage != null) { - result = responseMessage.IsSuccessStatusCode; - } - } - } - } catch (Exception e) { - Logging.LogGenericException("Utilities", e); - } - - return result; - } - - internal static async Task UrlPostRequestWithResponse(string request, Dictionary postData, Dictionary cookieVariables = null, string referer = null) { - if (string.IsNullOrEmpty(request) || postData == null) { - return null; - } - - HttpResponseMessage result = null; - - try { - using (HttpClientHandler clientHandler = new HttpClientHandler { UseCookies = false }) { - using (HttpClient client = new HttpClient(clientHandler)) { - client.Timeout = TimeSpan.FromSeconds(30); - client.DefaultRequestHeaders.UserAgent.ParseAdd("ArchiSteamFarm/1.0"); - HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, request); - requestMessage.Content = new FormUrlEncodedContent(postData); - if (cookieVariables != null && cookieVariables.Count > 0) { - StringBuilder cookie = new StringBuilder(); - foreach (KeyValuePair cookieVariable in cookieVariables) { - cookie.Append(cookieVariable.Key + "=" + cookieVariable.Value + ";"); - } - requestMessage.Headers.Add("Cookie", cookie.ToString()); - } - if (referer != null) { - requestMessage.Headers.Referrer = new Uri(referer); - } - HttpResponseMessage responseMessage = await client.SendAsync(requestMessage).ConfigureAwait(false); - if (responseMessage != null) { - result = responseMessage; - } - } - } - } catch (Exception e) { - Logging.LogGenericException("Utilities", e); - } - - return result; - } - - internal static async Task UrlToJObject(string request) { - if (string.IsNullOrEmpty(request)) { - return null; - } - - HttpResponseMessage httpResponseMessage = await UrlToHttpResponse(request, null).ConfigureAwait(false); - if (httpResponseMessage == null) { - return null; - } - - string source = await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); - if (string.IsNullOrEmpty(source)) { - return null; - } - - JObject result = null; - try { - result = JObject.Parse(source); - } catch { - } - - return result; - } } } diff --git a/ArchiSteamFarm/WebBrowser.cs b/ArchiSteamFarm/WebBrowser.cs new file mode 100644 index 000000000..71f842101 --- /dev/null +++ b/ArchiSteamFarm/WebBrowser.cs @@ -0,0 +1,220 @@ +using HtmlAgilityPack; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace ArchiSteamFarm { + internal static class WebBrowser { + internal const byte HttpTimeout = 180; // In seconds + + private static readonly HttpClientHandler HttpClientHandler = new HttpClientHandler { UseCookies = false }; + private static readonly HttpClient HttpClient = new HttpClient(HttpClientHandler) { Timeout = TimeSpan.FromSeconds(HttpTimeout) }; + + internal static void Init() { + HttpClient.DefaultRequestHeaders.UserAgent.ParseAdd("ArchiSteamFarm/" + Program.Version); + } + + private static async Task UrlRequest(string request, HttpMethod httpMethod, Dictionary data = null, Dictionary cookies = null, string referer = null) { + if (string.IsNullOrEmpty(request) || httpMethod == null) { + return null; + } + + HttpRequestMessage requestMessage = new HttpRequestMessage(httpMethod, request); + + if (httpMethod == HttpMethod.Post && data != null) { + requestMessage.Content = new FormUrlEncodedContent(data); + } + + if (cookies != null && cookies.Count > 0) { + StringBuilder cookieHeader = new StringBuilder(); + foreach (KeyValuePair cookie in cookies) { + cookieHeader.Append(cookie.Key + "=" + cookie.Value + ";"); + } + requestMessage.Headers.Add("Cookie", cookieHeader.ToString()); + } + + if (referer != null) { + requestMessage.Headers.Referrer = new Uri(referer); + } + + HttpResponseMessage responseMessage; + + 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; + } + + if (!responseMessage.IsSuccessStatusCode) { + return null; + } + + return responseMessage; + } + + internal static async Task UrlGet(string request, Dictionary cookies = null, string referer = null) { + if (string.IsNullOrEmpty(request)) { + return null; + } + + return await UrlRequest(request, HttpMethod.Get, null, cookies, referer).ConfigureAwait(false); + } + + internal static async Task UrlPost(string request, Dictionary postData = null, Dictionary cookies = null, string referer = null) { + if (string.IsNullOrEmpty(request)) { + return null; + } + + return await UrlRequest(request, HttpMethod.Post, postData, cookies, referer).ConfigureAwait(false); + } + + internal static async Task HttpResponseToHtmlDocument(HttpResponseMessage httpResponse) { + if (httpResponse == null || httpResponse.Content == null) { + return null; + } + + string content = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); + if (string.IsNullOrEmpty(content)) { + return null; + } + + content = WebUtility.HtmlDecode(content); + HtmlDocument htmlDocument = new HtmlDocument(); + htmlDocument.LoadHtml(content); + + return htmlDocument; + } + + internal static async Task UrlGetToContent(string request, Dictionary cookies = null, string referer = null) { + if (string.IsNullOrEmpty(request)) { + return null; + } + + HttpResponseMessage responseMessage = await UrlGet(request, cookies, referer).ConfigureAwait(false); + if (responseMessage == null || responseMessage.Content == null) { + return null; + } + + return await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); + } + + internal static async Task UrlPostToContent(string request, Dictionary postData = null, Dictionary cookies = null, string referer = null) { + if (string.IsNullOrEmpty(request)) { + return null; + } + + HttpResponseMessage responseMessage = await UrlPost(request, postData, cookies, referer).ConfigureAwait(false); + if (responseMessage == null || responseMessage.Content == null) { + return null; + } + + return await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); + } + + internal static async Task UrlGetToTitle(string request, Dictionary cookies = null, string referer = null) { + if (string.IsNullOrEmpty(request)) { + return null; + } + + HtmlDocument htmlDocument = await UrlGetToHtmlDocument(request, cookies, referer).ConfigureAwait(false); + if (htmlDocument == null) { + return null; + } + + HtmlNode htmlNode = htmlDocument.DocumentNode.SelectSingleNode("//head/title"); + if (htmlNode == null) { + return null; + } + + return htmlNode.InnerText; + } + + internal static async Task UrlGetToHtmlDocument(string request, Dictionary cookies = null, string referer = null) { + if (string.IsNullOrEmpty(request)) { + return null; + } + + HttpResponseMessage httpResponse = await UrlGet(request, cookies, referer).ConfigureAwait(false); + if (httpResponse == null) { + return null; + } + + return await HttpResponseToHtmlDocument(httpResponse).ConfigureAwait(false); + } + + internal static async Task UrlGetToJArray(string request, Dictionary 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; + } + + JArray jArray; + + try { + jArray = JArray.Parse(content); + } catch (Exception e) { + Logging.LogGenericException("WebBrowser", e); + return null; + } + + return jArray; + } + + internal static async Task UrlGetToJObject(string request, Dictionary 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("WebBrowser", e); + return null; + } + + return jObject; + } + + internal static async Task UrlGetToXML(string request, Dictionary 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("WebBrowser", e); + return null; + } + + return xmlDocument; + } + } +}